2020-05-28 22:33:00 +01:00
|
|
|
/*
|
2025-03-20 15:35:54 +00:00
|
|
|
Copyright 2024,2025 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
|
|
|
*/
|
|
|
|
|
|
2025-03-20 15:35:54 +00: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";
|
2025-03-20 15:35:54 +00:00
|
|
|
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;
|
2025-03-20 15:35:54 +00:00
|
|
|
description?: ReactNode;
|
2026-04-16 14:35:40 +01:00
|
|
|
formWrap?: boolean;
|
2020-05-28 22:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 15:35:54 +00:00
|
|
|
const StyledCheckbox: React.FC<IProps> = ({
|
|
|
|
|
id: initialId,
|
|
|
|
|
children: label,
|
|
|
|
|
className,
|
|
|
|
|
inputRef,
|
|
|
|
|
description,
|
2026-04-16 14:35:40 +01:00
|
|
|
formWrap = true,
|
2025-03-20 15:35:54 +00:00
|
|
|
...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>
|
2025-03-20 15:35:54 +00:00
|
|
|
);
|
2026-04-16 14:35:40 +01:00
|
|
|
|
|
|
|
|
if (formWrap) {
|
|
|
|
|
return <Form.Root>{field}</Form.Root>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return field;
|
2025-03-20 15:35:54 +00:00
|
|
|
};
|
2020-05-28 22:33:00 +01:00
|
|
|
|
2025-03-20 15:35:54 +00:00
|
|
|
export default StyledCheckbox;
|