react-select-input-v2 1.0.6

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.
package/lib/index.js ADDED
@@ -0,0 +1,328 @@
1
+ 'use strict';
2
+
3
+ exports.__esModule = true;
4
+ exports.default = undefined;
5
+
6
+ var _react = require('react');
7
+
8
+ var _react2 = _interopRequireDefault(_react);
9
+
10
+ var _reactAutosizeTextarea = require('react-autosize-textarea');
11
+
12
+ var _reactAutosizeTextarea2 = _interopRequireDefault(_reactAutosizeTextarea);
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
+
18
+ function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
19
+
20
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
21
+
22
+ function escapeRegExp(string) {
23
+ var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
24
+ var reHasRegExpChar = RegExp(reRegExpChar.source);
25
+ return string && reHasRegExpChar.test(string) ? string.replace(reRegExpChar, '\\$&') : string || '';
26
+ }
27
+
28
+ var SelectInput = function (_Component) {
29
+ _inherits(SelectInput, _Component);
30
+
31
+ function SelectInput() {
32
+ _classCallCheck(this, SelectInput);
33
+
34
+ var _this = _possibleConstructorReturn(this, _Component.call(this));
35
+
36
+ _this.renderOptions = function (options) {
37
+ return options && options.length > 0 ? options.map(function (option, index) {
38
+ return _react2.default.createElement(
39
+ 'div',
40
+ {
41
+ className: 'ris-option' + (_this.state.selectedOption === option[_this.props.valueKey] ? ' selected' : '') + (_this.state.currentOption === index ? ' current' : ''),
42
+ key: _this.props.uniqueKey + '-option-' + index,
43
+ onClick: _this.handleClick.bind(_this, index),
44
+ ref: function ref(_ref) {
45
+ return _this['option-' + option[_this.props.valueKey]] = _ref;
46
+ }
47
+ },
48
+ option[_this.props.labelKey]
49
+ );
50
+ }) : null;
51
+ };
52
+
53
+ _this.handleSelect = function (option, index) {
54
+ var state = _this.manipState(_this.state, 'value', option[_this.props.labelKey]);
55
+ state = _this.manipState(state, 'selectedOption', option[_this.props.valueKey]);
56
+ state = _this.manipState(state, 'currentOption', -1);
57
+ if (_this.props.collapseOnSelect) state = _this.setIsOpen(state, false);
58
+ state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, state.value));
59
+ _this.setState(state);
60
+
61
+ if (_this.isFunction(_this.props.onSelect)) _this.props.onSelect(option);
62
+ };
63
+
64
+ _this.handleClick = function (index, event) {
65
+ var option = _this.pickOption(index);
66
+ _this.handleSelect(option, index);
67
+ };
68
+
69
+ _this.handleChange = function (event) {
70
+ var state = _this.manipState(_this.state, 'value', event.target.value);
71
+ state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, event.target.value));
72
+ state = _this.setIsOpen(state, true);
73
+
74
+ if (!state.value) {
75
+ state = _this.manipState(state, 'currentOption', -1);
76
+ state = _this.manipState(state, 'selectedOption', null);
77
+ }
78
+
79
+ if (_this.isFunction(_this.props.onChange)) _this.props.onChange(event);
80
+
81
+ _this.setState(state);
82
+ };
83
+
84
+ _this.handleFocus = function (event) {
85
+ var state = _this.setIsOpen(_this.state, true);
86
+ state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, event.target.value));
87
+ _this.setState(state);
88
+
89
+ if (_this.isFunction(_this.props.onFocus)) _this.props.onFocus(event);
90
+ };
91
+
92
+ _this.handleClear = function () {
93
+ var value = '';
94
+ var state = _this.manipState(_this.state, 'value', value);
95
+ state = _this.manipState(state, 'searchMatchOptions', _this.matchingOptions(_this.props.options, state.value));
96
+ state = _this.manipState(state, 'currentOption', -1);
97
+ state = _this.manipState(state, 'selectedOption', null);
98
+ _this.setState(state);
99
+
100
+ if (_this.isFunction(_this.props.onClear)) _this.props.onClear();
101
+ };
102
+
103
+ _this.handleBlur = function (event) {
104
+ if (_this.props.collapseOnBlur) {
105
+ var state = _this.setIsOpen(_this.state, false);
106
+ _this.setState(state);
107
+ }
108
+
109
+ if (_this.isFunction(_this.props.onBlur)) _this.props.onBlur(event);
110
+ };
111
+
112
+ _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
+
123
+ if (_this.isFunction(_this.props.onKeyUp)) _this.props.onKeyUp(event);
124
+ };
125
+
126
+ _this.handleKeyDown = function (event) {
127
+ 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
+
136
+ if (_this.isFunction(_this.props.onKeyDown)) _this.props.onKeyDown(event);
137
+ };
138
+
139
+ _this.handleOptionNavigation = function (event) {
140
+ var state = {};
141
+ var currentOption = _this.state.currentOption;
142
+
143
+ if (event.key === 'ArrowUp' || event.key === 'ArrowDown' && _this.disableEnter) event.preventDefault();
144
+
145
+ if (event.key === 'ArrowUp' && currentOption === -1 && _this.props.openUp) currentOption = _this.state.searchMatchOptions.length;
146
+
147
+ if (_this.props.openUp) {
148
+ if (event.key === 'ArrowDown') {
149
+ if (currentOption > -1) currentOption++;else currentOption = -1;
150
+ } else if (event.key === 'ArrowUp') currentOption--;
151
+ } else {
152
+ if (event.key === 'ArrowDown') currentOption++;else if (event.key === 'ArrowUp') currentOption--;
153
+ }
154
+
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;
156
+
157
+ state = _this.manipState(_this.state, 'currentOption', currentOption);
158
+ var optionRef = currentOption > -1 ? _this['option-' + _this.state.searchMatchOptions[currentOption][_this.props.valueKey]] : null;
159
+ if (optionRef) optionRef.scrollIntoViewIfNeeded(false);
160
+
161
+ _this.setState(state);
162
+ };
163
+
164
+ _this.matchingOptions = function (options, value) {
165
+ value = escapeRegExp(value);
166
+ var searchValue = value.replace(/\s/g, '');
167
+ var searchOptions = [];
168
+
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] || '';
172
+ 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);
175
+
176
+ if (_this.props.openUp) searchOptions.reverse();
177
+
178
+ return searchOptions;
179
+ };
180
+
181
+ _this.pickOption = function (index) {
182
+ var _ref2;
183
+
184
+ index = index !== null && index !== undefined ? index : _this.state.currentOption;
185
+ if (index > -1) return _this.state.searchMatchOptions[index];else return _ref2 = {}, _ref2[_this.props.labelKey] = _this.input.value.trim(), _ref2[_this.props.valueKey] = _this.input.value.trim(), _ref2;
186
+ };
187
+
188
+ _this.setIsOpen = function (state, show) {
189
+ return Object.assign({}, state, {
190
+ isOpen: show || false
191
+ });
192
+ };
193
+
194
+ _this.manipState = function (state, key, value) {
195
+ var _Object$assign;
196
+
197
+ return Object.assign({}, state, (_Object$assign = {}, _Object$assign[key] = value, _Object$assign));
198
+ };
199
+
200
+ _this.isFunction = function (obj) {
201
+ return typeof obj === 'function';
202
+ };
203
+
204
+ _this.handleOutsideClick = function (event) {
205
+ if (!_this.ris.contains(event.target) && _this.props.collapseOnBlur) _this.setState(_this.setIsOpen(_this.state, false));
206
+ };
207
+
208
+ _this.state = {
209
+ isOpen: false,
210
+ value: null,
211
+ currentOption: -1,
212
+ selectedOption: null,
213
+ searchMatchOptions: []
214
+ };
215
+ return _this;
216
+ }
217
+
218
+ SelectInput.prototype.componentDidMount = function componentDidMount() {
219
+ document.addEventListener('click', this.handleOutsideClick);
220
+ };
221
+
222
+ SelectInput.prototype.componentDidUpdate = function componentDidUpdate() {
223
+ if (this.props.openUp && this.state.currentOption === -1 && this.state.isOpen && !this.state.selectedOption) {
224
+ this.options.scrollTop = this.options.scrollHeight;
225
+ } else if (this.props.openUp && this.state.selectedOption && this.state.isOpen) {
226
+ this['option-' + this.state.selectedOption].scrollIntoViewIfNeeded(true);
227
+ }
228
+ };
229
+
230
+ SelectInput.prototype.componentWillUnmount = function componentWillUnmount() {
231
+ document.removeEventListener('click', this.handleOutsideClick);
232
+ };
233
+
234
+ /*
235
+ Various renderers
236
+ */
237
+
238
+ /*
239
+ Handle different events
240
+ */
241
+
242
+ /*
243
+ Searching function
244
+ */
245
+
246
+ /*
247
+ Various helpers
248
+ */
249
+
250
+ SelectInput.prototype.render = function render() {
251
+ var _this2 = this;
252
+
253
+ return _react2.default.createElement(
254
+ 'div',
255
+ {
256
+ className: 'ris' + (this.props.openUp ? ' ris-open-up' : '') + (this.props.clearable ? ' ris-is-clearable' : '') + (this.props.className ? ' ' + this.props.className : ''),
257
+ key: 'ris-' + this.props.uniqueKey,
258
+ ref: function ref(_ref4) {
259
+ return _this2.ris = _ref4;
260
+ },
261
+ style: this.props.style
262
+ },
263
+ _react2.default.createElement(_reactAutosizeTextarea2.default, {
264
+ className: 'ris-input',
265
+ value: this.state.value !== null ? this.state.value : this.props.value,
266
+ placeholder: this.props.placeholder,
267
+ onChange: this.handleChange,
268
+ onFocus: this.handleFocus,
269
+ onBlur: this.props.onBlur,
270
+ onKeyUp: this.handleKeyUp,
271
+ onKeyDown: this.handleKeyDown,
272
+ autoFocus: this.props.autoFocus,
273
+ innerRef: function innerRef(ref) {
274
+ return _this2.input = ref;
275
+ }
276
+ }),
277
+ this.props.clearable ? _react2.default.createElement(
278
+ 'div',
279
+ { className: 'ris-clearable', onClick: this.handleClear },
280
+ 'x'
281
+ ) : null,
282
+ this.state.isOpen ? this.state.searchMatchOptions.length > 0 ? _react2.default.createElement(
283
+ 'div',
284
+ { className: 'ris-options', ref: function ref(_ref3) {
285
+ return _this2.options = _ref3;
286
+ } },
287
+ 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
293
+ );
294
+ };
295
+
296
+ return SelectInput;
297
+ }(_react.Component);
298
+
299
+ exports.default = SelectInput;
300
+
301
+
302
+ 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
327
+ };
328
+ module.exports = exports['default'];
@@ -0,0 +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
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "react-select-input-v2",
3
+ "version": "1.0.6",
4
+ "description": "react-select-input React component",
5
+ "main": "lib/index.js",
6
+ "module": "es/index.js",
7
+ "files": [
8
+ "css",
9
+ "es",
10
+ "lib",
11
+ "umd"
12
+ ],
13
+ "scripts": {
14
+ "build": "nwb build-react-component --copy-files",
15
+ "clean": "nwb clean-module && nwb clean-demo",
16
+ "start": "nwb serve-react-demo",
17
+ "test": "nwb test-react",
18
+ "test:coverage": "nwb test-react --coverage",
19
+ "test:watch": "nwb test-react --server",
20
+ "deploy": "gh-pages -d demo/dist"
21
+ },
22
+ "dependencies": {
23
+ "prop-types": "^15.6.2",
24
+ "react-autosize-textarea": "^0.4.8"
25
+ },
26
+ "peerDependencies": {
27
+ "react": "15.x"
28
+ },
29
+ "devDependencies": {
30
+ "eslint": "^5.4.0",
31
+ "eslint-config-prettier": "^3.0.1",
32
+ "eslint-plugin-import": "^2.14.0",
33
+ "eslint-plugin-prettier": "^2.6.2",
34
+ "eslint-plugin-react": "^7.11.1",
35
+ "gh-pages": "^1.0.0",
36
+ "nwb": "0.18.x",
37
+ "prettier": "^1.14.2",
38
+ "react": "^15.6.1",
39
+ "react-dom": "^15.6.1",
40
+ "babel-eslint": "^8.2.6"
41
+ },
42
+ "author": "Samrith Shankar",
43
+ "homepage": "https://samrith-s.github.io/react-select-input",
44
+ "license": "MIT",
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/samrith-s/react-select-input.git"
48
+ },
49
+ "keywords": [
50
+ "react-component",
51
+ "input",
52
+ "select",
53
+ "input select combo",
54
+ "select dropdown",
55
+ "custom select",
56
+ "searchable",
57
+ "searchable select",
58
+ "free input",
59
+ "free select"
60
+ ],
61
+ "sideEffects": false,
62
+ "bugs": {
63
+ "url": "https://github.com/samrith-s/react-select-input/issues"
64
+ },
65
+ "directories": {
66
+ "lib": "lib",
67
+ "test": "tests"
68
+ }
69
+ }