]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/comment-sort-select.tsx
Merge branch 'fix/notif_new_fetch_bug' of https://github.com/ernestwisniewski/lemmy...
[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   private emptyState: CommentSortSelectState = {
22     sort: this.props.sort,
23   };
24
25   constructor(props: any, context: any) {
26     super(props, context);
27     this.state = this.emptyState;
28   }
29
30   static getDerivedStateFromProps(props: any): CommentSortSelectState {
31     return {
32       sort: props.sort,
33     };
34   }
35
36   render() {
37     return (
38       <>
39         <select
40           id={this.id}
41           name={this.id}
42           value={this.state.sort}
43           onChange={linkEvent(this, this.handleSortChange)}
44           class="custom-select w-auto mr-2 mb-2"
45           aria-label={i18n.t("sort_type")}
46         >
47           <option disabled aria-hidden="true">
48             {i18n.t("sort_type")}
49           </option>
50           <option value={CommentSortType.Hot}>{i18n.t("hot")}</option>,
51           <option value={CommentSortType.Top}>{i18n.t("top")}</option>,
52           <option value={CommentSortType.New}>{i18n.t("new")}</option>
53           <option value={CommentSortType.Old}>{i18n.t("old")}</option>
54         </select>
55         <a
56           className="text-muted"
57           href={sortingHelpUrl}
58           rel={relTags}
59           title={i18n.t("sorting_help")}
60         >
61           <Icon icon="help-circle" classes="icon-inline" />
62         </a>
63       </>
64     );
65   }
66
67   handleSortChange(i: CommentSortSelect, event: any) {
68     i.props.onChange(event.target.value);
69   }
70 }