/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var icons = require('./icons.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var preventEvent = function (e) { return e.stopPropagation(); };
var Checkbox = /** @class */ (function (_super) {
    tslib.__extends(Checkbox, _super);
    function Checkbox() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.labelRef = React__default["default"].createRef();
        return _this;
    }
    Checkbox.prototype.handleCheck = function (e) {
        var _a = this.props, trueValue = _a.trueValue, falseValue = _a.falseValue, onChange = _a.onChange;
        if (!onChange) {
            return;
        }
        onChange(e.currentTarget.checked ? trueValue : falseValue, e.nativeEvent.shiftKey);
    };
    Checkbox.prototype.setClassName = function (className) {
        this.setState({
            className: className
        });
    };
    Checkbox.prototype.render = function () {
        var _a;
        var _b = this.props, size = _b.size, className = _b.className, cx = _b.classnames, value = _b.value, label = _b.label, partial = _b.partial, trueValue = _b.trueValue, children = _b.children, disabled = _b.disabled, description = _b.description, readOnly = _b.readOnly, checked = _b.checked, type = _b.type, name = _b.name, labelClassName = _b.labelClassName, _c = _b.optionType, optionType = _c === void 0 ? 'default' : _c, mobileUI = _b.mobileUI, testIdBuilder = _b.testIdBuilder;
        var stateClassName = (this.state || {}).className;
        var _checked = typeof checked !== 'undefined'
            ? checked
            : typeof value === 'undefined'
                ? value
                : value == trueValue;
        return (_J$X_("label", tslib.__assign({ className: cx('Checkbox', "Checkbox--".concat(type, "--").concat(optionType), className, stateClassName, (_a = {
                    'Checkbox--partial': partial
                },
                _a["Checkbox--".concat(size)] = size,
                _a['is-mobile'] = mobileUI,
                _a['disabled'] = disabled,
                _a['checked'] = _checked,
                _a["Checkbox--".concat(type, "--").concat(optionType, "--checked")] = !disabled && _checked,
                _a["Checkbox--".concat(type, "--").concat(optionType, "--disabled--unchecked")] = disabled && !_checked,
                _a["Checkbox--".concat(type, "--").concat(optionType, "--disabled--checked")] = disabled && _checked,
                _a)), "data-role": "checkbox" }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId(), { "data-amis-name": this.props.dataName, ref: this.labelRef }),
            _J$X_("input", { type: type, checked: typeof checked !== 'undefined'
                    ? checked
                    : typeof value === 'undefined'
                        ? value
                        : value == trueValue, onChange: this.handleCheck, onClick: preventEvent // 当点击 i 的时候，这个地方也会触发 click，很奇怪，干脆禁掉
                , disabled: disabled, readOnly: readOnly, name: name }),
            _J$X_("i", tslib.__assign({}, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('input').getTestId()),
                _J$X_(icons.Icon, { iconContent: "".concat(type, "-icon"), className: "icon" })),
            _J$X_("span", tslib.__assign({ className: cx(labelClassName) }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('label').getTestId()), children || label),
            description ? (_J$X_("div", { className: cx('Checkbox-desc') }, description)) : null));
    };
    Checkbox.defaultProps = {
        trueValue: true,
        falseValue: false,
        type: 'checkbox'
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], Checkbox.prototype, "handleCheck", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String]),
        tslib.__metadata("design:returntype", void 0)
    ], Checkbox.prototype, "setClassName", null);
    return Checkbox;
}(React__default["default"].Component));
var Checkbox$1 = amisCore.themeable(Checkbox);

exports.Checkbox = Checkbox;
exports["default"] = Checkbox$1;
