UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

276 lines (229 loc) 8.05 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _get from "@babel/runtime/helpers/get"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import { __decorate } from "tslib"; import React from 'react'; import { action, runInAction } from 'mobx'; import { observer } from 'mobx-react'; import { ShowHelp } from '../field/enum'; import { Radio } from '../radio/Radio'; import Icon from '../icon'; import { hide, show } from '../tooltip/singleton'; import { BooleanValue } from '../data-set/enum'; import autobind from '../_util/autobind'; export var CheckBox = /*#__PURE__*/function (_Radio) { _inherits(CheckBox, _Radio); var _super = _createSuper(CheckBox); function CheckBox(props, context) { var _this; _classCallCheck(this, CheckBox); _this = _super.call(this, props, context); _this.type = 'checkbox'; runInAction(function () { _this.value = _this.props.defaultChecked ? _this.checkedValue : _this.unCheckedValue; }); return _this; } _createClass(CheckBox, [{ key: "unCheckedValue", get: function get() { var unCheckedValue = this.props.unCheckedValue; if (unCheckedValue !== undefined) { return unCheckedValue; } var field = this.field; if (field) { return field.get(BooleanValue.falseValue); } return false; } }, { key: "checkedValue", get: function get() { var value = this.props.value; if (value !== undefined) { return value; } var field = this.field; if (field) { return field.get(BooleanValue.trueValue); } return true; } }, { key: "componentWillUnmount", value: function componentWillUnmount() { _get(_getPrototypeOf(CheckBox.prototype), "componentWillUnmount", this).call(this); if (this.showHelp === ShowHelp.tooltip) { hide(); } } }, { key: "getOmitPropsKeys", value: function getOmitPropsKeys() { return _get(_getPrototypeOf(CheckBox.prototype), "getOmitPropsKeys", this).call(this).concat(['defaultChecked', 'unCheckedValue', 'unCheckedChildren', 'indeterminate']); } }, { key: "handleHelpMouseEnter", value: function handleHelpMouseEnter(e) { var _this$context = this.context, getTooltipTheme = _this$context.getTooltipTheme, getTooltipPlacement = _this$context.getTooltipPlacement; var helpTooltipProps = this.helpTooltipProps; var helpTooltipCls = "".concat(this.getContextConfig('proPrefixCls'), "-tooltip-popup-help"); if (helpTooltipProps && helpTooltipProps.popupClassName) { helpTooltipCls = helpTooltipCls.concat(' ', helpTooltipProps.popupClassName); } show(e.currentTarget, _objectSpread(_objectSpread({ title: this.getDisplayProp('help'), theme: getTooltipTheme('help'), placement: getTooltipPlacement('help') }, helpTooltipProps), {}, { popupClassName: helpTooltipCls })); } }, { key: "handleHelpMouseLeave", value: function handleHelpMouseLeave() { hide(); } }, { key: "renderTooltipHelp", value: function renderTooltipHelp() { var help = this.getDisplayProp('help'); if (help) { return /*#__PURE__*/React.createElement(Icon, { type: "help", onMouseEnter: this.handleHelpMouseEnter, onMouseLeave: this.handleHelpMouseLeave }); } } }, { key: "renderInner", value: function renderInner() { return /*#__PURE__*/React.createElement("i", { className: "".concat(this.prefixCls, "-inner") }); } }, { key: "getChildrenText", value: function getChildrenText() { var _this$props = this.props, children = _this$props.children, unCheckedChildren = _this$props.unCheckedChildren; return this.isChecked() ? children : unCheckedChildren || children; } }, { key: "getWrapperClassNames", value: function getWrapperClassNames() { var _get2; var prefixCls = this.prefixCls, indeterminate = this.props.indeterminate; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return (_get2 = _get(_getPrototypeOf(CheckBox.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this, _defineProperty({}, "".concat(prefixCls, "-indeterminate"), indeterminate)].concat(args)); } }, { key: "isChecked", value: function isChecked() { var _this$props2 = this.props, checked = _this$props2.checked, indeterminate = _this$props2.indeterminate; if (indeterminate) { return false; } var name = this.name, dataSet = this.dataSet, checkedValue = this.checkedValue; if (!this.isControlled && dataSet && name) { return this.getValues().indexOf(checkedValue) !== -1; } if (checked !== undefined) { return checked; } return this.value === checkedValue; } }, { key: "getDataSetValues", value: function getDataSetValues() { var values = this.getDataSetValue(); if (values === undefined) { return []; } return [].concat(values); } }, { key: "setValue", value: function setValue(value, noVaidate) { var record = this.record, checkedValue = this.checkedValue, multiple = this.multiple; if (record) { var values; if (multiple) { values = this.getValues(); if (value === checkedValue) { values.push(value); } else { var index = values.indexOf(checkedValue); if (index !== -1) { values.splice(index, 1); } } } else { values = value; } _get(_getPrototypeOf(CheckBox.prototype), "setValue", this).call(this, values, noVaidate); } else { _get(_getPrototypeOf(CheckBox.prototype), "setValue", this).call(this, value, noVaidate); } } }, { key: "setChecked", value: function setChecked(checked) { this.setValue(checked ? this.checkedValue : this.unCheckedValue); } }, { key: "getOldValue", value: function getOldValue() { return this.isChecked() ? this.checkedValue : this.unCheckedValue; } }]); return CheckBox; }(Radio); CheckBox.displayName = 'CheckBox'; /** * tooltip disable sign */ // eslint-disable-next-line camelcase CheckBox.__PRO_CHECKBOX = true; // eslint-disable-next-line camelcase CheckBox.__IS_IN_CELL_EDITOR = true; CheckBox.defaultProps = _objectSpread(_objectSpread({}, Radio.defaultProps), {}, { suffixCls: 'checkbox', indeterminate: false }); __decorate([autobind], CheckBox.prototype, "handleHelpMouseEnter", null); __decorate([action], CheckBox.prototype, "setValue", null); __decorate([action], CheckBox.prototype, "setChecked", null); var ObserverCheckBox = /*#__PURE__*/function (_CheckBox) { _inherits(ObserverCheckBox, _CheckBox); var _super2 = _createSuper(ObserverCheckBox); function ObserverCheckBox() { _classCallCheck(this, ObserverCheckBox); return _super2.apply(this, arguments); } return _createClass(ObserverCheckBox); }(CheckBox); ObserverCheckBox.defaultProps = CheckBox.defaultProps; // eslint-disable-next-line camelcase ObserverCheckBox.__PRO_CHECKBOX = true; // eslint-disable-next-line camelcase ObserverCheckBox.__IS_IN_CELL_EDITOR = true; ObserverCheckBox = __decorate([observer], ObserverCheckBox); export default ObserverCheckBox; //# sourceMappingURL=CheckBox.js.map