UNPKG

15.5 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
10
11var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
12
13var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
14
15var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
16
17var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
18
19var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
20
21var _inherits2 = require('babel-runtime/helpers/inherits');
22
23var _inherits3 = _interopRequireDefault(_inherits2);
24
25var _class, _temp, _initialiseProps;
26
27var _react = require('react');
28
29var _react2 = _interopRequireDefault(_react);
30
31var _propTypes = require('prop-types');
32
33var _propTypes2 = _interopRequireDefault(_propTypes);
34
35var _classnames = require('classnames');
36
37var _classnames2 = _interopRequireDefault(_classnames);
38
39var _reactLifecyclesCompat = require('react-lifecycles-compat');
40
41var _input = require('../input');
42
43var _input2 = _interopRequireDefault(_input);
44
45var _select = require('../select');
46
47var _select2 = _interopRequireDefault(_select);
48
49var _button = require('../button');
50
51var _button2 = _interopRequireDefault(_button);
52
53var _icon = require('../icon');
54
55var _icon2 = _interopRequireDefault(_icon);
56
57var _util = require('../util');
58
59var _zhCn = require('../locale/zh-cn');
60
61var _zhCn2 = _interopRequireDefault(_zhCn);
62
63function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
64
65var Group = _input2.default.Group;
66var AutoComplete = _select2.default.AutoComplete;
67var noop = _util.func.noop;
68
69/**
70 * Search
71 * @description 输入框部分继承 Select.AutoComplete 的能力,可以直接用AutoComplete 的 api
72 */
73
74var Search = (_temp = _class = function (_React$Component) {
75 (0, _inherits3.default)(Search, _React$Component);
76
77 function Search(props) {
78 (0, _classCallCheck3.default)(this, Search);
79
80 var _this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call(this, props));
81
82 _initialiseProps.call(_this);
83
84 var value = 'value' in props ? props.value : props.defaultValue;
85 var filterValue = 'filterValue' in props ? props.filterValue : props.defaultFilterValue;
86
87 _this.state = {
88 value: typeof value === 'undefined' ? '' : value,
89 filterValue: filterValue
90 };
91
92 _this.highlightKey = null;
93 return _this;
94 }
95
96 Search.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
97 var nextState = {};
98 if ('value' in nextProps && nextProps.value !== prevState.value) {
99 var value = nextProps.value;
100 nextState.value = value === undefined || value === null ? '' : nextProps.value;
101 }
102
103 if ('filterValue' in nextProps && nextProps.filterValue !== prevState.filterValue) {
104 var filterValue = nextProps.filterValue;
105 nextState.filterValue = filterValue === undefined ? '' : filterValue;
106 }
107
108 if (Object.keys(nextState).length > 0) {
109 return nextState;
110 }
111
112 return null;
113 };
114
115 Search.prototype.focus = function focus() {
116 var _inputRef;
117
118 (_inputRef = this.inputRef).focus.apply(_inputRef, arguments);
119 };
120
121 Search.prototype.render = function render() {
122 var _classNames;
123
124 var _props = this.props,
125 shape = _props.shape,
126 filter = _props.filter,
127 hasIcon = _props.hasIcon,
128 disabled = _props.disabled,
129 placeholder = _props.placeholder,
130 type = _props.type,
131 className = _props.className,
132 style = _props.style,
133 size = _props.size,
134 prefix = _props.prefix,
135 searchText = _props.searchText,
136 dataSource = _props.dataSource,
137 filterProps = _props.filterProps,
138 buttonProps = _props.buttonProps,
139 fillProps = _props.fillProps,
140 popupContent = _props.popupContent,
141 followTrigger = _props.followTrigger,
142 hasClear = _props.hasClear,
143 visible = _props.visible,
144 locale = _props.locale,
145 rtl = _props.rtl,
146 icons = _props.icons,
147 autoHighlightFirstItem = _props.autoHighlightFirstItem,
148 others = (0, _objectWithoutProperties3.default)(_props, ['shape', 'filter', 'hasIcon', 'disabled', 'placeholder', 'type', 'className', 'style', 'size', 'prefix', 'searchText', 'dataSource', 'filterProps', 'buttonProps', 'fillProps', 'popupContent', 'followTrigger', 'hasClear', 'visible', 'locale', 'rtl', 'icons', 'autoHighlightFirstItem']);
149
150
151 var cls = (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'search'] = true, _classNames[prefix + 'search-' + shape] = true, _classNames['' + prefix + type] = type, _classNames['' + prefix + size] = size, _classNames[prefix + 'disabled'] = !!disabled, _classNames[className] = !!className, _classNames));
152
153 var searchIcon = null,
154 filterSelect = null,
155 searchBtn = null,
156 iconsSearch = icons.search;
157
158 if (!(0, _react.isValidElement)(icons.search) && icons.search) {
159 iconsSearch = _react2.default.createElement(
160 'span',
161 null,
162 icons.search
163 );
164 }
165
166 if (shape === 'simple') {
167 var _classNames2;
168
169 var _cls = (0, _classnames2.default)((_classNames2 = {}, _classNames2[prefix + 'search-icon'] = true, _classNames2[buttonProps.className] = !!buttonProps.className, _classNames2[prefix + 'search-symbol-icon'] = !iconsSearch, _classNames2));
170 hasIcon && (searchIcon = _react2.default.cloneElement(iconsSearch || _react2.default.createElement(_icon2.default, { type: 'search' }), (0, _extends3.default)({
171 role: 'button',
172 'aria-disabled': disabled,
173 'aria-label': locale.buttonText
174 }, buttonProps, {
175 className: _cls,
176 onClick: this.onSearch,
177 onKeyDown: this.onKeyDown
178 })));
179 } else {
180 var _classNames3;
181
182 var _cls2 = (0, _classnames2.default)((_classNames3 = {}, _classNames3[prefix + 'search-btn'] = true, _classNames3[buttonProps.className] = !!buttonProps.className, _classNames3));
183 searchBtn = _react2.default.createElement(
184 _button2.default,
185 (0, _extends3.default)({
186 tabIndex: '0',
187 'aria-disabled': disabled,
188 'aria-label': locale.buttonText,
189 className: _cls2,
190 disabled: disabled
191 }, buttonProps, {
192 onClick: this.onSearch,
193 onKeyDown: this.onKeyDown
194 }),
195 hasIcon ? iconsSearch || _react2.default.createElement(_icon2.default, { type: 'search', className: prefix + 'search-symbol-icon' }) : null,
196 searchText ? _react2.default.createElement(
197 'span',
198 { className: prefix + 'search-btn-text' },
199 searchText
200 ) : null
201 );
202 }
203
204 if (filter.length > 0) {
205 filterSelect = _react2.default.createElement(_select2.default, (0, _extends3.default)({}, filterProps, {
206 followTrigger: followTrigger,
207 hasBorder: false,
208 dataSource: filter,
209 size: size,
210 disabled: disabled,
211 value: this.state.filterValue,
212 onChange: this.onFilterChange
213 }));
214 }
215
216 var othersAttributes = _util.obj.pickOthers(Search.propTypes, others);
217 if (visible !== undefined) {
218 // 受控属性 visible 不能直接写在组件上
219 othersAttributes.visible = Boolean(visible);
220 }
221 var dataAttr = _util.obj.pickAttrsWith(others, 'data-');
222
223 var left = _react2.default.createElement(
224 Group,
225 {
226 addonBefore: filterSelect,
227 className: prefix + 'search-left',
228 addonBeforeClassName: prefix + 'search-left-addon'
229 },
230 _react2.default.createElement(AutoComplete, (0, _extends3.default)({
231 'aria-label': locale.buttonText
232 }, othersAttributes, {
233 followTrigger: followTrigger,
234 role: 'searchbox',
235 hasClear: hasClear,
236 className: prefix + 'search-input',
237 size: size,
238 fillProps: fillProps,
239 placeholder: placeholder,
240 dataSource: dataSource,
241 innerAfter: searchIcon,
242 onPressEnter: this.onPressEnter,
243 value: this.state.value,
244 onChange: this.onChange,
245 onToggleHighlightItem: this.onToggleHighlightItem,
246 autoHighlightFirstItem: autoHighlightFirstItem,
247 popupContent: popupContent,
248 disabled: disabled,
249 ref: this.saveInputRef
250 }))
251 );
252
253 return _react2.default.createElement(
254 'span',
255 (0, _extends3.default)({ className: cls, style: style }, dataAttr, { dir: rtl ? 'rtl' : undefined }),
256 searchBtn ? _react2.default.createElement(
257 Group,
258 { addonAfter: searchBtn },
259 left
260 ) : left
261 );
262 };
263
264 return Search;
265}(_react2.default.Component), _class.propTypes = {
266 /**
267 * 样式前缀
268 */
269 prefix: _propTypes2.default.string,
270 /**
271 * 形状
272 */
273 shape: _propTypes2.default.oneOf(['normal', 'simple']),
274 /**
275 * 类型 shape=normal: primary/secondary; shape=simple: normal/dark;
276 */
277 type: _propTypes2.default.oneOf(['primary', 'secondary', 'normal', 'dark']),
278 /**
279 * 大小
280 * @enumdesc '大', '小'
281 */
282 size: _propTypes2.default.oneOf(['large', 'medium']),
283 /**
284 * 搜索框默认值
285 */
286 defaultValue: _propTypes2.default.string,
287 /**
288 * 搜索框数值
289 */
290 value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
291 /**
292 * 输入关键字时的回掉
293 * @param {Object} value 输入值
294 */
295 onChange: _propTypes2.default.func,
296 /**
297 * 点击搜索按钮触发的回调
298 * @param {String} value 输入值
299 * @param {String} filterValue 选项值
300 */
301 onSearch: _propTypes2.default.func,
302 /**
303 * 选择器默认值
304 */
305 defaultFilterValue: _propTypes2.default.string,
306 /**
307 * 填充到输入框里的值的 key ,默认是value
308 */
309 fillProps: _propTypes2.default.string,
310 /**
311 * 选择器
312 */
313 filter: _propTypes2.default.array,
314 /**
315 * 选择器值
316 */
317 filterValue: _propTypes2.default.string,
318 /**
319 * 选择器发生变化时回调
320 * @param {Object} filter value
321 */
322 onFilterChange: _propTypes2.default.func,
323 /**
324 * 搜索框下拉联想列表
325 */
326 dataSource: _propTypes2.default.array,
327 /**
328 * 默认提示
329 */
330 placeholder: _propTypes2.default.string,
331 /**
332 * button 的内容
333 */
334 searchText: _propTypes2.default.node,
335 /**
336 * 自定义样式
337 */
338 style: _propTypes2.default.object,
339 /**
340 * 样式名称
341 */
342 className: _propTypes2.default.string,
343 /**
344 * 选择器的props
345 */
346 filterProps: _propTypes2.default.object,
347 /**
348 * 按钮的额外属性
349 */
350 buttonProps: _propTypes2.default.object,
351 /**
352 * 自定义渲染的的下拉框
353 */
354 popupContent: _propTypes2.default.node,
355 /**
356 * 是否跟随滚动
357 */
358 followTrigger: _propTypes2.default.bool,
359 /**
360 * 自定义渲染的的下拉框
361 */
362 visible: _propTypes2.default.bool,
363 /**
364 * 是否显示清除按钮
365 */
366 hasClear: _propTypes2.default.bool,
367 /**
368 * 是否显示搜索按钮
369 */
370 hasIcon: _propTypes2.default.bool,
371 /**
372 * 是否禁用
373 */
374 disabled: _propTypes2.default.bool,
375 locale: _propTypes2.default.object,
376 rtl: _propTypes2.default.bool,
377 /**
378 * 可配置的icons,包括 search 等
379 */
380 icons: _propTypes2.default.object,
381 /**
382 * 是否自动高亮第一个元素
383 */
384 autoHighlightFirstItem: _propTypes2.default.bool,
385 /**
386 * 上下箭头切换选项的回调
387 */
388 onToggleHighlightItem: _propTypes2.default.func
389}, _class.defaultProps = {
390 prefix: 'next-',
391 shape: 'normal',
392 type: 'normal',
393 size: 'medium',
394 hasIcon: true,
395 filter: [],
396 locale: _zhCn2.default.Search,
397 buttonProps: {},
398 onChange: noop,
399 onSearch: noop,
400 onFilterChange: noop,
401 onToggleHighlightItem: noop,
402 hasClear: false,
403 disabled: false,
404 icons: {},
405 autoHighlightFirstItem: true
406}, _initialiseProps = function _initialiseProps() {
407 var _this2 = this;
408
409 this.onChange = function (value, type) {
410 for (var _len = arguments.length, argv = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
411 argv[_key - 2] = arguments[_key];
412 }
413
414 var _props2;
415
416 if (_this2.props.disabled) {
417 return;
418 }
419
420 if (!('value' in _this2.props)) {
421 _this2.setState({ value: value });
422 }
423
424 (_props2 = _this2.props).onChange.apply(_props2, [value, type].concat(argv));
425 if (type === 'enter') {
426 _this2.highlightKey = '';
427 _this2.props.onSearch(value, _this2.state.filterValue);
428 }
429 };
430
431 this.onPressEnter = function () {
432 if (_this2.highlightKey) {
433 return;
434 }
435 _this2.onSearch();
436 };
437
438 this.onSearch = function () {
439 if (_this2.props.disabled) {
440 return;
441 }
442 _this2.props.onSearch(_this2.state.value, _this2.state.filterValue);
443 };
444
445 this.onFilterChange = function (filterValue) {
446 if (!('filterValue' in _this2.props)) {
447 _this2.setState({ filterValue: filterValue });
448 }
449
450 _this2.props.onFilterChange(filterValue);
451 };
452
453 this.onToggleHighlightItem = function (highlightKey) {
454 var _props3;
455
456 for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
457 args[_key2 - 1] = arguments[_key2];
458 }
459
460 _this2.highlightKey = highlightKey;
461
462 (_props3 = _this2.props).onToggleHighlightItem.apply(_props3, [highlightKey].concat(args));
463 };
464
465 this.onKeyDown = function (e) {
466 if (_this2.props.disabled) {
467 return;
468 }
469 if (e.keyCode !== _util.KEYCODE.ENTER) {
470 return;
471 }
472 _this2.onSearch();
473 };
474
475 this.saveInputRef = function (ref) {
476 if (ref && ref.getInstance()) {
477 _this2.inputRef = ref.getInstance();
478 }
479 };
480}, _temp);
481Search.displayName = 'Search';
482exports.default = (0, _reactLifecyclesCompat.polyfill)(Search);
483module.exports = exports['default'];
\No newline at end of file