UNPKG

14.2 kBJavaScriptView Raw
1import "core-js/modules/es6.regexp.replace";
2import "core-js/modules/es6.object.assign";
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 _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
8import _inherits from "@babel/runtime/helpers/inherits";
9import React, { Component } from 'react';
10import './styles/all.css';
11import DropButtonMain from './DropButtonMain/DropButtonMain';
12import CommonMount from '@beisen-platform/common-mount';
13import DropdownDisabledIcon from '@beisen-platform/react-icons/lib/icons/DropdownminDisabled.js';
14import DropdownNormalIcon from './icon/DropdownNormalIcon'; //ci
15
16var DropDownButton =
17/*#__PURE__*/
18function (_Component) {
19 _inherits(DropDownButton, _Component);
20
21 function DropDownButton(props) {
22 var _this;
23
24 _classCallCheck(this, DropDownButton);
25
26 _this = _possibleConstructorReturn(this, _getPrototypeOf(DropDownButton).call(this, props));
27 var defaultState = {
28 btnStyle: '',
29 btnSize: '',
30 iconName: '',
31 maxWidth: 'none',
32 listShow: false,
33 ulHide: false,
34 shadowHide: true,
35 btnDisabled: false,
36 btnDirection: {},
37 scrollHeight: 0,
38 notBtnIcon: props.iconName,
39 listViewDirection: 'right-down'
40 };
41 _this.CommonMount = new CommonMount({
42 containerId: 'common-mount-list',
43 // 容器ID
44 follow: true,
45 // 是否滚动跟随,默认true
46 fixed: true // 定位:fixed,false时为absolute
47
48 });
49 _this.state = Object.assign({}, defaultState, props);
50 _this.onClose = _this.onClose.bind(_assertThisInitialized(_this));
51 _this.btnMouseMove = _this.btnMouseMove.bind(_assertThisInitialized(_this));
52 _this.btnMouseOut = _this.btnMouseOut.bind(_assertThisInitialized(_this));
53 _this.itemClick = _this.itemClick.bind(_assertThisInitialized(_this));
54 _this.scrollUl = _this.scrollUl.bind(_assertThisInitialized(_this));
55 _this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
56 _this.renderDOM = _this.renderDOM.bind(_assertThisInitialized(_this));
57 return _this;
58 }
59
60 _createClass(DropDownButton, [{
61 key: "componentWillReceiveProps",
62 value: function componentWillReceiveProps(nextProps) {
63 this.state = Object.assign({}, this.state, nextProps);
64 this.setState(this.state);
65 }
66 }, {
67 key: "scrollUl",
68 value: function scrollUl(height) {
69 this.setState({
70 scrollHeight: height
71 });
72 }
73 }, {
74 key: "onClick",
75 value: function onClick(e) {
76 var self = this;
77
78 if (this.state.btnActive) {
79 this.CommonMount.unmountBox();
80 this.setState({
81 btnActive: false
82 });
83 return false;
84 }
85
86 if (this.props.onDropDownIconClick) {
87 this.props.onDropDownIconClick(e);
88 }
89
90 var allFun = function allFun() {
91 self.renderDOM(e.target);
92 document.removeEventListener('mousedown', self.onClose);
93 document.addEventListener('mousedown', self.onClose);
94 self.setState({
95 btnActive: true
96 });
97 };
98
99 var retFun = function retFun(children) {
100 if (children) {
101 self.state.children = children;
102 }
103
104 allFun();
105 };
106
107 if (!this.props.itemClick) {
108 allFun();
109 }
110
111 if (!self.state.children || self.state.children.length == 0) {
112 this.props.itemClick && this.props.itemClick(retFun);
113 } else {
114 allFun();
115 }
116 }
117 }, {
118 key: "renderDOM",
119 value: function renderDOM(tar) {
120 var _this$state = this.state,
121 autoDirection = _this$state.autoDirection,
122 direction = _this$state.direction,
123 children = _this$state.children,
124 sideTip = _this$state.sideTip,
125 hiddenTip = _this$state.hiddenTip;
126 var _this$props = this.props,
127 panelWidth = _this$props.panelWidth,
128 deviationLeft = _this$props.deviationLeft,
129 rightAlign = _this$props.rightAlign,
130 disabledShowTips = _this$props.disabledShowTips;
131 var self = this;
132 var target = this.refs.dropDownView.getBoundingClientRect();
133 var length = this.state.children.length >= 10 ? 260 : this.state.children.length * 26;
134 var winHeight = window.innerHeight;
135 var winWidth = window.innerWidth;
136 var horizontal = winWidth - target.left > panelWidth ? 'right' : 'left';
137 var vertical = winHeight - target.bottom > length ? '-down' : winHeight - target.top > length ? '-top' : winHeight - target.bottom > target.top ? '-down' : '-top';
138
139 if (this.state.disabled) {
140 return false;
141 }
142
143 var tempList = this.state.listShow == false ? true : false;
144 var showAutoUl = React.createElement(DropButtonMain, {
145 data: children,
146 disabledShowTips: disabledShowTips,
147 sideTip: sideTip,
148 hiddenTip: hiddenTip,
149 panelWidth: panelWidth,
150 itemClick: this.itemClick,
151 ulHide: !tempList,
152 scrollUl: this.scrollUl,
153 direction: autoDirection || autoDirection == undefined ? horizontal + vertical : direction,
154 listShow: tempList,
155 dataUdcList: this.props.dataUdcList,
156 dataUdtList: this.props.dataUdtList
157 });
158 this.CommonMount.renderDom('drop-down-list-not-active', // wrap's classname & id
159 showAutoUl, // content dom
160 self.refs.dropDownView, // 跟随目标
161 [self.refs.dropDownView, //挂载目标节点
162 {
163 // 挂载元素宽高 {width: '', height: ''}
164 width: panelWidth,
165 // 挂载宽度
166 height: length
167 }, 40], true, deviationLeft, rightAlign);
168 var parent = document.getElementById('drop-down-list-ul');
169 setTimeout(function () {
170 parent.className = parent.className + ' drop-down-list-active';
171 }, 10);
172 }
173 }, {
174 key: "onClose",
175 value: function onClose(event) {
176 var area = document.getElementById('common-mount-list');
177
178 if (area && !area.contains(event.target) && !this.refs.dropBtnComponent.contains(event.target)) {
179 this.CommonMount.unmountBox();
180 this.setState({
181 btnActive: false
182 });
183 document.removeEventListener('mousedown', this.onClose);
184 }
185 }
186 }, {
187 key: "itemClick",
188 value: function itemClick(e, val) {
189 var self = this;
190 var stateData = this.state;
191 stateData.children.map(function (item) {
192 if (item.value == val.value) item.active = true;
193 }); //背景效果展现,隐藏ul列表
194
195 this.setState({
196 shadowHide: false,
197 children: stateData.children,
198 ulHide: true,
199 iconName: this.props.iconName || ''
200 });
201 setTimeout(function () {
202 //延迟消失ul列表
203 var stateData = self.state;
204 stateData.children.map(function (item) {
205 if (item.active) item.active = false;
206 });
207 self.CommonMount.unmountBox();
208 self.setState({
209 btnActive: false
210 });
211 }, 300); // 关闭之前先将弹层的zIndex调低,处理common-pop zIndex是999的情况导致的遮不住dropdown
212
213 if (this.CommonMount.containerStyleUpdater) {
214 this.CommonMount.containerStyleUpdater('zIndex', '998');
215 }
216
217 this.props.onClick && this.props.onClick(val, e, true, this.state, this.refs.dropdownbtn);
218 this.state.listShow = false;
219 document.removeEventListener('mousedown', this.onClose);
220 }
221 }, {
222 key: "btnMouseMove",
223 value: function btnMouseMove() {
224 var _this$props2 = this.props,
225 activeName = _this$props2.activeName,
226 hoverName = _this$props2.hoverName,
227 isBtnStyle = _this$props2.isBtnStyle;
228
229 if (this.state.disabled) {
230 return false;
231 }
232
233 if (isBtnStyle) {
234 var iconName = this.state.iconName.replace('nomal', 'active');
235 this.setState({
236 iconName: iconName
237 });
238 } else {
239 if (this.refs.dropDownIcon) this.refs.dropDownIcon.className = this.refs.dropDownIcon.className.replace('nomal', hoverName ? 'hover' : 'active');
240 this.setState({
241 notBtnIcon: hoverName ? hoverName : activeName,
242 hovering: true
243 });
244 }
245 }
246 }, {
247 key: "btnMouseOut",
248 value: function btnMouseOut() {
249 var _this$props3 = this.props,
250 iconName = _this$props3.iconName,
251 hoverName = _this$props3.hoverName,
252 isBtnStyle = _this$props3.isBtnStyle;
253
254 if (this.state.disabled) {
255 return false;
256 }
257
258 if (isBtnStyle) {
259 var _iconName = this.state.iconName.replace('active', 'nomal');
260
261 !this.state.listShow && this.setState({
262 iconName: _iconName
263 });
264 } else {
265 if (this.refs.dropDownIcon) this.refs.dropDownIcon.className = this.refs.dropDownIcon.className.replace(hoverName ? 'hover' : 'active', 'nomal');
266 this.setState({
267 notBtnIcon: iconName,
268 hovering: false
269 }, this.forceUpdate());
270 }
271 }
272 }, {
273 key: "componentWillMount",
274 value: function componentWillMount() {
275 var initData = {
276 bsSize: {
277 small: 'drop-btns-bgc-small ',
278 default: 'drop-btns-bgc-big '
279 },
280 bsStyle: {
281 weaken: 'drop-btns-weaken ',
282 default: ' ',
283 link: ' '
284 },
285 btnDisabled: {
286 weaken: 'drop-btns-bg-gray-weaken ',
287 default: 'drop-btns-bg-gray-defalult '
288 },
289 btnDirection: {
290 small: 'drop-down-btn-small',
291 default: 'drop-down-btn-big'
292 },
293 btnIcon: {
294 weaken: this.props.iconName ? this.props.iconName : 'pc-sys-arrowdown-nomal-svg',
295 default: 'pc-sys-arrowdownM7-nomal-svg'
296 }
297 };
298 var _this$props4 = this.props,
299 bsStyle = _this$props4.bsStyle,
300 bsSize = _this$props4.bsSize,
301 open = _this$props4.open,
302 disabled = _this$props4.disabled,
303 maxWidth = _this$props4.maxWidth,
304 iconName = _this$props4.iconName; //初始化数据,为按钮添加样式
305
306 var btnActive = false;
307 var btnStyle = initData.bsStyle[bsStyle],
308 btnSize = initData.bsSize[bsSize],
309 listShow = open ? true : false,
310 btnDirection = initData.btnDirection[bsSize],
311 btnDisabled = disabled ? initData.btnDisabled[bsStyle] : this.state.btnDisabled = ' ',
312 iconBtnName = disabled ? iconName.replace('nomal', 'disabled') : initData.btnIcon[bsStyle],
313 maxBtnWidth = maxWidth && maxWidth.length > 0 ? maxWidth : 'none';
314 this.setState({
315 maxWidth: maxBtnWidth,
316 btnActive: btnActive,
317 btnStyle: btnStyle,
318 btnSize: btnSize,
319 listShow: listShow,
320 btnDirection: btnDirection,
321 btnDisabled: btnDisabled,
322 iconName: iconBtnName
323 });
324 }
325 }, {
326 key: "componentWillUnmount",
327 value: function componentWillUnmount() {
328 this.CommonMount.unmountBox();
329 document.removeEventListener('mousedown', this.onClose);
330 }
331 }, {
332 key: "render",
333 value: function render() {
334 var _this$state2 = this.state,
335 type = _this$state2.type,
336 btnSize = _this$state2.btnSize,
337 btnStyle = _this$state2.btnStyle,
338 btnDisabled = _this$state2.btnDisabled,
339 iconName = _this$state2.iconName,
340 hovering = _this$state2.hovering,
341 btnDirection = _this$state2.btnDirection,
342 maxWidth = _this$state2.maxWidth,
343 listShow = _this$state2.listShow,
344 notBtnIcon = _this$state2.notBtnIcon,
345 btnActive = _this$state2.btnActive;
346 var _this$props5 = this.props,
347 title = _this$props5.title,
348 hidden = _this$props5.hidden,
349 isBtnStyle = _this$props5.isBtnStyle,
350 hoverName = _this$props5.hoverName,
351 activeName = _this$props5.activeName;
352 var IconComponent = this.props.iconComponent;
353 var dropLabel, labelType, iconLabel, maxBtnWidth;
354
355 if (type == 'text') {
356 maxBtnWidth = maxWidth;
357 labelType = ' drop-down-btn-label ';
358
359 if (title.length > 0) {
360 dropLabel = React.createElement("div", null, React.createElement("span", {
361 className: "drop-down-btn-title"
362 }, title), React.createElement("span", {
363 className: "drop-down-btn-svg"
364 }, React.createElement(DropdownNormalIcon, {
365 width: 10,
366 height: 4
367 })));
368 } else {
369 dropLabel = '';
370 }
371 } else {
372 labelType = ' drop-down-btn-icon ';
373 var hoverClass = hoverName && btnActive ? activeName : hovering ? this.props.iconName : notBtnIcon;
374 iconLabel = React.createElement("span", {
375 ref: "dropDownIcon",
376 className: 'drop-down-icon ' + (isBtnStyle || isBtnStyle == undefined ? iconName : hoverClass)
377 });
378 }
379
380 if (hidden) {
381 return React.createElement("div", null);
382 } else {
383 return React.createElement("div", {
384 ref: "dropBtnComponent",
385 className: "feild drop-down-button"
386 }, React.createElement("div", {
387 ref: "dropDownView",
388 className: 'drop-down-btn-list ' + (isBtnStyle || isBtnStyle == undefined ? '' : 'is-not-btn-style') + (listShow ? 'drop-down-list-active ' : ' ') + btnDirection
389 }, React.createElement("div", {
390 ref: "dropdownbtn",
391 style: {
392 maxWidth: maxBtnWidth
393 },
394 className: btnSize + btnStyle + btnDisabled + labelType,
395 onClick: this.onClick,
396 onMouseOver: this.btnMouseMove,
397 onMouseOut: this.btnMouseOut
398 }, dropLabel, IconComponent ? React.createElement(IconComponent, null) : iconLabel)));
399 }
400 }
401 }]);
402
403 return DropDownButton;
404}(Component);
405
406DropDownButton.defaultProps = {
407 deviationLeft: 0,
408 panelWidth: 170
409};
410export { DropDownButton as default };
\No newline at end of file