/
downshift.js
933 lines (854 loc) 路 28.1 KB
/
downshift.js
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/* eslint camelcase:0 */
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import preval from 'preval.macro'
import setA11yStatus from './set-a11y-status'
import {
cbToCb,
composeEventHandlers,
debounce,
scrollIntoView,
generateId,
firstDefined,
getA11yStatusMessage,
unwrapArray,
isDOMElement,
getElementProps,
noop,
requiredProp,
pickState,
isPlainObject,
} from './utils'
class Downshift extends Component {
static propTypes = {
children: PropTypes.func,
render: PropTypes.func,
defaultHighlightedIndex: PropTypes.number,
defaultSelectedItem: PropTypes.any,
defaultInputValue: PropTypes.string,
defaultIsOpen: PropTypes.bool,
getA11yStatusMessage: PropTypes.func,
itemToString: PropTypes.func,
onChange: PropTypes.func,
onSelect: PropTypes.func,
onStateChange: PropTypes.func,
onInputValueChange: PropTypes.func,
onUserAction: PropTypes.func,
onClick: PropTypes.func,
onOuterClick: PropTypes.func,
selectedItemChanged: PropTypes.func,
itemCount: PropTypes.number,
id: PropTypes.string,
environment: PropTypes.shape({
addEventListener: PropTypes.func,
removeEventListener: PropTypes.func,
document: PropTypes.shape({
getElementById: PropTypes.func,
activeElement: PropTypes.any,
body: PropTypes.any,
}),
}),
// things we keep in state for uncontrolled components
// but can accept as props for controlled components
/* eslint-disable react/no-unused-prop-types */
selectedItem: PropTypes.any,
isOpen: PropTypes.bool,
inputValue: PropTypes.string,
highlightedIndex: PropTypes.number,
breakingChanges: PropTypes.shape({
resetInputOnSelection: PropTypes.bool,
}),
/* eslint-enable */
}
static defaultProps = {
defaultHighlightedIndex: null,
defaultSelectedItem: null,
defaultInputValue: '',
defaultIsOpen: false,
getA11yStatusMessage,
id: generateId('downshift'),
itemToString: i => {
if (i == null) {
return ''
}
if (process.env.NODE_ENV !== 'production' && isPlainObject(i)) {
//eslint-disable-next-line no-console
console.warn(
'downshift: An object was passed to the default implementation of `itemToString`. You should probably provide your own `itemToString` implementation. Please refer to the `itemToString` API documentation.',
'The object that was passed:',
i,
)
}
return String(i)
},
onStateChange: () => {},
onInputValueChange: () => {},
onUserAction: () => {},
onChange: () => {},
onSelect: () => {},
onOuterClick: () => {},
selectedItemChanged: (prevItem, item) => prevItem !== item,
environment:
typeof window === 'undefined' /* istanbul ignore next (ssr) */
? {}
: window,
breakingChanges: {},
}
// this is an experimental feature
// so we're not going to document this yet
// nor are we going to test it.
// We will try to avoid breaking it, but
// we make no guarantees.
// If you need it, we recommend that you lock
// down your version of downshift (don't use a
// version range) to avoid surprise breakages.
static stateChangeTypes = {
unknown: '__autocomplete_unknown__',
mouseUp: '__autocomplete_mouseup__',
itemMouseEnter: '__autocomplete_item_mouseenter__',
keyDownArrowUp: '__autocomplete_keydown_arrow_up__',
keyDownArrowDown: '__autocomplete_keydown_arrow_down__',
keyDownEscape: '__autocomplete_keydown_escape__',
keyDownEnter: '__autocomplete_keydown_enter__',
blurInput: '__autocomplete_blur_input__',
changeInput: '__autocomplete_change_input__',
keyDownSpaceButton: '__autocomplete_keydown_space_button__',
clickButton: '__autocomplete_click_button__',
blurButton: '__autocomplete_blur_button__',
controlledPropUpdatedSelectedItem:
'__autocomplete_controlled_prop_updated_selected_item__',
}
constructor(...args) {
super(...args)
const state = this.getState({
highlightedIndex: this.props.defaultHighlightedIndex,
isOpen: this.props.defaultIsOpen,
inputValue: this.props.defaultInputValue,
selectedItem: this.props.defaultSelectedItem,
})
if (state.selectedItem) {
state.inputValue = this.props.itemToString(state.selectedItem)
}
this.state = state
}
root_handleClick = composeEventHandlers(
this.props.onClick,
this.root_handleClick,
)
input = null
items = []
// itemCount can be changed asynchronously
// from within downshift (so it can't come from a prop)
// this is why we store it as an instance and use
// getItemCount rather than just use items.length
// (to support windowing + async)
itemCount = null
previousResultCount = 0
/**
* Gets the state based on internal state or props
* If a state value is passed via props, then that
* is the value given, otherwise it's retrieved from
* stateToMerge
*
* This will perform a shallow merge of the given state object
* with the state coming from props
* (for the controlled component scenario)
* This is used in state updater functions so they're referencing
* the right state regardless of where it comes from.
*
* @param {Object} stateToMerge defaults to this.state
* @return {Object} the state
*/
getState(stateToMerge = this.state) {
return Object.keys(stateToMerge).reduce((state, key) => {
state[key] = this.isControlledProp(key)
? this.props[key]
: stateToMerge[key]
return state
}, {})
}
/**
* This determines whether a prop is a "controlled prop" meaning it is
* state which is controlled by the outside of this component rather
* than within this component.
* @param {String} key the key to check
* @return {Boolean} whether it is a controlled controlled prop
*/
isControlledProp(key) {
return this.props[key] !== undefined
}
getItemCount() {
// things read better this way. They're in priority order:
// 1. `this.itemCount`
// 2. `this.props.itemCount`
// 3. `this.items.length`
/* eslint-disable no-negated-condition */
if (this.itemCount != null) {
return this.itemCount
} else if (this.props.itemCount !== undefined) {
return this.props.itemCount
} else {
return this.items.length
}
/* eslint-enable no-negated-condition */
}
setItemCount = count => (this.itemCount = count)
unsetItemCount = () => (this.itemCount = null)
getItemNodeFromIndex = index => {
return this.props.environment.document.getElementById(this.getItemId(index))
}
setHighlightedIndex = (
highlightedIndex = this.props.defaultHighlightedIndex,
otherStateToSet = {},
) => {
otherStateToSet = pickState(otherStateToSet)
this.internalSetState({highlightedIndex, ...otherStateToSet})
}
scrollHighlightedItemIntoView = () => {
const node = this.getItemNodeFromIndex(this.getState().highlightedIndex)
const rootNode = this._rootNode
scrollIntoView(node, rootNode)
}
openAndHighlightDefaultIndex = (otherStateToSet = {}) => {
this.setHighlightedIndex(undefined, {isOpen: true, ...otherStateToSet})
}
highlightDefaultIndex = (otherStateToSet = {}) => {
this.setHighlightedIndex(undefined, otherStateToSet)
}
moveHighlightedIndex = (amount, otherStateToSet) => {
if (this.getState().isOpen) {
this.changeHighlightedIndex(amount, otherStateToSet)
} else {
this.openAndHighlightDefaultIndex(otherStateToSet)
}
}
// eslint-disable-next-line complexity
changeHighlightedIndex = (moveAmount, otherStateToSet) => {
const itemsLastIndex = this.getItemCount() - 1
if (itemsLastIndex < 0) {
return
}
const {highlightedIndex} = this.getState()
let baseIndex = highlightedIndex
if (baseIndex === null) {
baseIndex = moveAmount > 0 ? -1 : itemsLastIndex + 1
}
let newIndex = baseIndex + moveAmount
if (newIndex < 0) {
newIndex = itemsLastIndex
} else if (newIndex > itemsLastIndex) {
newIndex = 0
}
this.setHighlightedIndex(newIndex, otherStateToSet)
}
clearSelection = cb => {
this.internalSetState(
{
selectedItem: null,
inputValue: '',
isOpen: false,
},
() => {
this.focusInput()
cbToCb(cb)()
},
)
}
focusInput() {
const inputNode = this._rootNode.querySelector(`[id="${this.inputId}"]`)
inputNode && inputNode.focus && inputNode.focus()
}
selectItem = (item, otherStateToSet, cb) => {
otherStateToSet = pickState(otherStateToSet)
this.internalSetState(
{
isOpen: false,
highlightedIndex: this.props.defaultHighlightedIndex,
selectedItem: item,
inputValue:
this.isControlledProp('selectedItem') &&
this.props.breakingChanges.resetInputOnSelection
? this.props.defaultInputValue
: this.props.itemToString(item),
...otherStateToSet,
},
(...args) => {
this.focusInput()
typeof cb === 'function' && cb(...args)
},
)
}
selectItemAtIndex = (itemIndex, otherStateToSet, cb) => {
const item = this.items[itemIndex]
if (item == null) {
return
}
this.selectItem(item, otherStateToSet, cb)
}
selectHighlightedItem = (otherStateToSet, cb) => {
return this.selectItemAtIndex(
this.getState().highlightedIndex,
otherStateToSet,
cb,
)
}
// any piece of our state can live in two places:
// 1. Uncontrolled: it's internal (this.state)
// We will call this.setState to update that state
// 2. Controlled: it's external (this.props)
// We will call this.props.onStateChange to update that state
//
// In addition, we'll call this.props.onChange if the
// selectedItem is changed.
internalSetState(stateToSet, cb) {
let isItemSelected, onChangeArg
const onStateChangeArg = {}
const isStateToSetFunction = typeof stateToSet === 'function'
// we want to call `onInputValueChange` before the `setState` call
// so someone controlling the `inputValue` state gets notified of
// the input change as soon as possible. This avoids issues with
// preserving the cursor position.
// See https://github.com/paypal/downshift/issues/217 for more info.
if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
this.props.onInputValueChange(stateToSet.inputValue, {
...this.getStateAndHelpers(),
...stateToSet,
})
}
return this.setState(
state => {
state = this.getState(state)
stateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet
// checks if an item is selected, regardless of if it's different from
// what was selected before
// used to determine if onSelect and onChange callbacks should be called
isItemSelected = stateToSet.hasOwnProperty('selectedItem')
// this keeps track of the object we want to call with setState
const nextState = {}
// this is just used to tell whether the state changed
const nextFullState = {}
// we need to call on change if the outside world is controlling any of our state
// and we're trying to update that state. OR if the selection has changed and we're
// trying to update the selection
if (isItemSelected && stateToSet.selectedItem !== state.selectedItem) {
onChangeArg = stateToSet.selectedItem
}
stateToSet.type = stateToSet.type || Downshift.stateChangeTypes.unknown
Object.keys(stateToSet).forEach(key => {
// onStateChangeArg should only have the state that is
// actually changing
if (state[key] !== stateToSet[key]) {
onStateChangeArg[key] = stateToSet[key]
}
// the type is useful for the onStateChangeArg
// but we don't actually want to set it in internal state.
// this is an undocumented feature for now... Not all internalSetState
// calls support it and I'm not certain we want them to yet.
// But it enables users controlling the isOpen state to know when
// the isOpen state changes due to mouseup events which is quite handy.
if (key === 'type') {
return
}
nextFullState[key] = stateToSet[key]
// if it's coming from props, then we don't care to set it internally
if (!this.isControlledProp(key)) {
nextState[key] = stateToSet[key]
}
})
// if stateToSet is a function, then we weren't able to call onInputValueChange
// earlier, so we'll call it now that we know what the inputValue state will be.
if (isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
this.props.onInputValueChange(stateToSet.inputValue, {
...this.getStateAndHelpers(),
...stateToSet,
})
}
return nextState
},
() => {
// call the provided callback if it's a callback
cbToCb(cb)()
// only call the onStateChange and onChange callbacks if
// we have relevant information to pass them.
const hasMoreStateThanType = Object.keys(onStateChangeArg).length > 1
if (hasMoreStateThanType) {
this.props.onStateChange(onStateChangeArg, this.getStateAndHelpers())
}
if (isItemSelected) {
this.props.onSelect(
stateToSet.selectedItem,
this.getStateAndHelpers(),
)
}
if (onChangeArg !== undefined) {
this.props.onChange(onChangeArg, this.getStateAndHelpers())
}
// this is currently undocumented and therefore subject to change
// We'll try to not break it, but just be warned.
this.props.onUserAction(onStateChangeArg, this.getStateAndHelpers())
},
)
}
getStateAndHelpers() {
const {highlightedIndex, inputValue, selectedItem, isOpen} = this.getState()
const {id, itemToString} = this.props
const {
getRootProps,
getButtonProps,
getLabelProps,
getInputProps,
getItemProps,
openMenu,
closeMenu,
toggleMenu,
selectItem,
selectItemAtIndex,
selectHighlightedItem,
setHighlightedIndex,
clearSelection,
clearItems,
reset,
setItemCount,
unsetItemCount,
} = this
return {
// prop getters
getRootProps,
getButtonProps,
getLabelProps,
getInputProps,
getItemProps,
// actions
reset,
openMenu,
closeMenu,
toggleMenu,
selectItem,
selectItemAtIndex,
selectHighlightedItem,
setHighlightedIndex,
clearSelection,
clearItems,
setItemCount,
unsetItemCount,
//props
itemToString,
id,
// state
highlightedIndex,
inputValue,
isOpen,
selectedItem,
}
}
//////////////////////////// ROOT
rootRef = node => (this._rootNode = node)
getRootProps = (
{refKey = 'ref', ...rest} = {},
{suppressRefError = false} = {},
) => {
// this is used in the render to know whether the user has called getRootProps.
// It uses that to know whether to apply the props automatically
this.getRootProps.called = true
this.getRootProps.refKey = refKey
this.getRootProps.suppressRefError = suppressRefError
return {
[refKey]: this.rootRef,
...rest,
}
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\ ROOT
keyDownHandlers = {
ArrowDown(event) {
event.preventDefault()
const amount = event.shiftKey ? 5 : 1
this.moveHighlightedIndex(amount, {
type: Downshift.stateChangeTypes.keyDownArrowDown,
})
},
ArrowUp(event) {
event.preventDefault()
const amount = event.shiftKey ? -5 : -1
this.moveHighlightedIndex(amount, {
type: Downshift.stateChangeTypes.keyDownArrowUp,
})
},
Enter(event) {
if (this.getState().isOpen) {
event.preventDefault()
this.selectHighlightedItem({
type: Downshift.stateChangeTypes.keyDownEnter,
})
}
},
Escape(event) {
event.preventDefault()
this.reset({type: Downshift.stateChangeTypes.keyDownEscape})
},
}
//////////////////////////// BUTTON
buttonKeyDownHandlers = {
...this.keyDownHandlers,
' '(event) {
event.preventDefault()
this.toggleMenu({type: Downshift.stateChangeTypes.keyDownSpaceButton})
},
}
getButtonProps = ({onClick, onKeyDown, onBlur, ...rest} = {}) => {
const {isOpen} = this.getState()
const eventHandlers = rest.disabled
? {}
: {
onClick: composeEventHandlers(onClick, this.button_handleClick),
onKeyDown: composeEventHandlers(onKeyDown, this.button_handleKeyDown),
onBlur: composeEventHandlers(onBlur, this.button_handleBlur),
}
return {
type: 'button',
role: 'button',
'aria-label': isOpen ? 'close menu' : 'open menu',
'aria-expanded': isOpen,
'aria-haspopup': true,
...eventHandlers,
...rest,
}
}
button_handleKeyDown = event => {
if (this.buttonKeyDownHandlers[event.key]) {
this.buttonKeyDownHandlers[event.key].call(this, event)
}
}
button_handleClick = event => {
event.preventDefault()
// handle odd case for Safari and Firefox which
// don't give the button the focus properly.
/* istanbul ignore if (can't reasonably test this) */
if (
this.props.environment.document.activeElement ===
this.props.environment.document.body
) {
event.target.focus()
}
this.toggleMenu({type: Downshift.stateChangeTypes.clickButton})
}
button_handleBlur = () => {
if (!this.isMouseDown) {
this.reset({type: Downshift.stateChangeTypes.blurButton})
}
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ BUTTON
/////////////////////////////// LABEL
getLabelProps = (props = {}) => {
this.getLabelProps.called = true
if (
this.getInputProps.called &&
props.htmlFor &&
props.htmlFor !== this.inputId
) {
throw new Error(
`downshift: You provided the htmlFor of "${
props.htmlFor
}" for your label, but the id of your input is "${
this.inputId
}". You must either remove the id from your input or set the htmlFor of the label equal to the input id.`,
)
}
this.inputId = firstDefined(
this.inputId,
props.htmlFor,
generateId('downshift-input'),
)
return {
...props,
htmlFor: this.inputId,
}
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LABEL
/////////////////////////////// INPUT
getInputProps = ({onKeyDown, onBlur, onChange, onInput, ...rest} = {}) => {
this.getInputProps.called = true
if (this.getLabelProps.called && rest.id && rest.id !== this.inputId) {
throw new Error(
`downshift: You provided the id of "${
rest.id
}" for your input, but the htmlFor of your label is "${
this.inputId
}". You must either remove the id from your input or set the htmlFor of the label equal to the input id.`,
)
}
this.inputId = firstDefined(
this.inputId,
rest.id,
generateId('downshift-input'),
)
// the boolean cast here is necessary due to a weird deal with
// babel-plugin-istanbul + preval.macro. No idea...
const isPreact = Boolean(
preval`module.exports = process.env.BUILD_PREACT === 'true'`,
)
const onChangeKey = isPreact /* istanbul ignore next (preact) */
? 'onInput'
: 'onChange'
const {inputValue, isOpen, highlightedIndex} = this.getState()
const eventHandlers = rest.disabled
? {}
: {
// preact compatibility
[onChangeKey]: composeEventHandlers(
onChange,
onInput,
this.input_handleChange,
),
onKeyDown: composeEventHandlers(onKeyDown, this.input_handleKeyDown),
onBlur: composeEventHandlers(onBlur, this.input_handleBlur),
}
return {
role: 'combobox',
'aria-autocomplete': 'list',
'aria-expanded': isOpen,
'aria-activedescendant':
isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0
? this.getItemId(highlightedIndex)
: null,
autoComplete: 'off',
value: inputValue,
...eventHandlers,
...rest,
id: this.inputId,
}
}
input_handleKeyDown = event => {
if (event.key && this.keyDownHandlers[event.key]) {
this.keyDownHandlers[event.key].call(this, event)
}
}
input_handleChange = event => {
this.internalSetState({
type: Downshift.stateChangeTypes.changeInput,
isOpen: true,
inputValue: event.target.value,
})
}
input_handleBlur = () => {
if (!this.isMouseDown) {
this.reset({type: Downshift.stateChangeTypes.blurInput})
}
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INPUT
/////////////////////////////// ITEM
getItemId(index) {
return `${this.props.id}-item-${index}`
}
getItemProps = ({
onMouseMove,
onClick,
index,
item = requiredProp('getItemProps', 'item'),
...rest
} = {}) => {
if (index === undefined) {
this.items.push(item)
index = this.items.indexOf(item)
} else {
this.items[index] = item
}
return {
id: this.getItemId(index),
// onMouseMove is used over onMouseEnter here. onMouseMove
// is only triggered on actual mouse movement while onMouseEnter
// can fire on DOM changes, interrupting keyboard navigation
onMouseMove: composeEventHandlers(onMouseMove, () => {
if (index === this.getState().highlightedIndex) {
return
}
this.setHighlightedIndex(index, {
type: Downshift.stateChangeTypes.itemMouseEnter,
})
// We never want to manually scroll when changing state based
// on `onMouseMove` because we will be moving the element out
// from under the user which is currently scrolling/moving the
// cursor
this.avoidScrolling = true
setTimeout(() => (this.avoidScrolling = false), 250)
}),
onClick: composeEventHandlers(onClick, () => {
this.selectItemAtIndex(index)
}),
...rest,
}
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ITEM
clearItems = () => {
this.items = []
}
reset = (otherStateToSet = {}, cb) => {
otherStateToSet = pickState(otherStateToSet)
this.internalSetState(
({selectedItem}) => ({
isOpen: false,
highlightedIndex: this.props.defaultHighlightedIndex,
inputValue: this.props.itemToString(selectedItem),
...otherStateToSet,
}),
cbToCb(cb),
)
}
toggleMenu = (otherStateToSet = {}, cb) => {
otherStateToSet = pickState(otherStateToSet)
this.internalSetState(
({isOpen}) => {
return {isOpen: !isOpen, ...otherStateToSet}
},
() => {
const {isOpen} = this.getState()
if (isOpen) {
this.highlightDefaultIndex()
}
cbToCb(cb)()
},
)
}
openMenu = cb => {
this.internalSetState({isOpen: true}, cbToCb(cb))
}
closeMenu = cb => {
this.internalSetState({isOpen: false}, cbToCb(cb))
}
updateStatus = debounce(() => {
if (!this._isMounted) {
return
}
const state = this.getState()
const item = this.items[state.highlightedIndex] || {}
const resultCount = this.getItemCount()
const status = this.props.getA11yStatusMessage({
itemToString: this.props.itemToString,
previousResultCount: this.previousResultCount,
resultCount,
highlightedItem: item,
...state,
})
this.previousResultCount = resultCount
setA11yStatus(status)
}, 200)
componentDidMount() {
// the _isMounted property is because we have `updateStatus` in a `debounce`
// and we don't want to update the status if the component has been umounted
this._isMounted = true
// this.isMouseDown helps us track whether the mouse is currently held down.
// This is useful when the user clicks on an item in the list, but holds the mouse
// down long enough for the list to disappear (because the blur event fires on the input)
// this.isMouseDown is used in the blur handler on the input to determine whether the blur event should
// trigger hiding the menu.
const onMouseDown = () => {
this.isMouseDown = true
}
const onMouseUp = event => {
this.isMouseDown = false
if (
(event.target === this._rootNode ||
!this._rootNode.contains(event.target)) &&
this.getState().isOpen
) {
this.reset({type: Downshift.stateChangeTypes.mouseUp}, () =>
this.props.onOuterClick(this.getStateAndHelpers()),
)
}
}
this.props.environment.addEventListener('mousedown', onMouseDown)
this.props.environment.addEventListener('mouseup', onMouseUp)
this.cleanup = () => {
this._isMounted = false
this.props.environment.removeEventListener('mousedown', onMouseDown)
this.props.environment.removeEventListener('mouseup', onMouseUp)
}
}
componentDidUpdate(prevProps, prevState) {
if (
this.isControlledProp('selectedItem') &&
this.props.selectedItemChanged(
prevProps.selectedItem,
this.props.selectedItem,
)
) {
this.internalSetState({
type: Downshift.stateChangeTypes.controlledPropUpdatedSelectedItem,
inputValue: this.props.itemToString(this.props.selectedItem),
})
}
const current =
this.props.highlightedIndex === undefined ? this.state : this.props
const prev =
prevProps.highlightedIndex === undefined ? prevState : prevProps
if (
current.highlightedIndex !== prev.highlightedIndex &&
!this.avoidScrolling
) {
this.scrollHighlightedItemIntoView()
}
this.updateStatus()
}
componentWillUnmount() {
this.cleanup() // avoids memory leak
}
// eslint-disable-next-line complexity
render() {
const children = unwrapArray(this.props.render || this.props.children, noop)
// because the items are rerendered every time we call the children
// we clear this out each render and
this.clearItems()
// we reset this so we know whether the user calls getRootProps during
// this render. If they do then we don't need to do anything,
// if they don't then we need to clone the element they return and
// apply the props for them.
this.getRootProps.called = false
this.getRootProps.refKey = undefined
this.getRootProps.suppressRefError = undefined
// we do something similar for getLabelProps
this.getLabelProps.called = false
// and something similar for getInputProps
this.getInputProps.called = false
const element = unwrapArray(children(this.getStateAndHelpers()))
if (!element) {
return null
}
if (this.getRootProps.called) {
if (!this.getRootProps.suppressRefError) {
validateGetRootPropsCalledCorrectly(element, this.getRootProps)
}
return element
} else if (isDOMElement(element)) {
// they didn't apply the root props, but we can clone
// this and apply the props ourselves
return React.cloneElement(
element,
this.getRootProps(getElementProps(element)),
)
} else {
// they didn't apply the root props, but they need to
// otherwise we can't query around the autocomplete
throw new Error(
'downshift: If you return a non-DOM element, you must use apply the getRootProps function',
)
}
}
}
export default Downshift
function validateGetRootPropsCalledCorrectly(element, {refKey}) {
const refKeySpecified = refKey !== 'ref'
const isComposite = !isDOMElement(element)
if (isComposite && !refKeySpecified) {
throw new Error(
'downshift: You returned a non-DOM element. You must specify a refKey in getRootProps',
)
} else if (!isComposite && refKeySpecified) {
throw new Error(
`downshift: You returned a DOM element. You should not specify a refKey in getRootProps. You specified "${refKey}"`,
)
}
if (!getElementProps(element)[refKey]) {
throw new Error(
`downshift: You must apply the ref prop "${refKey}" from getRootProps onto your root element.`,
)
}
}