]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/comment-sort-select.tsx
d0f7947c20c07158ef773a34ece1790bd21e072c
[lemmy-ui.git] / src / shared / components / common / comment-sort-select.tsx
1 import { Component, linkEvent } from "inferno";
2 import { CommentSortType } from "lemmy-js-client";
3 import { i18n } from "../../i18next";
4 import { randomStr, relTags, sortingHelpUrl } from "../../utils";
5 import { Icon } from "./icon";
6
7 interface CommentSortSelectProps {
8   sort: CommentSortType;
9   onChange?(val: CommentSortType): any;
10 }
11
12 interface CommentSortSelectState {
13   sort: CommentSortType;
14 }
15
16 export class CommentSortSelect extends Component<
17   CommentSortSelectProps,
18   CommentSortSelectState
19 > {
20   private id = `sort-select-${randomStr()}`;
21   state: CommentSortSelectState = {
22     sort: this.props.sort,
23   };
24
25   constructor(props: any, context: any) {
26     super(props, context);
27   }
28
29   static getDerivedStateFromProps(props: any): CommentSortSelectState {
30     return {
31       sort: props.sort,
32     };
33   }
34
35   render() {
36     return (
37       <>
38         <select
39           id={this.id}
40           name={this.id}
41           value={this.state.sort}
42           onChange={linkEvent(this, this.handleSortChange)}
43           className="form-select d-inline-block w-auto me-2 mb-2"
44           aria-label={i18n.t("sort_type")}
45         >
46           <option disabled aria-hidden="true">
47             {i18n.t("sort_type")}
48           </option>
49           <option value={"Hot"}>{i18n.t("hot")}</option>,
50           <option value={"Top"}>{i18n.t("top")}</option>,
51           <option value={"New"}>{i18n.t("new")}</option>
52           <option value={"Old"}>{i18n.t("old")}</option>
53         </select>
54         <a
55           className="text-muted"
56           href={sortingHelpUrl}
57           rel={relTags}
58           title={i18n.t("sorting_help")}
59         >
60           <Icon icon="help-circle" classes="icon-inline" />
61         </a>
62       </>
63     );
64   }
65
66   handleSortChange(i: CommentSortSelect, event: any) {
67     i.props.onChange?.(event.target.value);
68   }
69 }