From 00e4900d7bbbfe14b145f0a215b830d823d1f920 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Thu, 7 Apr 2022 22:46:30 +0500 Subject: [PATCH] fixed bug and simplified --- src/components/EntrySelect.tsx | 66 +++++++++++++++-------- src/components/TensePicker.tsx | 4 +- src/components/np-picker/picker-tools.tsx | 6 +++ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/components/EntrySelect.tsx b/src/components/EntrySelect.tsx index e9bc429..8e4548b 100644 --- a/src/components/EntrySelect.tsx +++ b/src/components/EntrySelect.tsx @@ -2,20 +2,59 @@ import { Types as T, } from "@lingdocs/pashto-inflector"; import Select from "react-select"; +import AsyncSelect from "react-select/async"; import { makeSelectOption, makeVerbSelectOption, zIndexProps, } from "./np-picker/picker-tools"; -function EntrySelect(props: { - entries: E[], +function EntrySelect(props: ({ + entries: E[] +} | { + searchF: (search: string) => E[], + getByTs: (ts: number) => E, +}) & { value: E | undefined, onChange: (value: E | undefined) => void, name: string | undefined, isVerbSelect?: boolean, opts: T.TextOptions, }) { + function makeOption(e: E | T.DictionaryEntry) { + if ("entry" in e) { + return (props.isVerbSelect ? makeVerbSelectOption : makeSelectOption)(e, props.opts); + } + return makeSelectOption(e, props.opts); + } + const value = props.value ? makeOption(props.value) : undefined; + if ("searchF" in props) { + const options = (searchString: string) => + new Promise<{ value: string, label: string | JSX.Element }[]>(resolve => { + resolve(props.searchF(searchString).map(makeOption)); + }); + const onChange = (v: { label: string | JSX.Element, value: string } | null) => { + if (!v) { + props.onChange(undefined); + return; + } + const s = props.getByTs(parseInt(v.value)); + if (!s) return; + props.onChange(s); + } + return
+ +
; + } const options = props.entries .sort((a, b) => { if ("entry" in a) { @@ -23,13 +62,8 @@ function EntrySelect(props: { } return a.p.localeCompare("p" in b ? b.p : b.entry.p, "af-PS"); }) - .map((e) => { - if ("entry" in e) { - return (props.isVerbSelect ? makeVerbSelectOption : makeSelectOption)(e, props.opts); - } - return makeSelectOption(e, props.opts); - }); - function onSelect(v: { label: string | JSX.Element, value: string } | null) { + .map(makeOption); + const onChange = (v: { label: string | JSX.Element, value: string } | null) => { if (!v) { props.onChange(undefined); return; @@ -42,21 +76,11 @@ function EntrySelect(props: { if (!s) return; props.onChange(s); } - const selectedEntry: T.DictionaryEntry | undefined = !props.value - ? undefined - : "entry" in props.value - ? props.value.entry - : "p" in props.value - ? props.value - : undefined; - const selected = !selectedEntry - ? undefined - : options.find(o => (selectedEntry && (o.value === selectedEntry.ts.toString()))); return