Skip to content

PickerItem - support asynchronous and ignore selection #2787

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
Oct 31, 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
9 changes: 7 additions & 2 deletions src/components/picker/PickerItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ const PickerItem = (props: PickerItemProps) => {
return [styles.labelText, isItemDisabled ? styles.labelTextDisabled : undefined, labelStyle];
}, [isItemDisabled, labelStyle]);

const _onPress = useCallback(() => {
onPress?.(context.isMultiMode ? !isSelected : undefined);
const _onPress = useCallback(async (props: any) => {
// Using !(await onPress?.(item)) does not work properly when onPress is not sent
// We have to explicitly state `false` so a synchronous void (undefined) will still work as expected
if (onPress && await onPress(context.isMultiMode ? !isSelected : undefined, props) === false) {
return;
}
if (migrate) {
context.onPress(value);
} else {
Expand Down Expand Up @@ -95,6 +99,7 @@ const PickerItem = (props: PickerItemProps) => {
disabled={isItemDisabled}
testID={testID}
throttleTime={0}
customValue={props.customValue}
{...accessibilityProps}
>
{customRenderItem ? customRenderItem(value, {...props, isSelected, isItemDisabled}, itemLabel) : _renderItem()}
Expand Down
8 changes: 5 additions & 3 deletions src/components/picker/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ModalTopBarProps} from '../modal/TopBar';
// TODO: Replace with new TextField Props after migration to new TextField has completed
// import {TextFieldProps} from '../../../typings/components/Inputs';
import {TextFieldMethods, TextFieldProps as NewTextFieldProps} from '../../incubator/TextField';
import {TouchableOpacityProps} from '../touchableOpacity';

// Note: enum values are uppercase due to legacy
export enum PickerModes {
Expand Down Expand Up @@ -197,7 +198,7 @@ export type PickerPropsWithMulti = PickerBaseProps & {

export type PickerProps = PickerPropsWithSingle | PickerPropsWithMulti;

export interface PickerItemProps {
export interface PickerItemProps extends Pick<TouchableOpacityProps, 'customValue'> {
/**
* Item's value
*/
Expand Down Expand Up @@ -235,10 +236,11 @@ export interface PickerItemProps {
*/
disabled?: boolean;
/**
* Callback for onPress action
* Callback for onPress action, will stop selection if false is returned
* @param selected true\false in multi mode and undefined in single mode
* @param props the props sent to the item
*/
onPress?: (selected?: boolean) => void;
onPress?: (selected: boolean | undefined, props: any) => void | Promise<boolean>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would add the note you added there about returning false to disable closing the picker

/**
* Component test id
*/
Expand Down