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'
|
|
|
|
|
|
|
|
type DownshiftInputProps = {
|
|
|
|
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
|
2022-04-22 09:49:26 -04:00
|
|
|
setValue: React.Dispatch<React.SetStateAction<string>>
|
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({
|
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))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classnames(
|
|
|
|
'ui-select-container ui-select-bootstrap dropdown',
|
|
|
|
{
|
|
|
|
open: isOpen && inputItems.length,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
>
|
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 */}
|
2023-11-08 05:07:42 -05:00
|
|
|
<label {...getLabelProps()} className={showLabel ? '' : 'sr-only'}>
|
2022-04-27 04:42:48 -04:00
|
|
|
{label}
|
|
|
|
</label>
|
2022-04-22 09:49:26 -04:00
|
|
|
<input
|
|
|
|
{...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
|
|
|
})}
|
|
|
|
className="form-control"
|
|
|
|
type="text"
|
|
|
|
placeholder={placeholder}
|
2022-04-27 04:42:48 -04:00
|
|
|
disabled={disabled}
|
2022-04-22 09:49:26 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<ul
|
|
|
|
{...getMenuProps()}
|
|
|
|
className="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu"
|
|
|
|
>
|
2023-11-08 05:07:42 -05:00
|
|
|
{showSuggestedText && inputItems.length && (
|
|
|
|
<li className="ui-select-title">{itemsTitle}</li>
|
|
|
|
)}
|
2022-04-22 09:49:26 -04:00
|
|
|
{inputItems.map((item, index) => (
|
|
|
|
<li
|
|
|
|
className="ui-select-choices-group"
|
|
|
|
key={`${item}${index}`}
|
|
|
|
{...getItemProps({ item, index })}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={classnames('ui-select-choices-row', {
|
|
|
|
active: selectedItem === item,
|
2022-04-29 07:10:10 -04:00
|
|
|
'ui-select-choices-row--highlighted':
|
|
|
|
highlightedIndex === index,
|
2022-04-22 09:49:26 -04:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<span className="ui-select-choices-row-inner">
|
|
|
|
<span>{item}</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</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
|