2023-05-15 05:17:13 -04:00
|
|
|
/* eslint-disable jsx-a11y/label-has-for */
|
|
|
|
/* eslint-disable jsx-a11y/label-has-associated-control */
|
2023-09-12 04:30:11 -04:00
|
|
|
import { useRef, useEffect } from 'react'
|
2023-05-15 05:17:13 -04:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import { useSelect } from 'downshift'
|
|
|
|
import Icon from './icon'
|
2023-05-17 03:13:27 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-05-15 05:17:13 -04:00
|
|
|
|
2023-09-12 04:30:11 -04:00
|
|
|
export type SelectProps<T> = {
|
|
|
|
// The items rendered as dropdown options.
|
2023-05-15 05:17:13 -04:00
|
|
|
items: T[]
|
2023-09-12 04:30:11 -04:00
|
|
|
// Stringifies an item of type T. The resulting string is rendered as a dropdown option.
|
|
|
|
itemToString: (item: T | null | undefined) => string
|
|
|
|
// Caption for the dropdown.
|
2023-05-15 05:17:13 -04:00
|
|
|
label?: string
|
2023-09-12 04:30:11 -04:00
|
|
|
// Attribute used to identify the component inside a Form. This name is used to
|
|
|
|
// retrieve FormData when the form is submitted. The value of the FormData entry
|
|
|
|
// is the string returned by `itemToString(selectedItem)`.
|
|
|
|
name?: string
|
|
|
|
// Hint text displayed in the initial render.
|
2023-05-15 05:17:13 -04:00
|
|
|
defaultText?: string
|
2023-09-12 04:30:11 -04:00
|
|
|
// Initial selected item, displayed in the initial render. When both `defaultText`
|
|
|
|
// and `defaultItem` are set the latter is ignored.
|
|
|
|
defaultItem?: T
|
|
|
|
// Stringifies an item. The resulting string is rendered as a subtitle in a dropdown option.
|
|
|
|
itemToSubtitle?: (item: T | null | undefined) => string
|
|
|
|
// Stringifies an item. The resulting string is rendered as a React `key` for each item.
|
2023-05-15 05:17:13 -04:00
|
|
|
itemToKey: (item: T) => string
|
2023-09-12 04:30:11 -04:00
|
|
|
// Callback invoked after the selected item is updated.
|
2023-05-15 05:17:13 -04:00
|
|
|
onSelectedItemChanged?: (item: T | null | undefined) => void
|
2023-09-12 04:30:11 -04:00
|
|
|
// When `true` item selection is disabled.
|
2023-05-15 05:17:13 -04:00
|
|
|
disabled?: boolean
|
2023-09-12 04:30:11 -04:00
|
|
|
// When `true` displays an "Optional" subtext after the `label` caption.
|
2023-05-15 05:17:13 -04:00
|
|
|
optionalLabel?: boolean
|
2023-09-12 04:30:11 -04:00
|
|
|
// When `true` displays a spinner next to the `label` caption.
|
2023-05-17 03:13:09 -04:00
|
|
|
loading?: boolean
|
2023-05-15 05:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Select = <T,>({
|
|
|
|
items,
|
|
|
|
itemToString = item => (item === null ? '' : String(item)),
|
|
|
|
label,
|
2023-09-12 04:30:11 -04:00
|
|
|
name,
|
2023-05-15 05:17:13 -04:00
|
|
|
defaultText = 'Items',
|
2023-09-12 04:30:11 -04:00
|
|
|
defaultItem,
|
2023-05-15 05:17:13 -04:00
|
|
|
itemToSubtitle,
|
|
|
|
itemToKey,
|
|
|
|
onSelectedItemChanged,
|
|
|
|
disabled = false,
|
|
|
|
optionalLabel = false,
|
2023-05-17 03:13:09 -04:00
|
|
|
loading = false,
|
2023-05-15 05:17:13 -04:00
|
|
|
}: SelectProps<T>) => {
|
2023-05-17 03:13:27 -04:00
|
|
|
const { t } = useTranslation()
|
2023-05-15 05:17:13 -04:00
|
|
|
const {
|
|
|
|
isOpen,
|
|
|
|
selectedItem,
|
|
|
|
getToggleButtonProps,
|
|
|
|
getLabelProps,
|
|
|
|
getMenuProps,
|
|
|
|
getItemProps,
|
|
|
|
highlightedIndex,
|
|
|
|
} = useSelect({
|
|
|
|
items: items ?? [],
|
|
|
|
itemToString,
|
|
|
|
onSelectedItemChange: changes => {
|
|
|
|
if (onSelectedItemChanged) {
|
|
|
|
onSelectedItemChanged(changes.selectedItem)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2023-09-12 04:30:11 -04:00
|
|
|
|
|
|
|
const rootRef = useRef<HTMLDivElement | null>(null)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!name || !rootRef.current) return
|
|
|
|
|
|
|
|
const parentForm: HTMLFormElement | null | undefined =
|
|
|
|
rootRef.current?.closest('form')
|
|
|
|
if (!parentForm) return
|
|
|
|
|
|
|
|
function handleFormDataEvent(event: FormDataEvent) {
|
|
|
|
const data = event.formData
|
|
|
|
const key = name as string // can't be undefined due to early exit in the effect
|
|
|
|
if (selectedItem || defaultItem) {
|
|
|
|
data.append(key, itemToString(selectedItem || defaultItem))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parentForm.addEventListener('formdata', handleFormDataEvent)
|
|
|
|
return () => {
|
|
|
|
parentForm.removeEventListener('formdata', handleFormDataEvent)
|
|
|
|
}
|
|
|
|
}, [name, itemToString, selectedItem, defaultItem])
|
|
|
|
|
|
|
|
let value: string | undefined
|
|
|
|
if (selectedItem || defaultItem) {
|
|
|
|
value = itemToString(selectedItem || defaultItem)
|
|
|
|
} else {
|
|
|
|
value = defaultText
|
|
|
|
}
|
2023-05-15 05:17:13 -04:00
|
|
|
return (
|
2023-09-12 04:30:11 -04:00
|
|
|
<div className="select-wrapper" ref={rootRef}>
|
2023-05-15 05:17:13 -04:00
|
|
|
<div>
|
|
|
|
{label ? (
|
|
|
|
<label {...getLabelProps()}>
|
|
|
|
{label}{' '}
|
|
|
|
{optionalLabel && (
|
|
|
|
<span className="select-optional-label text-muted">
|
2023-05-17 03:13:27 -04:00
|
|
|
({t('optional')})
|
2023-05-15 05:17:13 -04:00
|
|
|
</span>
|
2023-05-17 03:13:09 -04:00
|
|
|
)}{' '}
|
2023-09-12 04:30:11 -04:00
|
|
|
{loading && <Icon data-testid="spinner" fw type="spinner" spin />}
|
2023-05-15 05:17:13 -04:00
|
|
|
</label>
|
|
|
|
) : null}
|
|
|
|
<div
|
|
|
|
className={classNames({ disabled }, 'select-trigger')}
|
|
|
|
{...getToggleButtonProps({ disabled })}
|
|
|
|
>
|
2023-09-12 04:30:11 -04:00
|
|
|
<div>{value}</div>
|
2023-05-15 05:17:13 -04:00
|
|
|
<div>
|
|
|
|
{isOpen ? (
|
|
|
|
<Icon type="chevron-up" fw />
|
|
|
|
) : (
|
|
|
|
<Icon type="chevron-down" fw />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ul
|
|
|
|
className={classNames({ hidden: !isOpen }, 'select-items')}
|
|
|
|
{...getMenuProps({ disabled })}
|
|
|
|
>
|
|
|
|
{isOpen &&
|
|
|
|
items?.map((item, index) => (
|
|
|
|
<li
|
|
|
|
className={classNames({
|
|
|
|
'select-highlighted': highlightedIndex === index,
|
|
|
|
'selected-active': selectedItem === item,
|
|
|
|
})}
|
|
|
|
key={itemToKey(item)}
|
|
|
|
{...getItemProps({ item, index })}
|
|
|
|
>
|
|
|
|
<span className="select-item-title">{itemToString(item)}</span>
|
|
|
|
{itemToSubtitle ? (
|
|
|
|
<span className="text-muted select-item-subtitle">
|
|
|
|
{itemToSubtitle(item)}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|