2023-05-15 05:17:13 -04:00
|
|
|
/* eslint-disable jsx-a11y/label-has-for */
|
|
|
|
/* eslint-disable jsx-a11y/label-has-associated-control */
|
2024-01-18 06:39:37 -05:00
|
|
|
import {
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
KeyboardEventHandler,
|
|
|
|
useCallback,
|
|
|
|
ReactNode,
|
2024-06-25 02:08:24 -04:00
|
|
|
useState,
|
2024-01-18 06:39:37 -05:00
|
|
|
} 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'
|
2024-09-30 04:11:26 -04:00
|
|
|
import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
|
|
|
|
import { Form, Spinner } from 'react-bootstrap-5'
|
|
|
|
import FormControl from '@/features/ui/components/bootstrap-5/form/form-control'
|
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
|
|
|
import { DropdownItem } from '@/features/ui/components/bootstrap-5/dropdown-menu'
|
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.
|
2024-01-18 06:39:37 -05:00
|
|
|
label?: ReactNode
|
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.
|
2024-10-24 05:59:49 -04:00
|
|
|
defaultItem?: T | null
|
2023-09-12 04:30:11 -04:00
|
|
|
// 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
|
2024-06-25 02:08:24 -04:00
|
|
|
// Optionally directly control the selected item.
|
2024-10-24 05:59:49 -04:00
|
|
|
selected?: T | null
|
2023-09-12 04:30:11 -04:00
|
|
|
// When `true` item selection is disabled.
|
2023-05-15 05:17:13 -04:00
|
|
|
disabled?: boolean
|
2024-06-25 02:08:24 -04:00
|
|
|
// Determine which items should be disabled
|
|
|
|
itemToDisabled?: (item: T | null | undefined) => 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
|
2024-06-25 02:08:24 -04:00
|
|
|
// Show a checkmark next to the selected item
|
|
|
|
selectedIcon?: 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,
|
2024-06-25 02:08:24 -04:00
|
|
|
selected,
|
2023-05-15 05:17:13 -04:00
|
|
|
disabled = false,
|
2024-06-25 02:08:24 -04:00
|
|
|
itemToDisabled,
|
2023-05-15 05:17:13 -04:00
|
|
|
optionalLabel = false,
|
2023-05-17 03:13:09 -04:00
|
|
|
loading = false,
|
2024-06-25 02:08:24 -04:00
|
|
|
selectedIcon = false,
|
2023-05-15 05:17:13 -04:00
|
|
|
}: SelectProps<T>) => {
|
2024-06-25 02:08:24 -04:00
|
|
|
const [selectedItem, setSelectedItem] = useState<T | undefined | null>(
|
|
|
|
defaultItem
|
|
|
|
)
|
|
|
|
|
2023-05-17 03:13:27 -04:00
|
|
|
const { t } = useTranslation()
|
2023-05-15 05:17:13 -04:00
|
|
|
const {
|
|
|
|
isOpen,
|
|
|
|
getToggleButtonProps,
|
|
|
|
getLabelProps,
|
|
|
|
getMenuProps,
|
|
|
|
getItemProps,
|
|
|
|
highlightedIndex,
|
2024-01-05 04:18:30 -05:00
|
|
|
openMenu,
|
2024-09-30 04:11:26 -04:00
|
|
|
closeMenu,
|
2023-05-15 05:17:13 -04:00
|
|
|
} = useSelect({
|
|
|
|
items: items ?? [],
|
|
|
|
itemToString,
|
2024-06-25 02:08:24 -04:00
|
|
|
selectedItem: selected || defaultItem,
|
2023-05-15 05:17:13 -04:00
|
|
|
onSelectedItemChange: changes => {
|
|
|
|
if (onSelectedItemChanged) {
|
|
|
|
onSelectedItemChanged(changes.selectedItem)
|
|
|
|
}
|
2024-06-25 02:08:24 -04:00
|
|
|
setSelectedItem(changes.selectedItem)
|
2023-05-15 05:17:13 -04:00
|
|
|
},
|
|
|
|
})
|
2023-09-12 04:30:11 -04:00
|
|
|
|
2024-06-25 02:08:24 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setSelectedItem(selected)
|
|
|
|
}, [selected])
|
|
|
|
|
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])
|
|
|
|
|
2024-09-30 04:11:26 -04:00
|
|
|
const handleMenuKeyDown = (event: React.KeyboardEvent<HTMLUListElement>) => {
|
|
|
|
if (event.key === 'Escape' && isOpen) {
|
|
|
|
event.stopPropagation()
|
|
|
|
closeMenu()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 04:18:30 -05:00
|
|
|
const onKeyDown: KeyboardEventHandler<HTMLButtonElement> = useCallback(
|
|
|
|
event => {
|
2024-09-30 04:11:26 -04:00
|
|
|
if ((event.key === 'Enter' || event.key === ' ') && !isOpen) {
|
2024-01-05 04:18:30 -05:00
|
|
|
event.preventDefault()
|
|
|
|
;(event.nativeEvent as any).preventDownshiftDefault = true
|
|
|
|
openMenu()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isOpen, openMenu]
|
|
|
|
)
|
|
|
|
|
2023-09-12 04:30:11 -04:00
|
|
|
let value: string | undefined
|
|
|
|
if (selectedItem || defaultItem) {
|
|
|
|
value = itemToString(selectedItem || defaultItem)
|
|
|
|
} else {
|
|
|
|
value = defaultText
|
|
|
|
}
|
2023-05-15 05:17:13 -04:00
|
|
|
return (
|
2024-09-30 04:11:26 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={
|
|
|
|
<div className="select-wrapper" ref={rootRef}>
|
2023-05-15 05:17:13 -04:00
|
|
|
<div>
|
2024-09-30 04:11:26 -04:00
|
|
|
{label ? (
|
|
|
|
<label {...getLabelProps()}>
|
|
|
|
{label}{' '}
|
|
|
|
{optionalLabel && (
|
|
|
|
<span className="select-optional-label text-muted">
|
|
|
|
({t('optional')})
|
|
|
|
</span>
|
|
|
|
)}{' '}
|
|
|
|
{loading && (
|
|
|
|
<Icon data-testid="spinner" fw type="spinner" spin />
|
|
|
|
)}
|
|
|
|
</label>
|
|
|
|
) : null}
|
|
|
|
<div
|
|
|
|
className={classNames({ disabled }, 'select-trigger')}
|
|
|
|
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
|
|
|
tabIndex={0}
|
|
|
|
{...getToggleButtonProps({
|
|
|
|
disabled,
|
|
|
|
onKeyDown,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div>{value}</div>
|
|
|
|
<div>
|
|
|
|
{isOpen ? (
|
|
|
|
<Icon type="chevron-up" fw />
|
|
|
|
) : (
|
|
|
|
<Icon type="chevron-down" fw />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-15 05:17:13 -04:00
|
|
|
</div>
|
2024-09-30 04:11:26 -04:00
|
|
|
<ul
|
|
|
|
className={classNames({ hidden: !isOpen }, 'select-items')}
|
|
|
|
{...getMenuProps({ disabled, onKeyDown: handleMenuKeyDown })}
|
|
|
|
>
|
|
|
|
{isOpen &&
|
|
|
|
items?.map((item, index) => {
|
|
|
|
const isDisabled = itemToDisabled && itemToDisabled(item)
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
className={classNames({
|
|
|
|
'select-highlighted': highlightedIndex === index,
|
|
|
|
'selected-active': selectedItem === item,
|
|
|
|
'select-icon': selectedIcon,
|
|
|
|
'select-disabled': isDisabled,
|
|
|
|
})}
|
|
|
|
key={itemToKey(item)}
|
|
|
|
{...getItemProps({ item, index, disabled: isDisabled })}
|
|
|
|
>
|
|
|
|
<span className="select-item-title">
|
|
|
|
{selectedIcon && (
|
|
|
|
<div className="select-item-icon">
|
|
|
|
{(selectedItem === item ||
|
|
|
|
(!selectedItem && defaultItem === item)) && (
|
|
|
|
<Icon type="check" fw />
|
|
|
|
)}
|
|
|
|
</div>
|
2024-06-25 02:08:24 -04:00
|
|
|
)}
|
2024-09-30 04:11:26 -04:00
|
|
|
{itemToString(item)}
|
|
|
|
</span>
|
2024-06-25 02:08:24 -04:00
|
|
|
|
2024-09-30 04:11:26 -04:00
|
|
|
{itemToSubtitle ? (
|
|
|
|
<span className="text-muted select-item-subtitle">
|
|
|
|
{itemToSubtitle(item)}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
bs5={
|
|
|
|
<div className="select-wrapper" ref={rootRef}>
|
|
|
|
{label ? (
|
|
|
|
<Form.Label {...getLabelProps()}>
|
|
|
|
{label}{' '}
|
|
|
|
{optionalLabel && (
|
|
|
|
<span className="fw-normal">({t('optional')})</span>
|
|
|
|
)}{' '}
|
|
|
|
{loading && (
|
|
|
|
<span data-testid="spinner">
|
|
|
|
<Spinner
|
|
|
|
animation="border"
|
|
|
|
aria-hidden="true"
|
|
|
|
as="span"
|
|
|
|
role="status"
|
|
|
|
size="sm"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Form.Label>
|
|
|
|
) : null}
|
|
|
|
<FormControl
|
|
|
|
{...getToggleButtonProps({
|
|
|
|
disabled,
|
|
|
|
onKeyDown,
|
|
|
|
className: 'select-trigger',
|
|
|
|
})}
|
|
|
|
value={value}
|
|
|
|
readOnly
|
|
|
|
append={
|
|
|
|
<MaterialIcon
|
|
|
|
type={isOpen ? 'keyboard_arrow_up' : 'keyboard_arrow_down'}
|
|
|
|
className="align-text-bottom"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ul
|
|
|
|
{...getMenuProps({ disabled, onKeyDown: handleMenuKeyDown })}
|
|
|
|
className={classNames('dropdown-menu w-100', { show: isOpen })}
|
|
|
|
>
|
|
|
|
{isOpen &&
|
|
|
|
items?.map((item, index) => {
|
|
|
|
const isDisabled = itemToDisabled && itemToDisabled(item)
|
|
|
|
return (
|
|
|
|
<li role="none" key={itemToKey(item)}>
|
|
|
|
<DropdownItem
|
|
|
|
as="button"
|
|
|
|
className={classNames({
|
|
|
|
'select-highlighted': highlightedIndex === index,
|
|
|
|
})}
|
|
|
|
active={selectedItem === item}
|
|
|
|
trailingIcon={selectedItem === item ? 'check' : undefined}
|
|
|
|
description={
|
|
|
|
itemToSubtitle ? itemToSubtitle(item) : undefined
|
|
|
|
}
|
|
|
|
{...getItemProps({ item, index, disabled: isDisabled })}
|
|
|
|
>
|
|
|
|
{itemToString(item)}
|
|
|
|
</DropdownItem>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
2023-05-15 05:17:13 -04:00
|
|
|
)
|
|
|
|
}
|