fix: Modalities in study list should select starts with as primary (#5886)

This commit is contained in:
Bill Wallace 2026-03-11 14:28:20 -04:00 committed by GitHub
parent 4c2bd8d5ed
commit b83e978df8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ReactSelect, { components } from 'react-select';
@ -6,6 +6,34 @@ import { Icons } from '@ohif/ui-next';
import './Select.css';
/**
* Reorder options so items that START with the search text come first
* (shortest first), then items that only contain the text. This order is
* passed to react-select so the focused/highlighted option (e.g. on Enter) is correct.
*/
function orderOptionsByStartsWith(options, searchInput) {
if (!options?.length || !searchInput?.trim()) return options ?? [];
const input = searchInput.trim().toLowerCase();
const startsWith = options.filter(opt => {
const label = (opt.label ?? opt.value ?? '').toString().toLowerCase();
return label.startsWith(input);
});
const containsOnly = options.filter(opt => {
const label = (opt.label ?? opt.value ?? '').toString().toLowerCase();
return label.includes(input) && !label.startsWith(input);
});
const rest = options.filter(opt => {
const label = (opt.label ?? opt.value ?? '').toString().toLowerCase();
return !label.includes(input);
});
startsWith.sort((a, b) => {
const lenA = (a.label ?? a.value ?? '').toString().length;
const lenB = (b.label ?? b.value ?? '').toString().length;
return lenA - lenB;
});
return [...startsWith, ...containsOnly, ...rest];
}
const MultiValue = props => {
const values = props.selectProps.value;
const lastValue = values[values.length - 1];
@ -56,6 +84,11 @@ const Select = ({
components = {},
value = [],
}) => {
const [filterInput, setFilterInput] = useState('');
const orderedOptions = isSearchable
? orderOptionsByStartsWith(options, filterInput)
: options;
const _noIconComponents = {
DropdownIndicator: () => null,
IndicatorSeparator: () => null,
@ -92,9 +125,11 @@ const Select = ({
hideSelectedOptions={hideSelectedOptions}
components={_components}
placeholder={placeholder}
options={options}
options={orderedOptions}
blurInputOnSelect={true}
menuPortalTarget={document.body}
onInputChange={isSearchable ? setFilterInput : undefined}
onMenuClose={isSearchable ? () => setFilterInput('') : undefined}
styles={{
menuPortal: base => ({ ...base, zIndex: 9999 }),
}}