2023-02-22 11:47:39 +01:00
|
|
|
import React, {
|
|
|
|
useState,
|
2023-02-22 11:48:50 +01:00
|
|
|
useEffect,
|
2023-02-22 11:47:39 +01:00
|
|
|
useRef,
|
|
|
|
useId,
|
|
|
|
ChangeEventHandler
|
|
|
|
} from 'react';
|
2022-03-30 18:13:15 +02:00
|
|
|
import { useDebounce } from 'use-debounce';
|
2020-10-07 17:41:20 +02:00
|
|
|
import { useQuery } from 'react-query';
|
|
|
|
import {
|
|
|
|
Combobox,
|
|
|
|
ComboboxInput,
|
|
|
|
ComboboxPopover,
|
|
|
|
ComboboxList,
|
|
|
|
ComboboxOption
|
|
|
|
} from '@reach/combobox';
|
|
|
|
import '@reach/combobox/styles.css';
|
2022-01-05 11:34:43 +01:00
|
|
|
import invariant from 'tiny-invariant';
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-01-05 11:34:43 +01:00
|
|
|
import { useDeferredSubmit, useHiddenField, groupId } from './shared/hooks';
|
2021-06-09 17:13:26 +02:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
type TransformResults<Result> = (term: string, results: unknown) => Result[];
|
|
|
|
type TransformResult<Result> = (
|
|
|
|
result: Result
|
2022-04-15 13:05:46 +02:00
|
|
|
) => [key: string, value: string, label?: string];
|
2022-02-08 12:49:51 +01:00
|
|
|
|
2023-02-27 11:59:38 +01:00
|
|
|
export type ComboSearchProps<Result = unknown> = {
|
2022-02-08 12:49:51 +01:00
|
|
|
onChange?: (value: string | null, result?: Result) => void;
|
|
|
|
value?: string;
|
|
|
|
scope: string;
|
|
|
|
scopeExtra?: string;
|
|
|
|
minimumInputLength: number;
|
2022-04-15 13:05:46 +02:00
|
|
|
transformResults?: TransformResults<Result>;
|
2022-02-08 12:49:51 +01:00
|
|
|
transformResult: TransformResult<Result>;
|
|
|
|
allowInputValues?: boolean;
|
|
|
|
id?: string;
|
|
|
|
describedby?: string;
|
|
|
|
className?: string;
|
|
|
|
placeholder?: string;
|
2022-10-10 21:09:23 +02:00
|
|
|
debounceDelay?: number;
|
2023-02-22 18:32:42 +01:00
|
|
|
screenReaderInstructions: string;
|
|
|
|
announceTemplateId: string;
|
2022-02-08 12:49:51 +01:00
|
|
|
};
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
type QueryKey = readonly [
|
|
|
|
scope: string,
|
|
|
|
term: string,
|
|
|
|
extra: string | undefined
|
|
|
|
];
|
|
|
|
|
|
|
|
function ComboSearch<Result>({
|
2020-10-07 17:41:20 +02:00
|
|
|
onChange,
|
2022-01-05 11:34:43 +01:00
|
|
|
value: controlledValue,
|
2020-10-07 17:41:20 +02:00
|
|
|
scope,
|
2021-10-26 16:21:47 +02:00
|
|
|
scopeExtra,
|
2020-10-07 17:41:20 +02:00
|
|
|
minimumInputLength,
|
|
|
|
transformResult,
|
|
|
|
allowInputValues = false,
|
2022-02-08 12:49:51 +01:00
|
|
|
transformResults = (_, results) => results as Result[],
|
2022-01-05 11:34:43 +01:00
|
|
|
id,
|
|
|
|
describedby,
|
2023-02-22 18:32:42 +01:00
|
|
|
screenReaderInstructions,
|
|
|
|
announceTemplateId,
|
2022-10-10 21:09:23 +02:00
|
|
|
debounceDelay = 0,
|
2021-10-26 16:21:47 +02:00
|
|
|
...props
|
2022-02-08 12:49:51 +01:00
|
|
|
}: ComboSearchProps<Result>) {
|
2022-01-05 11:34:43 +01:00
|
|
|
invariant(id || onChange, 'ComboSearch: `id` or `onChange` are required');
|
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
const group = !onChange && id ? groupId(id) : undefined;
|
2022-01-05 11:34:43 +01:00
|
|
|
const [externalValue, setExternalValue, hiddenField] = useHiddenField(group);
|
|
|
|
const [, setExternalId] = useHiddenField(group, 'external_id');
|
|
|
|
const initialValue = externalValue ? externalValue : controlledValue;
|
2020-10-07 17:41:20 +02:00
|
|
|
const [searchTerm, setSearchTerm] = useState('');
|
2022-10-10 21:09:23 +02:00
|
|
|
const [debouncedSearchTerm] = useDebounce(searchTerm, debounceDelay);
|
2020-10-07 17:41:20 +02:00
|
|
|
const [value, setValue] = useState(initialValue);
|
2022-02-08 12:49:51 +01:00
|
|
|
const resultsMap = useRef<
|
|
|
|
Record<string, { key: string; value: string; result: Result }>
|
|
|
|
>({});
|
|
|
|
const getLabel = (result: Result) => {
|
2021-08-31 12:49:35 +02:00
|
|
|
const [, value, label] = transformResult(result);
|
|
|
|
return label ?? value;
|
|
|
|
};
|
2022-02-22 13:14:11 +01:00
|
|
|
const setExternalValueAndId = (label: string) => {
|
2021-08-31 12:49:35 +02:00
|
|
|
const { key, value, result } = resultsMap.current[label];
|
2020-10-07 17:41:20 +02:00
|
|
|
if (onChange) {
|
|
|
|
onChange(value, result);
|
2022-01-05 11:34:43 +01:00
|
|
|
} else {
|
|
|
|
setExternalId(key);
|
|
|
|
setExternalValue(value);
|
2020-10-07 17:41:20 +02:00
|
|
|
}
|
2022-02-22 13:14:11 +01:00
|
|
|
};
|
2022-01-05 11:34:43 +01:00
|
|
|
const awaitFormSubmit = useDeferredSubmit(hiddenField);
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-02-22 13:14:11 +01:00
|
|
|
const handleOnChange: ChangeEventHandler<HTMLInputElement> = ({
|
|
|
|
target: { value }
|
|
|
|
}) => {
|
|
|
|
setValue(value);
|
|
|
|
if (!value) {
|
|
|
|
if (onChange) {
|
|
|
|
onChange(null);
|
|
|
|
} else {
|
|
|
|
setExternalId('');
|
|
|
|
setExternalValue('');
|
2020-10-07 17:41:20 +02:00
|
|
|
}
|
2022-02-22 13:14:11 +01:00
|
|
|
} else if (value.length >= minimumInputLength) {
|
|
|
|
setSearchTerm(value.trim());
|
|
|
|
if (allowInputValues) {
|
|
|
|
setExternalId('');
|
|
|
|
setExternalValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-02-22 13:14:11 +01:00
|
|
|
const handleOnSelect = (value: string) => {
|
2021-01-14 23:54:32 +01:00
|
|
|
setExternalValueAndId(value);
|
2020-10-07 17:41:20 +02:00
|
|
|
setValue(value);
|
2021-06-09 17:13:26 +02:00
|
|
|
setSearchTerm('');
|
|
|
|
awaitFormSubmit.done();
|
2022-02-22 13:14:11 +01:00
|
|
|
};
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-02-08 12:49:51 +01:00
|
|
|
const { isSuccess, data } = useQuery<void, void, unknown, QueryKey>(
|
2021-10-26 16:21:47 +02:00
|
|
|
[scope, debouncedSearchTerm, scopeExtra],
|
|
|
|
{
|
|
|
|
enabled: !!debouncedSearchTerm,
|
|
|
|
refetchOnMount: false
|
|
|
|
}
|
|
|
|
);
|
2022-02-08 12:49:51 +01:00
|
|
|
const results =
|
|
|
|
isSuccess && data ? transformResults(debouncedSearchTerm, data) : [];
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2022-02-22 13:14:11 +01:00
|
|
|
const onBlur = () => {
|
2021-06-09 17:13:26 +02:00
|
|
|
if (!allowInputValues && isSuccess && results[0]) {
|
2021-08-31 12:49:35 +02:00
|
|
|
const label = getLabel(results[0]);
|
2021-06-09 17:13:26 +02:00
|
|
|
awaitFormSubmit(() => {
|
2021-08-31 12:49:35 +02:00
|
|
|
handleOnSelect(label);
|
2021-06-09 17:13:26 +02:00
|
|
|
});
|
|
|
|
}
|
2022-02-22 13:14:11 +01:00
|
|
|
};
|
2021-06-09 17:13:26 +02:00
|
|
|
|
2023-02-22 11:48:50 +01:00
|
|
|
const [announceLive, setAnnounceLive] = useState('');
|
|
|
|
const announceTimeout = useRef<ReturnType<typeof setTimeout>>();
|
2023-02-22 18:32:42 +01:00
|
|
|
const announceTemplate = document.querySelector<HTMLTemplateElement>(
|
|
|
|
`#${announceTemplateId}`
|
|
|
|
);
|
|
|
|
invariant(announceTemplate, `Missing #${announceTemplateId}`);
|
|
|
|
|
|
|
|
const announceFragment = useRef(
|
|
|
|
announceTemplate.content.cloneNode(true) as DocumentFragment
|
|
|
|
).current;
|
2023-02-22 11:48:50 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-22 18:32:42 +01:00
|
|
|
if (isSuccess) {
|
|
|
|
const slot = announceFragment.querySelector<HTMLSlotElement>(
|
|
|
|
'slot[name="' + (results.length <= 1 ? results.length : 'many') + '"]'
|
2023-02-22 11:48:50 +01:00
|
|
|
);
|
|
|
|
|
2023-02-22 18:32:42 +01:00
|
|
|
if (!slot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const countSlot =
|
|
|
|
slot.querySelector<HTMLSlotElement>('slot[name="count"]');
|
|
|
|
if (countSlot) {
|
|
|
|
countSlot.replaceWith(String(results.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
setAnnounceLive(slot.textContent ?? '');
|
2023-02-22 11:48:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 18:32:42 +01:00
|
|
|
announceTimeout.current = setTimeout(() => {
|
|
|
|
setAnnounceLive('');
|
|
|
|
}, 3000);
|
|
|
|
|
2023-02-22 11:48:50 +01:00
|
|
|
return () => clearTimeout(announceTimeout.current);
|
2023-02-22 18:32:42 +01:00
|
|
|
}, [announceFragment, results.length, isSuccess]);
|
2023-02-22 11:48:50 +01:00
|
|
|
|
2023-02-22 11:47:39 +01:00
|
|
|
const initInstrId = useId();
|
2023-02-22 11:49:26 +01:00
|
|
|
const resultsId = useId();
|
2023-02-22 11:47:39 +01:00
|
|
|
|
2020-10-07 17:41:20 +02:00
|
|
|
return (
|
2021-10-26 16:21:47 +02:00
|
|
|
<Combobox onSelect={handleOnSelect}>
|
2020-10-07 17:41:20 +02:00
|
|
|
<ComboboxInput
|
2021-10-26 16:21:47 +02:00
|
|
|
{...props}
|
2020-10-07 17:41:20 +02:00
|
|
|
onChange={handleOnChange}
|
2021-06-09 17:13:26 +02:00
|
|
|
onBlur={onBlur}
|
2022-07-07 11:34:48 +02:00
|
|
|
value={value ?? ''}
|
2022-01-05 11:34:43 +01:00
|
|
|
autocomplete={false}
|
|
|
|
id={id}
|
2023-02-22 11:47:39 +01:00
|
|
|
aria-describedby={describedby ?? initInstrId}
|
2023-02-22 11:49:26 +01:00
|
|
|
aria-owns={resultsId}
|
2020-10-07 17:41:20 +02:00
|
|
|
/>
|
|
|
|
{isSuccess && (
|
2023-02-22 11:49:26 +01:00
|
|
|
<ComboboxPopover id={resultsId} className="shadow-popup">
|
2020-10-07 17:41:20 +02:00
|
|
|
{results.length > 0 ? (
|
|
|
|
<ComboboxList>
|
2021-06-15 14:36:37 +02:00
|
|
|
{results.map((result, index) => {
|
2021-08-31 12:49:35 +02:00
|
|
|
const label = getLabel(result);
|
|
|
|
const [key, value] = transformResult(result);
|
|
|
|
resultsMap.current[label] = { key, value, result };
|
2022-01-13 13:18:49 +01:00
|
|
|
return <ComboboxOption key={`${key}-${index}`} value={label} />;
|
2020-10-07 17:41:20 +02:00
|
|
|
})}
|
|
|
|
</ComboboxList>
|
|
|
|
) : (
|
|
|
|
<span style={{ display: 'block', margin: 8 }}>
|
|
|
|
Aucun résultat trouvé
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</ComboboxPopover>
|
|
|
|
)}
|
2023-02-22 11:47:39 +01:00
|
|
|
{!describedby && (
|
|
|
|
<span id={initInstrId} className="hidden">
|
2023-02-22 18:32:42 +01:00
|
|
|
{screenReaderInstructions}
|
2023-02-22 11:47:39 +01:00
|
|
|
</span>
|
|
|
|
)}
|
2023-02-22 11:48:50 +01:00
|
|
|
<div aria-live="assertive" className="sr-only">
|
|
|
|
{announceLive}
|
|
|
|
</div>
|
2020-10-07 17:41:20 +02:00
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ComboSearch;
|