Skip to content

Commit 03ca7ff

Browse files
authored
Picker - fix usePickerLabel (#3162)
* Picker - usePickerLabel - fix 'value' is not returned in case the are no 'items' * format * change condition * check items first * spaces * remove redaundent check for undefined * add hook tests
1 parent 4232371 commit 03ca7ff

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

demo/src/screens/componentScreens/PickerScreen.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const dialogOptions = [
8282
{label: 'Option 7', value: 6},
8383
{label: 'Option 8', value: 6}
8484
];
85+
8586
export default class PickerScreen extends Component {
8687
picker = React.createRef<PickerMethods>();
8788
state = {
@@ -128,6 +129,7 @@ export default class PickerScreen extends Component {
128129
<Text text40 $textDefault>
129130
Picker
130131
</Text>
132+
131133
<Picker
132134
placeholder="Favorite Language"
133135
floatingPlaceholder
@@ -194,6 +196,7 @@ export default class PickerScreen extends Component {
194196
searchPlaceholder={'Search a language'}
195197
items={dialogOptions}
196198
/>
199+
197200
<Text marginB-10 text70 $textDefault>
198201
Custom Picker:
199202
</Text>
@@ -216,6 +219,7 @@ export default class PickerScreen extends Component {
216219
}}
217220
items={filters}
218221
/>
222+
219223
<Text marginT-20 marginB-10 text70 $textDefault>
220224
Custom Picker Items:
221225
</Text>
@@ -252,6 +256,7 @@ export default class PickerScreen extends Component {
252256
style={{alignSelf: 'flex-start'}}
253257
onPress={() => this.picker.current?.openExpandable?.()}
254258
/>
259+
255260
<Text text60 marginT-s5>
256261
Different Field Types
257262
</Text>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {renderHook} from '@testing-library/react-hooks';
2+
import usePickerLabel from '../usePickerLabel';
3+
4+
const colors = [
5+
{value: 'red', label: 'Red'},
6+
{
7+
value: 'green',
8+
label: 'Green',
9+
listItemProps: {subtitle: 'Subtitle'}
10+
},
11+
{
12+
value: 'blue',
13+
label: 'Blue',
14+
listItemProps: {
15+
subtitleLines: 2,
16+
subtitle: 'Subtitle with\n2 lines of text'
17+
}
18+
},
19+
{value: 'purple', label: 'Purple', disabled: true},
20+
{value: 'yellow', label: 'Yellow'},
21+
{value: 'grey', label: 'Grey'}
22+
];
23+
24+
describe('usePickerLabel hook tests', () => {
25+
const makeSUT = ({
26+
value,
27+
items,
28+
getLabel
29+
// getItemLabel,
30+
// accessibilityLabel,
31+
// accessibilityHint,
32+
// placeholder
33+
}) => {
34+
return renderHook(() =>
35+
usePickerLabel({
36+
value,
37+
items,
38+
getLabel
39+
// getItemLabel,
40+
// accessibilityLabel,
41+
// accessibilityHint,
42+
// placeholder
43+
}));
44+
};
45+
46+
it('expect label to equal an empty string when value is undefined', () => {
47+
const sut = makeSUT({items: colors, value: undefined, getLabel: undefined});
48+
expect(sut.result.current.label).toEqual('');
49+
});
50+
51+
it('expect label to equal an empty string when value is null', () => {
52+
const sut = makeSUT({items: colors, value: null, getLabel: undefined});
53+
expect(sut.result.current.label).toEqual('');
54+
});
55+
56+
it('expect label to equal returned string when getLabel passed', () => {
57+
const sut = makeSUT({items: colors, value: null, getLabel: () => 'Some label'});
58+
expect(sut.result.current.label).toEqual('Some label');
59+
});
60+
61+
it('expect label to equal array of values when value is array', () => {
62+
const sut = makeSUT({items: colors, value: ['red', 'green'], getLabel: undefined});
63+
expect(sut.result.current.label).toEqual('Red, Green');
64+
});
65+
66+
it('expect label to equal selected item label when value equals item value', () => {
67+
const sut = makeSUT({items: colors, value: 'red', getLabel: undefined});
68+
expect(sut.result.current.label).toEqual('Red');
69+
});
70+
71+
it('expect label to equal value when no items passed', () => {
72+
const sut = makeSUT({items: undefined, value: 'Some string', getLabel: undefined});
73+
expect(sut.result.current.label).toEqual('Some string');
74+
});
75+
});

src/components/picker/helpers/usePickerLabel.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {useCallback, useMemo} from 'react';
21
import _ from 'lodash';
2+
import {useCallback, useMemo} from 'react';
33
import {PickerProps, PickerValue} from '../types';
44

55
interface UsePickerLabelProps
@@ -15,12 +15,10 @@ const usePickerLabel = (props: UsePickerLabelProps) => {
1515

1616
const getLabelsFromArray = useCallback((value: PickerValue) => {
1717
const itemsByValue = _.keyBy(items, 'value');
18-
1918
return _.flow(arr =>
2019
_.map(arr, item => (_.isPlainObject(item) ? getItemLabel?.(item) || item?.label : itemsByValue[item]?.label)),
2120
arr => _.join(arr, ', '))(value);
22-
},
23-
[getItemLabel, items]);
21+
}, [getItemLabel, items]);
2422

2523
const _getLabel = useCallback((value: PickerValue) => {
2624
if (_.isFunction(getLabel) && !_.isUndefined(getLabel(value))) {
@@ -31,17 +29,15 @@ const usePickerLabel = (props: UsePickerLabelProps) => {
3129
return getLabelsFromArray(value);
3230
}
3331

34-
// TODO: Remove
35-
// if (typeof value === 'object') {
36-
// return value?.label;
37-
// }
38-
39-
// otherwise, extract from picker items
40-
const selectedItem = _.find(items, {value});
32+
if (!_.isEmpty(items)) {
33+
const selectedItem = _.find(items, {value});
34+
return _.get(selectedItem, 'label');
35+
}
4136

42-
return _.get(selectedItem, 'label');
43-
},
44-
[getLabelsFromArray, items]);
37+
if (typeof value === 'string') {
38+
return value;
39+
}
40+
}, [getLabel, getLabelsFromArray, items]);
4541

4642
const accessibilityInfo = useMemo(() => {
4743
const label = _getLabel(value);
@@ -51,7 +47,7 @@ const usePickerLabel = (props: UsePickerLabelProps) => {
5147
accessibilityHint:
5248
accessibilityHint ?? (label ? 'Double tap to edit' : `Goes to ${placeholder}. Suggestions will be provided`)
5349
};
54-
}, [value, accessibilityLabel, accessibilityHint]);
50+
}, [value, placeholder, accessibilityLabel, accessibilityHint, _getLabel]);
5551

5652
return {
5753
getLabelsFromArray,

0 commit comments

Comments
 (0)