From f1dd160b53df267f92323c15fe91cc4bef0aa206 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:23:01 +0500 Subject: [PATCH] abstracted proper entry picker component - ugly inside, nice on outside --- src/components/EntrySelect.tsx | 67 +++++++++++++++++++ src/components/VerbPicker.tsx | 22 +++--- src/components/np-picker/NPNounPicker.tsx | 31 +++------ .../np-picker/NPParticiplePicker.tsx | 32 +++------ src/components/np-picker/picker-tools.ts | 2 +- src/lib/type-predicates.ts | 2 +- 6 files changed, 102 insertions(+), 54 deletions(-) create mode 100644 src/components/EntrySelect.tsx diff --git a/src/components/EntrySelect.tsx b/src/components/EntrySelect.tsx new file mode 100644 index 0000000..d567d4f --- /dev/null +++ b/src/components/EntrySelect.tsx @@ -0,0 +1,67 @@ +import { + Types as T, +} from "@lingdocs/pashto-inflector"; +import Select from "react-select"; +import { + makeSelectOption, + makeVerbSelectOption, + zIndexProps, +} from "./np-picker/picker-tools"; + +function EntrySelect(props: { + entries: E[], + value: E | undefined, + onChange: (value: E | undefined) => void, + name: string | undefined, + isVerbSelect?: boolean, +}) { + const options = props.entries + .sort((a, b) => { + if ("entry" in a) { + return a.entry.p.localeCompare("p" in b ? b.p : b.entry.p, "af-PS") + } + 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); + } + return makeSelectOption(e); + }); + function onSelect(v: { label: string, value: string } | null) { + if (!v) { + props.onChange(undefined); + return; + } + const s = props.entries.find(e => ( + ("entry" in e) + ? e.entry.ts.toString() === v.value + : e.ts.toString() === v.value + )); + 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
+ + {/* o.value === (noun.entry).ts.toString())?.label : "Select Noun..."} - {...zIndexProps} + name="Noun" />
:
{noun &&
diff --git a/src/components/np-picker/NPParticiplePicker.tsx b/src/components/np-picker/NPParticiplePicker.tsx index 154f61f..d410ff1 100644 --- a/src/components/np-picker/NPParticiplePicker.tsx +++ b/src/components/np-picker/NPParticiplePicker.tsx @@ -1,8 +1,4 @@ -import Select from "react-select"; -import { - makeSelectOption, - zIndexProps, -} from "./picker-tools"; +import EntrySelect from "../EntrySelect"; function makeParticipleSelection(verb: VerbEntry): ParticipleSelection { return { @@ -14,31 +10,23 @@ function makeParticipleSelection(verb: VerbEntry): ParticipleSelection { function NPParticiplePicker({ onChange, participle, verbs, clearButton }: { verbs: VerbEntry[], participle: ParticipleSelection | undefined, - onChange: (p: ParticipleSelection) => void, + onChange: (p: ParticipleSelection | undefined) => void, clearButton: JSX.Element, }) { - const options = verbs.map(makeSelectOption) - function onEntrySelect({ value }: { label: string, value: string }) { - const verb = verbs.find(v => v.entry.ts.toString() === value); - if (!verb) { - console.error("entry not found"); + function onEntrySelect(entry: VerbEntry | undefined) { + if (!entry) { + onChange(undefined); return; } - onChange(makeParticipleSelection(verb)); + onChange(makeParticipleSelection(entry)); } return
{clearButton} -