Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit 24301da

Browse files
JoseVfpeacechen
authored andcommitted
Add selectedKey prop (peacechen#102)
* Add selectedKey prop * Update Readme
1 parent 2843080 commit 24301da

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Prop | Type | Optional | Default | Description
164164
`cancelButtonAccessibilityLabel` | string | Yes | undefined | Accessibility label for the cancel button
165165
`modalOpenerHitSlop` | object | Yes | {} | How far touch can stray away from touchable that opens modal ([RN docs](https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html#hitslop))
166166
`customSelector` | node | Yes | undefined | Render a custom node instead of the built-in select box.
167+
`selectedKey` | any | Yes | '' | Key of the item to be initially selected
167168

168169
### Methods
169170

index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const propTypes = {
7171
optionTextPassThruProps: PropTypes.object,
7272
modalOpenerHitSlop: PropTypes.object,
7373
customSelector: PropTypes.node,
74+
selectedKey: PropTypes.any,
7475
};
7576

7677
const defaultProps = {
@@ -106,7 +107,7 @@ const defaultProps = {
106107
supportedOrientations: ['portrait', 'landscape'],
107108
keyboardShouldPersistTaps: 'always',
108109
backdropPressToClose: false,
109-
openButtonContainerAccessible: false,
110+
openButtonContainerAccessible: false,
110111
listItemAccessible: false,
111112
cancelButtonAccessible: false,
112113
scrollViewAccessible: false,
@@ -117,18 +118,19 @@ const defaultProps = {
117118
optionTextPassThruProps: {},
118119
modalOpenerHitSlop: {top: 0, bottom: 0, left: 0, right: 0},
119120
customSelector: undefined,
121+
selectedKey: '',
120122
};
121123

122124
export default class ModalSelector extends React.Component {
123125

124126
constructor(props) {
125127
super(props);
126-
128+
let selectedItem = this.validateSelectedKey(props.selectedKey);
127129
this.state = {
128130
modalVisible: props.visible,
129-
selected: props.initValue,
131+
selected: selectedItem.label,
130132
cancelText: props.cancelText,
131-
changedItem: undefined,
133+
changedItem: selectedItem.key,
132134
};
133135
}
134136

@@ -143,11 +145,24 @@ export default class ModalSelector extends React.Component {
143145
newState.modalVisible = this.props.visible;
144146
doUpdate = true;
145147
}
148+
if(prevProps.selectedKey !== this.props.selectedKey){
149+
let selectedItem = this.validateSelectedKey(this.props.selectedKey);
150+
newState.selected = selectedItem.label;
151+
newState.changedItem = selectedItem.key;
152+
doUpdate = true;
153+
}
146154
if (doUpdate) {
147155
this.setState(newState);
148156
}
149157
}
150158

159+
validateSelectedKey = (key) => {
160+
let selectedItem = this.props.data.filter((item) => this.props.keyExtractor(item) === key);
161+
let selectedLabel = selectedItem.length > 0 ? this.props.labelExtractor(selectedItem[0]) : this.props.initValue;
162+
let selectedKey = selectedItem.length > 0 ? key : undefined;
163+
return {label: selectedLabel, key: selectedKey}
164+
}
165+
151166
onChange = (item) => {
152167
if (Platform.OS === 'android' || (Modal.propTypes !== undefined && !Modal.propTypes.onDismiss)) { // don't know if this will work for previous version, please check!
153168
// RN >= 0.50 on iOS comes with the onDismiss prop for Modal which solves RN issue #10471

0 commit comments

Comments
 (0)