Skip to content

Commit 4c47d27

Browse files
authored
Add a migrate prop to Picker component for migrating to the new API (#1048)
* Add a migrate prop to Picker component for migrating to the new API * acknoledge migrate prop when checking if picker item is selected
1 parent ab255a7 commit 4c47d27

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

demo/src/screens/componentScreens/PickerScreen.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export default class PickerScreen extends Component {
235235
<Text text60 marginT-s5 marginB-s2>Migrated Picker</Text>
236236

237237
<Picker
238+
migrate
238239
title="Language"
239240
placeholder="Favorite Language"
240241
value={this.state.language2}

src/components/picker/PickerItem.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const PickerItem = props => {
2929
testID
3030
} = props;
3131
const context = useContext(PickerContext);
32-
const {renderItem} = context;
33-
const isSelected = isItemSelected(value, context.value);
32+
const {migrate, renderItem} = context;
33+
const isSelected = isItemSelected(value, !migrate && _.isObject(context.value) ? context.value.value : context.value);
3434
const itemLabel = getItemLabel(label, value, props.getItemLabel || context.getItemLabel);
3535
const accessibilityProps = {
3636
accessibilityState: isSelected ? {selected: true} : undefined,
@@ -51,8 +51,12 @@ const PickerItem = props => {
5151
}, [isSelected, disabled, selectedIcon, selectedIconColor]);
5252

5353
const _onPress = useCallback(() => {
54-
context.onPress(value);
55-
}, [value, context.onPress]);
54+
if (migrate) {
55+
context.onPress(value);
56+
} else {
57+
context.onPress(_.isObject(value) ? value : {value, label: itemLabel});
58+
}
59+
}, [migrate, value, context.onPress]);
5660

5761
const onSelectedLayout = useCallback((...args) => {
5862
_.invoke(context, 'onSelectedLayout', ...args);

src/components/picker/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const ItemType = PropTypes.shape({
3030
class Picker extends PureComponent {
3131
static displayName = 'Picker';
3232
static propTypes = {
33+
/**
34+
* Temporary prop required for migration to Picker's new API
35+
*/
36+
migrate: PropTypes.bool,
3337
...TextField.propTypes,
3438
/**
3539
* Picker current value in the shape of {value: ..., label: ...}, for custom shape use 'getItemValue' prop
@@ -204,8 +208,9 @@ class Picker extends PureComponent {
204208

205209
getContextValue = () => {
206210
const {value, searchValue} = this.state;
207-
const {mode, getItemValue, getItemLabel, renderItem, showSearch} = this.props;
211+
const {migrate, mode, getItemValue, getItemLabel, renderItem, showSearch} = this.props;
208212
return {
213+
migrate,
209214
value,
210215
onPress: mode === Picker.modes.MULTI ? this.toggleItemSelection : this.onDoneSelecting,
211216
getItemValue,

0 commit comments

Comments
 (0)