fixed bug and simplified

This commit is contained in:
lingdocs 2022-04-07 22:46:30 +05:00
parent 5864a8c492
commit 00e4900d7b
3 changed files with 53 additions and 23 deletions

View File

@ -2,20 +2,59 @@ import {
Types as T, Types as T,
} from "@lingdocs/pashto-inflector"; } from "@lingdocs/pashto-inflector";
import Select from "react-select"; import Select from "react-select";
import AsyncSelect from "react-select/async";
import { import {
makeSelectOption, makeSelectOption,
makeVerbSelectOption, makeVerbSelectOption,
zIndexProps, zIndexProps,
} from "./np-picker/picker-tools"; } from "./np-picker/picker-tools";
function EntrySelect<E extends T.DictionaryEntry | VerbEntry>(props: { function EntrySelect<E extends T.DictionaryEntry | VerbEntry>(props: ({
entries: E[], entries: E[]
} | {
searchF: (search: string) => E[],
getByTs: (ts: number) => E,
}) & {
value: E | undefined, value: E | undefined,
onChange: (value: E | undefined) => void, onChange: (value: E | undefined) => void,
name: string | undefined, name: string | undefined,
isVerbSelect?: boolean, isVerbSelect?: boolean,
opts: T.TextOptions, 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 <div>
<AsyncSelect
isSearchable={true}
className="mb-2"
value={value}
onChange={onChange}
defaultOptions={[]}
loadOptions={options}
placeholder={props.name ? `Select ${props.name}...` : undefined}
{...zIndexProps}
/>
</div>;
}
const options = props.entries const options = props.entries
.sort((a, b) => { .sort((a, b) => {
if ("entry" in a) { if ("entry" in a) {
@ -23,13 +62,8 @@ function EntrySelect<E extends T.DictionaryEntry | VerbEntry>(props: {
} }
return a.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) => { .map(makeOption);
if ("entry" in e) { const onChange = (v: { label: string | JSX.Element, value: string } | null) => {
return (props.isVerbSelect ? makeVerbSelectOption : makeSelectOption)(e, props.opts);
}
return makeSelectOption(e, props.opts);
});
function onSelect(v: { label: string | JSX.Element, value: string } | null) {
if (!v) { if (!v) {
props.onChange(undefined); props.onChange(undefined);
return; return;
@ -42,21 +76,11 @@ function EntrySelect<E extends T.DictionaryEntry | VerbEntry>(props: {
if (!s) return; if (!s) return;
props.onChange(s); 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 <div> return <div>
<Select <Select
isSearchable={true} isSearchable={true}
value={selected} value={value}
onChange={onSelect} onChange={onChange}
className="mb-2" className="mb-2"
options={options} options={options}
placeholder={props.name ? `Select ${props.name}...` : undefined} placeholder={props.name ? `Select ${props.name}...` : undefined}

View File

@ -154,7 +154,7 @@ function TensePicker({ onChange, verb, mode }: {
{...zIndexProps} {...zIndexProps}
/> />
{verb && <div className="d-flex flex-row justify-content-between align-items-center mt-3 mb-1" style={{ width: "100%" }}> {verb && <div className="d-flex flex-row justify-content-between align-items-center mt-3 mb-1" style={{ width: "100%" }}>
<div onClick={moveTense("back")} className="clickable"> <div className="btn btn-light clickable" onClick={moveTense("back")}>
<i className="fas fa-chevron-left" /> <i className="fas fa-chevron-left" />
</div> </div>
{mode !== "charts" && <ButtonSelect {mode !== "charts" && <ButtonSelect
@ -169,7 +169,7 @@ function TensePicker({ onChange, verb, mode }: {
}]} }]}
handleChange={onPosNegSelect} handleChange={onPosNegSelect}
/>} />}
<div onClick={moveTense("forward")} className="clickable"> <div onClick={moveTense("forward")} className="btn btn-light clickable">
<i className="fas fa-chevron-right" /> <i className="fas fa-chevron-right" />
</div> </div>
</div>} </div>}

View File

@ -24,6 +24,12 @@ export const zIndexProps = {
export function makeVerbSelectOption(e: VerbEntry, opts: T.TextOptions): { value: string, label: string | JSX.Element } { export function makeVerbSelectOption(e: VerbEntry, opts: T.TextOptions): { value: string, label: string | JSX.Element } {
const eng = getEnglishVerb(e.entry); const eng = getEnglishVerb(e.entry);
if (!eng) {
console.log("yep no eng for", e);
} else if (e.entry.p === "اړېدل") {
console.log("eng for", e);
console.log(eng);
}
const ps = plainTextPsAdjustment( const ps = plainTextPsAdjustment(
{ p: e.entry.p, f: removeFVarients(e.entry.f) }, { p: e.entry.p, f: removeFVarients(e.entry.f) },
opts, opts,