-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathreact-tokeninput.js
More file actions
890 lines (769 loc) · 32.6 KB
/
react-tokeninput.js
File metadata and controls
890 lines (769 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("prop-types"), require("react"));
else if(typeof define === 'function' && define.amd)
define(["prop-types", "react"], factory);
else if(typeof exports === 'object')
exports["TokenInput"] = factory(require("prop-types"), require("react"));
else
root["TokenInput"] = factory(root["prop-types"], root["react"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Token = exports.Option = exports.Combobox = undefined;
var _combobox = __webpack_require__(1);
var _combobox2 = _interopRequireDefault(_combobox);
var _option = __webpack_require__(5);
var _option2 = _interopRequireDefault(_option);
var _token = __webpack_require__(6);
var _token2 = _interopRequireDefault(_token);
var _main = __webpack_require__(7);
var _main2 = _interopRequireDefault(_main);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.Combobox = _combobox2.default;
exports.Option = _option2.default;
exports.Token = _token2.default;
/**
* You can't do an import and then immediately export it :(
* And `export default TokenInput from './main'` doesn't seem to
* work either :(
* So this little variable swapping stuff gets it to work.
*/
var TokenInput = _main2.default;
exports.default = TokenInput;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var PropTypes = __webpack_require__(2);
var React = __webpack_require__(3);
var guid = 0;
var k = function k() {};
var addClass = __webpack_require__(4);
var ComboboxOption = __webpack_require__(5);
var Combobox = function (_React$Component) {
_inherits(Combobox, _React$Component);
function Combobox() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Combobox);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Combobox.__proto__ || Object.getPrototypeOf(Combobox)).call.apply(_ref, [this].concat(args))), _this), _this.makeMenu = function (children) {
var activedescendant;
var isEmpty = true;
// Should this instead use React.addons.cloneWithProps or React.cloneElement?
var _children = React.Children.map(children, function (child, index) {
// console.log(child.type, ComboboxOption.type)
if (child.type !== ComboboxOption || !child.props.isFocusable) {
// allow random elements to live in this list
return child;
}
isEmpty = false;
// TODO: cloneWithProps and map instead of altering the children in-place
var props = child.props;
var newProps = {};
if (this.state.value === child.props.value) {
// need an ID for WAI-ARIA
newProps.id = props.id || 'ic-tokeninput-selected-' + ++guid;
newProps.isSelected = true;
activedescendant = props.id;
}
newProps.onBlur = this.handleOptionBlur;
newProps.onClick = this.selectOption.bind(this, child);
newProps.onFocus = this.handleOptionFocus;
newProps.onKeyDown = this.handleOptionKeyDown.bind(this, child);
newProps.onMouseEnter = this.handleOptionMouseEnter.bind(this, index);
return React.cloneElement(child, newProps);
}.bind(_this));
return {
children: _children,
activedescendant: activedescendant,
isEmpty: isEmpty
};
}, _this.getClassName = function () {
var className = addClass(_this.props.className, 'ic-tokeninput');
if (_this.state.isOpen) className = addClass(className, 'ic-tokeninput-is-open');
return className;
}, _this.clearSelectedState = function (cb) {
_this.setState({
focusedIndex: null,
inputValue: null,
value: null,
matchedAutocompleteOption: null,
activedescendant: null
}, cb);
}, _this.handleInputChange = function () {
var value = _this.input.value;
_this.clearSelectedState(function () {
this.props.onInput(value);
}.bind(_this));
}, _this.handleInputFocus = function () {
_this.props.onFocus();
_this.maybeShowList();
}, _this.handleInputClick = function () {
_this.maybeShowList();
}, _this.maybeShowList = function () {
if (_this.props.showListOnFocus) {
_this.showList();
}
}, _this.handleInputBlur = function () {
var focusedAnOption = _this.state.focusedIndex != null;
if (focusedAnOption) return;
_this.maybeSelectAutocompletedOption();
_this.hideList();
}, _this.handleOptionBlur = function () {
// don't want to hide the list if we focused another option
_this.blurTimer = setTimeout(_this.hideList, 0);
}, _this.handleOptionFocus = function () {
// see `handleOptionBlur`
clearTimeout(_this.blurTimer);
}, _this.handleInputKeyUp = function (event) {
if (_this.state.menu.isEmpty ||
// autocompleting while backspacing feels super weird, so let's not
event.keyCode === 8 /*backspace*/ || !_this.props.autocomplete.match(/both|inline/)) return;
}, _this.handleButtonClick = function () {
_this.state.isOpen ? _this.hideList() : _this.showList();
_this.focusInput();
}, _this.showList = function () {
if (!_this.state.menu.children.length) {
return;
}
_this.setState({ isOpen: true });
}, _this.hideList = function () {
_this.setState({
isOpen: false,
focusedIndex: null
});
}, _this.hideOnEscape = function (event) {
_this.hideList();
_this.focusInput();
event.preventDefault();
}, _this.focusInput = function () {
_this.input.focus();
}, _this.selectInput = function () {
_this.input.select();
}, _this.inputKeydownMap = {
8: 'removeLastToken', // delete
13: 'selectOnEnter', // enter
188: 'selectOnEnter', // comma
27: 'hideOnEscape', // escape
38: 'focusPrevious', // up arrow
40: 'focusNext' // down arrow
}, _this.optionKeydownMap = {
13: 'selectOption',
27: 'hideOnEscape',
38: 'focusPrevious',
40: 'focusNext'
}, _this.handleKeydown = function (event) {
var handlerName = _this.inputKeydownMap[event.keyCode];
if (!handlerName) return;
_this.setState({ usingKeyboard: true });
return _this[handlerName].call(_this, event);
}, _this.handleOptionKeyDown = function (child, event) {
var handlerName = _this.optionKeydownMap[event.keyCode];
if (!handlerName) {
// if the user starts typing again while focused on an option, move focus
// to the inpute, select so it wipes out any existing value
_this.selectInput();
return;
}
event.preventDefault();
_this.setState({ usingKeyboard: true });
_this[handlerName].call(_this, child);
}, _this.handleOptionMouseEnter = function (index) {
if (_this.state.usingKeyboard) _this.setState({ usingKeyboard: false });else _this.focusOptionAtIndex(index);
}, _this.selectOnEnter = function (event) {
event.preventDefault();
_this.maybeSelectAutocompletedOption();
}, _this.maybeSelectAutocompletedOption = function () {
if (!_this.state.matchedAutocompleteOption) {
_this.selectText();
} else {
_this.selectOption(_this.state.matchedAutocompleteOption, { focus: false });
}
}, _this.selectOption = function (child, options) {
options = options || {};
_this.setState({
// value: child.props.value,
// inputValue: getLabel(child),
matchedAutocompleteOption: null
}, function () {
this.props.onSelect(child.props.value, child);
this.hideList();
this.clearSelectedState(); // added
if (options.focus !== false) this.selectInput();
}.bind(_this));
_this.input.value = ''; // added
}, _this.selectText = function () {
var value = _this.input.value;
if (!value) return;
_this.props.onSelect(value);
_this.clearSelectedState();
_this.input.value = ''; // added
}, _this.focusNext = function (event) {
if (event.preventDefault) event.preventDefault();
if (_this.state.menu.isEmpty) return;
var index = _this.nextFocusableIndex(_this.state.focusedIndex);
_this.focusOptionAtIndex(index);
}, _this.removeLastToken = function () {
if (_this.props.onRemoveLast && !_this.input.value) {
_this.props.onRemoveLast();
}
return true;
}, _this.focusPrevious = function (event) {
if (event.preventDefault) event.preventDefault();
if (_this.state.menu.isEmpty) return;
var index = _this.previousFocusableIndex(_this.state.focusedIndex);
_this.focusOptionAtIndex(index);
}, _this.focusSelectedOption = function () {
var selectedIndex;
React.Children.forEach(_this.props.children, function (child, index) {
if (child.props.value === this.state.value) selectedIndex = index;
}.bind(_this));
_this.showList();
_this.setState({
focusedIndex: selectedIndex
}, _this.focusOption);
}, _this.findInitialInputValue = function () {
// TODO: might not need this, we should know this in `makeMenu`
var inputValue;
React.Children.forEach(_this.props.children, function (child) {
if (child.props.value === this.props.value) inputValue = getLabel(child);
}.bind(_this));
return inputValue;
}, _this.clampIndex = function (index) {
if (index < 0) {
return _this.props.children.length - 1;
} else if (index >= _this.props.children.length) {
return 0;
}
return index;
}, _this.scanForFocusableIndex = function (index, increment) {
if (index === null || index === undefined) {
index = increment > 0 ? _this.clampIndex(-1) : 0;
}
var newIndex = index;
while (true) {
newIndex = _this.clampIndex(newIndex + increment);
if (newIndex === index || _this.props.children[newIndex].props.isFocusable) {
return newIndex;
}
}
}, _this.nextFocusableIndex = function (index) {
return _this.scanForFocusableIndex(index, 1);
}, _this.previousFocusableIndex = function (index) {
return _this.scanForFocusableIndex(index, -1);
}, _this.focusOptionAtIndex = function (index) {
if (!_this.state.isOpen && _this.state.value) return _this.focusSelectedOption();
_this.showList();
var length = _this.props.children.length;
if (index === -1) index = length - 1;else if (index === length) index = 0;
_this.setState({
focusedIndex: index
}, _this.focusOption);
}, _this.focusOption = function () {
var index = _this.state.focusedIndex;
_this.list.childNodes[index].focus();
}, _this.state = {
value: _this.props.value,
// the value displayed in the input
inputValue: _this.findInitialInputValue(),
isOpen: false,
focusedIndex: null,
matchedAutocompleteOption: null,
// this prevents crazy jumpiness since we focus options on mouseenter
usingKeyboard: false,
activedescendant: null,
listId: 'ic-tokeninput-list-' + ++guid,
menu: {
children: [],
activedescendant: null,
isEmpty: true
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Combobox, [{
key: 'componentWillMount',
value: function componentWillMount() {
this.setState({ menu: this.makeMenu(this.props.children) });
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(newProps) {
this.setState({ menu: this.makeMenu(newProps.children) }, function () {
if (newProps.children.length && (this.isOpen || document.activeElement === this.input)) {
if (!this.state.menu.children.length) {
return;
}
this.setState({
isOpen: true
}, function () {
this.list.scrollTop = 0;
}.bind(this));
} else {
this.hideList();
}
}.bind(this));
}
/**
* We don't create the <ComboboxOption> components, the user supplies them,
* so before rendering we attach handlers to facilitate communication from
* the ComboboxOption to the Combobox.
*/
/**
* When the user begins typing again we need to clear out any state that has
* to do with an existing or potential selection.
*/
}, {
key: 'render',
value: function render() {
var _this2 = this;
var ariaLabel = this.props['aria-label'] || 'Start typing to search. ' + 'Press the down arrow to navigate results. If you don\'t find an ' + 'acceptable option, you can input an alternative. Once you find or ' + 'input the tag you want, press Enter or Comma to add it.';
return React.createElement(
'div',
{ className: this.getClassName() },
this.props.value,
this.state.inputValue,
React.createElement('input', {
ref: function ref(e) {
return _this2.input = e;
},
autoComplete: 'off',
spellCheck: 'false',
'aria-label': ariaLabel,
'aria-expanded': this.state.isOpen + '',
'aria-haspopup': 'true',
'aria-activedescendant': this.state.menu.activedescendant,
'aria-autocomplete': 'list',
'aria-owns': this.state.listId,
id: this.props.id,
disabled: this.props.isDisabled,
className: 'ic-tokeninput-input',
onFocus: this.handleInputFocus,
onClick: this.handleInputClick,
onChange: this.handleInputChange,
onBlur: this.handleInputBlur,
onKeyDown: this.handleKeydown,
onKeyUp: this.handleInputKeyUp,
placeholder: this.props.placeholder,
role: 'combobox' }),
React.createElement(
'span',
{
'aria-hidden': 'true',
className: 'ic-tokeninput-button',
onClick: this.handleButtonClick },
'\u25BE'
),
React.createElement(
'div',
{
id: this.state.listId,
ref: function ref(e) {
return _this2.list = e;
},
className: 'ic-tokeninput-list',
role: 'listbox' },
this.state.menu.children
)
);
}
}]);
return Combobox;
}(React.Component);
Combobox.propTypes = {
onFocus: PropTypes.func,
/**
* Called when the combobox receives user input, this is your chance to
* filter the data and rerender the options.
*
* Signature:
*
* ```js
* function(userInput){}
* ```
*/
onInput: PropTypes.func,
/**
* Called when the combobox receives a selection. You probably want to reset
* the options to the full list at this point.
*
* Signature:
*
* ```js
* function(selectedValue){}
* ```
*/
onSelect: PropTypes.func,
/**
* Shown when the combobox is empty.
*/
placeholder: PropTypes.string
};
Combobox.defaultProps = {
autocomplete: 'both',
onFocus: k,
onInput: k,
onSelect: k,
value: null,
showListOnFocus: false
};
function getLabel(component) {
return component.props.label || component.props.children;
}
function matchFragment(userInput, firstChildLabel) {
userInput = userInput.toLowerCase();
firstChildLabel = firstChildLabel.toLowerCase();
if (userInput === '' || userInput === firstChildLabel) return false;
if (firstChildLabel.toLowerCase().indexOf(userInput.toLowerCase()) === -1) return false;
return true;
}
module.exports = Combobox;
/***/ }),
/* 2 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_2__;
/***/ }),
/* 3 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_3__;
/***/ }),
/* 4 */
/***/ (function(module, exports) {
'use strict';
module.exports = addClass;
function addClass(existing, added) {
if (!existing) return added;
if (existing.indexOf(added) > -1) return existing;
return existing + ' ' + added;
}
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var PropTypes = __webpack_require__(2);
var React = __webpack_require__(3);
var addClass = __webpack_require__(4);
var Option = function (_React$Component) {
_inherits(Option, _React$Component);
function Option() {
_classCallCheck(this, Option);
return _possibleConstructorReturn(this, (Option.__proto__ || Object.getPrototypeOf(Option)).apply(this, arguments));
}
_createClass(Option, [{
key: 'render',
value: function render() {
var props = this.props;
if (props.isSelected) {
props.className = addClass(props.className, 'ic-tokeninput-selected');
props.ariaSelected = true;
}
return React.createElement('div', props);
}
}]);
return Option;
}(React.Component);
Option.propTypes = {
/**
* The value that will be sent to the `onSelect` handler of the
* parent Combobox.
*/
value: PropTypes.any.isRequired,
/**
* What value to put into the input element when this option is
* selected, defaults to its children coerced to a string.
*/
label: PropTypes.string,
/**
* Whether the element should be selectable
*/
isFocusable: PropTypes.bool
};
Option.defaultProps = {
role: 'option',
tabIndex: '-1',
className: 'ic-tokeninput-option',
isSelected: false,
isFocusable: true
};
module.exports = Option;
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var React = __webpack_require__(3);
var Token = function (_React$Component) {
_inherits(Token, _React$Component);
function Token() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Token);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Token.__proto__ || Object.getPrototypeOf(Token)).call.apply(_ref, [this].concat(args))), _this), _this.handleClick = function () {
_this.props.onRemove(_this.props.value);
}, _this.handleKeyDown = function (key) {
var enterKey = 13;
if (key.keyCode === enterKey) _this.props.onRemove(_this.props.value);
}, _this.ariaLabelRemove = function () {
return _this.props.tokenAriaFunc ? _this.props.tokenAriaFunc(_this.props.name) : 'Remove \'' + _this.props.name + '\'';
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Token, [{
key: 'render',
value: function render() {
return React.createElement(
'li',
{ className: 'ic-token inline-flex' },
React.createElement(
'span',
{ className: 'ic-token-label' },
this.props.name
),
React.createElement(
'span',
{
role: 'button',
onClick: this.handleClick,
onFocus: this.props.onFocus,
onKeyDown: this.handleKeyDown,
'aria-label': this.ariaLabelRemove(),
className: 'ic-token-delete-button',
tabIndex: 0 },
'\u2715'
)
);
}
}]);
return Token;
}(React.Component);
module.exports = Token;
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var PropTypes = __webpack_require__(2);
var React = __webpack_require__(3);
var Combobox = __webpack_require__(1);
var Token = __webpack_require__(6);
var classnames = __webpack_require__(8);
var TokenInput = function (_React$Component) {
_inherits(TokenInput, _React$Component);
function TokenInput() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, TokenInput);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = TokenInput.__proto__ || Object.getPrototypeOf(TokenInput)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
selectedToken: null
}, _this.handleClick = function () {
// TODO: Expand combobox API for focus
_this.comboLi.querySelector('input').focus();
}, _this.handleFocus = function () {
if (_this.props.onFocus) {
_this.props.onFocus();
}
}, _this.handleInput = function (inputValue) {
_this.props.onInput(inputValue);
}, _this.handleSelect = function (event, option) {
var input = _this.comboLi.querySelector('input');
_this.props.onSelect(event, option);
_this.setState({
selectedToken: null
});
_this.props.onInput(input.value);
}, _this.handleRemove = function (value) {
var input = _this.comboLi.querySelector('input');
_this.props.onRemove(value);
input.focus();
}, _this.handleRemoveLast = function () {
_this.props.onRemove(_this.props.selected[_this.props.selected.length - 1]);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(TokenInput, [{
key: 'render',
value: function render() {
var _this2 = this;
var isDisabled = this.props.isDisabled;
var tokens = this.props.selected.map(function (token) {
return React.createElement(Token, {
tokenAriaFunc: this.props.tokenAriaFunc,
onFocus: this.handleFocus,
onRemove: this.handleRemove,
value: token,
name: token.name,
key: token.id });
}.bind(this));
var classes = classnames('ic-tokens flex', {
'ic-tokens-disabled': isDisabled
});
return React.createElement(
'ul',
{ className: classes, onClick: this.handleClick },
tokens,
React.createElement(
'li',
{ className: 'inline-flex', ref: function ref(e) {
return _this2.comboLi = e;
} },
React.createElement(
Combobox,
{
id: this.props.id,
'aria-label': this.props['combobox-aria-label'],
ariaDisabled: isDisabled,
onFocus: this.handleFocus,
onInput: this.handleInput,
showListOnFocus: this.props.showListOnFocus,
onSelect: this.handleSelect,
onRemoveLast: this.handleRemoveLast,
value: this.state.selectedToken,
isDisabled: isDisabled,
placeholder: this.props.placeholder },
this.props.menuContent
)
),
this.props.isLoading && React.createElement(
'li',
{ className: 'ic-tokeninput-loading flex' },
this.props.loadingComponent
)
);
}
}]);
return TokenInput;
}(React.Component);
TokenInput.propTypes = {
isLoading: PropTypes.bool,
loadingComponent: PropTypes.any,
onFocus: PropTypes.func,
onInput: PropTypes.func.isRequired,
onSelect: PropTypes.func.isRequired,
tokenAriaFunc: PropTypes.func,
onRemove: PropTypes.func.isRequired,
selected: PropTypes.array.isRequired,
menuContent: PropTypes.any,
showListOnFocus: PropTypes.bool,
placeholder: PropTypes.string
};
module.exports = TokenInput;
/***/ }),
/* 8 */
/***/ (function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/*!
Copyright (c) 2017 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */
(function () {
'use strict';
var hasOwn = {}.hasOwnProperty;
function classNames () {
var classes = [];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!arg) continue;
var argType = typeof arg;
if (argType === 'string' || argType === 'number') {
classes.push(arg);
} else if (Array.isArray(arg) && arg.length) {
var inner = classNames.apply(null, arg);
if (inner) {
classes.push(inner);
}
} else if (argType === 'object') {
for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(key);
}
}
}
}
return classes.join(' ');
}
if (typeof module !== 'undefined' && module.exports) {
classNames.default = classNames;
module.exports = classNames;
} else if (true) {
// register as 'classnames', consistent with npm package name
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function () {
return classNames;
}.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
} else {
window.classNames = classNames;
}
}());
/***/ })
/******/ ])
});
;
//# sourceMappingURL=react-tokeninput.js.map