Skip to content

Commit ca63fa1

Browse files
committed
Revert "getItemLabel, getItemValue deprecation"
This reverts commit 5f84a51.
1 parent 5f84a51 commit ca63fa1

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

src/components/picker/PickerItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const PickerItem = (props: PickerItemProps) => {
3434
// @ts-expect-error TODO: fix after removing migrate prop completely
3535
const itemValue = !migrate && typeof value === 'object' ? value?.value : value;
3636
const isSelected = isItemSelected(itemValue, context.value);
37-
const itemLabel = getItemLabel(label, value, props.getItemLabel);
37+
const itemLabel = getItemLabel(label, value, props.getItemLabel || context.getItemLabel);
3838
const selectedCounter = context.selectionLimit && _.isArray(context.value) && context.value?.length;
3939
const accessibilityProps = {
4040
accessibilityState: isSelected ? {selected: true} : undefined,

src/components/picker/PickerPresenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import React from 'react';
3-
import {PickerItemProps, PickerProps, PickerSingleValue, PickerValue} from './types';
3+
import {PickerProps, PickerSingleValue, PickerValue} from './types';
44

55
export function extractPickerItems(props: PickerProps) {
66
const {children} = props;
@@ -37,7 +37,7 @@ export function isItemSelected(childValue: PickerSingleValue, selectedValue?: Pi
3737
// return _.invoke(props, 'getItemValue', props.value) || _.get(props.value, 'value');
3838
// }
3939

40-
export function getItemLabel(label: string, value: PickerValue, getItemLabel: PickerItemProps['getItemLabel']) {
40+
export function getItemLabel(label: string, value: PickerValue, getItemLabel: PickerProps['getItemLabel']) {
4141
if (_.isObject(value)) {
4242
if (getItemLabel) {
4343
return getItemLabel(value);

src/components/picker/helpers/usePickerLabel.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ import _ from 'lodash';
33
import {PickerProps, PickerValue} from '../types';
44

55
interface UsePickerLabelProps
6-
extends Pick<PickerProps, 'value' | 'getLabel' | 'placeholder' | 'accessibilityLabel' | 'accessibilityHint'> {
6+
extends Pick<
7+
PickerProps,
8+
'value' | 'getLabel' | 'getItemLabel' | 'placeholder' | 'accessibilityLabel' | 'accessibilityHint'
9+
> {
710
items: {value: string | number; label: string}[] | null | undefined;
811
}
912

1013
const usePickerLabel = (props: UsePickerLabelProps) => {
11-
const {value, items, getLabel, placeholder, accessibilityLabel, accessibilityHint} = props;
14+
const {value, items, getLabel, getItemLabel, placeholder, accessibilityLabel, accessibilityHint} = props;
1215

1316
const getLabelsFromArray = useCallback((value: PickerValue) => {
1417
const itemsByValue = _.keyBy(items, 'value');
1518

16-
return _.flow(arr => _.map(arr, item => (_.isPlainObject(item) ? item?.label : itemsByValue[item]?.label)),
17-
arr => _.join(arr, ', '))(value);
19+
return _.flow(arr =>
20+
_.map(arr, item => (_.isPlainObject(item) ? getItemLabel?.(item) || item?.label : itemsByValue[item]?.label)),
21+
arr => _.join(arr, ', '))(value);
1822
},
19-
[items]);
23+
[getItemLabel, items]);
2024

2125
const _getLabel = useCallback((value: PickerValue) => {
2226
if (_.isFunction(getLabel) && !_.isUndefined(getLabel(value))) {

src/components/picker/helpers/usePickerSearch.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import _ from 'lodash';
33
import {PickerProps} from '../types';
44
import {getItemLabel as getItemLabelPresenter, shouldFilterOut} from '../PickerPresenter';
55

6-
type UsePickerSearchProps = Pick<PickerProps, 'showSearch' | 'onSearchChange' | 'children'>;
6+
type UsePickerSearchProps = Pick<PickerProps, 'showSearch' | 'onSearchChange' | 'children' | 'getItemLabel'>;
77

88
const usePickerSearch = (props: UsePickerSearchProps) => {
9-
const {showSearch, onSearchChange, children} = props;
9+
const {showSearch, onSearchChange, children, getItemLabel} = props;
1010
const [searchValue, setSearchValue] = useState('');
1111

1212
const filteredChildren = useMemo(() => {
@@ -15,7 +15,7 @@ const usePickerSearch = (props: UsePickerSearchProps) => {
1515
return _.filter(children, child => {
1616
// @ts-expect-error need to fix children type to be based on PickerItemProps
1717
const {label, value, getItemLabel: childGetItemLabel} = child.props;
18-
const itemLabel = getItemLabelPresenter(label, value, childGetItemLabel);
18+
const itemLabel = getItemLabelPresenter(label, value, childGetItemLabel || getItemLabel);
1919
return !shouldFilterOut(searchValue, itemLabel);
2020
});
2121
}

src/components/picker/helpers/usePickerSelection.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import {RefObject, useCallback, useState, useEffect} from 'react';
22
import _ from 'lodash';
33
import {PickerProps, PickerValue, PickerSingleValue, PickerMultiValue, PickerModes} from '../types';
44

5-
interface UsePickerSelectionProps extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'topBarProps' | 'mode'> {
5+
interface UsePickerSelectionProps
6+
extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'getItemValue' | 'topBarProps' | 'mode'> {
67
pickerExpandableRef: RefObject<any>;
78
setSearchValue: (searchValue: string) => void;
89
}
910

1011
const usePickerSelection = (props: UsePickerSelectionProps) => {
11-
const {migrate, value, onChange, topBarProps, pickerExpandableRef, setSearchValue, mode} = props;
12+
const {migrate, value, onChange, topBarProps, pickerExpandableRef, getItemValue, setSearchValue, mode} = props;
1213
const [multiDraftValue, setMultiDraftValue] = useState(value as PickerMultiValue);
1314
const [multiFinalValue, setMultiFinalValue] = useState(value as PickerMultiValue);
1415

@@ -31,14 +32,14 @@ const usePickerSelection = (props: UsePickerSelectionProps) => {
3132
let newValue;
3233
const itemAsArray = [item];
3334
if (!migrate) {
34-
newValue = _.xorBy(multiDraftValue, itemAsArray, 'value');
35+
newValue = _.xorBy(multiDraftValue, itemAsArray, getItemValue || 'value');
3536
} else {
3637
newValue = _.xor(multiDraftValue, itemAsArray);
3738
}
3839

3940
setMultiDraftValue(newValue);
4041
},
41-
[multiDraftValue]);
42+
[multiDraftValue, getItemValue]);
4243

4344
const cancelSelect = useCallback(() => {
4445
setSearchValue('');

src/components/picker/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// TODO: deprecate all places where we check if _.isPlainObject
2+
// TODO: deprecate getItemValue prop
3+
// TODO: deprecate getItemLabel prop
24
// TODO: Add initialValue prop
35
// TODO: consider deprecating renderCustomModal prop
46
// TODO: deprecate onShow cause it's already supported by passing it in pickerModalProps
@@ -80,6 +82,8 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
8082
listProps,
8183
value,
8284
getLabel,
85+
getItemLabel,
86+
getItemValue,
8387
renderItem,
8488
children,
8589
useSafeArea,
@@ -111,12 +115,13 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
111115
filteredChildren,
112116
setSearchValue,
113117
onSearchChange: _onSearchChange
114-
} = usePickerSearch({showSearch, onSearchChange, children});
118+
} = usePickerSearch({showSearch, onSearchChange, getItemLabel, children});
115119
const {multiDraftValue, onDoneSelecting, toggleItemSelection, cancelSelect} = usePickerSelection({
116120
migrate,
117121
value,
118122
onChange,
119123
pickerExpandableRef: pickerExpandable,
124+
getItemValue,
120125
topBarProps,
121126
setSearchValue,
122127
mode
@@ -125,6 +130,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
125130
const {label, accessibilityInfo} = usePickerLabel({
126131
value,
127132
items,
133+
getItemLabel,
128134
getLabel,
129135
accessibilityLabel,
130136
accessibilityHint,
@@ -144,6 +150,8 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
144150
value: mode === PickerModes.MULTI ? multiDraftValue : pickerValue,
145151
onPress: mode === PickerModes.MULTI ? toggleItemSelection : onDoneSelecting,
146152
isMultiMode: mode === PickerModes.MULTI,
153+
getItemValue,
154+
getItemLabel,
147155
onSelectedLayout: onSelectedItemLayout,
148156
renderItem,
149157
selectionLimit
@@ -154,6 +162,8 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
154162
value,
155163
multiDraftValue,
156164
renderItem,
165+
getItemValue,
166+
getItemLabel,
157167
selectionLimit,
158168
onSelectedItemLayout,
159169
toggleItemSelection,

src/components/picker/types.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ export type PickerBaseProps = Omit<NewTextFieldProps, 'value' | 'onChange'> & {
108108
* Add onPress callback for when pressing the picker
109109
*/
110110
onPress?: () => void;
111+
/**
112+
* @deprecated
113+
* A function that extract the unique value out of the value prop in case value has a custom structure (e.g. {myValue, myLabel})
114+
*/
115+
getItemValue?: (value: PickerValue) => any;
116+
/**
117+
* @deprecated
118+
* A function that extract the label out of the value prop in case value has a custom structure (e.g. {myValue, myLabel})
119+
*/
120+
getItemLabel?: (value: PickerValue) => string;
111121
/**
112122
* A function that returns the label to show for the selected Picker value
113123
*/
@@ -208,7 +218,11 @@ export interface PickerItemProps extends Pick<TouchableOpacityProps, 'customValu
208218
/**
209219
* Custom function for the item label (e.g (value) => customLabel)
210220
*/
211-
getItemLabel?: (value: PickerValue) => string;
221+
getItemLabel?: PickerProps['getItemLabel'];
222+
/**
223+
* @deprecated Function to return the value out of the item value prop when value is custom shaped.
224+
*/
225+
getItemValue?: PickerProps['getItemValue'];
212226
/**
213227
* Render custom item
214228
*/
@@ -237,7 +251,8 @@ export interface PickerItemProps extends Pick<TouchableOpacityProps, 'customValu
237251
testID?: string;
238252
}
239253

240-
export interface PickerContextProps extends Pick<PickerProps, 'migrate' | 'value' | 'renderItem' | 'selectionLimit'> {
254+
export interface PickerContextProps
255+
extends Pick<PickerProps, 'migrate' | 'value' | 'getItemValue' | 'getItemLabel' | 'renderItem' | 'selectionLimit'> {
241256
onPress: (value: PickerSingleValue) => void;
242257
isMultiMode: boolean;
243258
onSelectedLayout: (event: any) => any;

0 commit comments

Comments
 (0)