Files
ThreadNet-Web/apps/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.

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-05-28 22:33:00 +01:00
/*
Copyright 2024,2025 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
*/
import React, { useId, type ReactNode, type Ref } from "react";
2025-01-21 13:54:57 +00:00
import { secureRandomString } from "matrix-js-sdk/src/randomstring";
import { CheckboxInput, Form, HelpMessage, InlineField, Label } from "@vector-im/compound-web";
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>;
2022-01-27 09:55:08 +01:00
id?: string;
description?: ReactNode;
2026-04-16 14:35:40 +01:00
formWrap?: boolean;
2020-05-28 22:33:00 +01:00
}
const StyledCheckbox: React.FC<IProps> = ({
id: initialId,
children: label,
className,
inputRef,
description,
2026-04-16 14:35:40 +01:00
formWrap = true,
...otherProps
}) => {
const id = initialId || "checkbox_" + secureRandomString(10);
const name = useId();
const descriptionId = useId();
2026-04-16 14:35:40 +01:00
const field = (
<InlineField
className={className}
name={name}
control={
<CheckboxInput
ref={inputRef}
aria-describedby={description ? descriptionId : undefined}
id={id}
{...otherProps}
/>
}
>
{label && <Label htmlFor={id}>{label}</Label>}
{description && <HelpMessage id={descriptionId}>{description}</HelpMessage>}
</InlineField>
);
2026-04-16 14:35:40 +01:00
if (formWrap) {
return <Form.Root>{field}</Form.Root>;
}
return field;
};
2020-05-28 22:33:00 +01:00
export default StyledCheckbox;