Skip to content

Commit 0ddb1cf

Browse files
authored
useFieldType hook (#3130)
* useFieldType hook * renamed renderPickerInnerInput - pickerInnerInput
1 parent fb305e2 commit 0ddb1cf

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import _ from 'lodash';
2+
import React, {useMemo} from 'react';
3+
import Icon from '../../icon';
4+
import Text from '../../text';
5+
import View from '../../view';
6+
import {PickerProps, PickerFieldTypes} from '../types';
7+
import {Typography} from '../../../style';
8+
9+
type UseFieldTypeProps = Pick<
10+
PickerProps,
11+
'fieldType' | 'preset' | 'trailingAccessory' | 'label' | 'placeholder' | 'style' | 'labelStyle'
12+
>;
13+
14+
const dropdown = require('../assets/dropdown.png');
15+
16+
const useFieldType = (props: UseFieldTypeProps) => {
17+
const {fieldType, preset, trailingAccessory, label, placeholder, style, labelStyle} = props;
18+
19+
const propsByFieldType = useMemo(() => {
20+
if (fieldType === PickerFieldTypes.filter) {
21+
return {
22+
preset: preset || null,
23+
containerStyle: {flexDirection: 'row'},
24+
labelStyle: Typography.text70,
25+
trailingAccessory: trailingAccessory ?? <Icon marginL-s1 source={dropdown}/>
26+
};
27+
} else if (fieldType === PickerFieldTypes.settings) {
28+
return {
29+
preset: preset || null,
30+
label: undefined
31+
};
32+
}
33+
}, [fieldType, preset, trailingAccessory]);
34+
35+
const pickerInnerInput = useMemo(() => {
36+
if (fieldType === PickerFieldTypes.filter) {
37+
return (
38+
<Text text70 numberOfLines={1} style={style}>
39+
{_.isEmpty(label) ? placeholder : label}
40+
</Text>
41+
);
42+
} else if (fieldType === PickerFieldTypes.settings) {
43+
return (
44+
<View flexG row spread>
45+
<Text text70 style={labelStyle}>
46+
{label}
47+
</Text>
48+
<Text text70 $textPrimary style={style}>
49+
{_.isEmpty(label) ? placeholder : label}
50+
</Text>
51+
</View>
52+
);
53+
}
54+
}, [style, labelStyle, fieldType, placeholder, label]);
55+
56+
return {propsByFieldType, pickerInnerInput};
57+
};
58+
59+
export default useFieldType;

src/components/picker/index.tsx

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66
import _ from 'lodash';
77
import React, {useMemo, useState, useRef, useCallback, useEffect} from 'react';
88
import {LayoutChangeEvent} from 'react-native';
9-
import {Typography} from 'style';
109
import {useThemeProps} from 'hooks';
1110
import {Constants} from '../../commons/new';
1211
import ExpandableOverlay, {ExpandableOverlayProps, ExpandableOverlayMethods} from '../../incubator/expandableOverlay';
1312
import TextField from '../textField';
14-
import Icon from '../icon';
15-
import View from '../view';
16-
import Text from '../text';
1713
import PickerItemsList from './PickerItemsList';
1814
import PickerItem from './PickerItem';
1915
import PickerContext from './PickerContext';
2016
import usePickerSelection from './helpers/usePickerSelection';
2117
import usePickerLabel from './helpers/usePickerLabel';
2218
import usePickerSearch from './helpers/usePickerSearch';
2319
import useImperativePickerHandle from './helpers/useImperativePickerHandle';
20+
import useFieldType from './helpers/useFieldType';
2421
// import usePickerMigrationWarnings from './helpers/usePickerMigrationWarnings';
2522
import {extractPickerItems} from './PickerPresenter';
2623
import {
@@ -35,8 +32,6 @@ import {
3532
PickerMethods
3633
} from './types';
3734

38-
const dropdown = require('./assets/dropdown.png');
39-
4035
const DIALOG_PROPS = {
4136
bottom: true,
4237
width: '100%',
@@ -134,6 +129,17 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
134129
placeholder: themeProps.placeholder
135130
});
136131

132+
const {placeholder, style, trailingAccessory} = themeProps;
133+
const {propsByFieldType, pickerInnerInput} = useFieldType({
134+
fieldType,
135+
preset,
136+
trailingAccessory,
137+
style,
138+
placeholder,
139+
labelStyle,
140+
label
141+
});
142+
137143
const onSelectedItemLayout = useCallback((event: LayoutChangeEvent) => {
138144
const y = event.nativeEvent.layout.y;
139145
setSelectedItemPosition(y);
@@ -176,22 +182,6 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
176182
...pickerModalProps
177183
};
178184

179-
const propsByFieldType = useMemo(() => {
180-
if (fieldType === PickerFieldTypes.filter) {
181-
return {
182-
preset: preset || null,
183-
containerStyle: {flexDirection: 'row'},
184-
labelStyle: Typography.text70,
185-
trailingAccessory: themeProps.trailingAccessory ?? <Icon marginL-s1 source={dropdown}/>
186-
};
187-
} else if (fieldType === PickerFieldTypes.settings) {
188-
return {
189-
preset: preset || null,
190-
label: undefined
191-
};
192-
}
193-
}, [fieldType, preset, themeProps.trailingAccessory]);
194-
195185
const renderPickerItem = useCallback((item: PickerItemProps, index: number): React.ReactElement => {
196186
return <PickerItem key={`${index}-${item.value}`} {...item}/>;
197187
}, []);
@@ -270,27 +260,6 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
270260
items
271261
]);
272262

273-
const renderPickerInnerInput = () => {
274-
if (fieldType === PickerFieldTypes.filter) {
275-
return (
276-
<Text text70 numberOfLines={1} style={others.style}>
277-
{_.isEmpty(label) ? others.placeholder : label}
278-
</Text>
279-
);
280-
} else if (fieldType === PickerFieldTypes.settings) {
281-
return (
282-
<View flexG row spread>
283-
<Text text70 style={labelStyle}>
284-
{others.label}
285-
</Text>
286-
<Text text70 $textPrimary style={others.style}>
287-
{_.isEmpty(label) ? others.placeholder : label}
288-
</Text>
289-
</View>
290-
);
291-
}
292-
};
293-
294263
return (
295264
//TODO : fix the ExpandableOverlay ts error
296265
<PickerContext.Provider value={contextValue}>
@@ -326,7 +295,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
326295
value={label}
327296
selection={Constants.isAndroid ? {start: 0} : undefined}
328297
>
329-
{renderPickerInnerInput()}
298+
{pickerInnerInput}
330299
</TextField>
331300
)}
332301
</ExpandableOverlay>

0 commit comments

Comments
 (0)