index.js
5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
// eslint-disable-next-line import/no-extraneous-dependencies
import React, { Component } from 'react';
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);
_this.handleChange = function (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
});
}
if (onChange) {
onChange({
target: _objectSpread(_objectSpread({}, _this.props), {}, {
checked: e.target.checked
}),
stopPropagation: function stopPropagation() {
e.stopPropagation();
},
preventDefault: function preventDefault() {
e.preventDefault();
},
nativeEvent: e.nativeEvent
});
}
};
_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: "focus",
value: function focus() {
this.input.focus();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
className = _this$props2.className,
style = _this$props2.style,
name = _this$props2.name,
id = _this$props2.id,
type = _this$props2.type,
disabled = _this$props2.disabled,
readOnly = _this$props2.readOnly,
tabIndex = _this$props2.tabIndex,
onClick = _this$props2.onClick,
onFocus = _this$props2.onFocus,
onBlur = _this$props2.onBlur,
onKeyDown = _this$props2.onKeyDown,
onKeyPress = _this$props2.onKeyPress,
onKeyUp = _this$props2.onKeyUp,
autoFocus = _this$props2.autoFocus,
value = _this$props2.value,
required = _this$props2.required,
others = _objectWithoutProperties(_this$props2, ["prefixCls", "className", "style", "name", "id", "type", "disabled", "readOnly", "tabIndex", "onClick", "onFocus", "onBlur", "onKeyDown", "onKeyPress", "onKeyUp", "autoFocus", "value", "required"]);
var globalProps = Object.keys(others).reduce(function (prev, key) {
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
// eslint-disable-next-line no-param-reassign
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,
required: required,
readOnly: readOnly,
disabled: disabled,
tabIndex: tabIndex,
className: "".concat(prefixCls, "-input"),
checked: !!checked,
onClick: onClick,
onFocus: onFocus,
onBlur: onBlur,
onKeyUp: onKeyUp,
onKeyDown: onKeyDown,
onKeyPress: onKeyPress,
onChange: this.handleChange,
autoFocus: autoFocus,
ref: this.saveInput,
value: value
}, globalProps)), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-inner")
}));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, state) {
if ('checked' in props) {
return _objectSpread(_objectSpread({}, state), {}, {
checked: props.checked
});
}
return null;
}
}]);
return Checkbox;
}(Component);
Checkbox.defaultProps = {
prefixCls: 'rc-checkbox',
className: '',
style: {},
type: 'checkbox',
defaultChecked: false,
onFocus: function onFocus() {},
onBlur: function onBlur() {},
onChange: function onChange() {},
onKeyDown: function onKeyDown() {},
onKeyPress: function onKeyPress() {},
onKeyUp: function onKeyUp() {}
};
export default Checkbox;