lemmy-ui/src/shared/components/common/comment-sort-select.tsx

71 lines
2 KiB
TypeScript
Raw Normal View History

import { randomStr } from "@utils/helpers";
import { Component, linkEvent } from "inferno";
import { CommentSortType } from "lemmy-js-client";
import { relTags, sortingHelpUrl } from "../../config";
2023-06-22 00:54:35 +00:00
import { I18NextService } from "../../services";
import { Icon } from "./icon";
interface CommentSortSelectProps {
sort: CommentSortType;
onChange?(val: CommentSortType): any;
}
interface CommentSortSelectState {
sort: CommentSortType;
}
export class CommentSortSelect extends Component<
CommentSortSelectProps,
CommentSortSelectState
> {
private id = `sort-select-${randomStr()}`;
state: CommentSortSelectState = {
sort: this.props.sort,
};
constructor(props: any, context: any) {
super(props, context);
}
static getDerivedStateFromProps(props: any): CommentSortSelectState {
return {
sort: props.sort,
};
}
render() {
return (
<>
<select
id={this.id}
name={this.id}
value={this.state.sort}
onChange={linkEvent(this, this.handleSortChange)}
2023-06-20 18:46:16 +00:00
className="sort-select form-select d-inline-block w-auto me-2 mb-2"
2023-06-22 00:54:35 +00:00
aria-label={I18NextService.i18n.t("sort_type")}
>
<option disabled aria-hidden="true">
2023-06-22 00:54:35 +00:00
{I18NextService.i18n.t("sort_type")}
</option>
2023-06-22 00:54:35 +00:00
<option value={"Hot"}>{I18NextService.i18n.t("hot")}</option>,
<option value={"Top"}>{I18NextService.i18n.t("top")}</option>,
<option value={"New"}>{I18NextService.i18n.t("new")}</option>
<option value={"Old"}>{I18NextService.i18n.t("old")}</option>
</select>
<a
2023-06-20 18:46:16 +00:00
className="sort-select-help text-muted"
href={sortingHelpUrl}
rel={relTags}
2023-06-22 00:54:35 +00:00
title={I18NextService.i18n.t("sorting_help")}
>
<Icon icon="help-circle" classes="icon-inline" />
</a>
</>
);
}
handleSortChange(i: CommentSortSelect, event: any) {
i.props.onChange?.(event.target.value);
}
}