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
fix search when passing items #3070
base: master
Are you sure you want to change the base?
Conversation
@@ -44,7 +44,7 @@ const PickerItem = (props: PickerItemProps) => { | |||
|
|||
const isItemDisabled = useMemo(() => { | |||
return !!(disabled || (!isSelected && context.selectionLimit && context.selectionLimit === selectedCounter)); | |||
}, [selectedCounter]); | |||
}, [selectedCounter, disabled]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related to the main issue?
Why not add the other missing deps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it's related.
If the disabled
is not in the deps of the useMemo
then the search return the disabled item as selectable item.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
about the other missing deps, I'll add isSelected
.
Not sure if I should add the context we can talk about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the issue you're talking about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go to the PickerScreen, in the first example change it from children to items:
<Picker
placeholder="Favorite Language"
floatingPlaceholder
value={this.state.language}
enableModalBlur={false}
onChange={item => this.setState({language: item})}
topBarProps={{title: 'Languages'}}
// style={{color: Colors.red20}}
showSearch
searchPlaceholder={'Search a language'}
searchStyle={{color: Colors.blue30, placeholderTextColor: Colors.grey50}}
// onSearchChange={value => console.warn('value', value)}
items={longOptions}
/>
Search for the first disabled item you see ABAP5
and see although it's disabled you can pick it when searching for it.
@@ -35,7 +35,7 @@ const PickerItemsList = (props: PickerItemsListProps) => { | |||
testID | |||
} = props; | |||
const context = useContext(PickerContext); | |||
const [wheelPickerValue, setWheelPickerValue] = useState<PickerSingleValue>(context.value ?? items?.[0].value); | |||
const [wheelPickerValue, setWheelPickerValue] = useState<PickerSingleValue>(context.value ?? items?.[0]?.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related to the main issue?
Can you provide an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it related.
When searching and there are no items matching the search it cause an issue for me and crash with error of undefined value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example = code example 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go to the PickerScreen, in the first example change it from children to items:
<Picker
placeholder="Favorite Language"
floatingPlaceholder
value={this.state.language}
enableModalBlur={false}
onChange={item => this.setState({language: item})}
topBarProps={{title: 'Languages'}}
// style={{color: Colors.red20}}
showSearch
searchPlaceholder={'Search a language'}
searchStyle={{color: Colors.blue30, placeholderTextColor: Colors.grey50}}
// onSearchChange={value => console.warn('value', value)}
items={longOptions}
/>
Enter some bla-bla
phrase in the search box and see the difference.
src/components/picker/types.tsx
Outdated
filteredChildren?: ReactNode | undefined, | ||
filteredItems?: Pick<PickerItemProps, 'label' | 'value' | 'disabled'>[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user should not have to do something like:
onSearchChange={(value, _bla, changed) => {
console.warn('value', value);
console.warn('changed', changed);
}}
in order to get the changes.
You can simply change the name and type of the second value (this should be changed in the API of course)
const filteredItems = useMemo(() => { | ||
if (showSearch && !_.isEmpty(searchValue)) { | ||
return _.filter(items, item => { | ||
const {label, value} = item; | ||
const itemLabel = getItemLabelPresenter(label, value); | ||
return !shouldFilterOut(searchValue, itemLabel); | ||
}); | ||
} | ||
|
||
return items; | ||
}, [showSearch, searchValue, items]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you reduce this code duplication?
IMO there should be a single function that uses both children
and items
src/components/picker/index.tsx
Outdated
@@ -224,7 +225,7 @@ const Picker = React.forwardRef((props: PickerProps, ref) => { | |||
useWheelPicker={useWheelPicker} | |||
mode={mode} | |||
useDialog={useDialog} | |||
items={useItems ? items : undefined} | |||
items={useItems ? filteredItems || items : undefined} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should be filteredItems
and not filteredItems || items
, just like below there's only {filteredChildren}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another issue: the onSearchChange
value
we get is correct, but the children
`items` is from the previous search (it's one search behind).
It does not look like a regression, but you should definitely fix it or add a ticket to fix it.
@@ -35,7 +35,7 @@ const PickerItemsList = (props: PickerItemsListProps) => { | |||
testID | |||
} = props; | |||
const context = useContext(PickerContext); | |||
const [wheelPickerValue, setWheelPickerValue] = useState<PickerSingleValue>(context.value ?? items?.[0].value); | |||
const [wheelPickerValue, setWheelPickerValue] = useState<PickerSingleValue>(context.value ?? items?.[0]?.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example = code example 🙏
@@ -44,7 +44,7 @@ const PickerItem = (props: PickerItemProps) => { | |||
|
|||
const isItemDisabled = useMemo(() => { | |||
return !!(disabled || (!isSelected && context.selectionLimit && context.selectionLimit === selectedCounter)); | |||
}, [selectedCounter]); | |||
}, [selectedCounter, disabled]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the issue you're talking about.
const {label, value, getItemLabel: childGetItemLabel} = child.props; | ||
const itemLabel = getItemLabelPresenter(label, value, childGetItemLabel || getItemLabel); | ||
return _.filter(items, item => { | ||
const {label, value, getItemLabel: itemGetItemLabel} = item.props || item; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getItemLabel
seems clear (clearer than itemGetItemLabel
IMO)
Maybe you can rename getItemLabel
to something else like getItemLabelProps
(line 9) which will make this simpler.
Not sure you even need to send it (getItemLabelFunction
) as an input to the filterItems
function
|
||
return children; | ||
}, [showSearch, searchValue, children]); | ||
const filteredItems = useMemo(() => filterItems(children || items, getItemLabel), [showSearch, searchValue, items]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably need children
as a dependency here
Description
Picker fix search when using
items
instead ofchildren
.usePickerSearch hook returns filteredItems now.
Changelog
Picker fix search when using
items
instead ofchildren
.Additional info
MADS-4193