Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Filter Products by Attribute: Fix dropdown search case sensitivity (#…
Browse files Browse the repository at this point in the history
…6096)

* Filter Products by Attribute: Fix dropdown search case sensitivity handling

* Refactor the dropdown search logic and add an isCaseSensitive option

* Dropdown search: Match against the name rather than value

* Clean up and refactor the search dropdown code
  • Loading branch information
danieldudzic committed Mar 25, 2022
1 parent b99ad09 commit 3fea2eb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions assets/js/base/components/dropdown-selector/index.js
Expand Up @@ -31,6 +31,7 @@ import './style.scss';
* @param {boolean} props.multiple Whether multi-select is allowed.
* @param {function():any} props.onChange Function to be called when onChange event fires.
* @param {Array} props.options The option values to show in the select.
* @param {boolean} [props.isCaseSensitive=false] Whether the dropdown search should be case-sensitive.
*/
const DropdownSelector = ( {
attributeLabel = '',
Expand All @@ -43,6 +44,7 @@ const DropdownSelector = ( {
multiple = false,
onChange = () => {},
options = [],
isCaseSensitive = false,
} ) => {
const inputRef = useRef( null );

Expand Down Expand Up @@ -182,11 +184,18 @@ const DropdownSelector = ( {
getItemProps={ getItemProps }
getMenuProps={ getMenuProps }
highlightedIndex={ highlightedIndex }
options={ options.filter(
( option ) =>
! inputValue ||
option.value.startsWith( inputValue )
) }
options={ options.filter( ( option ) => {
let optionName = option.name;
let nameQuery = inputValue?.trim();
if ( ! isCaseSensitive ) {
optionName = optionName.toLowerCase();
nameQuery = nameQuery?.toLowerCase();
}
return (
! nameQuery ||
optionName.includes( nameQuery )
);
} ) }
/>
) }
</div>
Expand All @@ -210,6 +219,7 @@ DropdownSelector.propTypes = {
value: PropTypes.string.isRequired,
} )
),
isCaseSensitive: PropTypes.bool,
};

export default DropdownSelector;

0 comments on commit 3fea2eb

Please sign in to comment.