Skip to content

Picker - add onItemPress #2786

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions src/components/picker/helpers/usePickerSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import _ from 'lodash';
import {PickerProps, PickerValue, PickerSingleValue, PickerMultiValue, PickerModes} from '../types';

interface UsePickerSelectionProps
extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'getItemValue' | 'topBarProps' | 'mode'> {
extends Pick<PickerProps, 'migrate' | 'value' | 'onChange' | 'getItemValue' | 'topBarProps' | 'mode' | 'onItemPress'> {
pickerExpandableRef: RefObject<any>;
setSearchValue: (searchValue: string) => void;
}

const usePickerSelection = (props: UsePickerSelectionProps) => {
const {migrate, value, onChange, topBarProps, pickerExpandableRef, getItemValue, setSearchValue, mode} = props;
const {migrate, value, onChange, topBarProps, pickerExpandableRef, getItemValue, setSearchValue, mode, onItemPress} =
props;
const [multiDraftValue, setMultiDraftValue] = useState(value as PickerMultiValue);
const [multiFinalValue, setMultiFinalValue] = useState(value as PickerMultiValue);

Expand All @@ -20,7 +21,11 @@ const usePickerSelection = (props: UsePickerSelectionProps) => {
}
}, [value]);

const onDoneSelecting = useCallback((item: PickerValue) => {
const onDoneSelecting = useCallback(async (item: PickerValue) => {
// Using !(await onItemPress?.(item)) does not work properly when onItemPress is not sent
if (onItemPress && !(await onItemPress(item))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have an onPress callback in PickerItemProps, do you think we should use that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The other way I found to do this is shown in another PR, you choose

return;
}
setSearchValue('');
setMultiFinalValue(item as PickerMultiValue);
pickerExpandableRef.current?.closeExpandable?.();
Expand Down
4 changes: 3 additions & 1 deletion src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
getItemLabel,
getItemValue,
renderItem,
onItemPress,
children,
useSafeArea,
// TODO: Remove migrate props and migrate code
Expand Down Expand Up @@ -123,7 +124,8 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
getItemValue,
topBarProps,
setSearchValue,
mode
mode,
onItemPress
});

const {label, accessibilityInfo} = usePickerLabel({
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 @@ -95,6 +95,10 @@ export type PickerBaseProps = Omit<NewTextFieldProps, 'value' | 'onChange'> & {
itemProps: PickerItemProps & {isSelected: boolean; isItemDisabled: boolean},
label?: string
) => React.ReactElement;
/**
* A callback to be used when an item has been pressed, will stop selection if false is returned
*/
onItemPress?: (item: PickerValue) => 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.

assuming people dont need onItemPress to be asynchronous, typing wise they will still need to return a promise and that can be annoying, no?
if my logic is synchronous, TS won't complain?

/**
* Render custom picker modal (e.g ({visible, children, toggleModal}) => {...})
*/
Expand Down