"use strict";
/**
 * @file checkboxes 多选输入框
 *
 * @author fex
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var uncontrollable = require("uncontrollable");
var Checkbox_1 = require("./Checkbox");
var Checkboxes_1 = require("./Checkboxes");
var chunk = require("lodash/chunk");
var theme_1 = require("../theme");
var Radios = /** @class */ (function (_super) {
    tslib_1.__extends(Radios, _super);
    function Radios() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Radios.prototype.toggleOption = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, joinValues = _a.joinValues, extractValue = _a.extractValue, valueField = _a.valueField, clearable = _a.clearable, delimiter = _a.delimiter, options = _a.options;
        var valueArray = Checkboxes_1.value2array(value, {
            multiple: false,
            delimiter: delimiter,
            valueField: valueField,
            options: options
        });
        var idx = valueArray.indexOf(option);
        if (~idx) {
            clearable && valueArray.splice(idx, 1);
        }
        else {
            valueArray = [option];
        }
        var newValue = valueArray[0];
        if (newValue && (joinValues || extractValue)) {
            newValue = newValue[valueField || 'value'];
        }
        // if (joinValues && newValue) {
        //     newValue = newValue[valueField || 'value'];
        // }
        onChange && onChange(newValue);
    };
    Radios.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, options = _a.options, className = _a.className, cx = _a.classnames, placeholder = _a.placeholder, columnsCount = _a.columnsCount, disabled = _a.disabled, inline = _a.inline, delimiter = _a.delimiter, valueField = _a.valueField, classPrefix = _a.classPrefix;
        var valueArray = Checkboxes_1.value2array(value, {
            multiple: false,
            delimiter: delimiter,
            valueField: valueField,
            options: options
        });
        var body = [];
        if (options) {
            body = options.map(function (option, key) { return (React.createElement(Checkbox_1.default, { type: "radio", classPrefix: classPrefix, key: key, onChange: function () { return _this.toggleOption(option); }, checked: !!~valueArray.indexOf(option), className: option.className, disabled: disabled || option.disabled, inline: inline }, option.label)); });
        }
        if (!inline && columnsCount > 1) {
            var weight = 12 / columnsCount;
            var cellClassName_1 = "Grid-col--sm" + (weight === Math.round(weight) ? weight : '');
            body = chunk(body, columnsCount).map(function (group, groupIndex) { return (React.createElement("div", { className: cx('Grid'), key: groupIndex }, group.map(function (item, index) { return (React.createElement("div", { key: index, className: cx(cellClassName_1) }, item)); }))); });
        }
        return (React.createElement("div", { className: className }, body && body.length ? body : placeholder));
    };
    Radios.defaultProps = {
        joinValues: true,
        clearable: false,
        columnsCount: 1 // 一行显示一个
    };
    return Radios;
}(React.Component));
exports.Radios = Radios;
exports.default = theme_1.themeable(uncontrollable(Radios, {
    value: 'onChange'
}));
//# sourceMappingURL=./components/Radios.js.map
