import { Option } from "@sniptt/monads"; import classNames from "classnames"; import { Component, linkEvent } from "inferno"; import { Language } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { randomStr } from "../../utils"; import { Icon } from "./icon"; interface LanguageSelectProps { allLanguages: Language[]; selectedLanguageIds: Option; multiple: boolean; onChange(val: number[]): any; } export class LanguageSelect extends Component { private id = `language-select-${randomStr()}`; constructor(props: any, context: any) { super(props, context); } componentDidMount() { this.setSelectedValues(); } // Necessary because there is no HTML way to set selected for multiple in value= setSelectedValues() { this.props.selectedLanguageIds.map(toString).match({ some: ids => { var select = (document.getElementById(this.id) as HTMLSelectElement) .options; for (let i = 0; i < select.length; i++) { let o = select[i]; if (ids.includes(o.value)) { o.selected = true; } } }, none: void 0, }); } render() { let selectedLangs = this.props.selectedLanguageIds; return (
{this.props.multiple && (
)}
); } handleLanguageChange(i: LanguageSelect, event: any) { let options: HTMLOptionElement[] = Array.from(event.target.options); let selected: number[] = options .filter(o => o.selected) .map(o => Number(o.value)); i.props.onChange(selected); } handleDeselectAll(i: LanguageSelect, event: any) { event.preventDefault(); i.props.onChange([]); } }