import React from 'react';
import PropTypes from 'prop-types';
import Checkbox from 'react-bootstrap/lib/Checkbox';

import {isIndeterminate} from './FormControl';

export class PureCheckbox extends React.Component {
    static propTypes = {
        hiddenLabel: PropTypes.bool,
        children: PropTypes.node,
        inputRef: PropTypes.func,
        label: PropTypes.string
    };

    static defaultProps = {
        hiddenLabel: false,
        children: '',
        inputRef: null,
        label: ''
    };

    input = {};

    render() {
        let {hiddenLabel, children, label, inputRef, ...props} = this.props;

        if (!inputRef) {
            inputRef = ref => (this.input = ref);
        }

        this.input.indeterminate = isIndeterminate(props.checked);

        return (
            <div data-test-automation-id="checkbox">
                <Checkbox {...props} data-test-automation-id="input" inputRef={inputRef}>
                    <span
                        data-test-automation-id="label"
                        className={!!hiddenLabel ? 'sr-only' : null}>
                        {children || label}
                    </span>
                </Checkbox>
            </div>
        );
    }
}
