Skip to content

useFieldType hook #3130

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 3 commits into from
Jun 16, 2024
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
59 changes: 59 additions & 0 deletions src/components/picker/helpers/useFieldType.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import _ from 'lodash';
import React, {useMemo} from 'react';
import Icon from '../../icon';
import Text from '../../text';
import View from '../../view';
import {PickerProps, PickerFieldTypes} from '../types';
import {Typography} from '../../../style';

type UseFieldTypeProps = Pick<
PickerProps,
'fieldType' | 'preset' | 'trailingAccessory' | 'label' | 'placeholder' | 'style' | 'labelStyle'
>;

const dropdown = require('../assets/dropdown.png');

const useFieldType = (props: UseFieldTypeProps) => {
const {fieldType, preset, trailingAccessory, label, placeholder, style, labelStyle} = props;

const propsByFieldType = useMemo(() => {
if (fieldType === PickerFieldTypes.filter) {
return {
preset: preset || null,
containerStyle: {flexDirection: 'row'},
labelStyle: Typography.text70,
trailingAccessory: trailingAccessory ?? <Icon marginL-s1 source={dropdown}/>
};
} else if (fieldType === PickerFieldTypes.settings) {
return {
preset: preset || null,
label: undefined
};
}
}, [fieldType, preset, trailingAccessory]);

const pickerInnerInput = useMemo(() => {
if (fieldType === PickerFieldTypes.filter) {
return (
<Text text70 numberOfLines={1} style={style}>
{_.isEmpty(label) ? placeholder : label}
</Text>
);
} else if (fieldType === PickerFieldTypes.settings) {
return (
<View flexG row spread>
<Text text70 style={labelStyle}>
{label}
</Text>
<Text text70 $textPrimary style={style}>
{_.isEmpty(label) ? placeholder : label}
</Text>
</View>
);
}
}, [style, labelStyle, fieldType, placeholder, label]);

return {propsByFieldType, pickerInnerInput};
};

export default useFieldType;
57 changes: 13 additions & 44 deletions src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
import _ from 'lodash';
import React, {useMemo, useState, useRef, useCallback, useEffect} from 'react';
import {LayoutChangeEvent} from 'react-native';
import {Typography} from 'style';
import {useThemeProps} from 'hooks';
import {Constants} from '../../commons/new';
import ExpandableOverlay, {ExpandableOverlayProps, ExpandableOverlayMethods} from '../../incubator/expandableOverlay';
import TextField from '../textField';
import Icon from '../icon';
import View from '../view';
import Text from '../text';
import PickerItemsList from './PickerItemsList';
import PickerItem from './PickerItem';
import PickerContext from './PickerContext';
import usePickerSelection from './helpers/usePickerSelection';
import usePickerLabel from './helpers/usePickerLabel';
import usePickerSearch from './helpers/usePickerSearch';
import useImperativePickerHandle from './helpers/useImperativePickerHandle';
import useFieldType from './helpers/useFieldType';
// import usePickerMigrationWarnings from './helpers/usePickerMigrationWarnings';
import {extractPickerItems} from './PickerPresenter';
import {
Expand All @@ -35,8 +32,6 @@ import {
PickerMethods
} from './types';

const dropdown = require('./assets/dropdown.png');

const DIALOG_PROPS = {
bottom: true,
width: '100%',
Expand Down Expand Up @@ -134,6 +129,17 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
placeholder: themeProps.placeholder
});

const {placeholder, style, trailingAccessory} = themeProps;
const {propsByFieldType, pickerInnerInput} = useFieldType({
fieldType,
preset,
trailingAccessory,
style,
placeholder,
labelStyle,
label
});

const onSelectedItemLayout = useCallback((event: LayoutChangeEvent) => {
const y = event.nativeEvent.layout.y;
setSelectedItemPosition(y);
Expand Down Expand Up @@ -176,22 +182,6 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
...pickerModalProps
};

const propsByFieldType = useMemo(() => {
if (fieldType === PickerFieldTypes.filter) {
return {
preset: preset || null,
containerStyle: {flexDirection: 'row'},
labelStyle: Typography.text70,
trailingAccessory: themeProps.trailingAccessory ?? <Icon marginL-s1 source={dropdown}/>
};
} else if (fieldType === PickerFieldTypes.settings) {
return {
preset: preset || null,
label: undefined
};
}
}, [fieldType, preset, themeProps.trailingAccessory]);

const renderPickerItem = useCallback((item: PickerItemProps, index: number): React.ReactElement => {
return <PickerItem key={`${index}-${item.value}`} {...item}/>;
}, []);
Expand Down Expand Up @@ -270,27 +260,6 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
items
]);

const renderPickerInnerInput = () => {
if (fieldType === PickerFieldTypes.filter) {
return (
<Text text70 numberOfLines={1} style={others.style}>
{_.isEmpty(label) ? others.placeholder : label}
</Text>
);
} else if (fieldType === PickerFieldTypes.settings) {
return (
<View flexG row spread>
<Text text70 style={labelStyle}>
{others.label}
</Text>
<Text text70 $textPrimary style={others.style}>
{_.isEmpty(label) ? others.placeholder : label}
</Text>
</View>
);
}
};

return (
//TODO : fix the ExpandableOverlay ts error
<PickerContext.Provider value={contextValue}>
Expand Down Expand Up @@ -326,7 +295,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
value={label}
selection={Constants.isAndroid ? {start: 0} : undefined}
>
{renderPickerInnerInput()}
{pickerInnerInput}
</TextField>
)}
</ExpandableOverlay>
Expand Down