UNPKG

acud

Version:

acg react 组件库

116 lines • 4.71 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _extends from "@babel/runtime/helpers/esm/extends"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import classNames from 'classnames'; import OutlinedButtonCheck from "acud-icon/es/icons/OutlinedButtonCheck"; import { lengthLimit } from '../_util/lengthLimit'; var ButtonCheckbox = /*#__PURE__*/function (_React$Component) { function ButtonCheckbox(props) { var _this; _classCallCheck(this, ButtonCheckbox); _this = _callSuper(this, ButtonCheckbox, [props]); _this.handleClick = function (e) { var _this$props = _this.props, disabled = _this$props.disabled, onChange = _this$props.onChange; if (disabled) { return; } var curChecked = _this.state.checked; if (!('checked' in _this.props)) { _this.setState({ checked: !curChecked }); } if (onChange) { onChange({ target: _extends(_extends({}, _this.props), { checked: !curChecked }), stopPropagation: function stopPropagation() { e.stopPropagation(); }, preventDefault: function preventDefault() { e.preventDefault(); }, nativeEvent: e.nativeEvent }); } }; var checked = 'checked' in props ? props.checked : props.defaultChecked; _this.state = { checked: checked }; return _this; } _inherits(ButtonCheckbox, _React$Component); return _createClass(ButtonCheckbox, [{ key: "render", value: function render() { var _a = this.props, prefixCls = _a.prefixCls, className = _a.className, style = _a.style, disabled = _a.disabled, children = _a.children, others = __rest(_a, ["prefixCls", "className", "style", "disabled", "children"]); var title = typeof children === 'string' && children.length > 8 ? children : undefined; var childrenFormat = typeof children === 'string' ? lengthLimit(children, 8) : children; var globalProps = Object.keys(others).reduce(function (prev, key) { if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') { // eslint-disable-next-line no-param-reassign prev[key] = others[key]; } return prev; }, {}); var checked = this.state.checked; var classString = classNames(prefixCls, className, _defineProperty(_defineProperty({}, "".concat(prefixCls, "-checked"), checked), "".concat(prefixCls, "-disabled"), disabled)); return /*#__PURE__*/React.createElement("span", _extends({ className: classString, style: style, "aria-disabled": disabled, onClick: this.handleClick }, globalProps, { title: title }), childrenFormat, checked ? /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-checked-flag") }) : null, checked ? /*#__PURE__*/React.createElement(OutlinedButtonCheck, { className: "".concat(prefixCls, "-checked-icon"), width: 12, height: 12 }) : null); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if ('checked' in props) { return _extends(_extends({}, state), { checked: props.checked }); } return null; } }]); }(React.Component); ButtonCheckbox.defaultProps = { prefixCls: 'acud-checkbox-button', className: '', style: {}, defaultChecked: false }; export default ButtonCheckbox;