Skip to content

Feat/Picker selectionLimit prop #1270

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 1 commit into from
Apr 27, 2021
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
3 changes: 2 additions & 1 deletion demo/src/screens/componentScreens/PickerScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ export default class PickerScreen extends Component {

<Picker
marginT-20
placeholder="Favorite Languages"
placeholder="Favorite Languages (up to 3)"
value={this.state.languages}
onChange={items => this.setState({languages: items})}
mode={Picker.modes.MULTI}
selectionLimit={3}
rightIconSource={dropdown}
>
{_.map(options, option => (
Expand Down
13 changes: 9 additions & 4 deletions src/components/picker/PickerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ const PickerItem = props => {
const itemValue = !migrate && _.isPlainObject(value) ? value?.value : value;
const isSelected = isItemSelected(itemValue, context.value);
const itemLabel = getItemLabel(label, value, props.getItemLabel || context.getItemLabel);
const selectedCounter = context.selectionLimit && context.value?.length;
const accessibilityProps = {
accessibilityState: isSelected ? {selected: true} : undefined,
accessibilityHint: 'Double click to select this suggestion',
...Modifiers.extractAccessibilityProps(props)
};

const isItemDisabled = useMemo(() => {
return disabled || (!isSelected && context.selectionLimit && context.selectionLimit === selectedCounter);
}, [selectedCounter]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Notice you use a simple variable as a dependency in this useMemo.
Each time the component will render, selectedCounter will be recreated and will invoke the useMemo
Overall it's not that dramatic in this case, but better do it right.

I'd move selectedCounter into the useMemo method and include in the dependencies array values from props/context/state.


useEffect(() => {
if (_.isPlainObject(value)) {
LogService.warn('UILib Picker.Item will stop supporting passing object as value & label (e.g {value, label}) in the next major version. Please pass separate label and value props');
Expand All @@ -48,9 +53,9 @@ const PickerItem = props => {

const selectedIndicator = useMemo(() => {
if (isSelected) {
return <Image source={selectedIcon} tintColor={disabled ? Colors.dark60 : selectedIconColor}/>;
return <Image source={selectedIcon} tintColor={isItemDisabled ? Colors.dark60 : selectedIconColor}/>;
}
}, [isSelected, disabled, selectedIcon, selectedIconColor]);
}, [isSelected, isItemDisabled, selectedIcon, selectedIconColor]);

const _onPress = useCallback(() => {
if (migrate) {
Expand All @@ -67,7 +72,7 @@ const PickerItem = props => {
const _renderItem = () => {
return (
<View style={styles.container} flex row spread centerV>
<Text numberOfLines={1} style={[styles.labelText, disabled && styles.labelTextDisabled]}>
<Text numberOfLines={1} style={[styles.labelText, isItemDisabled && styles.labelTextDisabled]}>
{itemLabel}
</Text>
{selectedIndicator}
Expand All @@ -84,7 +89,7 @@ const PickerItem = props => {
activeOpacity={0.5}
onPress={_onPress}
onLayout={isSelected ? onSelectedLayout : undefined}
disabled={disabled}
disabled={isItemDisabled}
testID={testID}
throttleTime={0}
{...accessibilityProps}
Expand Down
9 changes: 7 additions & 2 deletions src/components/picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Picker extends Component {
* SINGLE mode or MULTI mode
*/
mode: PropTypes.oneOf(Object.keys(PICKER_MODES)),
/**
* Limit the number of selected items
*/
selectionLimit: PropTypes.number,
/**
* Adds blur effect to picker modal (iOS only)
*/
Expand Down Expand Up @@ -217,7 +221,7 @@ class Picker extends Component {

getContextValue = () => {
const {value, searchValue} = this.state;
const {migrate, mode, getItemValue, getItemLabel, renderItem, showSearch} = this.props;
const {migrate, mode, getItemValue, getItemLabel, renderItem, showSearch, selectionLimit} = this.props;
const pickerValue = !migrate && _.isPlainObject(value) ? value?.value : value;
return {
migrate,
Expand All @@ -229,7 +233,8 @@ class Picker extends Component {
onSelectedLayout: this.onSelectedItemLayout,
renderItem,
showSearch,
searchValue
searchValue,
selectionLimit
};
};

Expand Down