Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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