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

fix search when passing items #3070

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

adids1221
Copy link
Contributor

Description

Picker fix search when using items instead of children.
usePickerSearch hook returns filteredItems now.

Changelog

Picker fix search when using items instead of children.

Additional info

MADS-4193

src/components/picker/PickerItem.tsx Show resolved Hide resolved
src/components/picker/PickerItemsList.tsx Show resolved Hide resolved
Comment on lines 146 to 147
filteredChildren?: ReactNode | undefined,
filteredItems?: Pick<PickerItemProps, 'label' | 'value' | 'disabled'>[]
Copy link
Contributor

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)

Comment on lines 26 to 36
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]);
Copy link
Contributor

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

@@ -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}
Copy link
Contributor

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}

@adids1221 adids1221 requested a review from M-i-k-e-l May 19, 2024 14:08
Copy link
Contributor

@M-i-k-e-l M-i-k-e-l left a 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.

src/components/picker/PickerItemsList.tsx Show resolved Hide resolved
src/components/picker/PickerItem.tsx Show resolved Hide resolved
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;
Copy link
Contributor

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]);
Copy link
Contributor

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

@M-i-k-e-l M-i-k-e-l assigned adids1221 and unassigned M-i-k-e-l May 21, 2024
@adids1221 adids1221 requested a review from M-i-k-e-l May 22, 2024 07:56
@adids1221 adids1221 assigned M-i-k-e-l and unassigned adids1221 May 22, 2024
Comment on lines +12 to +23
const filterItems = (items: any, getItemLabelFunction: any) => {
if (showSearch && !_.isEmpty(searchValue)) {
// @ts-expect-error need to fix children type
return _.filter(children, child => {
// @ts-expect-error need to fix children type to be based on PickerItemProps
const {label, value, getItemLabel: childGetItemLabel} = child.props;
const itemLabel = getItemLabelPresenter(label, value, childGetItemLabel || getItemLabel);
return _.filter(items, item => {
const {label, value, getItemLabel} = item.props || item;
const itemLabel = getItemLabelPresenter(label, value, getItemLabel || getItemLabelFunction);
return !shouldFilterOut(searchValue, itemLabel);
});
}
return items;
};

return children;
}, [showSearch, searchValue, children]);
const filteredItems = useMemo(() => filterItems(children || items, getItemLabel), [showSearch, searchValue, items, children]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets move everything into the useMemo?

@M-i-k-e-l M-i-k-e-l assigned adids1221 and unassigned M-i-k-e-l May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants