ming-demo3
Version:
mdf metaui web
216 lines (192 loc) • 6.77 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _baseui = require("@mdf/baseui");
var _label = _interopRequireDefault(require("./label"));
var _text = _interopRequireDefault(require("./text"));
var SwitchControl = function (_React$Component) {
(0, _inherits2["default"])(SwitchControl, _React$Component);
function SwitchControl(props) {
var _this;
(0, _classCallCheck2["default"])(this, SwitchControl);
_this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(SwitchControl).call(this, props));
_this.state = {
bIsNull: props.bIsNull,
focus: props.focus,
value: props.checked || false,
checkedChildren: props.checkedChildren || '是',
unCheckedChildren: props.unCheckedChildren || '否',
defaultChecked: false,
size: 'default',
disabled: props.disabled || false,
visible: !props.bHidden,
readOnly: props.readOnly,
style: {},
className: props.className || ''
};
_this.handleBodyClick = _this.handleBodyClick.bind((0, _assertThisInitialized2["default"])(_this));
_this.getControl = _this.getControl.bind((0, _assertThisInitialized2["default"])(_this));
_this.baseControl = _this.baseControl.bind((0, _assertThisInitialized2["default"])(_this));
return _this;
}
(0, _createClass2["default"])(SwitchControl, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.model) this.props.model.addListener(this);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
if (this.props.model) this.props.model.addListener(this);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.props.model) this.props.model.removeListener(this);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (nextProps.model) {
if (!this.props.model) {
nextProps.model.addListener(this);
} else {
return;
}
} else {
var checked = false;
if (this.props.model) {
this.props.model.removeListener(this);
if (this.props.checked) this.setState({
value: this.props.checked
});
} else {
this.setState({
value: nextProps.checked
});
}
}
this.setState({
readOnly: nextProps.readOnly,
focus: nextProps.focus,
className: nextProps.className
});
}
}, {
key: "onChange",
value: function onChange(value) {
if (this.state.readOnly) return;
if (this.props.model) {
this.props.model.setValue(value, true);
} else {
if (this.props.onChange) this.props.onChange(value);
}
}
}, {
key: "handleBodyClick",
value: function handleBodyClick(e) {
if (this.contains(this.refs.div, e.target)) return;
document.body.removeEventListener('click', this.handleBodyClick);
this.setState({
focus: false
});
if (this.props.model) this.props.model.execute('blur');
}
}, {
key: "contains",
value: function contains(elem, target) {
if (elem === target) return true;
if (!elem.children.length) return false;
for (var i = 0, len = elem.children.length; i < len; i++) {
if (this.contains(elem.children[i], target)) return true;
}
return false;
}
}, {
key: "setDisabled",
value: function setDisabled(value) {
this.setState({
disabled: value
});
}
}, {
key: "setVisible",
value: function setVisible(value) {
this.setState({
visible: value
});
}
}, {
key: "setValue",
value: function setValue(value) {
if (value === 'false') {
this.props.model.setValue(false);
return;
}
this.setState({
value: value
});
}
}, {
key: "baseControl",
value: function baseControl() {
var _this2 = this;
var _this$state = this.state,
value = _this$state.value,
checkedChildren = _this$state.checkedChildren,
unCheckedChildren = _this$state.unCheckedChildren;
return this.state.readOnly ? value ? checkedChildren : unCheckedChildren : _react["default"].createElement("div", {
ref: "div"
}, _react["default"].createElement(_baseui.Switch, {
checked: value,
size: this.state.size,
style: this.state.style,
disabled: this.state.disabled,
onChange: function onChange(e) {
return _this2.onChange(e);
}
}));
}
}, {
key: "getControl",
value: function getControl() {
var cShowCaption = this.props.cShowCaption;
var title = !this.state.readOnly && this.state.bIsNull === false && cShowCaption ? _react["default"].createElement("label", null, _react["default"].createElement(_baseui.Icon, {
type: "star"
}), cShowCaption) : _react["default"].createElement("label", null, cShowCaption);
var control = cShowCaption ? _react["default"].createElement(_label["default"], {
control: this.baseControl(),
title: title
}) : this.baseControl();
return control;
}
}, {
key: "render",
value: function render() {
var control = this.getControl();
var style = this.state.visible ? {} : {
display: "none"
};
var classname = this.state.className;
if (!this.state.readOnly) classname = classname + ' contorl-switch';
if (this.state.focus) document.body.addEventListener('click', this.handleBodyClick);
return _react["default"].createElement("div", {
className: classname,
style: style
}, control);
}
}]);
return SwitchControl;
}(_react["default"].Component);
exports["default"] = SwitchControl;
//# sourceMappingURL=switch.js.map