]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/comment-sort-select.tsx
ad4eebfe8cdebe5d3310838fffbd33702147e878
[lemmy-ui.git] / src / shared / components / common / comment-sort-select.tsx
1 import { randomStr } from "@utils/helpers";
2 import { Component, linkEvent } from "inferno";
3 import { CommentSortType } from "lemmy-js-client";
4 import { relTags, sortingHelpUrl } from "../../config";
5 import { I18NextService } from "../../services";
6 import { Icon } from "./icon";
7
8 interface CommentSortSelectProps {
9   sort: CommentSortType;
10   onChange?(val: CommentSortType): any;
11 }
12
13 interface CommentSortSelectState {
14   sort: CommentSortType;
15 }
16
17 export class CommentSortSelect extends Component<
18   CommentSortSelectProps,
19   CommentSortSelectState
20 > {
21   private id = `sort-select-${randomStr()}`;
22   state: CommentSortSelectState = {
23     sort: this.props.sort,
24   };
25
26   constructor(props: any, context: any) {
27     super(props, context);
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           className="sort-select form-select d-inline-block w-auto me-2 mb-2"
45           aria-label={I18NextService.i18n.t("sort_type")}
46         >
47           <option disabled aria-hidden="true">
48             {I18NextService.i18n.t("sort_type")}
49           </option>
50           <option value={"Hot"}>{I18NextService.i18n.t("hot")}</option>,
51           <option value={"Top"}>{I18NextService.i18n.t("top")}</option>,
52           <option value={"New"}>{I18NextService.i18n.t("new")}</option>
53           <option value={"Old"}>{I18NextService.i18n.t("old")}</option>
54         </select>
55         <a
56           className="sort-select-help text-muted"
57           href={sortingHelpUrl}
58           rel={relTags}
59           title={I18NextService.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 }