Skip to content

Picker update items when children is changed #2599

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 8 commits into from
Jun 20, 2023
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
8 changes: 8 additions & 0 deletions src/components/picker/PickerItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Icon from '../icon';
import WheelPicker from '../WheelPicker';
import {PickerItemProps, PickerItemsListProps, PickerSingleValue} from './types';
import PickerContext from './PickerContext';
import PickerItem from './PickerItem';

const keyExtractor = (_item: string, index: number) => index.toString();

Expand Down Expand Up @@ -66,7 +67,14 @@ const PickerItemsList = (props: PickerItemsListProps) => {
},
[children]);

const renderPropItems = useCallback(({item}: ListRenderItemInfo<PickerItemProps>) => {
return <PickerItem {...item}/>;
}, []);

const renderList = () => {
if (items) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like adding another return, but I can't say I can think of a better alternative.

return <FlatList data={items} renderItem={renderPropItems} keyExtractor={keyExtractor} {...listProps}/>;
}
return (
<FlatList
data={_.times(React.Children.count(children))}
Expand Down
3 changes: 2 additions & 1 deletion src/components/picker/api/picker.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"description": "Pass props to the list component that wraps the picker options (allows to control FlatList behavior)"
},
{"name": "pickerModalProps", "type": "ModalProps", "description": "Pass props to the picker modal"},
{"name": "useSafeArea", "type": "boolean", "description": "Add safe area in the Picker modal view"}
{"name": "useSafeArea", "type": "boolean", "description": "Add safe area in the Picker modal view"},
{"name": "items", "type": "{label: string, value: string | number}[]", "description": "Data source for Picker"}
],
"snippet": [
"<Picker",
Expand Down
2 changes: 1 addition & 1 deletion src/components/picker/helpers/usePickerLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const usePickerLabel = (props: UsePickerLabelProps) => {

return _.get(selectedItem, 'label');
},
[getLabelsFromArray]);
[getLabelsFromArray, items]);

const accessibilityInfo = useMemo(() => {
const label = _getLabel(value);
Expand Down
14 changes: 11 additions & 3 deletions src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// TODO: consider deprecating renderCustomModal prop
// TODO: deprecate onShow cause it's already supported by passing it in pickerModalProps
import _ from 'lodash';
import React, {useMemo, useState, useRef, useCallback} from 'react';
import React, {useMemo, useState, useRef, useCallback, useEffect} from 'react';
import {LayoutChangeEvent} from 'react-native';
import {Typography} from 'style';
import {useThemeProps} from 'hooks';
Expand Down Expand Up @@ -90,12 +90,19 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
migrateTextField = true,
accessibilityLabel,
accessibilityHint,
items: propItems,
...others
} = themeProps;
const {preset} = others;

const [selectedItemPosition, setSelectedItemPosition] = useState(0);
const [items] = useState(extractPickerItems(themeProps));
const [items, setItems] = useState(propItems || extractPickerItems(themeProps));

useEffect(() => {
if (propItems) {
setItems(propItems);
}
}, [propItems]);

const pickerExpandable = useRef<ExpandableOverlayMethods>(null);

Expand Down Expand Up @@ -204,11 +211,12 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
};

const expandableModalContent = useMemo(() => {
const useItems = useWheelPicker || propItems;
return (
<PickerItemsList
testID={`${testID}.modal`}
useWheelPicker={useWheelPicker}
items={useWheelPicker ? items : undefined}
items={useItems ? items : undefined}
topBarProps={{
...topBarProps,
onCancel: cancelSelect,
Expand Down
4 changes: 4 additions & 0 deletions src/components/picker/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export type PickerBaseProps = Omit<NewTextFieldProps, 'value' | 'onChange'> &
* Add safe area in the Picker modal view
*/
useSafeArea?: boolean;
/**
* Data source for Picker
*/
items?: Pick<PickerItemProps, 'label' | 'value' | 'disabled'>[];
/**
* Component test id
*/
Expand Down