import type { ChangeEvent } from 'react'; import type { FC, TeactNode } from '../../lib/teact/teact'; import type React from '../../lib/teact/teact'; import { memo, useRef, useState, } from '../../lib/teact/teact'; import type { ApiUser } from '../../api/types'; import type { IconName } from '../../types/icons'; import type { IRadioOption } from './CheckboxGroup'; import buildClassName from '../../util/buildClassName'; import { REM } from '../common/helpers/mediaDimensions'; import renderText from '../common/helpers/renderText'; import useCurrentOrPrev from '../../hooks/useCurrentOrPrev'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; import Avatar from '../common/Avatar'; import Icon from '../common/icons/Icon'; import Button from './Button'; import Spinner from './Spinner'; import './Checkbox.scss'; type OwnProps = { id?: string; name?: string; value?: string; user?: ApiUser; label?: TeactNode; labelText?: TeactNode; subLabel?: string; checked?: boolean; rightIcon?: IconName; disabled?: boolean; tabIndex?: number; withIcon?: boolean; blocking?: boolean; permissionGroup?: boolean; isLoading?: boolean; withCheckedCallback?: boolean; onlyInput?: boolean; isRound?: boolean; className?: string; nestedCheckbox?: boolean; nestedCheckboxCount?: number | undefined; nestedOptionList?: IRadioOption[]; leftElement?: TeactNode; values?: string[]; onChange?: (e: ChangeEvent, nestedOptionList?: IRadioOption[]) => void; onCheck?: (isChecked: boolean) => void; onClickLabel?: (e: React.MouseEvent, value?: string) => void; }; const AVATAR_SIZE = 1.25 * REM; const Checkbox: FC = ({ id, name, value, label, user, labelText, subLabel, checked, tabIndex, disabled, withIcon, blocking, permissionGroup, isLoading, className, rightIcon, onlyInput, isRound, nestedCheckbox, nestedCheckboxCount, nestedOptionList, leftElement, values, onChange, onCheck, onClickLabel, }) => { const lang = useOldLang(); const labelRef = useRef(); const [showNested, setShowNested] = useState(false); const renderingUser = useCurrentOrPrev(user, true); const handleChange = useLastCallback((event: ChangeEvent) => { if (disabled) { return; } if (onChange) { onChange(event, nestedOptionList); } if (onCheck) { onCheck(event.currentTarget.checked); } }); const toggleNested = useLastCallback(() => { setShowNested(!showNested); }); function handleClick(event: React.MouseEvent) { if (event.target !== labelRef.current) { onClickLabel?.(event, value); } } function handleInputClick(event: React.MouseEvent) { event.stopPropagation(); } const labelClassName = buildClassName( 'Checkbox', disabled && 'disabled', withIcon && 'withIcon', isLoading && 'loading', blocking && 'blocking', nestedCheckbox && 'nested', subLabel && 'withSubLabel', permissionGroup && 'permission-group', Boolean(leftElement) && 'avatar', onlyInput && 'onlyInput', isRound && 'round', Boolean(rightIcon) && 'withNestedList', className, ); return ( <> {/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */} {nestedCheckbox && (
{nestedOptionList?.map((nestedOption) => ( ))}
)} ); }; export default memo(Checkbox);