2022-02-10 07:27:34 +00:00
|
|
|
import Select from "react-select";
|
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
const languageOptions = [
|
|
|
|
{ value: "ar", label: "Arabic" },
|
|
|
|
{ value: "fa", label: "Farsi" },
|
|
|
|
{ value: "prs", label: "Dari" },
|
|
|
|
{ value: "ps", label: "Pashto" },
|
|
|
|
{ value: "ps-AF", label: "Pashto (Afghanistan) "},
|
|
|
|
{ value: "ps-PK", label: "Pashto (Pakistan) "},
|
|
|
|
{ value: "ur", label: "Urdu" },
|
|
|
|
{ value: "other", label: "Other..." },
|
|
|
|
];
|
|
|
|
|
|
|
|
function LanguageSelect({ value, onChange }: {
|
2022-02-10 07:34:15 +00:00
|
|
|
value: string | undefined,
|
2022-02-10 07:27:34 +00:00
|
|
|
onChange: (language: string | null) => void,
|
|
|
|
}) {
|
|
|
|
const [showingOther, setShowingOther] = useState<boolean>(false);
|
|
|
|
function handleChange(o: { value: string, label: string }) {
|
|
|
|
if (!o) {
|
|
|
|
onChange(null);
|
2022-02-10 07:34:15 +00:00
|
|
|
if (showingOther) setShowingOther(false);
|
2022-02-10 07:27:34 +00:00
|
|
|
} else if (o.value === "other") {
|
|
|
|
setShowingOther(true);
|
|
|
|
onChange(null);
|
|
|
|
} else {
|
|
|
|
if (showingOther) setShowingOther(false);
|
|
|
|
onChange(o.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return <div>
|
|
|
|
<div className="mt-4 mb-2">language:</div>
|
|
|
|
<Select
|
|
|
|
className="basic-single"
|
|
|
|
classNamePrefix="select"
|
|
|
|
isClearable={true}
|
2022-02-10 07:34:15 +00:00
|
|
|
value={languageOptions.find(o => value === o.value)}
|
2022-02-10 07:27:34 +00:00
|
|
|
isSearchable
|
|
|
|
// @ts-ignore
|
|
|
|
onChange={handleChange}
|
|
|
|
// @ts-ignore
|
|
|
|
options={languageOptions}
|
|
|
|
/>
|
|
|
|
{showingOther && <div className="my-2">
|
|
|
|
<label htmlFor="otherLang" className="form-label d-flex flex-row align-items-center">
|
|
|
|
<span>Custom <a href="https://www.w3.org/International/articles/language-tags/" target="_blank">IETF BCP 47</a> Language Code</span>
|
|
|
|
</label>
|
|
|
|
{/* TODO: for some reason can't use value={value} with this - but it still works */}
|
|
|
|
<input onChange={(e) => onChange(e.target.value)} type="text" className="form-control" id="otherLang" />
|
|
|
|
</div>}
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LanguageSelect;
|