Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions platform/ui/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ReactSelect, { components } from 'react-select';
Expand Down Expand Up @@ -56,6 +56,44 @@ const Select = ({
components = {},
value = [],
}) => {
const [inputValue, setInputValue] = React.useState('');

// Memoized and sorted list of options based on inputValue:
const filteredOptions = React.useMemo(() => {
if (!options || !Array.isArray(options)) {
return [];
}

return [...options].sort((a, b) => {
const input = inputValue.toLowerCase();

const aLabel = (a.label || '').toLowerCase();
const bLabel = (b.label || '').toLowerCase();

const aExact = aLabel === input;
const bExact = bLabel === input;

if (aExact && !bExact) {
return -1;
}
if (!aExact && bExact) {
return 1;
}

const aIncludes = aLabel.includes(input);
const bIncludes = bLabel.includes(input);

if (aIncludes && !bIncludes) {
return -1;
}
if (!aIncludes && bIncludes) {
return 1;
}

return aLabel.localeCompare(bLabel);
});
}, [options, inputValue]);

const _noIconComponents = {
DropdownIndicator: () => null,
IndicatorSeparator: () => null,
Expand Down Expand Up @@ -92,12 +130,18 @@ const Select = ({
hideSelectedOptions={hideSelectedOptions}
components={_components}
placeholder={placeholder}
options={options}
options={filteredOptions}
blurInputOnSelect={true}
menuPortalTarget={document.body}
styles={{
menuPortal: base => ({ ...base, zIndex: 9999 }),
}}
inputValue={inputValue}
onInputChange={(value, { action }) => {
if (action !== 'input-blur' && action !== 'menu-close') {
setInputValue(value);
}
}}
value={value && Array.isArray(value) ? selectedOptions : value}
onChange={(selectedOptions, { action }) => {
if (selectedOptions === null) {
Expand Down