diff --git a/src/index.js b/src/index.js index 7d6f44db..d0034e82 100644 --- a/src/index.js +++ b/src/index.js @@ -158,14 +158,15 @@ export default class RNPickerSelect extends PureComponent { }).concat(this.props.items); const itemsChanged = !isEqual(prevState.items, items); - // update selectedItem if value prop is defined and differs from currently selected item + // update selectedItem if value or itemKey prop is defined and differs from currently selected item const { selectedItem, idx } = RNPickerSelect.getSelectedItem({ items, key: this.props.itemKey, value: this.props.value, }); const selectedItemChanged = - !isEqual(this.props.value, undefined) && !isEqual(prevState.selectedItem, selectedItem); + (!isEqual(this.props.value, undefined) && !isEqual(prevState.selectedItem, selectedItem)) || + (!isEqual(this.props.itemKey, prevProps.itemKey)); if (itemsChanged || selectedItemChanged) { this.props.onValueChange(selectedItem.value, idx); @@ -328,8 +329,7 @@ export default class RNPickerSelect extends PureComponent { { - this.setState({ doneDepressed: true }); + this.setState({ + doneDepressed: true, + }); }} onPressOut={() => { - this.setState({ doneDepressed: false }); - }} - hitSlop={{ - top: 4, - right: 4, - bottom: 4, - left: 4, + this.setState({ + doneDepressed: false, + }); }} + hitSlop={{ top: 4, right: 4, bottom: 4, left: 4 }} {...touchableDoneProps} > - + {doneText} @@ -391,30 +403,32 @@ export default class RNPickerSelect extends PureComponent { } renderIcon() { - const { style, Icon } = this.props; + const { Icon } = this.props; - if (!Icon) { - return null; + if (Icon) { + return ; } - return ( - - - - ); + return null; } renderTextInputOrChildren() { - const { children, style, textInputProps } = this.props; + const { children, style, textInputProps, fixAndroidTouchableBug } = this.props; const { selectedItem } = this.state; - const containerStyle = - Platform.OS === 'ios' ? style.inputIOSContainer : style.inputAndroidContainer; + const containerStyle = Platform.OS === 'android' && fixAndroidTouchableBug + ? { height: 0, width: 0, flex: 1 } + : {}; if (children) { return ( - {children} + {React.Children.map(children, (child) => + React.cloneElement(child, { + pointerEvents: 'none', + style: [child.props.style, this.getPlaceholderStyle()], + }) + )} ); } @@ -423,11 +437,8 @@ export default class RNPickerSelect extends PureComponent { {this.renderTextInputOrChildren()} + { + this.togglePicker(true, undefined, true); + }} + onRequestClose={() => { + this.togglePicker(true, undefined, true); + }} onOrientationChange={this.onOrientationChange} {...modalProps} > { - this.togglePicker(true); + this.togglePicker(true, undefined, true); }} /> {this.renderInputAccessoryView()} @@ -474,14 +490,14 @@ export default class RNPickerSelect extends PureComponent { style={[ defaultStyles.modalViewBottom, this.isDarkTheme() ? defaultStyles.modalViewBottomDark : {}, - { height: orientation === 'portrait' ? 215 : 162 }, this.isDarkTheme() ? style.modalViewBottomDark : style.modalViewBottom, ]} > {this.renderPickerItems()} @@ -493,67 +509,43 @@ export default class RNPickerSelect extends PureComponent { } renderAndroidHeadless() { - const { - disabled, - Icon, - style, - pickerProps, - onOpen, - touchableWrapperProps, - fixAndroidTouchableBug, - } = this.props; + const { pickerProps, style, disabled } = this.props; const { selectedItem } = this.state; - const Component = fixAndroidTouchableBug ? View : TouchableOpacity; return ( - - - {this.renderTextInputOrChildren()} - - {this.renderPickerItems()} - - - + + {this.renderTextInputOrChildren()} + + + {this.renderPickerItems()} + + ); } renderAndroidNativePickerStyle() { - const { disabled, Icon, style, pickerProps } = this.props; + const { pickerProps, style, disabled } = this.props; const { selectedItem } = this.state; return ( {this.renderPickerItems()} - {this.renderIcon()} ); } @@ -564,23 +556,27 @@ export default class RNPickerSelect extends PureComponent { return ( - { + this.onValueChange(e.target.value, e.target.selectedIndex); + }} + style={[defaultStyles.inputWeb, style.inputWeb]} + value={selectedItem.value} {...pickerProps} > - {this.renderPickerItems()} - - {this.renderIcon()} + {this.renderPickerItems().map((item) => ( + + ))} + ); } render() { - const { children, useNativeAndroidPickerStyle } = this.props; + const { useNativeAndroidPickerStyle, fixAndroidTouchableBug } = this.props; if (Platform.OS === 'ios') { return this.renderIOS(); @@ -590,11 +586,11 @@ export default class RNPickerSelect extends PureComponent { return this.renderWeb(); } - if (children || !useNativeAndroidPickerStyle) { - return this.renderAndroidHeadless(); + if (useNativeAndroidPickerStyle) { + return this.renderAndroidNativePickerStyle(); } - return this.renderAndroidNativePickerStyle(); + return this.renderAndroidHeadless(); } }