Skip to content

Support useWheelPicker in Picker component #2100

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 2 commits into from
Jul 10, 2022
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
5 changes: 3 additions & 2 deletions demo/src/screens/componentScreens/PickerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ export default class PickerScreen extends Component {
</Picker>

<Picker
title="Native Picker"
title="Wheel Picker"
placeholder="Pick a Language"
useNativePicker
// useNativePicker
useWheelPicker
value={this.state.nativePickerValue}
onChange={nativePickerValue => this.setState({nativePickerValue})}
rightIconSource={dropdown}
Expand Down
46 changes: 39 additions & 7 deletions src/components/picker/PickerItemsList.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import _ from 'lodash';
import React, {useCallback} from 'react';
import React, {useCallback, useContext, useState} from 'react';
import {StyleSheet, FlatList, TextInput, ListRenderItemInfo} from 'react-native';
import {Typography, Colors} from '../../style';
import Assets from '../../assets';
import Modal from '../modal';
import View from '../view';
import Text from '../text';
import Icon from '../icon';
import {PickerItemProps, PickerItemsListProps} from './types';
import WheelPicker from '../../incubator/WheelPicker';
import {PickerItemProps, PickerItemsListProps, PickerSingleValue} from './types';
import PickerContext from './PickerContext';

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

const PickerItemsList = (props: PickerItemsListProps) => {
const {
useWheelPicker,
topBarProps,
listProps,
children,
items,
showSearch,
searchStyle = {},
searchPlaceholder = 'Search...',
Expand All @@ -23,6 +28,8 @@ const PickerItemsList = (props: PickerItemsListProps) => {
useSafeArea,
testID
} = props;
const context = useContext(PickerContext);
const [wheelPickerValue, setWheelPickerValue] = useState<PickerSingleValue>(context.value ?? items?.[0].value);

const renderSearchInput = () => {
if (showSearch) {
Expand Down Expand Up @@ -59,18 +66,43 @@ const PickerItemsList = (props: PickerItemsListProps) => {
},
[children]);

return (
<View bg-$backgroundDefault flex useSafeArea={useSafeArea}>
<Modal.TopBar {...topBarProps}/>
{renderSearchInput()}

const renderList = () => {
return (
<FlatList
data={_.times(React.Children.count(children))}
// @ts-expect-error
renderItem={renderItem}
keyExtractor={keyExtractor}
{...listProps}
/>
);
};

const renderWheel = () => {
return (
<View>
<View row spread padding-page>
<Text>{topBarProps.title}</Text>
<Text text70 primary accessibilityRole={'button'} onPress={() => context.onPress(wheelPickerValue)}>
{topBarProps.doneLabel ?? 'Select'}
</Text>
</View>
<WheelPicker initialValue={context.value as PickerSingleValue} items={items} onChange={setWheelPickerValue}/>
</View>
);
};

return (
<View bg-$backgroundDefault flex useSafeArea={useSafeArea}>
{!useWheelPicker && (
<>
{<Modal.TopBar {...topBarProps}/>}
{renderSearchInput()}
{renderList()}
</>
)}

{useWheelPicker && renderWheel()}
</View>
);
};
Expand Down
5 changes: 5 additions & 0 deletions src/components/picker/api/picker.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
"type": "boolean",
"description": "Allow to use the native picker solution (different style for iOS and Android)"
},
{
"name": "useWheelPicker",
"type": "boolean",
"description": "Use wheel picker instead of a list picker"
},
{
"name": "renderNativePicker",
"type": "(props) => void",
Expand Down
17 changes: 15 additions & 2 deletions src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ import {

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

const DIALOG_PROPS = {
bottom: true,
width: '100%',
height: 250
};

const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps & BaseComponentInjectedProps) => {
const {
mode,
Expand All @@ -53,6 +59,7 @@ const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps
searchPlaceholder,
renderCustomSearch,
useNativePicker,
useWheelPicker,
renderPicker,
customPickerProps,
containerStyle,
Expand Down Expand Up @@ -190,6 +197,8 @@ const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps
return (
<PickerItemsList
testID={`${testID}.modal`}
useWheelPicker={useWheelPicker}
items={useWheelPicker ? items : undefined}
topBarProps={{
...topBarProps,
onCancel: cancelSelect,
Expand Down Expand Up @@ -221,7 +230,9 @@ const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps
renderCustomSearch,
listProps,
filteredChildren,
useSafeArea
useSafeArea,
useWheelPicker,
items
]);

const renderPickerInnerInput = () => {
Expand Down Expand Up @@ -253,7 +264,9 @@ const Picker = (props: PropsWithChildren<PickerProps> & ForwardRefInjectedProps
<PickerContext.Provider value={contextValue}>
<ExpandableOverlay
ref={pickerExpandable}
useDialog={useWheelPicker}
modalProps={modalProps}
dialogProps={DIALOG_PROPS}
expandableContent={expandableModalContent}
renderCustomOverlay={renderCustomModal ? _renderCustomModal : undefined}
onPress={onPress}
Expand Down Expand Up @@ -304,7 +317,7 @@ Picker.extractPickerItems = (props: PropsWithChildren<PickerProps>) => {
// @ts-expect-error handle use PickerItemProps once exist
label: child?.props.label
}));
return items;
return items ?? [];
};

export {PickerProps, PickerItemProps, PickerValue, PickerModes, PickerFieldTypes, PickerSearchStyle, PickerMethods};
Expand Down
9 changes: 8 additions & 1 deletion src/components/picker/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export type PickerBaseProps = Omit<TextFieldProps, 'value' | 'onChange'> &
* Allow to use the native picker solution (different style for iOS and Android)
*/
useNativePicker?: boolean;
/**
* Use wheel picker instead of a list picker
*/
useWheelPicker?: boolean;
/**
* Callback for rendering a custom native picker inside the dialog (relevant to native picker only)
*/
Expand Down Expand Up @@ -258,7 +262,10 @@ export type PickerItemsListProps = Pick<
| 'onSearchChange'
| 'renderCustomSearch'
| 'useSafeArea'
| 'useWheelPicker'
| 'testID'
>;
> & {
items?: {value: any; label: any}[];
};

export type PickerMethods = TextFieldMethods & ExpandableOverlayMethods;