@jdcfe/yep-react
Version:
一套移动端的React组件库
87 lines (69 loc) • 3 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
var CheckBoxGroup = /*#__PURE__*/function (_React$Component) {
_inherits(CheckBoxGroup, _React$Component);
var _super = _createSuper(CheckBoxGroup);
function CheckBoxGroup() {
var _this;
_classCallCheck(this, CheckBoxGroup);
_this = _super.apply(this, arguments);
_this.handleChange = function (e) {
var value = _this.props.value;
var _e$target = e.target,
itemValue = _e$target.value,
checked = _e$target.checked;
var tmpValue = value.concat();
var idx = value.indexOf(itemValue);
if (checked) {
if (idx > -1) {} else {
tmpValue.push(itemValue);
}
} else {
if (idx > -1) {
tmpValue.splice(idx, 1);
}
}
_this.props.onChange && _this.props.onChange(tmpValue);
};
return _this;
}
_createClass(CheckBoxGroup, [{
key: "validateChecked",
value: function validateChecked(childProps) {
return this.props.value.indexOf(childProps.value) > -1;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var children = this.props.children;
return /*#__PURE__*/React.createElement("div", null, React.Children.map(children, function (child, index) {
var childProps = child.props;
var checked = _this2.validateChecked(childProps);
if (child.type.displayName === 'Checkbox') {
return /*#__PURE__*/React.cloneElement(child, {
key: index,
checked: checked,
onChange: _this2.handleChange
});
} else {
return /*#__PURE__*/React.cloneElement(child);
}
}));
}
}]);
return CheckBoxGroup;
}(React.Component);
CheckBoxGroup.defaultProps = {
value: [],
onChange: function onChange() {
return null;
}
};
export default CheckBoxGroup;