Does QueryBuilder allow the input field to be set as an autocomplete select input field ? #430
-
I am looking for a select input field which will fetch data from a JSON and will also be an autocomplete select text input - I'm not sure if this is possible looking at the documentation provided but wanted to reach out. Example:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
There are a few autocomplete components out there that you can use with this library. I generally use Here is an example of a custom https://codesandbox.io/s/broken-snow-b78i8f?file=/src/MyValueEditor.tsx import { ValueEditor, ValueEditorProps } from 'react-querybuilder';
import type { GroupBase, OptionsOrGroups } from 'react-select';
import AsyncSelect from 'react-select/async';
const options = [
{ value: 'v1', label: 'abc' },
{ value: 'v2', label: 'def' },
{ value: 'v3', label: 'ghi' },
{ value: 'v4', label: 'jkl' },
{ value: 'v5', label: 'mno' },
{ value: 'v6', label: 'pqr' },
{ value: 'v7', label: 'stu' },
{ value: 'v8', label: 'vwx' },
{ value: 'v9', label: 'yz' },
];
const loadDelay = 500;
const loadOptions = (
iv: string
): Promise<OptionsOrGroups<any, GroupBase<any>>> =>
new Promise(r =>
setTimeout(() => r(options.filter(op => op.label.includes(iv))), loadDelay)
);
export const MyValueEditor = (props: ValueEditorProps) => {
if (props.field === 'autocomplete') {
return (
<AsyncSelect
loadOptions={loadOptions}
onChange={v => props.handleOnChange(v)}
value={props.value}
options={options}
/>
);
}
return <ValueEditor {...props} />;
}; |
Beta Was this translation helpful? Give feedback.
There are a few autocomplete components out there that you can use with this library. I generally use
react-select
, either the standardSelect
component or theAsync
component if I need to load options asynchronously from an API.Here is an example of a custom
ValueEditor
that usesreact-select
when the field is "autocomplete" and falls back to the defaultValueEditor
. The list fakes a delay for an API call withsetTimeout
and then loads the options that match what the user has typed.https://codesandbox.io/s/broken-snow-b78i8f?file=/src/MyValueEditor.tsx