/**
 * empty comment line Denis Makarov
 *
 * @author: Denis Makarov
 * @date: 2019-07-22
 */

import * as React from 'react';
import CheckboxAntd, {CheckboxChangeEvent} from 'antd/es/checkbox';
import {joinClassNames} from '../../index';

import * as styles from './checkbox.m.scss';

export interface ICheckboxProps {
    name?: string;
    'data-qaid'?: string;
    disabled?: boolean;
    readOnly?: boolean;
    checked: boolean;
    indeterminate?: boolean; // indicator when not everything is selected
    className?: string; // @deprecated
    onChange?: (e: CheckboxChangeEvent) => void;
    onClick?: React.MouseEventHandler<HTMLElement>;
    style?: React.CSSProperties;
}

export {
    CheckboxChangeEvent
};

export class Checkbox extends React.PureComponent<ICheckboxProps> {

    override render () {
        const {name, disabled, indeterminate, checked, onChange, onClick, className, style, children, readOnly} = this.props;

        const checkboxProps = {
            name,
            disabled,
            className,
            checked,
            indeterminate,
            style,
            onClick,
            onChange
        };

        if (readOnly === true) {
            checkboxProps.onClick = undefined;
            checkboxProps.onChange = undefined;
            checkboxProps.className = joinClassNames(className, styles.readonly);
        }

        return (
            <CheckboxAntd {...checkboxProps} data-component-id={'Checkbox'} data-qaid={this.props['data-qaid']}>
                {children}
            </CheckboxAntd>
        );
    }
}
