2022-01-05 11:34:43 +01:00
|
|
|
import React, {
|
|
|
|
useMemo,
|
|
|
|
useState,
|
|
|
|
useRef,
|
|
|
|
useContext,
|
|
|
|
createContext,
|
2022-03-30 18:13:05 +02:00
|
|
|
useId,
|
2022-03-02 10:48:42 +01:00
|
|
|
ReactNode,
|
|
|
|
ChangeEventHandler,
|
|
|
|
KeyboardEventHandler
|
2022-01-05 11:34:43 +01:00
|
|
|
} from 'react';
|
|
|
|
import {
|
|
|
|
Combobox,
|
|
|
|
ComboboxInput,
|
|
|
|
ComboboxList,
|
|
|
|
ComboboxOption,
|
|
|
|
ComboboxPopover
|
|
|
|
} from '@reach/combobox';
|
|
|
|
import '@reach/combobox/styles.css';
|
|
|
|
import { matchSorter } from 'match-sorter';
|
|
|
|
import { XIcon } from '@heroicons/react/outline';
|
|
|
|
import isHotkey from 'is-hotkey';
|
|
|
|
import invariant from 'tiny-invariant';
|
|
|
|
|
|
|
|
import { useDeferredSubmit, useHiddenField } from './shared/hooks';
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const Context = createContext<{
|
|
|
|
onRemove: (value: string) => void;
|
|
|
|
} | null>(null);
|
2022-01-05 11:34:43 +01:00
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
type Option = [label: string, value: string];
|
|
|
|
|
|
|
|
function isOptions(options: string[] | Option[]): options is Option[] {
|
|
|
|
return Array.isArray(options[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const optionLabelByValue = (
|
|
|
|
values: string[],
|
|
|
|
options: Option[],
|
|
|
|
value: string
|
|
|
|
): string => {
|
|
|
|
const maybeOption: Option | undefined = values.includes(value)
|
2022-02-22 13:14:11 +01:00
|
|
|
? [value, value]
|
|
|
|
: options.find(([, optionValue]) => optionValue == value);
|
2022-03-02 10:48:42 +01:00
|
|
|
return maybeOption ? maybeOption[0] : '';
|
2022-02-22 13:14:11 +01:00
|
|
|
};
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
export type ComboMultipleProps = {
|
|
|
|
options: string[] | Option[];
|
|
|
|
id: string;
|
|
|
|
labelledby: string;
|
|
|
|
describedby: string;
|
|
|
|
label: string;
|
|
|
|
group: string;
|
|
|
|
name?: string;
|
|
|
|
selected: string[];
|
|
|
|
acceptNewValues?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function ComboMultiple({
|
2022-01-05 11:34:43 +01:00
|
|
|
options,
|
|
|
|
id,
|
|
|
|
labelledby,
|
|
|
|
describedby,
|
|
|
|
label,
|
|
|
|
group,
|
|
|
|
name = 'value',
|
|
|
|
selected,
|
|
|
|
acceptNewValues = false
|
2022-03-02 10:48:42 +01:00
|
|
|
}: ComboMultipleProps) {
|
2022-01-05 11:34:43 +01:00
|
|
|
invariant(id || label, 'ComboMultiple: `id` or a `label` are required');
|
|
|
|
invariant(group, 'ComboMultiple: `group` is required');
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
2022-01-05 11:34:43 +01:00
|
|
|
const [term, setTerm] = useState('');
|
|
|
|
const [selections, setSelections] = useState(selected);
|
2022-03-02 10:48:42 +01:00
|
|
|
const [newValues, setNewValues] = useState<string[]>([]);
|
2022-03-30 18:13:05 +02:00
|
|
|
const internalId = useId();
|
|
|
|
const inputId = id ?? internalId;
|
2022-01-05 11:34:43 +01:00
|
|
|
const removedLabelledby = `${inputId}-remove`;
|
|
|
|
const selectedLabelledby = `${inputId}-selected`;
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const optionsWithLabels = useMemo<Option[]>(
|
2022-02-22 13:14:11 +01:00
|
|
|
() =>
|
2022-03-02 10:48:42 +01:00
|
|
|
isOptions(options)
|
2022-02-22 13:14:11 +01:00
|
|
|
? options
|
|
|
|
: options.filter((o) => o).map((o) => [o, o]),
|
|
|
|
[options]
|
|
|
|
);
|
2024-02-02 17:30:12 +01:00
|
|
|
|
2022-01-05 11:34:43 +01:00
|
|
|
const extraOptions = useMemo(
|
|
|
|
() =>
|
2022-02-22 13:14:11 +01:00
|
|
|
acceptNewValues &&
|
|
|
|
term &&
|
|
|
|
term.length > 2 &&
|
|
|
|
!optionLabelByValue(newValues, optionsWithLabels, term)
|
2022-01-05 11:34:43 +01:00
|
|
|
? [[term, term]]
|
|
|
|
: [],
|
2022-02-22 13:14:11 +01:00
|
|
|
[acceptNewValues, term, optionsWithLabels, newValues]
|
2022-01-05 11:34:43 +01:00
|
|
|
);
|
2024-02-02 17:30:12 +01:00
|
|
|
|
|
|
|
const extraListOptions = useMemo(
|
|
|
|
() =>
|
|
|
|
acceptNewValues && term && term.length > 2 && term.includes(';')
|
|
|
|
? term.split(';').map((val) => [val.trim(), val.trim()])
|
|
|
|
: [],
|
|
|
|
[acceptNewValues, term]
|
|
|
|
);
|
|
|
|
|
2022-01-05 11:34:43 +01:00
|
|
|
const results = useMemo(
|
|
|
|
() =>
|
|
|
|
[
|
|
|
|
...extraOptions,
|
|
|
|
...(term
|
|
|
|
? matchSorter(
|
2022-02-22 13:14:11 +01:00
|
|
|
optionsWithLabels.filter(([label]) => !label.startsWith('--')),
|
2022-01-05 11:34:43 +01:00
|
|
|
term
|
|
|
|
)
|
2022-02-22 13:14:11 +01:00
|
|
|
: optionsWithLabels)
|
2022-01-05 11:34:43 +01:00
|
|
|
].filter(([, value]) => !selections.includes(value)),
|
2022-02-22 13:14:11 +01:00
|
|
|
[term, selections, extraOptions, optionsWithLabels]
|
2022-01-05 11:34:43 +01:00
|
|
|
);
|
|
|
|
const [, setHiddenFieldValue, hiddenField] = useHiddenField(group, name);
|
|
|
|
const awaitFormSubmit = useDeferredSubmit(hiddenField);
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
|
2022-01-05 11:34:43 +01:00
|
|
|
setTerm(event.target.value);
|
|
|
|
};
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const saveSelection = (fn: (selections: string[]) => string[]) => {
|
2022-01-05 11:34:43 +01:00
|
|
|
setSelections((selections) => {
|
|
|
|
selections = fn(selections);
|
|
|
|
setHiddenFieldValue(JSON.stringify(selections));
|
|
|
|
return selections;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const onSelect = (value: string) => {
|
2022-02-22 13:14:11 +01:00
|
|
|
const maybeValue = [...extraOptions, ...optionsWithLabels].find(
|
2022-05-10 12:57:47 +02:00
|
|
|
([, val]) => val == value
|
2022-01-05 11:34:43 +01:00
|
|
|
);
|
2024-02-02 17:30:12 +01:00
|
|
|
|
|
|
|
const maybeValueFromListOptions = extraListOptions.find(
|
|
|
|
([, val]) => val == value
|
|
|
|
);
|
|
|
|
|
|
|
|
const selectedValue =
|
|
|
|
term.includes(';') && acceptNewValues
|
|
|
|
? maybeValueFromListOptions && maybeValueFromListOptions[1]
|
|
|
|
: maybeValue && maybeValue[1];
|
|
|
|
|
2022-01-05 11:34:43 +01:00
|
|
|
if (selectedValue) {
|
|
|
|
if (
|
2024-02-02 17:30:12 +01:00
|
|
|
(acceptNewValues &&
|
|
|
|
extraOptions[0] &&
|
|
|
|
extraOptions[0][0] == selectedValue) ||
|
|
|
|
(acceptNewValues && extraListOptions[0])
|
2022-01-05 11:34:43 +01:00
|
|
|
) {
|
2022-01-13 14:01:31 +01:00
|
|
|
setNewValues((newValues) => {
|
|
|
|
const set = new Set(newValues);
|
|
|
|
set.add(selectedValue);
|
|
|
|
return [...set];
|
|
|
|
});
|
2022-01-05 11:34:43 +01:00
|
|
|
}
|
2022-01-13 14:01:31 +01:00
|
|
|
saveSelection((selections) => {
|
|
|
|
const set = new Set(selections);
|
|
|
|
set.add(selectedValue);
|
|
|
|
return [...set];
|
|
|
|
});
|
2022-01-05 11:34:43 +01:00
|
|
|
}
|
|
|
|
setTerm('');
|
|
|
|
awaitFormSubmit.done();
|
2022-01-11 12:11:45 +01:00
|
|
|
hidePopover();
|
2022-01-05 11:34:43 +01:00
|
|
|
};
|
|
|
|
|
2022-05-10 12:57:47 +02:00
|
|
|
const onRemove = (optionValue: string) => {
|
2022-01-05 11:34:43 +01:00
|
|
|
if (optionValue) {
|
|
|
|
saveSelection((selections) =>
|
|
|
|
selections.filter((value) => value != optionValue)
|
|
|
|
);
|
|
|
|
setNewValues((newValues) =>
|
|
|
|
newValues.filter((value) => value != optionValue)
|
|
|
|
);
|
|
|
|
}
|
2022-03-02 10:48:42 +01:00
|
|
|
inputRef.current?.focus();
|
2022-01-05 11:34:43 +01:00
|
|
|
};
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
const onKeyDown: KeyboardEventHandler<HTMLInputElement> = (event) => {
|
2022-01-05 11:34:43 +01:00
|
|
|
if (
|
|
|
|
isHotkey('enter', event) ||
|
|
|
|
isHotkey(' ', event) ||
|
|
|
|
isHotkey(',', event) ||
|
|
|
|
isHotkey(';', event)
|
|
|
|
) {
|
2024-02-02 17:30:12 +01:00
|
|
|
if (term.includes(';')) {
|
|
|
|
for (const val of term.split(';')) {
|
|
|
|
event.preventDefault();
|
|
|
|
onSelect(val.trim());
|
|
|
|
}
|
|
|
|
} else if (
|
2022-01-05 11:34:43 +01:00
|
|
|
term &&
|
2022-02-22 13:14:11 +01:00
|
|
|
[...extraOptions, ...optionsWithLabels]
|
|
|
|
.map(([label]) => label)
|
|
|
|
.includes(term)
|
2022-01-05 11:34:43 +01:00
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
onSelect(term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-11 12:11:45 +01:00
|
|
|
const hidePopover = () => {
|
|
|
|
document
|
|
|
|
.querySelector(`[data-reach-combobox-popover-id="${inputId}"]`)
|
|
|
|
?.setAttribute('hidden', 'true');
|
|
|
|
};
|
|
|
|
|
|
|
|
const showPopover = () => {
|
|
|
|
document
|
|
|
|
.querySelector(`[data-reach-combobox-popover-id="${inputId}"]`)
|
|
|
|
?.removeAttribute('hidden');
|
|
|
|
};
|
|
|
|
|
2022-01-05 11:34:43 +01:00
|
|
|
const onBlur = () => {
|
2022-01-13 14:01:31 +01:00
|
|
|
const shouldSelect =
|
2022-01-05 11:34:43 +01:00
|
|
|
term &&
|
2022-02-22 13:14:11 +01:00
|
|
|
[...extraOptions, ...optionsWithLabels]
|
|
|
|
.map(([label]) => label)
|
|
|
|
.includes(term);
|
2022-01-13 14:01:31 +01:00
|
|
|
|
|
|
|
awaitFormSubmit(() => {
|
2024-02-02 17:30:12 +01:00
|
|
|
if (term.includes(';')) {
|
|
|
|
for (const val of term.split(';')) {
|
|
|
|
onSelect(val.trim());
|
|
|
|
}
|
|
|
|
} else if (shouldSelect) {
|
2022-01-05 11:34:43 +01:00
|
|
|
onSelect(term);
|
2022-01-13 14:01:31 +01:00
|
|
|
} else {
|
2022-01-11 12:11:45 +01:00
|
|
|
hidePopover();
|
2022-01-13 14:01:31 +01:00
|
|
|
}
|
|
|
|
});
|
2022-01-05 11:34:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Combobox openOnFocus={true} onSelect={onSelect}>
|
|
|
|
<ComboboxTokenLabel onRemove={onRemove}>
|
|
|
|
<span id={removedLabelledby} className="hidden">
|
|
|
|
désélectionner
|
|
|
|
</span>
|
|
|
|
<ul
|
|
|
|
id={selectedLabelledby}
|
|
|
|
aria-live="polite"
|
|
|
|
aria-atomic={true}
|
|
|
|
data-reach-combobox-token-list
|
|
|
|
>
|
|
|
|
{selections.map((selection) => (
|
|
|
|
<ComboboxToken
|
|
|
|
key={selection}
|
2022-05-10 12:57:47 +02:00
|
|
|
value={selection}
|
2022-01-05 11:34:43 +01:00
|
|
|
describedby={removedLabelledby}
|
2022-05-10 12:57:47 +02:00
|
|
|
>
|
|
|
|
{optionLabelByValue(newValues, optionsWithLabels, selection)}
|
|
|
|
</ComboboxToken>
|
2022-01-05 11:34:43 +01:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
<ComboboxInput
|
|
|
|
ref={inputRef}
|
|
|
|
value={term}
|
|
|
|
onChange={handleChange}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
onBlur={onBlur}
|
2022-01-11 12:11:45 +01:00
|
|
|
onClick={showPopover}
|
2022-01-05 11:34:43 +01:00
|
|
|
autocomplete={false}
|
|
|
|
id={inputId}
|
|
|
|
aria-label={label}
|
|
|
|
aria-labelledby={[labelledby, selectedLabelledby]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join(' ')}
|
|
|
|
aria-describedby={describedby}
|
|
|
|
/>
|
|
|
|
</ComboboxTokenLabel>
|
|
|
|
{results && (results.length > 0 || !acceptNewValues) && (
|
2022-01-11 12:11:45 +01:00
|
|
|
<ComboboxPopover
|
|
|
|
className="shadow-popup"
|
|
|
|
data-reach-combobox-popover-id={inputId}
|
|
|
|
>
|
2022-01-05 11:34:43 +01:00
|
|
|
<ComboboxList>
|
2022-01-11 12:10:20 +01:00
|
|
|
{results.length === 0 && (
|
|
|
|
<li data-reach-combobox-no-results>
|
|
|
|
Aucun résultat{' '}
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
setTerm('');
|
|
|
|
inputRef.current?.focus();
|
|
|
|
}}
|
|
|
|
className="button"
|
|
|
|
>
|
|
|
|
Effacer
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
)}
|
2022-05-10 12:57:47 +02:00
|
|
|
{results.map(([label, value], index) => {
|
2022-01-05 11:34:43 +01:00
|
|
|
if (label.startsWith('--')) {
|
|
|
|
return <ComboboxSeparator key={index} value={label} />;
|
|
|
|
}
|
2022-05-10 12:57:47 +02:00
|
|
|
return (
|
|
|
|
<ComboboxOption key={index} value={value}>
|
|
|
|
{label}
|
|
|
|
</ComboboxOption>
|
|
|
|
);
|
2022-01-05 11:34:43 +01:00
|
|
|
})}
|
|
|
|
</ComboboxList>
|
|
|
|
</ComboboxPopover>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
function ComboboxTokenLabel({
|
|
|
|
onRemove,
|
|
|
|
children
|
|
|
|
}: {
|
|
|
|
onRemove: (value: string) => void;
|
|
|
|
children: ReactNode;
|
|
|
|
}) {
|
2022-01-05 11:34:43 +01:00
|
|
|
return (
|
2022-05-10 12:57:47 +02:00
|
|
|
<Context.Provider value={{ onRemove }}>
|
2022-03-02 10:48:42 +01:00
|
|
|
<div data-reach-combobox-token-label>{children}</div>
|
2022-01-05 11:34:43 +01:00
|
|
|
</Context.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
function ComboboxSeparator({ value }: { value: string }) {
|
2022-01-05 11:34:43 +01:00
|
|
|
return (
|
|
|
|
<li aria-disabled="true" role="option" data-reach-combobox-separator>
|
|
|
|
{value.slice(2, -2)}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-02 10:48:42 +01:00
|
|
|
function ComboboxToken({
|
|
|
|
value,
|
|
|
|
describedby,
|
2022-05-10 12:57:47 +02:00
|
|
|
children,
|
2022-03-02 10:48:42 +01:00
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
value: string;
|
|
|
|
describedby: string;
|
2022-05-10 12:57:47 +02:00
|
|
|
children: ReactNode;
|
2022-03-02 10:48:42 +01:00
|
|
|
}) {
|
|
|
|
const context = useContext(Context);
|
|
|
|
invariant(context, 'invalid context');
|
2022-05-10 12:57:47 +02:00
|
|
|
const { onRemove } = context;
|
2022-01-05 11:34:43 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<li data-reach-combobox-token {...props}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
onRemove(value);
|
|
|
|
}}
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
if (event.key === 'Backspace') {
|
|
|
|
onRemove(value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
aria-describedby={describedby}
|
|
|
|
>
|
|
|
|
<XIcon className="icon-size mr-1" aria-hidden="true" />
|
2022-05-10 12:57:47 +02:00
|
|
|
{children}
|
2022-01-05 11:34:43 +01:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|