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 {
);
};

onTopElementPress = (allOptionsSelected: boolean) => {
if (allOptionsSelected) {
this.setState({statOption: []});
} else {
const allValues = statusOptions.map(option => option.value);
this.setState({statOption: 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}
renderCustomTopElement={value => {
const allOptionsSelected = Array.isArray(value) && value.length === statusOptions.length;
return (
<View margin-s3>
<Button
label={allOptionsSelected ? 'Unselect All' : 'Select All'}
onPress={() => this.onTopElementPress(allOptionsSelected)}
size="small"
/>
</View>
);
}}
/>

<Text marginB-10 text70 $textDefault>
Custom Picker:
</Text>
Expand Down
4 changes: 3 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,
renderCustomTopElement
} = props;
const context = useContext(PickerContext);

Expand Down Expand Up @@ -167,6 +168,7 @@ const PickerItemsList = (props: PickerItemsListProps) => {
) : (
<>
{renderSearchInput()}
{!!renderCustomTopElement && renderCustomTopElement(context.value)}
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
{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,
renderCustomTopElement,
...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}
renderCustomTopElement={renderCustomTopElement}
>
{filteredItems}
</PickerItemsList>
Expand Down
10 changes: 10 additions & 0 deletions src/components/picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,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 All @@ -206,6 +210,10 @@ export type PickerBaseProps = Omit<TextFieldProps, 'value' | 'onChange'> &
itemProps: PickerItemProps & {isSelected: boolean; isItemDisabled: boolean},
label?: string
) => React.ReactElement;
/**
* Custom top element, value prop is relevant for multi picker use cases
M-i-k-e-l marked this conversation as resolved.
Show resolved Hide resolved
*/
renderCustomTopElement?: (value?: PickerValue) => React.ReactElement;
/**
* Add onPress callback for when pressing the picker
*/
Expand Down Expand Up @@ -305,6 +313,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 +324,7 @@ export type PickerItemsListProps = Pick<
| 'useSafeArea'
| 'showLoader'
| 'customLoaderElement'
| 'renderCustomTopElement'
| 'showSearch'
| 'searchStyle'
| 'searchPlaceholder'
Expand Down