"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* @flow */
const React = __importStar(require("react"));
{
    CheckBoxProps;
}
from;
'./types';
const prop_types_1 = __importDefault(require("prop-types"));
const classnames_1 = __importDefault(require("classnames"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const ErrorBoundary_1 = __importDefault(require("components/hoc/ErrorBoundary"));
const helpers_1 = require("components/helpers");
const styles_1 = __importDefault(require("./styles"));
class CheckBox extends React.Component {
    constructor(props) {
        super(props);
        this.changeValue = value => {
            this.setState({ value });
            if (this.props.onChange) {
                this.props.onChange(value);
            }
        };
        this._toggleValue = () => {
            const newValue = !this.state.value;
            this.setState({ value: newValue });
            if (this.props.onChange) {
                this.props.onChange(newValue);
            }
        };
        this._renderCheckbox = () => {
            const { classes, label, disabled, id } = this.props;
            return (React.createElement("div", { id: id + '-checkbox', className: classnames_1.default(classes.wrapper, { disabled }), onClick: this._toggleValue },
                React.createElement("div", { className: classnames_1.default(classes.checkbox, {
                        checked: this.state.value,
                    }) }, this.state.value && (React.createElement("i", { className: classnames_1.default('icon icon-tick', classes.tickIcon) }))),
                Boolean(label) && React.createElement("div", { className: classnames_1.default(classes.label) }, label)));
        };
        this.state = {
            value: props.value,
        };
    }
    componentDidUpdate(prevProps) {
        if (this.props.value !== prevProps.value) {
            this.setState({
                value: this.props.value,
            });
        }
    }
    render() {
        const { classes, error, id } = this.props;
        return (React.createElement(ErrorBoundary_1.default, null,
            React.createElement("div", { className: classnames_1.default(classes.component), id: id },
                this._renderCheckbox(),
                Boolean(error) && (React.createElement("div", { id: id + '-error', className: classnames_1.default(classes.error) }, error)))));
    }
}
CheckBox.propTypes = {
    label: prop_types_1.default.node,
    color: prop_types_1.default.string,
    error: prop_types_1.default.string,
    size: prop_types_1.default.oneOf(['small', 'medium', 'large']),
    onChange: prop_types_1.default.func,
    disabled: prop_types_1.default.bool,
    value: prop_types_1.default.bool,
    classes: prop_types_1.default.object,
    id: prop_types_1.default.string,
};
CheckBox.defaultProps = {
    label: '',
    onChange: () => { },
    disabled: false,
    value: false,
    size: 'large',
    id: `checkbox-${helpers_1.randomNumber()}`,
};
exports.default = withStyles_1.default(styles_1.default)(CheckBox);
//# sourceMappingURL=index.js.map