Files
element-web/src/components/views/elements/StyledCheckbox.tsx
David Baker 56eafc908e Switch to secure random strings (#29013)
* Switch to secure random strings

Because the js-sdk methods are changing and there's no reason for these
not to use the secure versions. The dedicated upper/lower functions were
*only* used in this one case, so this should do the exact same thing with
the one exported function.

Requires https://github.com/matrix-org/matrix-js-sdk/pull/4621 (merge both together)

* Change remaining instances of randomString

which I somehow entirely missed the first time.

* Fix import order
2025-01-21 13:54:57 +00:00

67 lines
2.1 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
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
Please see LICENSE files in the repository root for full details.
*/
import React, { Ref } from "react";
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
import classnames from "classnames";
export enum CheckboxStyle {
Solid = "solid",
Outline = "outline",
}
interface IProps extends React.InputHTMLAttributes<HTMLInputElement> {
inputRef?: Ref<HTMLInputElement>;
kind?: CheckboxStyle;
id?: string;
}
interface IState {}
export default class StyledCheckbox extends React.PureComponent<IProps, IState> {
private id: string;
public static readonly defaultProps = {
className: "",
};
public constructor(props: IProps) {
super(props);
// 56^10 so unlikely chance of collision.
this.id = this.props.id || "checkbox_" + secureRandomString(10);
}
public render(): React.ReactNode {
/* eslint @typescript-eslint/no-unused-vars: ["error", { "ignoreRestSiblings": true }] */
const { children, className, kind = CheckboxStyle.Solid, inputRef, ...otherProps } = this.props;
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"
/>
<label htmlFor={this.id}>
{/* Using the div to center the image */}
<div className="mx_Checkbox_background">
<div className="mx_Checkbox_checkmark" />
</div>
{!!this.props.children && <div>{this.props.children}</div>}
</label>
</span>
);
}
}