react-select-input-v2 1.0.6 → 1.0.8-react16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,92 +1,92 @@
1
-
2
-
3
- .ris {
4
- display: block;
5
- position: relative;
6
- box-sizing: border-box;
7
- font-size:12px;
8
- }
9
-
10
- .ris * {
11
- box-sizing: border-box;
12
- }
13
-
14
- .ris.ris-is-clearable .ris-clearable {
15
- position: absolute;
16
- top: 50%;
17
- right: 5px;
18
- font-size:1.1em;
19
- margin-top:-1px;
20
- transform: translate(0, -50%);
21
- padding:5px;
22
- color: #555;
23
- cursor:pointer;
24
- }
25
-
26
- .ris.ris-is-creatable .ris-input {
27
- padding-right:15px;
28
- }
29
-
30
- .ris .ris-input {
31
- display: block;
32
- resize: none;
33
- width: 100%;
34
- padding: 5px;
35
- border: 5px solid #eee;
36
- border-radius: 5px;
37
- font-size:inherit;
38
- }
39
-
40
- .ris .ris-input:active, .ris .ris-input:focus {
41
- outline: 0;
42
- border: 5px solid #1e88e5;
43
- }
44
-
45
- .ris.ris-open-up .ris-options {
46
- top:0;
47
- transform:translate(-50%,-100%);
48
- }
49
-
50
- .ris .ris-options {
51
- position: absolute;
52
- top: 100%;
53
- left: 50%;
54
- margin:0;
55
- width: calc(100% - 10px);
56
- background: #fff;
57
- transform:translate(-50%, 0);
58
- border: 1px solid #eee;
59
- overflow: auto;
60
- box-shadow: 0 2px 3px 1px #eee;
61
- max-height:245px;
62
- font-size:inherit;
63
- }
64
-
65
- .ris .ris-options .ris-option {
66
- padding: 5px;
67
- cursor: pointer;
68
- border-bottom: 1px solid #efefef;
69
- }
70
-
71
- .ris .ris-options .ris-option:last-child {
72
- border-bottom: 0;
73
- }
74
-
75
- .ris .ris-options .ris-option:hover {
76
- background: #bbdefb;
77
- }
78
-
79
- .ris .ris-options .ris-option.current {
80
- background:#bbdefb;
81
- }
82
-
83
- .ris .ris-options .ris-option.selected {
84
- background:#1e88e5;
85
- color: #fff;
86
- }
87
-
88
- .ris .ris-options.ris-no-options {
89
- padding:5px;
90
- text-align:center;
91
- color:#ccc;
92
- }
1
+
2
+
3
+ .ris {
4
+ display: block;
5
+ position: relative;
6
+ box-sizing: border-box;
7
+ font-size:12px;
8
+ }
9
+
10
+ .ris * {
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ .ris.ris-is-clearable .ris-clearable {
15
+ position: absolute;
16
+ top: 50%;
17
+ right: 5px;
18
+ font-size:1.1em;
19
+ margin-top:-1px;
20
+ transform: translate(0, -50%);
21
+ padding:5px;
22
+ color: #555;
23
+ cursor:pointer;
24
+ }
25
+
26
+ .ris.ris-is-creatable .ris-input {
27
+ padding-right:15px;
28
+ }
29
+
30
+ .ris .ris-input {
31
+ display: block;
32
+ resize: none;
33
+ width: 100%;
34
+ padding: 5px;
35
+ border: 5px solid #eee;
36
+ border-radius: 5px;
37
+ font-size:inherit;
38
+ }
39
+
40
+ .ris .ris-input:active, .ris .ris-input:focus {
41
+ outline: 0;
42
+ border: 5px solid #1e88e5;
43
+ }
44
+
45
+ .ris.ris-open-up .ris-options {
46
+ top:0;
47
+ transform:translate(-50%,-100%);
48
+ }
49
+
50
+ .ris .ris-options {
51
+ position: absolute;
52
+ top: 100%;
53
+ left: 50%;
54
+ margin:0;
55
+ width: calc(100% - 10px);
56
+ background: #fff;
57
+ transform:translate(-50%, 0);
58
+ border: 1px solid #eee;
59
+ overflow: auto;
60
+ box-shadow: 0 2px 3px 1px #eee;
61
+ max-height:245px;
62
+ font-size:inherit;
63
+ }
64
+
65
+ .ris .ris-options .ris-option {
66
+ padding: 5px;
67
+ cursor: pointer;
68
+ border-bottom: 1px solid #efefef;
69
+ }
70
+
71
+ .ris .ris-options .ris-option:last-child {
72
+ border-bottom: 0;
73
+ }
74
+
75
+ .ris .ris-options .ris-option:hover {
76
+ background: #bbdefb;
77
+ }
78
+
79
+ .ris .ris-options .ris-option.current {
80
+ background:#bbdefb;
81
+ }
82
+
83
+ .ris .ris-options .ris-option.selected {
84
+ background:#1e88e5;
85
+ color: #fff;
86
+ }
87
+
88
+ .ris .ris-options.ris-no-options {
89
+ padding:5px;
90
+ text-align:center;
91
+ color:#ccc;
92
+ }
package/lib/index.js CHANGED
@@ -3,10 +3,16 @@
3
3
  exports.__esModule = true;
4
4
  exports.default = undefined;
5
5
 
6
+ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
7
+
6
8
  var _react = require('react');
7
9
 
8
10
  var _react2 = _interopRequireDefault(_react);
9
11
 
12
+ var _propTypes = require('prop-types');
13
+
14
+ var _propTypes2 = _interopRequireDefault(_propTypes);
15
+
10
16
  var _reactAutosizeTextarea = require('react-autosize-textarea');
11
17
 
12
18
  var _reactAutosizeTextarea2 = _interopRequireDefault(_reactAutosizeTextarea);
@@ -57,7 +63,6 @@ var SelectInput = function (_Component) {
57
63
  if (_this.props.collapseOnSelect) state = _this.setIsOpen(state, false);
58
64
  state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, state.value));
59
65
  _this.setState(state);
60
-
61
66
  if (_this.isFunction(_this.props.onSelect)) _this.props.onSelect(option);
62
67
  };
63
68
 
@@ -70,14 +75,11 @@ var SelectInput = function (_Component) {
70
75
  var state = _this.manipState(_this.state, 'value', event.target.value);
71
76
  state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, event.target.value));
72
77
  state = _this.setIsOpen(state, true);
73
-
74
78
  if (!state.value) {
75
79
  state = _this.manipState(state, 'currentOption', -1);
76
80
  state = _this.manipState(state, 'selectedOption', null);
77
81
  }
78
-
79
82
  if (_this.isFunction(_this.props.onChange)) _this.props.onChange(event);
80
-
81
83
  _this.setState(state);
82
84
  };
83
85
 
@@ -85,7 +87,6 @@ var SelectInput = function (_Component) {
85
87
  var state = _this.setIsOpen(_this.state, true);
86
88
  state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, event.target.value));
87
89
  _this.setState(state);
88
-
89
90
  if (_this.isFunction(_this.props.onFocus)) _this.props.onFocus(event);
90
91
  };
91
92
 
@@ -96,65 +97,47 @@ var SelectInput = function (_Component) {
96
97
  state = _this.manipState(state, 'currentOption', -1);
97
98
  state = _this.manipState(state, 'selectedOption', null);
98
99
  _this.setState(state);
99
-
100
100
  if (_this.isFunction(_this.props.onClear)) _this.props.onClear();
101
101
  };
102
102
 
103
103
  _this.handleBlur = function (event) {
104
104
  if (_this.props.collapseOnBlur) {
105
- var state = _this.setIsOpen(_this.state, false);
106
- _this.setState(state);
105
+ _this.setState(_this.setIsOpen(_this.state, false));
107
106
  }
108
-
109
107
  if (_this.isFunction(_this.props.onBlur)) _this.props.onBlur(event);
110
108
  };
111
109
 
112
110
  _this.handleKeyUp = function (event) {
113
- if (event.key === 'Enter' && _this.state.currentOption > -1) {
114
- _this.handleSelect(_this.pickOption());
115
- } else if (event.key === 'Enter' && _this.state.currentOption === -1) {
116
- _this.handleSelect(_this.pickOption());
117
- }
118
-
119
- if (event.key === 'Escape' && _this.props.collapseOnEscape) {
120
- _this.setState(_this.setIsOpen(_this.state, false));
121
- }
122
-
111
+ if (event.key === 'Enter' && _this.state.currentOption > -1) _this.handleSelect(_this.pickOption());
112
+ if (event.key === 'Escape' && _this.props.collapseOnEscape) _this.setState(_this.setIsOpen(_this.state, false));
123
113
  if (_this.isFunction(_this.props.onKeyUp)) _this.props.onKeyUp(event);
124
114
  };
125
115
 
126
116
  _this.handleKeyDown = function (event) {
127
117
  if (event.key === 'Enter' && _this.props.disableEnter) event.preventDefault();
128
-
129
- if (event.key === 'Escape' && _this.props.collapseOnEscape) {
130
- var state = _this.setIsOpen(_this.state, false);
131
- _this.setState(state);
132
- }
133
-
134
- _this.handleOptionNavigation(event);
135
-
118
+ if (event.key === 'Escape' && _this.props.collapseOnEscape) _this.setState(_this.setIsOpen(_this.state, false));
136
119
  if (_this.isFunction(_this.props.onKeyDown)) _this.props.onKeyDown(event);
120
+ _this.handleOptionNavigation(event);
137
121
  };
138
122
 
139
123
  _this.handleOptionNavigation = function (event) {
140
124
  var state = {};
141
125
  var currentOption = _this.state.currentOption;
142
126
 
143
- if (event.key === 'ArrowUp' || event.key === 'ArrowDown' && _this.disableEnter) event.preventDefault();
127
+ if (event.key === 'ArrowUp' || event.key === 'ArrowDown') event.preventDefault();
144
128
 
145
129
  if (event.key === 'ArrowUp' && currentOption === -1 && _this.props.openUp) currentOption = _this.state.searchMatchOptions.length;
146
130
 
147
131
  if (_this.props.openUp) {
148
- if (event.key === 'ArrowDown') {
149
- if (currentOption > -1) currentOption++;else currentOption = -1;
150
- } else if (event.key === 'ArrowUp') currentOption--;
132
+ if (event.key === 'ArrowDown') currentOption = currentOption > -1 ? currentOption + 1 : -1;else if (event.key === 'ArrowUp') currentOption--;
151
133
  } else {
152
134
  if (event.key === 'ArrowDown') currentOption++;else if (event.key === 'ArrowUp') currentOption--;
153
135
  }
154
136
 
155
- if (currentOption < -1) currentOption = -1;else if (currentOption > _this.state.searchMatchOptions.length - 1 && !_this.props.openUp) currentOption = _this.state.searchMatchOptions.length - 1;else if (currentOption > _this.state.searchMatchOptions.length - 1 && _this.props.openUp) currentOption = -1;
137
+ if (currentOption < -1) currentOption = -1;else if (currentOption > _this.state.searchMatchOptions.length - 1) currentOption = _this.state.searchMatchOptions.length - 1;
156
138
 
157
139
  state = _this.manipState(_this.state, 'currentOption', currentOption);
140
+
158
141
  var optionRef = currentOption > -1 ? _this['option-' + _this.state.searchMatchOptions[currentOption][_this.props.valueKey]] : null;
159
142
  if (optionRef) optionRef.scrollIntoViewIfNeeded(false);
160
143
 
@@ -166,12 +149,12 @@ var SelectInput = function (_Component) {
166
149
  var searchValue = value.replace(/\s/g, '');
167
150
  var searchOptions = [];
168
151
 
169
- if (options && options.length > 0 && value) searchOptions = options.filter(function (option, index) {
170
- var regexp = new RegExp(searchValue, 'gi');
171
- var value = option[_this.props.valueKey] || '';
152
+ if (options && options.length > 0 && value) searchOptions = options.filter(function (option) {
153
+ var val = option[_this.props.valueKey] || '';
172
154
  var label = option[_this.props.labelKey] || '';
173
- return regexp.test(label.replace(/\s/g, '')) || regexp.test(value.replace(/\s/g, ''));
174
- });else searchOptions = options.slice(0, options.length);
155
+ var regexp = new RegExp(searchValue, 'gi');
156
+ return regexp.test(label.replace(/\s/g, '')) || regexp.test(val.replace(/\s/g, ''));
157
+ });else searchOptions = options.slice();
175
158
 
176
159
  if (_this.props.openUp) searchOptions.reverse();
177
160
 
@@ -186,15 +169,13 @@ var SelectInput = function (_Component) {
186
169
  };
187
170
 
188
171
  _this.setIsOpen = function (state, show) {
189
- return Object.assign({}, state, {
190
- isOpen: show || false
191
- });
172
+ return _extends({}, state, { isOpen: show || false });
192
173
  };
193
174
 
194
175
  _this.manipState = function (state, key, value) {
195
- var _Object$assign;
176
+ var _extends2;
196
177
 
197
- return Object.assign({}, state, (_Object$assign = {}, _Object$assign[key] = value, _Object$assign));
178
+ return _extends({}, state, (_extends2 = {}, _extends2[key] = value, _extends2));
198
179
  };
199
180
 
200
181
  _this.isFunction = function (obj) {
@@ -202,7 +183,7 @@ var SelectInput = function (_Component) {
202
183
  };
203
184
 
204
185
  _this.handleOutsideClick = function (event) {
205
- if (!_this.ris.contains(event.target) && _this.props.collapseOnBlur) _this.setState(_this.setIsOpen(_this.state, false));
186
+ if (_this.ris && !_this.ris.contains(event.target) && _this.props.collapseOnBlur) _this.setState({ isOpen: false });
206
187
  };
207
188
 
208
189
  _this.state = {
@@ -221,9 +202,10 @@ var SelectInput = function (_Component) {
221
202
 
222
203
  SelectInput.prototype.componentDidUpdate = function componentDidUpdate() {
223
204
  if (this.props.openUp && this.state.currentOption === -1 && this.state.isOpen && !this.state.selectedOption) {
224
- this.options.scrollTop = this.options.scrollHeight;
205
+ if (this.options) this.options.scrollTop = this.options.scrollHeight;
225
206
  } else if (this.props.openUp && this.state.selectedOption && this.state.isOpen) {
226
- this['option-' + this.state.selectedOption].scrollIntoViewIfNeeded(true);
207
+ var ref = this['option-' + this.state.selectedOption];
208
+ if (ref && ref.scrollIntoViewIfNeeded) ref.scrollIntoViewIfNeeded(true);
227
209
  }
228
210
  };
229
211
 
@@ -231,21 +213,11 @@ var SelectInput = function (_Component) {
231
213
  document.removeEventListener('click', this.handleOutsideClick);
232
214
  };
233
215
 
234
- /*
235
- Various renderers
236
- */
216
+ // ======= 渲染选项 =======
237
217
 
238
- /*
239
- Handle different events
240
- */
241
218
 
242
- /*
243
- Searching function
244
- */
219
+ // ======= 事件处理 =======
245
220
 
246
- /*
247
- Various helpers
248
- */
249
221
 
250
222
  SelectInput.prototype.render = function render() {
251
223
  var _this2 = this;
@@ -274,22 +246,18 @@ var SelectInput = function (_Component) {
274
246
  return _this2.input = ref;
275
247
  }
276
248
  }),
277
- this.props.clearable ? _react2.default.createElement(
249
+ this.props.clearable && _react2.default.createElement(
278
250
  'div',
279
251
  { className: 'ris-clearable', onClick: this.handleClear },
280
252
  'x'
281
- ) : null,
282
- this.state.isOpen ? this.state.searchMatchOptions.length > 0 ? _react2.default.createElement(
253
+ ),
254
+ this.state.isOpen && this.state.searchMatchOptions.length > 0 && _react2.default.createElement(
283
255
  'div',
284
256
  { className: 'ris-options', ref: function ref(_ref3) {
285
257
  return _this2.options = _ref3;
286
258
  } },
287
259
  this.renderOptions(this.state.searchMatchOptions)
288
- ) : this.props.noOptions ? _react2.default.createElement(
289
- 'div',
290
- { className: 'ris-options ris-no-options' },
291
- this.props.noOptions
292
- ) : null : null
260
+ )
293
261
  );
294
262
  };
295
263
 
@@ -300,29 +268,42 @@ exports.default = SelectInput;
300
268
 
301
269
 
302
270
  SelectInput.defaultProps = {
303
- uniqueKey: 'react-select-input', //String
304
- style: null, //Object
305
- ref: null, //Function
306
- value: '', //String
307
- valueKey: 'value', //String
308
- labelKey: 'label', //String
309
- placeholder: 'Enter text', //String
310
- className: '', //String
311
- openUp: false, //Boolean
312
- disableEnter: true, //Boolean
313
- collapseOnBlur: true, //Boolean
314
- collapseOnEscape: true, //Boolean
315
- collapseOnSelect: true, //Boolean
316
- autoFocus: true, //Boolean
317
- clearable: true, //Boolean
318
- options: [], //Array
319
- onChange: undefined, //Function
320
- onSelect: undefined, //Function
321
- onFocus: undefined, //Function
322
- onBlur: undefined, //Function
323
- onKeyUp: undefined, //Function
324
- onKeyDown: undefined, //Function
325
- onClear: undefined, //Function
326
- noOptions: undefined //JSX
271
+ uniqueKey: 'react-select-input',
272
+ value: '',
273
+ valueKey: 'value',
274
+ labelKey: 'label',
275
+ placeholder: 'Enter text',
276
+ openUp: false,
277
+ disableEnter: true,
278
+ collapseOnBlur: true,
279
+ collapseOnEscape: true,
280
+ collapseOnSelect: true,
281
+ autoFocus: true,
282
+ clearable: true,
283
+ options: []
327
284
  };
285
+
286
+ SelectInput.propTypes = process.env.NODE_ENV !== "production" ? {
287
+ uniqueKey: _propTypes2.default.string,
288
+ value: _propTypes2.default.string,
289
+ valueKey: _propTypes2.default.string,
290
+ labelKey: _propTypes2.default.string,
291
+ placeholder: _propTypes2.default.string,
292
+ openUp: _propTypes2.default.bool,
293
+ disableEnter: _propTypes2.default.bool,
294
+ collapseOnBlur: _propTypes2.default.bool,
295
+ collapseOnEscape: _propTypes2.default.bool,
296
+ collapseOnSelect: _propTypes2.default.bool,
297
+ autoFocus: _propTypes2.default.bool,
298
+ clearable: _propTypes2.default.bool,
299
+ options: _propTypes2.default.array,
300
+ onChange: _propTypes2.default.func,
301
+ onSelect: _propTypes2.default.func,
302
+ onFocus: _propTypes2.default.func,
303
+ onBlur: _propTypes2.default.func,
304
+ onKeyUp: _propTypes2.default.func,
305
+ onKeyDown: _propTypes2.default.func,
306
+ onClear: _propTypes2.default.func,
307
+ noOptions: _propTypes2.default.node
308
+ } : {};
328
309
  module.exports = exports['default'];
@@ -1,92 +1,92 @@
1
-
2
-
3
- .ris {
4
- display: block;
5
- position: relative;
6
- box-sizing: border-box;
7
- font-size:12px;
8
- }
9
-
10
- .ris * {
11
- box-sizing: border-box;
12
- }
13
-
14
- .ris.ris-is-clearable .ris-clearable {
15
- position: absolute;
16
- top: 50%;
17
- right: 5px;
18
- font-size:1.1em;
19
- margin-top:-1px;
20
- transform: translate(0, -50%);
21
- padding:5px;
22
- color: #555;
23
- cursor:pointer;
24
- }
25
-
26
- .ris.ris-is-creatable .ris-input {
27
- padding-right:15px;
28
- }
29
-
30
- .ris .ris-input {
31
- display: block;
32
- resize: none;
33
- width: 100%;
34
- padding: 5px;
35
- border: 5px solid #eee;
36
- border-radius: 5px;
37
- font-size:inherit;
38
- }
39
-
40
- .ris .ris-input:active, .ris .ris-input:focus {
41
- outline: 0;
42
- border: 5px solid #1e88e5;
43
- }
44
-
45
- .ris.ris-open-up .ris-options {
46
- top:0;
47
- transform:translate(-50%,-100%);
48
- }
49
-
50
- .ris .ris-options {
51
- position: absolute;
52
- top: 100%;
53
- left: 50%;
54
- margin:0;
55
- width: calc(100% - 10px);
56
- background: #fff;
57
- transform:translate(-50%, 0);
58
- border: 1px solid #eee;
59
- overflow: auto;
60
- box-shadow: 0 2px 3px 1px #eee;
61
- max-height:245px;
62
- font-size:inherit;
63
- }
64
-
65
- .ris .ris-options .ris-option {
66
- padding: 5px;
67
- cursor: pointer;
68
- border-bottom: 1px solid #efefef;
69
- }
70
-
71
- .ris .ris-options .ris-option:last-child {
72
- border-bottom: 0;
73
- }
74
-
75
- .ris .ris-options .ris-option:hover {
76
- background: #bbdefb;
77
- }
78
-
79
- .ris .ris-options .ris-option.current {
80
- background:#bbdefb;
81
- }
82
-
83
- .ris .ris-options .ris-option.selected {
84
- background:#1e88e5;
85
- color: #fff;
86
- }
87
-
88
- .ris .ris-options.ris-no-options {
89
- padding:5px;
90
- text-align:center;
91
- color:#ccc;
92
- }
1
+
2
+
3
+ .ris {
4
+ display: block;
5
+ position: relative;
6
+ box-sizing: border-box;
7
+ font-size:12px;
8
+ }
9
+
10
+ .ris * {
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ .ris.ris-is-clearable .ris-clearable {
15
+ position: absolute;
16
+ top: 50%;
17
+ right: 5px;
18
+ font-size:1.1em;
19
+ margin-top:-1px;
20
+ transform: translate(0, -50%);
21
+ padding:5px;
22
+ color: #555;
23
+ cursor:pointer;
24
+ }
25
+
26
+ .ris.ris-is-creatable .ris-input {
27
+ padding-right:15px;
28
+ }
29
+
30
+ .ris .ris-input {
31
+ display: block;
32
+ resize: none;
33
+ width: 100%;
34
+ padding: 5px;
35
+ border: 5px solid #eee;
36
+ border-radius: 5px;
37
+ font-size:inherit;
38
+ }
39
+
40
+ .ris .ris-input:active, .ris .ris-input:focus {
41
+ outline: 0;
42
+ border: 5px solid #1e88e5;
43
+ }
44
+
45
+ .ris.ris-open-up .ris-options {
46
+ top:0;
47
+ transform:translate(-50%,-100%);
48
+ }
49
+
50
+ .ris .ris-options {
51
+ position: absolute;
52
+ top: 100%;
53
+ left: 50%;
54
+ margin:0;
55
+ width: calc(100% - 10px);
56
+ background: #fff;
57
+ transform:translate(-50%, 0);
58
+ border: 1px solid #eee;
59
+ overflow: auto;
60
+ box-shadow: 0 2px 3px 1px #eee;
61
+ max-height:245px;
62
+ font-size:inherit;
63
+ }
64
+
65
+ .ris .ris-options .ris-option {
66
+ padding: 5px;
67
+ cursor: pointer;
68
+ border-bottom: 1px solid #efefef;
69
+ }
70
+
71
+ .ris .ris-options .ris-option:last-child {
72
+ border-bottom: 0;
73
+ }
74
+
75
+ .ris .ris-options .ris-option:hover {
76
+ background: #bbdefb;
77
+ }
78
+
79
+ .ris .ris-options .ris-option.current {
80
+ background:#bbdefb;
81
+ }
82
+
83
+ .ris .ris-options .ris-option.selected {
84
+ background:#1e88e5;
85
+ color: #fff;
86
+ }
87
+
88
+ .ris .ris-options.ris-no-options {
89
+ padding:5px;
90
+ text-align:center;
91
+ color:#ccc;
92
+ }