-
Notifications
You must be signed in to change notification settings - Fork 929
/
utils.js
754 lines (662 loc) · 20.6 KB
/
utils.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
import React, {
useRef,
useCallback,
useReducer,
useEffect,
useLayoutEffect,
} from 'react'
import PropTypes from 'prop-types'
import {isReactNative} from '../is.macro'
import {
scrollIntoView,
getState,
generateId,
debounce,
validateControlledUnchanged,
noop,
targetWithinDownshift,
} from '../utils'
import {cleanupStatusDiv, setStatus} from '../set-a11y-status'
const dropdownDefaultStateValues = {
highlightedIndex: -1,
isOpen: false,
selectedItem: null,
inputValue: '',
}
function callOnChangeProps(action, state, newState) {
const {props, type} = action
const changes = {}
Object.keys(state).forEach(key => {
invokeOnChangeHandler(key, action, state, newState)
if (newState[key] !== state[key]) {
changes[key] = newState[key]
}
})
if (props.onStateChange && Object.keys(changes).length) {
props.onStateChange({type, ...changes})
}
}
function invokeOnChangeHandler(key, action, state, newState) {
const {props, type} = action
const handler = `on${capitalizeString(key)}Change`
if (
props[handler] &&
newState[key] !== undefined &&
newState[key] !== state[key]
) {
props[handler]({type, ...newState})
}
}
/**
* Default state reducer that returns the changes.
*
* @param {Object} s state.
* @param {Object} a action with changes.
* @returns {Object} changes.
*/
function stateReducer(s, a) {
return a.changes
}
/**
* Debounced call for updating the a11y message.
*/
const updateA11yStatus = debounce((status, document) => {
setStatus(status, document)
}, 200)
// istanbul ignore next
const useIsomorphicLayoutEffect =
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
? useLayoutEffect
: useEffect
// istanbul ignore next
const useElementIds =
'useId' in React // Avoid conditional useId call
? function useElementIds({
id,
labelId,
menuId,
getItemId,
toggleButtonId,
inputId,
}) {
// Avoid conditional useId call
const reactId = `downshift-${React.useId()}`
if (!id) {
id = reactId
}
const elementIdsRef = useRef({
labelId: labelId || `${id}-label`,
menuId: menuId || `${id}-menu`,
getItemId: getItemId || (index => `${id}-item-${index}`),
toggleButtonId: toggleButtonId || `${id}-toggle-button`,
inputId: inputId || `${id}-input`,
})
return elementIdsRef.current
}
: function useElementIds({
id = `downshift-${generateId()}`,
labelId,
menuId,
getItemId,
toggleButtonId,
inputId,
}) {
const elementIdsRef = useRef({
labelId: labelId || `${id}-label`,
menuId: menuId || `${id}-menu`,
getItemId: getItemId || (index => `${id}-item-${index}`),
toggleButtonId: toggleButtonId || `${id}-toggle-button`,
inputId: inputId || `${id}-input`,
})
return elementIdsRef.current
}
function getItemAndIndex(itemProp, indexProp, items, errorMessage) {
let item, index
if (itemProp === undefined) {
if (indexProp === undefined) {
throw new Error(errorMessage)
}
item = items[indexProp]
index = indexProp
} else {
index = indexProp === undefined ? items.indexOf(itemProp) : indexProp
item = itemProp
}
return [item, index]
}
function isAcceptedCharacterKey(key) {
return /^\S{1}$/.test(key)
}
function capitalizeString(string) {
return `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`
}
function useLatestRef(val) {
const ref = useRef(val)
// technically this is not "concurrent mode safe" because we're manipulating
// the value during render (so it's not idempotent). However, the places this
// hook is used is to support memoizing callbacks which will be called
// *during* render, so we need the latest values *during* render.
// If not for this, then we'd probably want to use useLayoutEffect instead.
ref.current = val
return ref
}
/**
* Computes the controlled state using a the previous state, props,
* two reducers, one from downshift and an optional one from the user.
* Also calls the onChange handlers for state values that have changed.
*
* @param {Function} reducer Reducer function from downshift.
* @param {Object} props The hook props, also passed to createInitialState.
* @param {Function} createInitialState Function that returns the initial state.
* @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
* @returns {Array} An array with the state and an action dispatcher.
*/
function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
const prevStateRef = useRef()
const actionRef = useRef()
const enhancedReducer = useCallback(
(state, action) => {
actionRef.current = action
state = getState(state, action.props)
const changes = reducer(state, action)
const newState = action.props.stateReducer(state, {...action, changes})
return newState
},
[reducer],
)
const [state, dispatch] = useReducer(
enhancedReducer,
props,
createInitialState,
)
const propsRef = useLatestRef(props)
const dispatchWithProps = useCallback(
action => dispatch({props: propsRef.current, ...action}),
[propsRef],
)
const action = actionRef.current
useEffect(() => {
const prevState = getState(prevStateRef.current, action?.props)
const shouldCallOnChangeProps =
action && prevStateRef.current && !isStateEqual(prevState, state)
if (shouldCallOnChangeProps) {
callOnChangeProps(action, prevState, state)
}
prevStateRef.current = state
}, [state, action, isStateEqual])
return [state, dispatchWithProps]
}
/**
* Wraps the useEnhancedReducer and applies the controlled prop values before
* returning the new state.
*
* @param {Function} reducer Reducer function from downshift.
* @param {Object} props The hook props, also passed to createInitialState.
* @param {Function} createInitialState Function that returns the initial state.
* @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
* @returns {Array} An array with the state and an action dispatcher.
*/
function useControlledReducer(
reducer,
props,
createInitialState,
isStateEqual,
) {
const [state, dispatch] = useEnhancedReducer(
reducer,
props,
createInitialState,
isStateEqual,
)
return [getState(state, props), dispatch]
}
const defaultProps = {
itemToString(item) {
return item ? String(item) : ''
},
itemToKey(item) {
return item
},
stateReducer,
scrollIntoView,
environment:
/* istanbul ignore next (ssr) */
typeof window === 'undefined' || isReactNative ? undefined : window,
}
function getDefaultValue(
props,
propKey,
defaultStateValues = dropdownDefaultStateValues,
) {
const defaultValue = props[`default${capitalizeString(propKey)}`]
if (defaultValue !== undefined) {
return defaultValue
}
return defaultStateValues[propKey]
}
function getInitialValue(
props,
propKey,
defaultStateValues = dropdownDefaultStateValues,
) {
const value = props[propKey]
if (value !== undefined) {
return value
}
const initialValue = props[`initial${capitalizeString(propKey)}`]
if (initialValue !== undefined) {
return initialValue
}
return getDefaultValue(props, propKey, defaultStateValues)
}
function getInitialState(props) {
const selectedItem = getInitialValue(props, 'selectedItem')
const isOpen = getInitialValue(props, 'isOpen')
const highlightedIndex = getInitialHighlightedIndex(props)
const inputValue = getInitialValue(props, 'inputValue')
return {
highlightedIndex:
highlightedIndex < 0 && selectedItem && isOpen
? props.items.findIndex(
item => props.itemToKey(item) === props.itemToKey(selectedItem),
)
: highlightedIndex,
isOpen,
selectedItem,
inputValue,
}
}
function getHighlightedIndexOnOpen(props, state, offset) {
const {
items,
initialHighlightedIndex,
defaultHighlightedIndex,
isItemDisabled,
itemToKey,
} = props
const {selectedItem, highlightedIndex} = state
if (items.length === 0) {
return -1
}
// initialHighlightedIndex will give value to highlightedIndex on initial state only.
if (
initialHighlightedIndex !== undefined &&
highlightedIndex === initialHighlightedIndex &&
!isItemDisabled(items[initialHighlightedIndex], initialHighlightedIndex)
) {
return initialHighlightedIndex
}
if (
defaultHighlightedIndex !== undefined &&
!isItemDisabled(items[defaultHighlightedIndex], defaultHighlightedIndex)
) {
return defaultHighlightedIndex
}
if (selectedItem) {
return items.findIndex(item => itemToKey(selectedItem) === itemToKey(item))
}
if (
offset < 0 &&
!isItemDisabled(items[items.length - 1], items.length - 1)
) {
return items.length - 1
}
if (offset > 0 && !isItemDisabled(items[0], 0)) {
return 0
}
return -1
}
/**
* Tracks mouse and touch events, such as mouseDown, touchMove and touchEnd.
*
* @param {Window} environment The environment to add the event listeners to, for instance window.
* @param {() => void} handleBlur The function that is called if mouseDown or touchEnd occured outside the downshiftElements.
* @param {Array<{current: HTMLElement}>} downshiftElementsRefs The refs for the elements that should not trigger a blur action from mouseDown or touchEnd.
* @returns {{isMouseDown: boolean, isTouchMove: boolean, isTouchEnd: boolean}} The mouse and touch events information, if any of are happening.
*/
function useMouseAndTouchTracker(
environment,
handleBlur,
downshiftElementsRefs,
) {
const mouseAndTouchTrackersRef = useRef({
isMouseDown: false,
isTouchMove: false,
isTouchEnd: false,
})
useEffect(() => {
if (isReactNative || !environment) {
return noop
}
const downshiftElements = downshiftElementsRefs.map(ref => ref.current)
function onMouseDown() {
mouseAndTouchTrackersRef.current.isTouchEnd = false // reset this one.
mouseAndTouchTrackersRef.current.isMouseDown = true
}
function onMouseUp(event) {
mouseAndTouchTrackersRef.current.isMouseDown = false
if (
!targetWithinDownshift(event.target, downshiftElements, environment)
) {
handleBlur()
}
}
function onTouchStart() {
mouseAndTouchTrackersRef.current.isTouchEnd = false
mouseAndTouchTrackersRef.current.isTouchMove = false
}
function onTouchMove() {
mouseAndTouchTrackersRef.current.isTouchMove = true
}
function onTouchEnd(event) {
mouseAndTouchTrackersRef.current.isTouchEnd = true
if (
!mouseAndTouchTrackersRef.current.isTouchMove &&
!targetWithinDownshift(
event.target,
downshiftElements,
environment,
false,
)
) {
handleBlur()
}
}
environment.addEventListener('mousedown', onMouseDown)
environment.addEventListener('mouseup', onMouseUp)
environment.addEventListener('touchstart', onTouchStart)
environment.addEventListener('touchmove', onTouchMove)
environment.addEventListener('touchend', onTouchEnd)
return function cleanup() {
environment.removeEventListener('mousedown', onMouseDown)
environment.removeEventListener('mouseup', onMouseUp)
environment.removeEventListener('touchstart', onTouchStart)
environment.removeEventListener('touchmove', onTouchMove)
environment.removeEventListener('touchend', onTouchEnd)
}
}, [downshiftElementsRefs, environment, handleBlur])
return mouseAndTouchTrackersRef.current
}
/* istanbul ignore next */
// eslint-disable-next-line import/no-mutable-exports
let useGetterPropsCalledChecker = () => noop
/**
* Custom hook that checks if getter props are called correctly.
*
* @param {...any} propKeys Getter prop names to be handled.
* @returns {Function} Setter function called inside getter props to set call information.
*/
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'production') {
useGetterPropsCalledChecker = (...propKeys) => {
const getterPropsCalledRef = useRef(
propKeys.reduce((acc, propKey) => {
acc[propKey] = {}
return acc
}, {}),
)
useEffect(() => {
Object.keys(getterPropsCalledRef.current).forEach(propKey => {
const propCallInfo = getterPropsCalledRef.current[propKey]
if (!Object.keys(propCallInfo).length) {
// eslint-disable-next-line no-console
console.error(
`downshift: You forgot to call the ${propKey} getter function on your component / element.`,
)
return
}
const {suppressRefError, refKey, elementRef} = propCallInfo
if (suppressRefError) {
return
}
if (!elementRef?.current) {
// eslint-disable-next-line no-console
console.error(
`downshift: The ref prop "${refKey}" from ${propKey} was not applied correctly on your element.`,
)
}
})
}, [])
const setGetterPropCallInfo = useCallback(
(propKey, suppressRefError, refKey, elementRef) => {
getterPropsCalledRef.current[propKey] = {
suppressRefError,
refKey,
elementRef,
}
},
[],
)
return setGetterPropCallInfo
}
}
/**
* Adds an a11y aria live status message if getA11yStatusMessage is passed.
* @param {(options: Object) => string} getA11yStatusMessage The function that builds the status message.
* @param {Object} options The options to be passed to getA11yStatusMessage if called.
* @param {Array<unknown>} dependencyArray The dependency array that triggers the status message setter via useEffect.
* @param {{document: Document}} environment The environment object containing the document.
*/
function useA11yMessageStatus(
getA11yStatusMessage,
options,
dependencyArray,
environment = {},
) {
const document = environment.document
const isInitialMount = useIsInitialMount()
// Adds an a11y aria live status message if getA11yStatusMessage is passed.
useEffect(() => {
if (!getA11yStatusMessage || isInitialMount || isReactNative || !document) {
return
}
const status = getA11yStatusMessage(options)
updateA11yStatus(status, document)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencyArray)
// Cleanup the status message container.
useEffect(() => {
return () => {
updateA11yStatus.cancel()
cleanupStatusDiv(document)
}
}, [document])
}
function useScrollIntoView({
highlightedIndex,
isOpen,
itemRefs,
getItemNodeFromIndex,
menuElement,
scrollIntoView: scrollIntoViewProp,
}) {
// used not to scroll on highlight by mouse.
const shouldScrollRef = useRef(true)
// Scroll on highlighted item if change comes from keyboard.
useIsomorphicLayoutEffect(() => {
if (
highlightedIndex < 0 ||
!isOpen ||
!Object.keys(itemRefs.current).length
) {
return
}
if (shouldScrollRef.current === false) {
shouldScrollRef.current = true
} else {
scrollIntoViewProp(getItemNodeFromIndex(highlightedIndex), menuElement)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [highlightedIndex])
return shouldScrollRef
}
// eslint-disable-next-line import/no-mutable-exports
let useControlPropsValidator = noop
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'production') {
useControlPropsValidator = ({props, state}) => {
// used for checking when props are moving from controlled to uncontrolled.
const prevPropsRef = useRef(props)
const isInitialMount = useIsInitialMount()
useEffect(() => {
if (isInitialMount) {
return
}
validateControlledUnchanged(state, prevPropsRef.current, props)
prevPropsRef.current = props
}, [state, props, isInitialMount])
}
}
/**
* Handles selection on Enter / Alt + ArrowUp. Closes the menu and resets the highlighted index, unless there is a highlighted.
* In that case, selects the item and resets to defaults for open state and highlighted idex.
* @param {Object} props The useCombobox props.
* @param {number} highlightedIndex The index from the state.
* @param {boolean} inputValue Also return the input value for state.
* @returns The changes for the state.
*/
function getChangesOnSelection(props, highlightedIndex, inputValue = true) {
const shouldSelect = props.items?.length && highlightedIndex >= 0
return {
isOpen: false,
highlightedIndex: -1,
...(shouldSelect && {
selectedItem: props.items[highlightedIndex],
isOpen: getDefaultValue(props, 'isOpen'),
highlightedIndex: getDefaultValue(props, 'highlightedIndex'),
...(inputValue && {
inputValue: props.itemToString(props.items[highlightedIndex]),
}),
}),
}
}
/**
* Check if a state is equal for dropdowns, by comparing isOpen, inputValue, highlightedIndex and selected item.
* Used by useSelect and useCombobox.
*
* @param {Object} prevState
* @param {Object} newState
* @returns {boolean} Wheather the states are deeply equal.
*/
function isDropdownsStateEqual(prevState, newState) {
return (
prevState.isOpen === newState.isOpen &&
prevState.inputValue === newState.inputValue &&
prevState.highlightedIndex === newState.highlightedIndex &&
prevState.selectedItem === newState.selectedItem
)
}
/**
* Tracks if it's the first render.
*/
function useIsInitialMount() {
const isInitialMountRef = React.useRef(true)
React.useEffect(() => {
isInitialMountRef.current = false
return () => {
isInitialMountRef.current = true
}
}, [])
return isInitialMountRef.current
}
/**
* Returns the new highlightedIndex based on the defaultHighlightedIndex prop, if it's not disabled.
*
* @param {Object} props Props from useCombobox or useSelect.
* @returns {number} The highlighted index.
*/
function getDefaultHighlightedIndex(props) {
const highlightedIndex = getDefaultValue(props, 'highlightedIndex')
if (
highlightedIndex > -1 &&
props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)
) {
return -1
}
return highlightedIndex
}
/**
* Returns the new highlightedIndex based on the initialHighlightedIndex prop, if not disabled.
*
* @param {Object} props Props from useCombobox or useSelect.
* @returns {number} The highlighted index.
*/
function getInitialHighlightedIndex(props) {
const highlightedIndex = getInitialValue(props, 'highlightedIndex')
if (
highlightedIndex > -1 &&
props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)
) {
return -1
}
return highlightedIndex
}
// Shared between all exports.
const commonPropTypes = {
environment: PropTypes.shape({
addEventListener: PropTypes.func.isRequired,
removeEventListener: PropTypes.func.isRequired,
document: PropTypes.shape({
createElement: PropTypes.func.isRequired,
getElementById: PropTypes.func.isRequired,
activeElement: PropTypes.any.isRequired,
body: PropTypes.any.isRequired,
}).isRequired,
Node: PropTypes.func.isRequired,
}),
itemToString: PropTypes.func,
itemToKey: PropTypes.func,
stateReducer: PropTypes.func,
}
// Shared between useSelect, useCombobox, Downshift.
const commonDropdownPropTypes = {
...commonPropTypes,
getA11yStatusMessage: PropTypes.func,
highlightedIndex: PropTypes.number,
defaultHighlightedIndex: PropTypes.number,
initialHighlightedIndex: PropTypes.number,
isOpen: PropTypes.bool,
defaultIsOpen: PropTypes.bool,
initialIsOpen: PropTypes.bool,
selectedItem: PropTypes.any,
initialSelectedItem: PropTypes.any,
defaultSelectedItem: PropTypes.any,
id: PropTypes.string,
labelId: PropTypes.string,
menuId: PropTypes.string,
getItemId: PropTypes.func,
toggleButtonId: PropTypes.string,
onSelectedItemChange: PropTypes.func,
onHighlightedIndexChange: PropTypes.func,
onStateChange: PropTypes.func,
onIsOpenChange: PropTypes.func,
scrollIntoView: PropTypes.func,
}
export {
useControlPropsValidator,
useScrollIntoView,
updateA11yStatus,
useGetterPropsCalledChecker,
useMouseAndTouchTracker,
getHighlightedIndexOnOpen,
getInitialState,
getInitialValue,
getDefaultValue,
defaultProps,
useControlledReducer,
useEnhancedReducer,
useLatestRef,
capitalizeString,
isAcceptedCharacterKey,
getItemAndIndex,
useElementIds,
getChangesOnSelection,
isDropdownsStateEqual,
commonDropdownPropTypes,
commonPropTypes,
useIsInitialMount,
useA11yMessageStatus,
getDefaultHighlightedIndex,
}