2021-10-26 16:21:47 +02:00
|
|
|
import React, {
|
|
|
|
useState,
|
|
|
|
useMemo,
|
|
|
|
useCallback,
|
|
|
|
useRef,
|
|
|
|
useEffect
|
|
|
|
} from 'react';
|
2021-02-16 13:36:47 +01:00
|
|
|
import { useDebounce } from 'use-debounce';
|
2020-10-07 17:41:20 +02:00
|
|
|
import { useQuery } from 'react-query';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
Combobox,
|
|
|
|
ComboboxInput,
|
|
|
|
ComboboxPopover,
|
|
|
|
ComboboxList,
|
|
|
|
ComboboxOption
|
|
|
|
} from '@reach/combobox';
|
|
|
|
import '@reach/combobox/styles.css';
|
2021-01-14 17:31:20 +01:00
|
|
|
import { fire } from '@utils';
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2021-06-09 17:13:26 +02:00
|
|
|
import { useDeferredSubmit } from './shared/hooks';
|
|
|
|
|
2020-10-07 17:41:20 +02:00
|
|
|
function defaultTransformResults(_, results) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ComboSearch({
|
|
|
|
hiddenFieldId,
|
|
|
|
onChange,
|
|
|
|
scope,
|
2021-10-26 16:21:47 +02:00
|
|
|
inputId,
|
|
|
|
scopeExtra,
|
2020-10-07 17:41:20 +02:00
|
|
|
minimumInputLength,
|
|
|
|
transformResult,
|
|
|
|
allowInputValues = false,
|
2021-05-06 18:51:19 +02:00
|
|
|
transformResults = defaultTransformResults,
|
2021-10-26 16:21:47 +02:00
|
|
|
...props
|
2020-10-07 17:41:20 +02:00
|
|
|
}) {
|
2021-01-14 23:54:32 +01:00
|
|
|
const hiddenValueField = useMemo(
|
2020-10-07 17:41:20 +02:00
|
|
|
() => document.querySelector(`input[data-uuid="${hiddenFieldId}"]`),
|
|
|
|
[hiddenFieldId]
|
|
|
|
);
|
2021-10-26 16:21:47 +02:00
|
|
|
const comboInputId = useMemo(
|
|
|
|
() => hiddenValueField?.id || inputId,
|
|
|
|
[inputId, hiddenValueField]
|
|
|
|
);
|
2021-01-14 23:54:32 +01:00
|
|
|
const hiddenIdField = useMemo(
|
|
|
|
() =>
|
|
|
|
document.querySelector(
|
|
|
|
`input[data-uuid="${hiddenFieldId}"] + input[data-reference]`
|
|
|
|
),
|
|
|
|
[hiddenFieldId]
|
|
|
|
);
|
2021-11-17 12:52:47 +01:00
|
|
|
const initialValue = hiddenValueField ? hiddenValueField.value : props.value;
|
2020-10-07 17:41:20 +02:00
|
|
|
const [searchTerm, setSearchTerm] = useState('');
|
2021-02-16 13:36:47 +01:00
|
|
|
const [debouncedSearchTerm] = useDebounce(searchTerm, 300);
|
2020-10-07 17:41:20 +02:00
|
|
|
const [value, setValue] = useState(initialValue);
|
|
|
|
const resultsMap = useRef({});
|
2021-08-31 12:49:35 +02:00
|
|
|
const getLabel = (result) => {
|
|
|
|
const [, value, label] = transformResult(result);
|
|
|
|
return label ?? value;
|
|
|
|
};
|
2021-06-09 17:09:07 +02:00
|
|
|
const setExternalValue = useCallback(
|
|
|
|
(value) => {
|
|
|
|
if (hiddenValueField) {
|
|
|
|
hiddenValueField.setAttribute('value', value);
|
|
|
|
fire(hiddenValueField, 'autosave:trigger');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[hiddenValueField]
|
|
|
|
);
|
|
|
|
const setExternalId = useCallback(
|
|
|
|
(key) => {
|
|
|
|
if (hiddenIdField) {
|
|
|
|
hiddenIdField.setAttribute('value', key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[hiddenIdField]
|
|
|
|
);
|
2021-08-31 12:49:35 +02:00
|
|
|
const setExternalValueAndId = useCallback((label) => {
|
|
|
|
const { key, value, result } = resultsMap.current[label];
|
2021-01-14 23:54:32 +01:00
|
|
|
setExternalId(key);
|
|
|
|
setExternalValue(value);
|
2020-10-07 17:41:20 +02:00
|
|
|
if (onChange) {
|
|
|
|
onChange(value, result);
|
|
|
|
}
|
2021-06-09 17:09:07 +02:00
|
|
|
}, []);
|
2021-06-09 17:13:26 +02:00
|
|
|
const awaitFormSubmit = useDeferredSubmit(hiddenValueField);
|
2020-10-07 17:41:20 +02:00
|
|
|
|
|
|
|
const handleOnChange = useCallback(
|
|
|
|
({ target: { value } }) => {
|
|
|
|
setValue(value);
|
2021-10-26 16:21:47 +02:00
|
|
|
if (!value) {
|
|
|
|
setExternalId('');
|
|
|
|
setExternalValue('');
|
|
|
|
if (onChange) {
|
|
|
|
onChange(null);
|
|
|
|
}
|
|
|
|
} else if (value.length >= minimumInputLength) {
|
2020-10-07 17:41:20 +02:00
|
|
|
setSearchTerm(value.trim());
|
|
|
|
if (allowInputValues) {
|
2021-01-14 23:54:32 +01:00
|
|
|
setExternalId('');
|
2020-10-07 17:41:20 +02:00
|
|
|
setExternalValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[minimumInputLength]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleOnSelect = useCallback((value) => {
|
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();
|
2021-06-09 17:09:07 +02:00
|
|
|
}, []);
|
2020-10-07 17:41:20 +02:00
|
|
|
|
2021-10-26 16:21:47 +02:00
|
|
|
const { isSuccess, data } = useQuery(
|
|
|
|
[scope, debouncedSearchTerm, scopeExtra],
|
|
|
|
{
|
|
|
|
enabled: !!debouncedSearchTerm,
|
|
|
|
notifyOnStatusChange: false,
|
|
|
|
refetchOnMount: false
|
|
|
|
}
|
|
|
|
);
|
2020-10-07 17:41:20 +02:00
|
|
|
const results = isSuccess ? transformResults(debouncedSearchTerm, data) : [];
|
|
|
|
|
2021-06-09 17:13:26 +02:00
|
|
|
const onBlur = useCallback(() => {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
2021-10-26 16:21:47 +02:00
|
|
|
useEffect(() => {
|
|
|
|
document
|
|
|
|
.querySelector(`#${comboInputId}[type="hidden"]`)
|
|
|
|
?.removeAttribute('id');
|
|
|
|
}, [comboInputId]);
|
|
|
|
|
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}
|
|
|
|
id={comboInputId}
|
2020-10-07 17:41:20 +02:00
|
|
|
onChange={handleOnChange}
|
2021-06-09 17:13:26 +02:00
|
|
|
onBlur={onBlur}
|
2020-10-07 17:41:20 +02:00
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
{isSuccess && (
|
|
|
|
<ComboboxPopover className="shadow-popup">
|
|
|
|
{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 };
|
2020-10-07 17:41:20 +02:00
|
|
|
return (
|
|
|
|
<ComboboxOption
|
2021-06-15 14:36:37 +02:00
|
|
|
key={`${key}-${index}`}
|
2021-08-31 12:49:35 +02:00
|
|
|
value={label}
|
|
|
|
data-option-value={value}
|
2020-10-07 17:41:20 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ComboboxList>
|
|
|
|
) : (
|
|
|
|
<span style={{ display: 'block', margin: 8 }}>
|
|
|
|
Aucun résultat trouvé
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</ComboboxPopover>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComboSearch.propTypes = {
|
2021-11-17 12:52:47 +01:00
|
|
|
value: PropTypes.string,
|
2020-10-07 17:41:20 +02:00
|
|
|
hiddenFieldId: PropTypes.string,
|
|
|
|
scope: PropTypes.string,
|
|
|
|
minimumInputLength: PropTypes.number,
|
|
|
|
transformResult: PropTypes.func,
|
|
|
|
transformResults: PropTypes.func,
|
|
|
|
allowInputValues: PropTypes.bool,
|
2021-05-06 18:51:19 +02:00
|
|
|
onChange: PropTypes.func,
|
2021-10-26 16:21:47 +02:00
|
|
|
inputId: PropTypes.string,
|
|
|
|
scopeExtra: PropTypes.string
|
2020-10-07 17:41:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ComboSearch;
|