@jdcfe/yep-react
Version:
一套移动端的React组件库
196 lines (176 loc) • 6.95 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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";
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; } }
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 CircleOutlined from '@jdcfe/icons-react/CircleOutlined';
import CircleTwoTone from '@jdcfe/icons-react/CircleTwoTone';
import CheckCircleFilled from '@jdcfe/icons-react/CheckCircleFilled';
var BaseCheckbox = /*#__PURE__*/function (_React$PureComponent) {
_inherits(BaseCheckbox, _React$PureComponent);
var _super = _createSuper(BaseCheckbox);
function BaseCheckbox(props) {
var _this;
_classCallCheck(this, BaseCheckbox);
_this = _super.call(this, props);
var checked = 'checked' in props ? props.checked : props.defaultChecked;
_this.state = {
checked: checked
};
_this.createRef = _this.createRef.bind(_assertThisInitialized(_this));
_this.blur = _this.blur.bind(_assertThisInitialized(_this));
_this.focus = _this.focus.bind(_assertThisInitialized(_this));
_this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(BaseCheckbox, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: nextProps.checked
});
}
}
}, {
key: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "createRef",
value: function createRef(node) {
this.input = node;
}
}, {
key: "handleChange",
value: function handleChange(e) {
var _this$props = this.props,
disabled = _this$props.disabled,
onChange = _this$props.onChange;
if (disabled) {
return;
}
if (!('checked' in this.props)) {
this.setState({
checked: e.target.checked
});
}
onChange({
target: Object.assign(Object.assign({}, this.props), {
checked: e.target.checked
}),
stopPropagation: function stopPropagation() {
e.stopPropagation();
},
preventDefault: function preventDefault() {
e.preventDefault();
},
nativeEvent: e.nativeEvent
});
}
}, {
key: "render",
value: function render() {
var _classNames;
var _a = this.props,
prefixCls = _a.prefixCls,
className = _a.className,
style = _a.style,
name = _a.name,
id = _a.id,
type = _a.type,
disabled = _a.disabled,
readOnly = _a.readOnly,
tabIndex = _a.tabIndex,
onClick = _a.onClick,
onFocus = _a.onFocus,
onBlur = _a.onBlur,
autoFocus = _a.autoFocus,
value = _a.value,
label = _a.label,
icon = _a.icon,
others = __rest(_a, ["prefixCls", "className", "style", "name", "id", "type", "disabled", "readOnly", "tabIndex", "onClick", "onFocus", "onBlur", "autoFocus", "value", "label", "icon"]);
var globalProps = Object.keys(others).reduce(function (prev, key) {
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
//@ts-ignore
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 /*#__PURE__*/React.createElement("span", {
className: classString,
style: style
}, /*#__PURE__*/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.createRef,
value: value
}, globalProps)), checked ? icon ? icon : type === 'checkbox' ? /*#__PURE__*/React.createElement(CheckCircleFilled, {
className: "".concat(prefixCls, "-inner"),
style: {
color: '#F0250F'
}
}) : /*#__PURE__*/React.createElement(CircleTwoTone, {
style: {
color: '#F0250F'
},
className: "".concat(prefixCls, "-inner")
}) : /*#__PURE__*/React.createElement(CircleOutlined, {
className: "".concat(prefixCls, "-inner"),
style: {
color: '#8c8c8c'
}
}), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-label")
}, label));
}
}]);
return BaseCheckbox;
}(React.PureComponent);
export { BaseCheckbox as default };
BaseCheckbox.defaultProps = {
prefixCls: 'Yep-checkbox',
className: '',
style: {},
type: 'checkbox',
defaultChecked: false,
onFocus: function onFocus() {},
onBlur: function onBlur() {},
onChange: function onChange() {}
};