1 | import _extends from "@babel/runtime/helpers/extends";
|
2 | import "core-js/modules/es6.object.assign";
|
3 | import "core-js/modules/es6.regexp.to-string";
|
4 | import "core-js/modules/es6.object.to-string";
|
5 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
6 | import _createClass from "@babel/runtime/helpers/createClass";
|
7 | import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
8 | import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
9 | import _inherits from "@babel/runtime/helpers/inherits";
|
10 | import React, { Component } from 'react';
|
11 | import { findDOMNode } from 'react-dom';
|
12 | import './index.css';
|
13 | import CommonMount from '@beisen-platform/common-mount';
|
14 | import Edit from './edit';
|
15 | import Search from './search';
|
16 | import contains from './contains';
|
17 | import getUid from './uid';
|
18 |
|
19 | function empty() {}
|
20 |
|
21 | var LookUpV2 =
|
22 |
|
23 | function (_Component) {
|
24 | _inherits(LookUpV2, _Component);
|
25 |
|
26 | function LookUpV2(props) {
|
27 | var _this;
|
28 |
|
29 | _classCallCheck(this, LookUpV2);
|
30 |
|
31 | _this = _possibleConstructorReturn(this, _getPrototypeOf(LookUpV2).call(this, props));
|
32 |
|
33 | _this.setComState = function (key, value) {
|
34 | _this.setState({
|
35 | isShowDoropList: value
|
36 | });
|
37 | };
|
38 |
|
39 | _this.handlerMouseDown = function (event) {
|
40 | if (_this.props.readOnly) return;
|
41 |
|
42 | if (!_this.isClickValid(event)) {
|
43 | _this.setState({
|
44 | isShowDoropList: false
|
45 | });
|
46 |
|
47 | _this.commonMount.unmountBox();
|
48 |
|
49 | _this.props.onEditMouseDown(event);
|
50 | }
|
51 | };
|
52 |
|
53 | _this.isClickValid = function (event) {
|
54 | var mountContainer = document.getElementById('BS_lookupV2_container__' + _this.state.uid);
|
55 | var lookupContainer = findDOMNode(_this.refs.edit.refs.input);
|
56 |
|
57 | if (mountContainer && !contains(mountContainer, event.target) && lookupContainer && !contains(lookupContainer, event.target)) {
|
58 | return false;
|
59 | }
|
60 |
|
61 | return true;
|
62 | };
|
63 |
|
64 | _this.setListHeight = function () {
|
65 | var height = 0,
|
66 | listHeight = 0;
|
67 |
|
68 | if (Array.isArray(_this.props.listValue) && _this.props.listValue.length) {
|
69 | if (_this.props.listValue[0][1]) {
|
70 | listHeight = 46;
|
71 | } else {
|
72 | if (_this.props.listValue[0][0] && _this.props.listValue[0][0]['avatars']) {
|
73 | listHeight = 40;
|
74 | } else {
|
75 | listHeight = 32;
|
76 | }
|
77 | }
|
78 |
|
79 |
|
80 | height = _this.props.listValue.length * listHeight;
|
81 | height = height > 300 ? 300 : height;
|
82 | height = height + 40;
|
83 |
|
84 | if (!_this.props.isShowFindAll) {
|
85 | height = height - 32;
|
86 | }
|
87 |
|
88 | } else {
|
89 |
|
90 | height = 320;
|
91 | }
|
92 |
|
93 | if (_this.props.isFetching) {
|
94 | height = 360;
|
95 | }
|
96 |
|
97 | return {
|
98 | itemHeight: listHeight,
|
99 | listHeight: height
|
100 | };
|
101 | };
|
102 |
|
103 | _this.handlerFindAll = function (event) {
|
104 | _this.commonMount.unmountBox();
|
105 |
|
106 | _this.props.onFindAll();
|
107 | };
|
108 |
|
109 | _this.clearDom = function () {
|
110 | var doms = document.getElementsByClassName('BS_lookupV2_wrapper_common');
|
111 | var parDoms;
|
112 |
|
113 | for (var i = 0; i < doms.length; i++) {
|
114 | if (doms[i]) {
|
115 | parDoms = doms[i].parentNode;
|
116 | parDoms.removeChild(doms[i]);
|
117 | }
|
118 | }
|
119 | };
|
120 |
|
121 | _this.onBlurChange = function (event) {
|
122 | if (_this.props.onBlur) {
|
123 | _this.props.onBlur(event);
|
124 | }
|
125 | };
|
126 |
|
127 | _this.setClassName = function (id) {
|
128 | var dom = document.getElementById(id);
|
129 | dom.className = 'BS_lookupV2_wrapper_common';
|
130 | };
|
131 |
|
132 | _this.state = {
|
133 | uid: getUid(),
|
134 | isShowDoropList: false
|
135 | };
|
136 |
|
137 | if (!Array.isArray) {
|
138 | Array.isArray = function (arg) {
|
139 | return Object.prototype.toString.call(arg) === '[object Array]';
|
140 | };
|
141 | }
|
142 |
|
143 | return _this;
|
144 | }
|
145 |
|
146 | _createClass(LookUpV2, [{
|
147 | key: "componentDidUpdate",
|
148 | value: function componentDidUpdate(prevProps, prevState) {
|
149 | var props = Object.assign({}, this.props);
|
150 |
|
151 | if (props.disabled || props.readOnly) {
|
152 | this.commonMount.unmountBox();
|
153 | }
|
154 | }
|
155 | }, {
|
156 | key: "componentWillMount",
|
157 | value: function componentWillMount() {
|
158 | this.commonMount = new CommonMount({
|
159 | containerId: 'BS_lookupV2_wrapper__' + this.state.uid,
|
160 | follow: true,
|
161 |
|
162 | fixed: true
|
163 |
|
164 | });
|
165 | this.setClassName('BS_lookupV2_wrapper__' + this.state.uid);
|
166 | }
|
167 | }, {
|
168 | key: "componentDidMount",
|
169 | value: function componentDidMount() {
|
170 | if (this.props.status === 'edit') {
|
171 | document.removeEventListener('mousedown', this.handlerMouseDown);
|
172 | document.addEventListener('mousedown', this.handlerMouseDown);
|
173 | }
|
174 | }
|
175 | }, {
|
176 | key: "componentWillUnmount",
|
177 | value: function componentWillUnmount() {
|
178 | if (this.props.status === 'edit') {
|
179 | document.removeEventListener('mousedown', this.handlerMouseDown);
|
180 |
|
181 | this.commonMount.unmountBox();
|
182 | }
|
183 | }
|
184 | }, {
|
185 | key: "render",
|
186 | value: function render() {
|
187 | var props = this.props;
|
188 |
|
189 | if (props.status === 'edit') {
|
190 | return React.createElement(Edit, _extends({
|
191 | ref: "edit"
|
192 | }, props, {
|
193 | setListHeight: this.setListHeight,
|
194 | uid: this.state.uid,
|
195 | isShowDoropList: this.state.isShowDoropList,
|
196 | setComState: this.setComState,
|
197 | onBlur: this.onBlurChange,
|
198 | commonMount: this.commonMount,
|
199 | handlerFindAll: this.handlerFindAll
|
200 | }));
|
201 | } else {
|
202 | return React.createElement(Search, _extends({
|
203 | ref: "search"
|
204 | }, props, {
|
205 | setListHeight: this.setListHeight,
|
206 | isShowDoropList: this.state.isShowDoropList,
|
207 | setComState: this.setComState,
|
208 | commonMount: this.commonMount,
|
209 | handlerFindAll: this.handlerFindAll
|
210 | }));
|
211 | }
|
212 | }
|
213 | }]);
|
214 |
|
215 | return LookUpV2;
|
216 | }(Component);
|
217 |
|
218 | LookUpV2.defaultProps = {
|
219 | status: 'edit',
|
220 | isShowFindAll: true,
|
221 | onFindAll: empty,
|
222 | onEditMouseDown: empty
|
223 | };
|
224 | export { LookUpV2 as default }; |
\ | No newline at end of file |