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

@@ -44,7 +44,7 @@ const PickerItem = (props: PickerItemProps) => {

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

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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

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?

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

example = code example 🙏

Copy link
Contributor Author

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.

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.

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

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

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;
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
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