1 import classNames from "classnames";
2 import { Component, linkEvent } from "inferno";
3 import { Language } from "lemmy-js-client";
4 import { i18n } from "../../i18next";
5 import { UserService } from "../../services/UserService";
6 import { randomStr, selectableLanguages } from "../../utils";
7 import { Icon } from "./icon";
9 interface LanguageSelectProps {
10 allLanguages: Language[];
11 siteLanguages: number[];
12 selectedLanguageIds?: number[];
14 onChange(val: number[]): any;
17 iconVersion?: boolean;
20 export class LanguageSelect extends Component<LanguageSelectProps, any> {
21 private id = `language-select-${randomStr()}`;
23 constructor(props: any, context: any) {
24 super(props, context);
28 this.setSelectedValues();
31 // Necessary because there is no HTML way to set selected for multiple in value=
33 let ids = this.props.selectedLanguageIds?.map(toString);
35 let select = (document.getElementById(this.id) as HTMLSelectElement)
37 for (let i = 0; i < select.length; i++) {
39 if (ids.includes(o.value)) {
47 return this.props.iconVersion ? (
51 <div className="alert alert-warning" role="alert">
52 {i18n.t("undetermined_language_warning")}
54 <div className="form-group row">
56 className={classNames("col-form-label", {
57 "col-sm-3": this.props.multiple,
58 "col-sm-2": !this.props.multiple,
62 {i18n.t(this.props.multiple ? "language_plural" : "language")}
65 className={classNames("input-group", {
66 "col-sm-9": this.props.multiple,
67 "col-sm-10": !this.props.multiple,
71 {this.props.multiple && (
72 <div className="input-group-append">
74 className="input-group-text"
75 onClick={linkEvent(this, this.handleDeselectAll)}
88 let selectedLangs = this.props.selectedLanguageIds;
89 let filteredLangs = selectableLanguages(
90 this.props.allLanguages,
91 this.props.siteLanguages,
94 UserService.Instance.myUserInfo
99 className={classNames("lang-select-action", {
100 "form-control custom-select": !this.props.iconVersion,
101 "btn btn-sm text-muted": this.props.iconVersion,
104 onChange={linkEvent(this, this.handleLanguageChange)}
106 multiple={this.props.multiple}
108 {filteredLangs.map(l => (
112 selected={selectedLangs?.includes(l.id)}
121 handleLanguageChange(i: LanguageSelect, event: any) {
122 let options: HTMLOptionElement[] = Array.from(event.target.options);
123 let selected: number[] = options
124 .filter(o => o.selected)
125 .map(o => Number(o.value));
127 i.props.onChange(selected);
130 handleDeselectAll(i: LanguageSelect, event: any) {
131 event.preventDefault();
132 i.props.onChange([]);