-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathutils.js
More file actions
121 lines (108 loc) · 3.4 KB
/
utils.js
File metadata and controls
121 lines (108 loc) · 3.4 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
import PropTypes from 'prop-types'
import {noop} from '../../utils-ts'
import {getInitialValue} from '../utils-ts'
import {dropdownDefaultProps, dropdownPropTypes} from '../utils.dropdown'
export const defaultStateValues = {
activeIndex: -1,
selectedItems: [],
}
/**
* Gets the initial state based on the provided props. It uses initial, default
* and controlled props related to state in order to compute the initial value.
*
* @param {Object} props Props passed to the hook.
* @returns {Object} The initial state.
*/
function getInitialState(props) {
const activeIndex = getInitialValue(
props.activeIndex,
props.initialActiveIndex,
props.defaultActiveIndex,
defaultStateValues.activeIndex,
)
const selectedItems = getInitialValue(
props.selectedItems,
props.initialSelectedItems,
props.defaultSelectedItems,
defaultStateValues.selectedItems,
)
return {
activeIndex,
selectedItems,
}
}
/**
* Returns true if dropdown keydown operation is permitted. Should not be
* allowed on keydown with modifier keys (ctrl, alt, shift, meta), on
* input element with text content that is either highlighted or selection
* cursor is not at the starting position.
*
* @param {KeyboardEvent} event The event from keydown.
* @returns {boolean} Whether the operation is allowed.
*/
function isKeyDownOperationPermitted(event) {
if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
return false
}
const element = event.target
if (
element instanceof HTMLInputElement && // if element is a text input
element.value !== '' && // and we have text in it
// and cursor is either not at the start or is currently highlighting text.
(element.selectionStart !== 0 || element.selectionEnd !== 0)
) {
return false
}
return true
}
/**
* Check if a state is equal for taglist, by comparing active index and selected items.
* Used by useSelect and useCombobox.
*
* @param {Object} prevState
* @param {Object} newState
* @returns {boolean} Wheather the states are deeply equal.
*/
function isStateEqual(prevState, newState) {
return (
prevState.selectedItems === newState.selectedItems &&
prevState.activeIndex === newState.activeIndex
)
}
const propTypes = {
stateReducer: dropdownPropTypes.stateReducer,
itemToKey: dropdownPropTypes.itemToKey,
environment: dropdownPropTypes.environment,
selectedItems: PropTypes.array,
initialSelectedItems: PropTypes.array,
defaultSelectedItems: PropTypes.array,
getA11yStatusMessage: PropTypes.func,
activeIndex: PropTypes.number,
initialActiveIndex: PropTypes.number,
defaultActiveIndex: PropTypes.number,
onActiveIndexChange: PropTypes.func,
onSelectedItemsChange: PropTypes.func,
keyNavigationNext: PropTypes.string,
keyNavigationPrevious: PropTypes.string,
}
export const defaultProps = {
itemToKey: dropdownDefaultProps.itemToKey,
stateReducer: dropdownDefaultProps.stateReducer,
environment: dropdownDefaultProps.environment,
keyNavigationNext: 'ArrowRight',
keyNavigationPrevious: 'ArrowLeft',
}
// eslint-disable-next-line import/no-mutable-exports
let validatePropTypes = noop
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'production') {
validatePropTypes = (options, caller) => {
PropTypes.checkPropTypes(propTypes, options, 'prop', caller.name)
}
}
export {
validatePropTypes,
getInitialState,
isKeyDownOperationPermitted,
isStateEqual,
}