Skip to content

Commit

Permalink
Add a migrate prop to Picker component for migrating to the new API (#…
Browse files Browse the repository at this point in the history
…1048)

* Add a migrate prop to Picker component for migrating to the new API

* acknoledge migrate prop when checking if picker item is selected
  • Loading branch information
ethanshar authored Nov 26, 2020
1 parent ab255a7 commit 4c47d27
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions demo/src/screens/componentScreens/PickerScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default class PickerScreen extends Component {
<Text text60 marginT-s5 marginB-s2>Migrated Picker</Text>

<Picker
migrate
title="Language"
placeholder="Favorite Language"
value={this.state.language2}
Expand Down
12 changes: 8 additions & 4 deletions src/components/picker/PickerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const PickerItem = props => {
testID
} = props;
const context = useContext(PickerContext);
const {renderItem} = context;
const isSelected = isItemSelected(value, context.value);
const {migrate, renderItem} = context;
const isSelected = isItemSelected(value, !migrate && _.isObject(context.value) ? context.value.value : context.value);
const itemLabel = getItemLabel(label, value, props.getItemLabel || context.getItemLabel);
const accessibilityProps = {
accessibilityState: isSelected ? {selected: true} : undefined,
Expand All @@ -51,8 +51,12 @@ const PickerItem = props => {
}, [isSelected, disabled, selectedIcon, selectedIconColor]);

const _onPress = useCallback(() => {
context.onPress(value);
}, [value, context.onPress]);
if (migrate) {
context.onPress(value);
} else {
context.onPress(_.isObject(value) ? value : {value, label: itemLabel});
}
}, [migrate, value, context.onPress]);

const onSelectedLayout = useCallback((...args) => {
_.invoke(context, 'onSelectedLayout', ...args);
Expand Down
7 changes: 6 additions & 1 deletion src/components/picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const ItemType = PropTypes.shape({
class Picker extends PureComponent {
static displayName = 'Picker';
static propTypes = {
/**
* Temporary prop required for migration to Picker's new API
*/
migrate: PropTypes.bool,
...TextField.propTypes,
/**
* Picker current value in the shape of {value: ..., label: ...}, for custom shape use 'getItemValue' prop
Expand Down Expand Up @@ -204,8 +208,9 @@ class Picker extends PureComponent {

getContextValue = () => {
const {value, searchValue} = this.state;
const {mode, getItemValue, getItemLabel, renderItem, showSearch} = this.props;
const {migrate, mode, getItemValue, getItemLabel, renderItem, showSearch} = this.props;
return {
migrate,
value,
onPress: mode === Picker.modes.MULTI ? this.toggleItemSelection : this.onDoneSelecting,
getItemValue,
Expand Down

0 comments on commit 4c47d27

Please sign in to comment.