2020-10-07 17:41:20 +02:00
|
|
|
import React, { useState, useMemo, useCallback, useRef } 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
|
|
|
|
|
|
|
function defaultTransformResults(_, results) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ComboSearch({
|
|
|
|
placeholder,
|
|
|
|
required,
|
|
|
|
hiddenFieldId,
|
|
|
|
onChange,
|
|
|
|
scope,
|
|
|
|
minimumInputLength,
|
|
|
|
transformResult,
|
|
|
|
allowInputValues = false,
|
2021-05-06 18:51:19 +02:00
|
|
|
transformResults = defaultTransformResults,
|
|
|
|
className
|
2020-10-07 17:41:20 +02:00
|
|
|
}) {
|
|
|
|
const label = scope;
|
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-01-14 23:54:32 +01:00
|
|
|
const hiddenIdField = useMemo(
|
|
|
|
() =>
|
|
|
|
document.querySelector(
|
|
|
|
`input[data-uuid="${hiddenFieldId}"] + input[data-reference]`
|
|
|
|
),
|
|
|
|
[hiddenFieldId]
|
|
|
|
);
|
|
|
|
const initialValue = hiddenValueField && hiddenValueField.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({});
|
|
|
|
const setExternalValue = useCallback((value) => {
|
2021-01-14 23:54:32 +01:00
|
|
|
if (hiddenValueField) {
|
|
|
|
hiddenValueField.setAttribute('value', value);
|
|
|
|
fire(hiddenValueField, 'autosave:trigger');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const setExternalId = useCallback((key) => {
|
|
|
|
if (hiddenIdField) {
|
|
|
|
hiddenIdField.setAttribute('value', key);
|
2020-10-07 17:41:20 +02:00
|
|
|
}
|
2021-01-14 23:54:32 +01:00
|
|
|
});
|
|
|
|
const setExternalValueAndId = useCallback((value) => {
|
|
|
|
const [key, result] = resultsMap.current[value];
|
|
|
|
setExternalId(key);
|
|
|
|
setExternalValue(value);
|
2020-10-07 17:41:20 +02:00
|
|
|
if (onChange) {
|
|
|
|
onChange(value, result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleOnChange = useCallback(
|
|
|
|
({ target: { value } }) => {
|
|
|
|
setValue(value);
|
|
|
|
if (value.length >= minimumInputLength) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
const { isSuccess, data } = useQuery([scope, debouncedSearchTerm], {
|
|
|
|
enabled: !!debouncedSearchTerm,
|
|
|
|
notifyOnStatusChange: false,
|
|
|
|
refetchOnMount: false
|
|
|
|
});
|
|
|
|
const results = isSuccess ? transformResults(debouncedSearchTerm, data) : [];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Combobox aria-label={label} onSelect={handleOnSelect}>
|
|
|
|
<ComboboxInput
|
2021-05-06 18:51:19 +02:00
|
|
|
className={className}
|
2020-10-07 17:41:20 +02:00
|
|
|
placeholder={placeholder}
|
|
|
|
onChange={handleOnChange}
|
|
|
|
value={value}
|
|
|
|
required={required}
|
|
|
|
/>
|
|
|
|
{isSuccess && (
|
|
|
|
<ComboboxPopover className="shadow-popup">
|
|
|
|
{results.length > 0 ? (
|
|
|
|
<ComboboxList>
|
|
|
|
{results.map((result) => {
|
|
|
|
const [key, str] = transformResult(result);
|
2021-01-14 23:54:32 +01:00
|
|
|
resultsMap.current[str] = [key, result];
|
2020-10-07 17:41:20 +02:00
|
|
|
return (
|
|
|
|
<ComboboxOption
|
|
|
|
key={key}
|
|
|
|
value={str}
|
|
|
|
data-option-value={str}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ComboboxList>
|
|
|
|
) : (
|
|
|
|
<span style={{ display: 'block', margin: 8 }}>
|
|
|
|
Aucun résultat trouvé
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</ComboboxPopover>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComboSearch.propTypes = {
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
required: PropTypes.bool,
|
|
|
|
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,
|
|
|
|
className: PropTypes.string
|
2020-10-07 17:41:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ComboSearch;
|