Files
ThreadNet-Web/src/components/views/elements/StyledCheckbox.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-05-28 22:33:00 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-05-28 22:33:00 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-05-28 22:33:00 +01:00
*/
2024-11-13 20:39:17 +05:30
import React, { Ref } from "react";
2020-05-28 22:33:00 +01:00
import { randomString } from "matrix-js-sdk/src/randomstring";
2021-12-09 09:10:23 +00:00
import classnames from "classnames";
2021-10-22 17:23:32 -05:00
2021-10-29 21:57:32 -04:00
export enum CheckboxStyle {
Solid = "solid",
Outline = "outline",
}
2020-05-28 22:33:00 +01:00
interface IProps extends React.InputHTMLAttributes<HTMLInputElement> {
2024-11-13 20:39:17 +05:30
inputRef?: Ref<HTMLInputElement>;
2021-10-29 21:57:32 -04:00
kind?: CheckboxStyle;
2022-01-27 09:55:08 +01:00
id?: string;
2020-05-28 22:33:00 +01:00
}
interface IState {}
export default class StyledCheckbox extends React.PureComponent<IProps, IState> {
2020-06-01 12:04:47 +01:00
private id: string;
2020-05-28 22:33:00 +01:00
public static readonly defaultProps = {
className: "",
2020-06-18 14:32:43 +01:00
};
2020-05-28 22:33:00 +01:00
public constructor(props: IProps) {
2020-05-28 22:33:00 +01:00
super(props);
2020-06-01 15:36:55 +01:00
// 56^10 so unlikely chance of collision.
2022-01-27 09:55:08 +01:00
this.id = this.props.id || "checkbox_" + randomString(10);
2020-05-28 22:33:00 +01:00
}
public render(): React.ReactNode {
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
const { children, className, kind = CheckboxStyle.Solid, inputRef, ...otherProps } = this.props;
2021-10-29 21:57:32 -04:00
const newClassName = classnames("mx_Checkbox", className, {
mx_Checkbox_hasKind: kind,
[`mx_Checkbox_kind_${kind}`]: kind,
});
return (
<span className={newClassName}>
<input
// Pass through the ref - used for keyboard shortcut access to some buttons
ref={inputRef}
id={this.id}
{...otherProps}
type="checkbox"
/>
2020-06-01 15:35:25 +01:00
<label htmlFor={this.id}>
{/* Using the div to center the image */}
2020-06-01 15:35:25 +01:00
<div className="mx_Checkbox_background">
2021-10-29 21:57:32 -04:00
<div className="mx_Checkbox_checkmark" />
</div>
{!!this.props.children && <div>{this.props.children}</div>}
2020-06-01 15:35:25 +01:00
</label>
2020-06-18 14:32:43 +01:00
</span>
);
2020-05-28 22:33:00 +01:00
}
}