UNPKG

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