signal-desktop/ts/components/Checkbox.tsx

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-18 20:08:14 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useMemo } from 'react';
import { v4 as uuid } from 'uuid';
2021-08-18 20:08:14 +00:00
import { getClassNamesFor } from '../util/getClassNamesFor';
export type PropsType = {
checked?: boolean;
description?: string;
disabled?: boolean;
isRadio?: boolean;
2021-08-18 20:08:14 +00:00
label: string;
moduleClassName?: string;
name: string;
onChange: (value: boolean) => unknown;
};
export const Checkbox = ({
checked,
description,
disabled,
isRadio,
2021-08-18 20:08:14 +00:00
label,
moduleClassName,
name,
onChange,
}: PropsType): JSX.Element => {
const getClassName = getClassNamesFor('Checkbox', moduleClassName);
const id = useMemo(() => `${name}::${uuid()}`, [name]);
2021-08-18 20:08:14 +00:00
return (
<div className={getClassName('')}>
<div className={getClassName('__container')}>
<div className={getClassName('__checkbox')}>
<input
checked={Boolean(checked)}
disabled={disabled}
id={id}
2021-08-18 20:08:14 +00:00
name={name}
onChange={ev => onChange(ev.target.checked)}
type={isRadio ? 'radio' : 'checkbox'}
2021-08-18 20:08:14 +00:00
/>
</div>
<div>
<label htmlFor={id}>{label}</label>
2021-08-18 20:08:14 +00:00
<div className={getClassName('__description')}>{description}</div>
</div>
</div>
</div>
);
};