fix typeahead
This commit is contained in:
parent
76c0c8c14d
commit
0e4ddc0caa
|
|
@ -44,6 +44,7 @@ export const Typeahead: FC<{
|
|||
open: false,
|
||||
options: [] as { value: string; label: string }[],
|
||||
loaded: false,
|
||||
loading: false,
|
||||
search: {
|
||||
input: "",
|
||||
timeout: null as any,
|
||||
|
|
@ -88,10 +89,19 @@ export const Typeahead: FC<{
|
|||
|
||||
useEffect(() => {
|
||||
if (!isEditor) {
|
||||
if (local.options.length === 0) {
|
||||
loadOptions().then(() => {
|
||||
if (typeof value === "object" && value) {
|
||||
local.value = value;
|
||||
local.render();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (typeof value === "object" && value) {
|
||||
local.value = value;
|
||||
local.render();
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
|
|
@ -234,9 +244,11 @@ export const Typeahead: FC<{
|
|||
[local.value, local.select, select, options, local.search.input]
|
||||
);
|
||||
|
||||
const openOptions = useCallback(async () => {
|
||||
if (typeof options_fn === "function") {
|
||||
local.loaded = true;
|
||||
const loadOptions = useCallback(async () => {
|
||||
if (typeof options_fn === "function" && !local.loading) {
|
||||
local.loading = true;
|
||||
local.loaded = false;
|
||||
local.render();
|
||||
const res = options_fn({
|
||||
search: local.search.input,
|
||||
existing: local.options,
|
||||
|
|
@ -256,6 +268,9 @@ export const Typeahead: FC<{
|
|||
} else {
|
||||
applyOptions(res);
|
||||
}
|
||||
local.loaded = true;
|
||||
local.loading = false;
|
||||
local.render();
|
||||
}
|
||||
}
|
||||
}, [options_fn]);
|
||||
|
|
@ -336,17 +351,20 @@ export const Typeahead: FC<{
|
|||
local.open = false;
|
||||
|
||||
resetSearch();
|
||||
if (local.mode === "single") {
|
||||
const item = local.options.find((item) => item.value === value);
|
||||
if (item) {
|
||||
let search = local.search.input;
|
||||
if (local.mode === "single") {
|
||||
local.search.input = item.label;
|
||||
} else {
|
||||
local.search.input = "";
|
||||
}
|
||||
|
||||
select({
|
||||
search: local.search.input,
|
||||
search,
|
||||
item,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
local.render();
|
||||
}}
|
||||
|
|
@ -368,7 +386,7 @@ export const Typeahead: FC<{
|
|||
|
||||
if (!local.open) {
|
||||
if (local.on_focus_open) {
|
||||
openOptions();
|
||||
loadOptions();
|
||||
local.open = true;
|
||||
local.render();
|
||||
}
|
||||
|
|
@ -392,7 +410,7 @@ export const Typeahead: FC<{
|
|||
if (local.search.searching) {
|
||||
if (local.local_search) {
|
||||
if (!local.loaded) {
|
||||
await openOptions();
|
||||
await loadOptions();
|
||||
}
|
||||
const search = local.search.input.toLowerCase();
|
||||
if (search) {
|
||||
|
|
@ -476,4 +494,4 @@ export const Typeahead: FC<{
|
|||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue