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

Filter Products by Attribute: Fix dropdown search case sensitivity #6096

Merged
merged 4 commits into from Mar 25, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 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 Whether the dropdown search should be case-sensitive.
danieldudzic marked this conversation as resolved.
Show resolved Hide resolved
*/
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,20 @@ const DropdownSelector = ( {
getItemProps={ getItemProps }
getMenuProps={ getMenuProps }
highlightedIndex={ highlightedIndex }
options={ options.filter(
( option ) =>
options={ options.filter( ( option ) => {
if ( isCaseSensitive ) {
return (
! inputValue ||
option.value.includes( inputValue )
);
}
return (
! inputValue ||
option.value.startsWith( inputValue )
) }
option.value
.toLowerCase()
.includes( inputValue.toLowerCase() )
);
} ) }
danieldudzic marked this conversation as resolved.
Show resolved Hide resolved
/>
) }
</div>
Expand All @@ -210,6 +221,7 @@ DropdownSelector.propTypes = {
value: PropTypes.string.isRequired,
} )
),
isCaseSensitive: PropTypes.bool,
};

export default DropdownSelector;