UNPKG

4.73 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4import _createClass from "@babel/runtime/helpers/createClass";
5import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7import _inherits from "@babel/runtime/helpers/inherits";
8import * as React from 'react';
9import Tooltip from '@beisen-phoenix/tooltip';
10import './style.css';
11import utils from '@beisen-phoenix/common-utils';
12
13var Checkbox =
14/*#__PURE__*/
15function (_React$PureComponent) {
16 _inherits(Checkbox, _React$PureComponent);
17
18 function Checkbox(props) {
19 var _this;
20
21 _classCallCheck(this, Checkbox);
22
23 _this = _possibleConstructorReturn(this, _getPrototypeOf(Checkbox).call(this, props));
24 _this.inputRef = React.createRef();
25 _this.txtRef = React.createRef();
26 _this.containerRef = React.createRef();
27
28 _this.onChange = function (event) {
29 var checked = _this.state.checked;
30
31 var _this$props = _this.props,
32 onChange = _this$props.onChange,
33 disabled = _this$props.disabled,
34 restProps = _objectWithoutProperties(_this$props, ["onChange", "disabled"]);
35
36 if (disabled) return;
37
38 if (!_this.props.partialChecked) {
39 _this.setState({
40 checked: !checked
41 });
42
43 restProps.checked = !checked;
44 }
45
46 onChange && onChange(restProps, event);
47 event.stopPropagation && event.stopPropagation();
48 };
49
50 var _checked = 'checked' in props ? props.checked : props.defaultChecked;
51
52 _this.state = {
53 checked: _checked
54 };
55 return _this;
56 }
57
58 _createClass(Checkbox, [{
59 key: "componentWillReceiveProps",
60 value: function componentWillReceiveProps(nextProps) {
61 'checked' in nextProps && this.setState({
62 checked: nextProps.checked
63 });
64 }
65 }, {
66 key: "render",
67 value: function render() {
68 var _this$props2 = this.props,
69 disabled = _this$props2.disabled,
70 _this$props2$label = _this$props2.label,
71 label = _this$props2$label === void 0 ? '' : _this$props2$label,
72 partialChecked = _this$props2.partialChecked,
73 value = _this$props2.value,
74 tipsType = _this$props2.tipsType,
75 fontSize = _this$props2.fontSize,
76 textColor = _this$props2.textColor;
77 var checked = this.state.checked;
78 var onChange = this.onChange;
79 var inputProps = {
80 value: value,
81 onChange: onChange
82 }; // 生成符合BEM规范的className
83
84 var classes = utils.BEMClass('checkbox');
85 var labelCls = classes({
86 modifiers: {
87 disabled: disabled
88 },
89 extra: this.props.extraCls
90 });
91 var boxCls = classes({
92 element: 'box',
93 modifiers: {
94 partialChecked: partialChecked
95 }
96 });
97 var inputCls = classes({
98 element: 'input',
99 modifiers: {
100 disabled: disabled
101 }
102 });
103 var realCls = classes({
104 element: 'realInput',
105 modifiers: {
106 checked: checked,
107 disabled: disabled,
108 partialChecked: partialChecked,
109 disabledChecked: disabled && checked
110 }
111 });
112 var tipCls = classes({
113 element: 'tooltip',
114 extra: this.props.extraCls
115 });
116 var txtCls = classes({
117 element: 'text',
118 modifiers: {
119 disabled: disabled,
120 fontMiddle: fontSize === 'middle'
121 },
122 extra: "color-".concat(textColor)
123 });
124 return React.createElement("label", {
125 className: labelCls,
126 ref: this.containerRef
127 }, React.createElement("span", {
128 className: boxCls
129 }, React.createElement("input", _extends({
130 type: "checkbox",
131 ref: this.inputRef
132 }, inputProps, {
133 className: inputCls
134 })), React.createElement("span", {
135 className: realCls
136 })), label ? tipsType === 'animate' ? React.createElement(Tooltip, {
137 extraCls: tipCls,
138 showOverflowTooltip: true,
139 title: label
140 }, React.createElement("span", {
141 className: txtCls,
142 ref: this.txtRef
143 }, label)) : React.createElement("span", {
144 className: txtCls,
145 title: label,
146 ref: this.txtRef
147 }, label) : null);
148 }
149 }]);
150
151 return Checkbox;
152}(React.PureComponent);
153
154Checkbox.defaultProps = {
155 disabled: false,
156 defaultChecked: false,
157 label: '',
158 tipsType: 'animate',
159 fontSize: 'normal',
160 textColor: 'M1'
161};
162export { Checkbox as default };
\No newline at end of file