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EG;;IAGH;;OAEG;;IAGH;;OAEG;;IAOH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;;IAlGG;;OAEG;;IAGH;;OAEG;;IAGH;;;EAGH;;;IAIH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAOH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;;AAsOP,wBAA4D","file":"checkbox.d.ts","sourcesContent":["/* This Source Code Form is subject to the terms of the Mozilla Public\n * License, v. 2.0. If a copy of the MPL was not distributed with this\n * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n\nimport React from 'react';\nimport { createCn } from 'bem-react-classname';\nimport {\n    TickXsIcon,\n} from '@alfalab/icons-classic/TickXsIcon';\nimport {\n    TickSIcon,\n} from '@alfalab/icons-classic/TickSIcon';\nimport {\n    CheckIndeterminateMIcon,\n} from '@alfalab/icons-classic/CheckIndeterminateMIcon';\nimport {\n    CheckIndeterminateSIcon,\n} from '@alfalab/icons-classic/CheckIndeterminateSIcon';\nimport { withTheme, Theme } from '../cn';\n\nimport TagButton from '../tag-button/tag-button';\n\nimport scrollTo from '../lib/scroll-to';\nimport { SCROLL_TO_CORRECTION } from '../vars';\n\nexport type CheckboxProps = (\n    | {\n          /**\n           * Тип чекбокса\n           */\n          type?: 'normal';\n\n          /**\n           * Размер компонента\n           */\n          size?: 'm' | 'l';\n      }\n    | {\n          /**\n           * Тип чекбокса\n           */\n          type?: 'button';\n\n          /**\n           * Размер компонента\n           */\n          size?: 's' | 'm' | 'l' | 'xl';\n\n          /**\n     * Управление шириной кнопки для типа 'button'. При значении\n     'available' растягивает кнопку на ширину родителя\n     */\n          width?: 'default' | 'available';\n      }\n) & {\n    /**\n     * Текст подписи к чекбоксу\n     */\n    text?: React.ReactNode;\n\n    /**\n     * Идентификатор компонента в DOM\n     */\n    id?: string;\n\n    /**\n     * Имя компонента в DOM\n     */\n    name?: string;\n\n    /**\n     * Текст всплывающей подсказки\n     */\n    title?: string;\n\n    /**\n     * Значение чекбокса, которое будет отправлено на сервер, если он выбран\n     */\n    value?: string;\n\n    /**\n     * Управление возможностью изменять состояние 'checked' компонента\n     */\n    disabled?: boolean;\n\n    /**\n     * Управление состоянием вкл/выкл компонента\n     */\n    checked?: boolean;\n\n    /**\n     * Управление неопределенным состоянием чекбокса\n     */\n    indeterminate?: boolean;\n\n    /**\n     * Тема компонента\n     */\n    theme?: Theme;\n\n    /**\n     * Дополнительный класс\n     */\n    className?: string;\n\n    /**\n     * Обработчик изменения значения 'checked' компонента, принимает на вход isChecked и value компонента\n     */\n    onChange?: (\n        isChecked?: boolean,\n        value?: string,\n        event?: React.ChangeEvent<any>\n    ) => void;\n\n    /**\n     * Обработчик фокуса комнонента\n     */\n    onFocus?: (event?: React.FocusEvent<any>) => void;\n\n    /**\n     * Обработчик снятия фокуса компонента\n     */\n    onBlur?: (event?: React.FocusEvent<any>) => void;\n\n    /**\n     * Обработчик события наведения курсора на чекбокс\n     */\n    onMouseEnter?: (event?: React.MouseEvent<any>) => void;\n\n    /**\n     * Обработчик события снятия курсора с чекбокса\n     */\n    onMouseLeave?: (event?: React.MouseEvent<any>) => void;\n\n    /**\n     * Идентификатор для систем автоматизированного тестирования\n     */\n    'data-test-id'?: string;\n};\n\n/**\n * Компонент чекбокса.\n */\nexport class CheckBox extends React.PureComponent<CheckboxProps> {\n    protected cn = createCn('checkbox');\n\n    static defaultProps: Partial<CheckboxProps> = {\n        type: 'normal',\n        size: 'm',\n    };\n\n    state = {\n        focused: false,\n        hovered: false,\n        checked: false,\n    };\n\n    private root;\n\n    render() {\n        const checked = this.props.checked === undefined\n            ? this.state.checked\n            : this.props.checked;\n\n        return (\n            // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions\n            <label\n                className={ this.cn({\n                    size: this.props.size,\n                    disabled: this.props.disabled,\n                    checked: checked || this.props.indeterminate,\n                    indeterminate: this.props.indeterminate,\n                    focused: this.state.focused,\n                    hovered: this.state.hovered,\n                    width:\n                        this.props.type === 'button' ? this.props.width : null,\n                }) }\n                htmlFor={ this.props.id }\n                onBlur={ this.handleBlur }\n                onFocus={ this.handleFocus }\n                onMouseEnter={ this.handleMouseEnter }\n                onMouseLeave={ this.handleMouseLeave }\n                onMouseDown={ this.handleUnfocus }\n                onMouseUp={ this.handleUnfocus }\n                ref={ (root) => {\n                    this.root = root;\n                } }\n                data-test-id={ this.props['data-test-id'] }\n            >\n                { this.props.type === 'button'\n                    ? this.renderButtonCheckbox(checked)\n                    : this.renderNormalCheckbox(checked) }\n            </label>\n        );\n    }\n\n    renderNormalCheckbox(checked) {\n        let TickIcon = TickXsIcon;\n        let CheckIndeterminateIcon = CheckIndeterminateSIcon;\n        if (this.props.size === 'l') {\n            TickIcon = TickSIcon;\n            CheckIndeterminateIcon = CheckIndeterminateMIcon;\n        }\n\n        return [\n            <span className={ this.cn('box') } key=\"box\">\n                <input\n                    className={ this.cn('control') }\n                    type=\"checkbox\"\n                    autoComplete=\"off\"\n                    name={ this.props.name }\n                    id={ this.props.id }\n                    value={ this.props.value }\n                    checked={ checked }\n                    disabled={ this.props.disabled }\n                    onClick={ this.handleInputControlClick }\n                    onChange={ this.handleChange }\n                />\n                { !this.props.indeterminate && (\n                    <TickIcon className={ this.cn('icon') } />\n                ) }\n                { !checked\n                    && this.props.indeterminate && (\n                    <CheckIndeterminateIcon\n                        className={ this.cn('icon') }\n                    />\n                ) }\n                { checked\n                    && this.props.indeterminate\n                    && (\n                        <TickIcon className={ this.cn('icon') } />\n                    ) }\n            </span>,\n            this.props.text && (\n                <span\n                    className={ this.cn('text') }\n                    key=\"text\"\n                    role=\"presentation\"\n                >\n                    { this.props.text }\n                </span>\n            ),\n        ];\n    }\n\n    renderButtonCheckbox(checked) {\n        return [\n            <TagButton\n                key=\"button\"\n                togglable=\"check\"\n                checked={ checked }\n                title={ this.props.title }\n                disabled={ this.props.disabled }\n                size={ this.props.size || 'm' }\n                width={\n                    this.props.type === 'button' ? this.props.width : undefined\n                }\n                focused={ this.state.focused }\n                onClick={ this.handleChange }\n            >\n                { this.props.text ? this.props.text : '' }\n            </TagButton>,\n            <input\n                className={ this.cn('control') }\n                key=\"control\"\n                type=\"checkbox\"\n                tabIndex={ -1 }\n                autoComplete=\"off\"\n                name={ this.props.name }\n                id={ this.props.id }\n                value={ this.props.value }\n                checked={ checked }\n                disabled={ this.props.disabled }\n                onChange={ this.handleChange }\n            />,\n        ];\n    }\n\n    // eslint-disable-next-line class-methods-use-this\n    private handleInputControlClick(event) {\n        event.stopPropagation();\n    }\n\n    private handleChange = (event) => {\n        if (!this.props.disabled) {\n            const nextCheckedValue = !(this.props.checked === undefined\n                // eslint-disable-next-line react/no-access-state-in-setstate\n                ? this.state.checked\n                : this.props.checked);\n\n            this.setState({ checked: nextCheckedValue });\n\n            if (this.props.onChange) {\n                this.props.onChange(nextCheckedValue, this.props.value, event);\n            }\n        }\n    };\n\n    private handleFocus = (event) => {\n        this.setState({ focused: true });\n\n        if (this.props.onFocus) {\n            this.props.onFocus(event);\n        }\n    };\n\n    private handleUnfocus = () => setImmediate(() => this.setState({ focused: false }));\n\n    private handleBlur = (event) => {\n        this.setState({ focused: false });\n\n        if (this.props.onBlur) {\n            this.props.onBlur(event);\n        }\n    };\n\n    private handleMouseEnter = (event) => {\n        if (!this.props.disabled) {\n            this.setState({ hovered: true });\n        }\n\n        if (this.props.onMouseEnter) {\n            this.props.onMouseEnter(event);\n        }\n    };\n\n    private handleMouseLeave = (event) => {\n        if (!this.props.disabled) {\n            this.setState({ hovered: false });\n        }\n\n        if (this.props.onMouseLeave) {\n            this.props.onMouseLeave(event);\n        }\n    };\n\n    /**\n     * Устанавливает фокус на чекбокс.\n     */\n    public focus() {\n        this.root.focus();\n    }\n\n    /**\n     * Убирает фокус с чекбокса.\n     */\n    // eslint-disable-next-line class-methods-use-this\n    public blur() {\n        if (document.activeElement) {\n            (document.activeElement as HTMLElement).blur();\n        }\n    }\n\n    /**\n     * Скроллит страницу до чекбокса.\n     */\n    public scrollTo() {\n        const elementRect = this.root.getBoundingClientRect();\n\n        scrollTo({\n            // eslint-disable-next-line no-mixed-operators\n            targetY: elementRect.top + window.pageYOffset - SCROLL_TO_CORRECTION,\n        });\n    }\n}\n\nexport default withTheme<CheckboxProps, CheckBox>(CheckBox);\n"]}