choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
220 lines (192 loc) • 6.72 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import PureRenderMixin from '../util/PureRenderMixin';
import classNames from 'classnames';
var Checkbox =
/*#__PURE__*/
function (_Component) {
_inherits(Checkbox, _Component);
var _super = _createSuper(Checkbox);
function Checkbox(_props) {
var _this;
_classCallCheck(this, Checkbox);
_this = _super.call(this, _props);
_defineProperty(_assertThisInitialized(_this), "handleChange", function (e) {
var _assertThisInitialize = _assertThisInitialized(_this),
props = _assertThisInitialize.props;
if (props.disabled) {
return;
}
if (!('checked' in props)) {
_this.setState({
checked: e.target.checked
});
}
props.onChange({
target: _objectSpread({}, props, {
checked: e.target.checked
}),
stopPropagation: function stopPropagation() {
e.stopPropagation();
},
preventDefault: function preventDefault() {
e.preventDefault();
},
nativeEvent: e.nativeEvent
});
});
_defineProperty(_assertThisInitialized(_this), "saveInput", function (node) {
_this.input = node;
});
var checked = 'checked' in _props ? _props.checked : _props.defaultChecked;
_this.state = {
checked: checked
};
return _this;
}
_createClass(Checkbox, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: nextProps.checked
});
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
}, {
key: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props = this.props,
prefixCls = _this$props.prefixCls,
className = _this$props.className,
style = _this$props.style,
name = _this$props.name,
id = _this$props.id,
type = _this$props.type,
disabled = _this$props.disabled,
readOnly = _this$props.readOnly,
tabIndex = _this$props.tabIndex,
onClick = _this$props.onClick,
onFocus = _this$props.onFocus,
onBlur = _this$props.onBlur,
autoFocus = _this$props.autoFocus,
value = _this$props.value,
others = _objectWithoutProperties(_this$props, ["prefixCls", "className", "style", "name", "id", "type", "disabled", "readOnly", "tabIndex", "onClick", "onFocus", "onBlur", "autoFocus", "value"]);
var globalProps = Object.keys(others).reduce(function (prev, key) {
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
prev[key] = others[key];
}
return prev;
}, {});
var checked = this.state.checked;
var classString = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-checked"), checked), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
return React.createElement("span", {
className: classString,
style: style
}, React.createElement("input", _extends({
name: name,
id: id,
type: type,
readOnly: readOnly,
disabled: disabled,
tabIndex: tabIndex,
className: "".concat(prefixCls, "-input"),
checked: !!checked,
onClick: onClick,
onFocus: onFocus,
onBlur: onBlur,
onChange: this.handleChange,
autoFocus: autoFocus,
ref: this.saveInput,
value: value
}, globalProps)), React.createElement("span", {
className: "".concat(prefixCls, "-inner")
}));
}
}]);
return Checkbox;
}(Component);
_defineProperty(Checkbox, "propTypes", {
prefixCls: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
name: PropTypes.string,
id: PropTypes.string,
type: PropTypes.string,
defaultChecked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
checked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
disabled: PropTypes.bool,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onClick: PropTypes.func,
tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
readOnly: PropTypes.bool,
autoFocus: PropTypes.bool,
value: PropTypes.any
});
_defineProperty(Checkbox, "defaultProps", {
prefixCls: 'rc-checkbox',
className: '',
style: {},
type: 'checkbox',
defaultChecked: false,
onFocus: function onFocus() {},
onBlur: function onBlur() {},
onChange: function onChange() {}
});
export { Checkbox as default };
//# sourceMappingURL=Checkbox.js.map