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.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
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
|
|
|
|
2022-12-16 12:29:59 +00: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
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2020-08-29 01:11:08 +01:00
|
|
|
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
|
2021-12-17 11:08:56 -06:00
|
|
|
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}>
|
2021-12-17 11:08:56 -06:00
|
|
|
<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}>
|
2021-07-19 22:43:11 +01:00
|
|
|
{/* 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" />
|
2022-08-09 15:07:25 +02:00
|
|
|
</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
|
|
|
}
|
2020-08-29 01:11:08 +01:00
|
|
|
}
|