2022-04-29 07:10:10 -04:00
|
|
|
import { useState, useEffect, forwardRef } from 'react'
|
2022-04-22 09:49:26 -04:00
|
|
|
import { useCombobox } from 'downshift'
|
|
|
|
import classnames from 'classnames'
|
2024-01-04 10:20:25 -05:00
|
|
|
import { escapeRegExp } from 'lodash'
|
2024-04-16 11:06:42 -04:00
|
|
|
import { bsVersion } from '@/features/utils/bootstrap-5'
|
2024-05-15 10:31:00 -04:00
|
|
|
import OLFormControl from '@/features/ui/components/ol/ol-form-control'
|
2024-05-20 08:50:31 -04:00
|
|
|
import { DropdownItem } from '@/features/ui/components/bootstrap-5/dropdown-menu'
|
|
|
|
import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
|
2022-04-22 09:49:26 -04:00
|
|
|
|
|
|
|
type DownshiftInputProps = {
|
2024-01-04 10:20:25 -05:00
|
|
|
highlightMatches?: boolean
|
2022-04-22 09:49:26 -04:00
|
|
|
items: string[]
|
2023-11-08 05:07:42 -05:00
|
|
|
itemsTitle?: string
|
2022-04-22 09:49:26 -04:00
|
|
|
inputValue: string
|
2022-04-27 04:42:48 -04:00
|
|
|
label: string
|
2023-11-24 05:44:03 -05:00
|
|
|
setValue: (value: string) => void
|
2022-04-29 07:10:10 -04:00
|
|
|
inputRef?: React.ForwardedRef<HTMLInputElement>
|
2023-11-08 05:07:42 -05:00
|
|
|
showLabel?: boolean
|
|
|
|
showSuggestedText?: boolean
|
2022-04-22 09:49:26 -04:00
|
|
|
} & React.InputHTMLAttributes<HTMLInputElement>
|
|
|
|
|
|
|
|
const filterItemsByInputValue = (
|
|
|
|
items: DownshiftInputProps['items'],
|
|
|
|
inputValue: DownshiftInputProps['inputValue']
|
|
|
|
) => items.filter(item => item.toLowerCase().includes(inputValue.toLowerCase()))
|
|
|
|
|
2022-04-29 07:10:10 -04:00
|
|
|
function Downshift({
|
2024-01-04 10:20:25 -05:00
|
|
|
highlightMatches = false,
|
2022-04-22 09:49:26 -04:00
|
|
|
items,
|
2023-11-08 05:07:42 -05:00
|
|
|
itemsTitle,
|
2022-04-22 09:49:26 -04:00
|
|
|
inputValue,
|
|
|
|
placeholder,
|
2022-04-27 04:42:48 -04:00
|
|
|
label,
|
2022-04-22 09:49:26 -04:00
|
|
|
setValue,
|
2022-04-27 04:42:48 -04:00
|
|
|
disabled,
|
2022-04-29 07:10:10 -04:00
|
|
|
inputRef,
|
2023-11-08 05:07:42 -05:00
|
|
|
showLabel = false,
|
|
|
|
showSuggestedText = false,
|
2022-04-22 09:49:26 -04:00
|
|
|
}: DownshiftInputProps) {
|
|
|
|
const [inputItems, setInputItems] = useState(items)
|
|
|
|
|
2022-04-27 04:42:48 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setInputItems(items)
|
|
|
|
}, [items])
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
const {
|
|
|
|
isOpen,
|
2022-04-27 04:42:48 -04:00
|
|
|
getLabelProps,
|
2022-04-22 09:49:26 -04:00
|
|
|
getMenuProps,
|
|
|
|
getInputProps,
|
|
|
|
getComboboxProps,
|
|
|
|
getItemProps,
|
2022-04-29 07:10:10 -04:00
|
|
|
highlightedIndex,
|
2022-04-22 09:49:26 -04:00
|
|
|
openMenu,
|
|
|
|
selectedItem,
|
|
|
|
} = useCombobox({
|
|
|
|
inputValue,
|
|
|
|
items: inputItems,
|
|
|
|
initialSelectedItem: inputValue,
|
|
|
|
onSelectedItemChange: ({ selectedItem }) => {
|
|
|
|
setValue(selectedItem ?? '')
|
|
|
|
},
|
|
|
|
onInputValueChange: ({ inputValue = '' }) => {
|
|
|
|
setInputItems(filterItemsByInputValue(items, inputValue))
|
|
|
|
},
|
|
|
|
onStateChange: ({ type }) => {
|
|
|
|
if (type === useCombobox.stateChangeTypes.FunctionOpenMenu) {
|
|
|
|
setInputItems(filterItemsByInputValue(items, inputValue))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-01-04 10:20:25 -05:00
|
|
|
const highlightMatchedCharacters = (item: string, query: string) => {
|
|
|
|
if (!query || !highlightMatches) return item
|
|
|
|
const regex = new RegExp(`(${escapeRegExp(query)})`, 'gi')
|
|
|
|
const parts = item.split(regex)
|
|
|
|
return parts.map((part, index) =>
|
|
|
|
regex.test(part) ? <strong key={`${part}-${index}`}>{part}</strong> : part
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-20 08:50:31 -04:00
|
|
|
const shouldOpen = isOpen && inputItems.length
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classnames(
|
2024-05-20 08:50:31 -04:00
|
|
|
'dropdown',
|
|
|
|
bsVersion({
|
|
|
|
bs5: 'd-block',
|
|
|
|
bs3: classnames('ui-select-container ui-select-bootstrap', {
|
|
|
|
open: shouldOpen,
|
|
|
|
}),
|
|
|
|
})
|
2022-04-22 09:49:26 -04:00
|
|
|
)}
|
|
|
|
>
|
2022-05-16 04:03:04 -04:00
|
|
|
<div {...getComboboxProps()}>
|
2022-04-27 04:42:48 -04:00
|
|
|
{/* eslint-disable-next-line jsx-a11y/label-has-for */}
|
2024-04-03 07:01:56 -04:00
|
|
|
<label
|
|
|
|
{...getLabelProps()}
|
|
|
|
className={
|
|
|
|
showLabel
|
|
|
|
? ''
|
2024-04-16 11:06:42 -04:00
|
|
|
: bsVersion({ bs5: 'visually-hidden', bs3: 'sr-only' })
|
2024-04-03 07:01:56 -04:00
|
|
|
}
|
|
|
|
>
|
2022-04-27 04:42:48 -04:00
|
|
|
{label}
|
|
|
|
</label>
|
2024-05-15 10:31:00 -04:00
|
|
|
<OLFormControl
|
2022-04-22 09:49:26 -04:00
|
|
|
{...getInputProps({
|
|
|
|
onChange: (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setValue(event.target.value)
|
|
|
|
},
|
|
|
|
onFocus: () => {
|
|
|
|
if (!isOpen) {
|
|
|
|
openMenu()
|
|
|
|
}
|
|
|
|
},
|
2022-04-29 07:10:10 -04:00
|
|
|
ref: inputRef,
|
2022-04-22 09:49:26 -04:00
|
|
|
})}
|
|
|
|
placeholder={placeholder}
|
2022-04-27 04:42:48 -04:00
|
|
|
disabled={disabled}
|
2022-04-22 09:49:26 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<ul
|
|
|
|
{...getMenuProps()}
|
2024-05-20 08:50:31 -04:00
|
|
|
className={classnames(
|
|
|
|
'dropdown-menu',
|
|
|
|
bsVersion({
|
|
|
|
bs5: classnames('select-dropdown-menu', { show: shouldOpen }),
|
|
|
|
bs3: 'ui-select-choices ui-select-choices-content ui-select-dropdown',
|
|
|
|
})
|
|
|
|
)}
|
2022-04-22 09:49:26 -04:00
|
|
|
>
|
2023-11-08 05:07:42 -05:00
|
|
|
{showSuggestedText && inputItems.length && (
|
2024-05-20 08:50:31 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={<li className="ui-select-title">{itemsTitle}</li>}
|
|
|
|
bs5={
|
|
|
|
<li>
|
|
|
|
<DropdownItem as="span" role={undefined} disabled>
|
|
|
|
{itemsTitle}
|
|
|
|
</DropdownItem>
|
|
|
|
</li>
|
|
|
|
}
|
|
|
|
/>
|
2023-11-08 05:07:42 -05:00
|
|
|
)}
|
2022-04-22 09:49:26 -04:00
|
|
|
{inputItems.map((item, index) => (
|
2024-05-20 08:50:31 -04:00
|
|
|
// eslint-disable-next-line jsx-a11y/role-supports-aria-props
|
2022-04-22 09:49:26 -04:00
|
|
|
<li
|
2024-05-20 08:50:31 -04:00
|
|
|
className={bsVersion({ bs3: 'ui-select-choices-group' })}
|
2022-04-22 09:49:26 -04:00
|
|
|
key={`${item}${index}`}
|
|
|
|
{...getItemProps({ item, index })}
|
2024-05-20 08:50:31 -04:00
|
|
|
aria-selected={selectedItem === item}
|
2022-04-22 09:49:26 -04:00
|
|
|
>
|
2024-05-20 08:50:31 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={
|
|
|
|
<div
|
|
|
|
className={classnames('ui-select-choices-row', {
|
|
|
|
active: selectedItem === item,
|
|
|
|
'ui-select-choices-row--highlighted':
|
|
|
|
highlightedIndex === index,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<span className="ui-select-choices-row-inner">
|
|
|
|
<span>{highlightMatchedCharacters(item, inputValue)}</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
bs5={
|
|
|
|
<DropdownItem
|
|
|
|
as="span"
|
|
|
|
role={undefined}
|
|
|
|
className={classnames({
|
|
|
|
active: selectedItem === item,
|
2024-07-22 04:10:54 -04:00
|
|
|
'dropdown-item-highlighted': highlightedIndex === index,
|
2024-05-20 08:50:31 -04:00
|
|
|
})}
|
|
|
|
trailingIcon={selectedItem === item ? 'check' : undefined}
|
|
|
|
>
|
|
|
|
{highlightMatchedCharacters(item, inputValue)}
|
|
|
|
</DropdownItem>
|
|
|
|
}
|
|
|
|
/>
|
2022-04-22 09:49:26 -04:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-29 07:10:10 -04:00
|
|
|
const DownshiftInput = forwardRef<
|
|
|
|
HTMLInputElement,
|
|
|
|
Omit<DownshiftInputProps, 'inputRef'>
|
|
|
|
>((props, ref) => <Downshift {...props} inputRef={ref} />)
|
|
|
|
|
|
|
|
DownshiftInput.displayName = 'DownshiftInput'
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
export default DownshiftInput
|