TelegramPWA/src/components/ui/Checkbox.tsx
Alexander Zinchuk 3afcde3217 Initial commit
2021-04-09 14:11:51 +03:00

79 lines
1.6 KiB
TypeScript

import { ChangeEvent } from 'react';
import React, { FC, memo, useCallback } from '../../lib/teact/teact';
import buildClassName from '../../util/buildClassName';
import Spinner from './Spinner';
import './Checkbox.scss';
type OwnProps = {
id?: string;
name?: string;
value?: string;
label: string;
subLabel?: string;
checked: boolean;
disabled?: boolean;
round?: boolean;
blocking?: boolean;
isLoading?: boolean;
withCheckedCallback?: boolean;
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
onCheck?: (isChecked: boolean) => void;
};
const Checkbox: FC<OwnProps> = ({
id,
name,
value,
label,
subLabel,
checked,
disabled,
round,
blocking,
isLoading,
onChange,
onCheck,
}) => {
const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
if (onChange) {
onChange(event);
}
if (onCheck) {
onCheck(event.currentTarget.checked);
}
}, [onChange, onCheck]);
const className = buildClassName(
'Checkbox',
disabled && 'disabled',
round && 'round',
isLoading && 'loading',
blocking && 'blocking',
);
return (
<label className={className}>
<input
type="checkbox"
id={id}
name={name}
value={value}
checked={checked}
disabled={disabled}
onChange={handleChange}
/>
<div className="Checkbox-main">
<span className="label">{label}</span>
{subLabel && <span className="subLabel">{subLabel}</span>}
</div>
{isLoading && <Spinner />}
</label>
);
};
export default memo(Checkbox);