From 33e9c211a2730c53d7eb16f1f1eef9ce971bcb3d Mon Sep 17 00:00:00 2001 From: Athul Nambiar <108534940+athul-22@users.noreply.github.com> Date: Sun, 9 Jun 2024 16:56:29 +0000 Subject: [PATCH] Added support for itemKey property in RNPickerSelect component --- src/index.js | 194 +++++++++++++++++++++++++-------------------------- 1 file changed, 95 insertions(+), 99 deletions(-) diff --git a/src/index.js b/src/index.js index 7ae6730f..6951d365 100644 --- a/src/index.js +++ b/src/index.js @@ -156,14 +156,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); @@ -324,8 +325,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} @@ -387,30 +399,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()], + }) + )} ); } @@ -419,11 +433,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()} @@ -470,14 +486,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()} @@ -489,67 +505,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()} ); } @@ -560,23 +552,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(); @@ -586,11 +582,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(); } }