import * as tslib_1 from "tslib";
import React, { useState, useEffect } from 'react';
import classnames from 'classnames';
var defaultProps = {
    prefixCls: 'ty-checkbox-group',
    defaultValue: [],
};
var CheckboxGroup = function (props) {
    var onChange = props.onChange, disabled = props.disabled, prefixCls = props.prefixCls, className = props.className, style = props.style, children = props.children;
    var cls = classnames(prefixCls, className);
    var _a = useState(props.value ? props.value : props.defaultValue), value = _a[0], setValue = _a[1];
    var _onChange = function (checked, event) {
        if (!disabled) {
            var name_1 = event.currentTarget.name;
            var idx = value.indexOf(name_1);
            if (idx > -1) {
                value.splice(idx, 1);
            }
            else {
                value.push(name_1);
            }
            !('value' in props) && setValue(value.slice());
            onChange && onChange(value);
        }
    };
    useEffect(function () {
        ('value' in props) && setValue(props.value.slice());
    });
    return (React.createElement("div", { className: cls, style: style }, React.Children.map(children, function (child) {
        var childProps = tslib_1.__assign({}, child.props, { disabled: child.props.disabled || disabled, checked: child.props.value ? value.includes(child.props.value) : false, onChange: _onChange });
        return React.cloneElement(child, childProps);
    })));
};
CheckboxGroup.defaultProps = defaultProps;
export default CheckboxGroup;
//# sourceMappingURL=checkbox-group.js.map