Skip to content
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

Feat/picker custom top element #3465

Merged
merged 9 commits into from
Jan 19, 2025
46 changes: 45 additions & 1 deletion demo/src/screens/componentScreens/PickerScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ const dialogOptions = [
{label: 'Option 8', value: 6}
];

const statusOptions = [
{label: 'Overview', value: 'overview'},
{label: 'In Progress', value: 'inProgress'},
{label: 'Completed', value: 'completed'},
{label: 'Pending Review', value: 'pendingReview'},
{label: 'Approved', value: 'approved'},
{label: 'Rejected', value: 'rejected'}
];

export default class PickerScreen extends Component {
picker = React.createRef<PickerMethods>();
state = {
Expand All @@ -96,6 +105,7 @@ export default class PickerScreen extends Component {
dialogPickerValue: 'java',
customModalValues: [],
filter: undefined,
statOption: [],
scheme: undefined,
contact: 0
};
Expand All @@ -122,6 +132,15 @@ export default class PickerScreen extends Component {
);
};

handleTopElementPress = (allOptionsSelected: boolean, setValue: any) => {
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
if (allOptionsSelected) {
setValue([]);
} else {
const allValues = statusOptions.map(option => option.value);
setValue(allValues);
}
};

render() {
return (
<ScrollView keyboardShouldPersistTaps="always">
Expand Down Expand Up @@ -195,7 +214,32 @@ export default class PickerScreen extends Component {
searchPlaceholder={'Search a language'}
items={dialogOptions}
/>


<Text text70 $textDefault>
Custom Top Element:
</Text>
<Picker
placeholder="Status"
floatingPlaceholder
value={this.state.statOption}
onChange={items => this.setState({statOption: items})}
topBarProps={{title: 'Status'}}
mode={Picker.modes.MULTI}
items={statusOptions}
customTopElement={({value, setValue}) => {
const allOptionsSelected = Array.isArray(value) && value.length === statusOptions.length;
return (
<View margin-s3>
<Button
label={allOptionsSelected ? 'Unselect All' : 'Select All'}
onPress={() => this.handleTopElementPress(allOptionsSelected, setValue)}
size="small"
/>
</View>
);
}}
/>

<Text marginB-10 text70 $textDefault>
Custom Picker:
</Text>
Expand Down
17 changes: 16 additions & 1 deletion src/components/picker/PickerItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const PickerItemsList = (props: PickerItemsListProps) => {
mode,
testID,
showLoader,
customLoaderElement
customLoaderElement,
customTopElement
} = props;
const context = useContext(PickerContext);

Expand Down Expand Up @@ -161,12 +162,26 @@ const PickerItemsList = (props: PickerItemsListProps) => {
);
};

const renderCustomTopElement = () => {
const {isMultiMode, value, setValue} = context;
if (customTopElement) {
if (isMultiMode) {
console.log(`renderCustomTopElement, isMultiMode!, value:`, value);
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
//@ts-expect-error - PickerWithMultiValue props need to pass the correct props
return customTopElement({value, setValue});
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
}
//@ts-expect-error - PickerWithSingleValue props
return customTopElement();
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
}
};

const renderContent = () => {
return useWheelPicker ? (
renderWheel()
) : (
<>
{renderSearchInput()}
{renderCustomTopElement()}
{renderList()}
</>
);
Expand Down
24 changes: 22 additions & 2 deletions src/components/picker/helpers/usePickerSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ 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' | 'onItemSelection' | 'getItemValue' | 'topBarProps' | 'mode'
> {
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,
onItemSelection,
topBarProps,
pickerExpandableRef,
getItemValue,
setSearchValue,
mode
} = props;
const [multiDraftValue, setMultiDraftValue] = useState(value as PickerMultiValue);
const [multiFinalValue, setMultiFinalValue] = useState(value as PickerMultiValue);

useEffect(() => {
if (mode === PickerModes.MULTI && multiFinalValue !== multiDraftValue) {
onItemSelection?.(multiDraftValue);
}
}, [multiDraftValue]);

useEffect(() => {
if (mode === PickerModes.MULTI && multiFinalValue !== value) {
setMultiDraftValue(value as PickerMultiValue);
Expand Down Expand Up @@ -50,6 +69,7 @@ const usePickerSelection = (props: UsePickerSelectionProps) => {

return {
multiDraftValue,
setMultiDraftValue,
onDoneSelecting,
toggleItemSelection,
cancelSelect
Expand Down
7 changes: 6 additions & 1 deletion src/components/picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
labelStyle,
testID,
onChange,
onItemSelection,
onPress,
onSearchChange,
topBarProps,
Expand All @@ -78,6 +79,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
items: propItems,
showLoader,
customLoaderElement,
customTopElement,
...others
} = themeProps;
const {preset, placeholder, style, trailingAccessory, label: propsLabel} = others;
Expand All @@ -101,10 +103,11 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
setSearchValue,
onSearchChange: _onSearchChange
} = usePickerSearch({showSearch, onSearchChange, getItemLabel, children, items});
const {multiDraftValue, onDoneSelecting, toggleItemSelection, cancelSelect} = usePickerSelection({
const {multiDraftValue, setMultiDraftValue, onDoneSelecting, toggleItemSelection, cancelSelect} = usePickerSelection({
migrate,
value,
onChange,
onItemSelection,
pickerExpandableRef: pickerExpandable,
getItemValue,
topBarProps,
Expand Down Expand Up @@ -145,6 +148,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
return {
migrate,
value: mode === PickerModes.MULTI ? multiDraftValue : pickerValue,
setValue: mode === PickerModes.MULTI ? setMultiDraftValue : undefined,
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
onPress: mode === PickerModes.MULTI ? toggleItemSelection : onDoneSelecting,
isMultiMode: mode === PickerModes.MULTI,
getItemValue,
Expand Down Expand Up @@ -245,6 +249,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => {
useSafeArea={useSafeArea}
showLoader={showLoader}
customLoaderElement={customLoaderElement}
customTopElement={customTopElement}
>
{filteredItems}
</PickerItemsList>
Expand Down
25 changes: 25 additions & 0 deletions src/components/picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import {ModalTopBarProps} from '../modal/TopBar';
import {TextFieldMethods, TextFieldProps} from '../textField';
import {TouchableOpacityProps} from '../touchableOpacity';

export interface CustomTopElementProps {
/**
* The current multi draft value
*/
value: PickerMultiValue;
/*
* Set the new multi draft value
*/
setValue: (value: PickerMultiValue) => void;
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
}

// Note: enum values are uppercase due to legacy
export enum PickerModes {
SINGLE = 'SINGLE',
Expand Down Expand Up @@ -180,6 +191,10 @@ export type PickerBaseProps = Omit<TextFieldProps, 'value' | 'onChange'> &
* Callback for when picker value change
*/
onChange?: (value: PickerValue) => void;
/**
* Callback for when picker item is selected (only for multi mode)
*/
onItemSelection?: (value: PickerMultiValue) => void;
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
/**
* SINGLE or MULTI selection mode
*/
Expand Down Expand Up @@ -237,12 +252,20 @@ export type PickerBaseProps = Omit<TextFieldProps, 'value' | 'onChange'> &
};

export type PickerPropsWithSingle = PickerBaseProps & {
/**
* Custom top element
*/
customTopElement?: () => React.ReactElement;
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
mode?: PickerModes.SINGLE;
value?: PickerSingleValue;
onChange?: (value: PickerSingleValue) => void;
};

export type PickerPropsWithMulti = PickerBaseProps & {
/**
* Custom top element, props (vale, setValue) are for multi picker only
*/
customTopElement?: (props: CustomTopElementProps) => React.ReactElement;
mode?: PickerModes.MULTI;
value?: PickerMultiValue;
onChange?: (value: PickerMultiValue) => void;
Expand Down Expand Up @@ -305,6 +328,7 @@ export interface PickerContextProps
isMultiMode: boolean;
onSelectedLayout: (event: any) => any;
selectionLimit: PickerProps['selectionLimit'];
setValue?: React.Dispatch<React.SetStateAction<PickerMultiValue>> | undefined;
}

export type PickerItemsListProps = Pick<
Expand All @@ -315,6 +339,7 @@ export type PickerItemsListProps = Pick<
| 'useSafeArea'
| 'showLoader'
| 'customLoaderElement'
| 'customTopElement'
| 'showSearch'
| 'searchStyle'
| 'searchPlaceholder'
Expand Down