UNPKG

6.11 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2import _createClass from "@babel/runtime/helpers/createClass";
3import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
4import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
5import _inherits from "@babel/runtime/helpers/inherits";
6import React from 'react';
7import Tooltip from '@beisen-phoenix/tooltip';
8import utils from '@beisen-phoenix/common-utils';
9import './style.css';
10var classes = utils.BEMClass('radio');
11
12function isPxStyle(value) {
13 return /^([\d]+)px$/.test(value);
14}
15
16var Radio =
17/*#__PURE__*/
18function (_React$Component) {
19 _inherits(Radio, _React$Component);
20
21 function Radio(props) {
22 var _this;
23
24 _classCallCheck(this, Radio);
25
26 _this = _possibleConstructorReturn(this, _getPrototypeOf(Radio).call(this, props));
27 _this.txtRef = React.createRef();
28
29 _this.radioOnclick = function (e) {
30 var isDisabled = _this.isDisabled();
31
32 var isRadioControled = _this.isRadioControled();
33
34 var isChecked = _this.isChecked();
35
36 if (isDisabled || isChecked) return;
37
38 _this.setState({
39 "radioClicked": true
40 });
41
42 if (!isRadioControled) {
43 // 非受控情况下先更新状态,然后同步syncClick
44 _this.setState({
45 checked: true
46 }, function () {
47 _this.syncClick(e);
48 });
49 } else {
50 // 受控情况下,直接syncClick,checked状态为true
51 _this.syncClick(e);
52 }
53 };
54
55 _this.isRadioControled = function () {
56 return _this.props.checked !== undefined;
57 };
58
59 _this.getValue = function () {
60 return {
61 label: _this.props.label || _this.props.children,
62 value: _this.props.value,
63 checked: _this.isChecked()
64 };
65 };
66
67 _this.syncClick = function (e) {
68 var isRadioControled = _this.isRadioControled();
69
70 var curRadioData = _this.getValue();
71
72 if (isRadioControled) {
73 curRadioData.checked = true;
74 }
75
76 _this.props.onChange && _this.props.onChange(curRadioData, e);
77 };
78
79 _this.isChecked = function () {
80 var checked = _this.props.checked;
81 return checked === undefined ? _this.state.checked : checked;
82 };
83
84 _this.isDisabled = function () {
85 var disabled = _this.props.disabled;
86 return disabled === undefined ? false : disabled;
87 };
88
89 _this.getMinWidth = function () {
90 // 先检查是否是有值
91 // 然后检查是否是以px为单位
92 // 检查值得数值部分是否大于42(设计规定raido文字加图标最小宽度为42px)
93 var minWidth = _this.props.minWidth;
94 if (minWidth === undefined) return '42px';
95 if (!isPxStyle(minWidth)) return '42px';
96 var minWidthInNumber = parseInt(minWidth);
97 return minWidthInNumber >= 42 ? "".concat(minWidthInNumber, "px") : '42px';
98 };
99
100 _this.getMaxWidth = function () {
101 var maxWidth = _this.props.maxWidth;
102 if (maxWidth === undefined) return '';
103 return isPxStyle(maxWidth) ? maxWidth : '100%';
104 };
105
106 _this.state = {
107 checked: !!_this.props.defaultChecked,
108 radioClicked: false
109 };
110 return _this;
111 }
112
113 _createClass(Radio, [{
114 key: "render",
115 value: function render() {
116 var _this$props = this.props,
117 _this$props$label = _this$props.label,
118 label = _this$props$label === void 0 ? this.props.children : _this$props$label,
119 _this$props$extraCls = _this$props.extraCls,
120 extraCls = _this$props$extraCls === void 0 ? '' : _this$props$extraCls,
121 fontSize = _this$props.fontSize,
122 textColor = _this$props.textColor;
123 var hasLabel = !!label;
124 var containerCls = classes({
125 element: "",
126 modifier: {
127 disabled: this.isDisabled(),
128 checked: this.isChecked(),
129 withLabel: label
130 },
131 extra: extraCls + ' animation'
132 });
133 var flexWrapperCls = classes({
134 element: "wrapper",
135 modifier: {}
136 });
137 var circleWrapperCls = classes({
138 element: "circle-wrapper",
139 modifier: {
140 disabled: this.isDisabled(),
141 checked: this.isChecked(),
142 disabledWithChecked: this.isDisabled() && this.isChecked()
143 }
144 });
145 var circleCls = classes({
146 element: "circle",
147 modifier: {
148 // disabled:this.isDisabled() && !this.isChecked(),
149 disabled: this.isDisabled(),
150 checked: this.isChecked(),
151 disabledWithChecked: this.isDisabled() && this.isChecked()
152 },
153 extral: 'center'
154 });
155 var dotCls = classes({
156 element: "dot",
157 modifier: {
158 disabled: this.isDisabled(),
159 checked: this.isChecked(),
160 checkedWithRadioClicked: this.isChecked() && this.state.radioClicked
161 }
162 });
163 var textCls = classes({
164 element: "radio-text",
165 modifier: {
166 disabled: this.isDisabled(),
167 appear: hasLabel,
168 fontMiddle: fontSize === 'middle'
169 },
170 extra: "color-".concat(textColor)
171 });
172 var widthStyle = label ? {
173 maxWidth: this.getMaxWidth(),
174 minWidth: this.getMinWidth()
175 } : {};
176 var tipCls = classes({
177 element: 'tooltip',
178 extra: this.props.extraCls
179 });
180 return React.createElement("label", {
181 className: containerCls,
182 onClick: this.radioOnclick,
183 style: widthStyle
184 }, React.createElement("div", {
185 className: flexWrapperCls
186 }, React.createElement("div", {
187 className: circleWrapperCls
188 }, React.createElement("div", {
189 className: circleCls
190 }), React.createElement("div", {
191 className: dotCls
192 })), React.createElement(Tooltip, {
193 extraCls: tipCls,
194 title: label,
195 showOverflowTooltip: true
196 }, React.createElement("span", {
197 className: textCls,
198 ref: this.txtRef
199 }, label))));
200 }
201 }]);
202
203 return Radio;
204}(React.Component);
205
206export default Radio;
\No newline at end of file