/
index.js
234 lines (192 loc) · 6.41 KB
/
index.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
import React, {useState, useRef, useEffect} from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import {moleculeDropdownListSizes as SIZES} from '@s-ui/react-molecule-dropdown-list'
import {inputSizes as SELECT_SIZES} from '@s-ui/react-atom-input'
import MoleculeSelectSingleSelection from './components/SingleSelection'
import MoleculeSelectMultipleSelection from './components/MultipleSelection'
import {withOpenToggle} from '@s-ui/hoc'
import {getTarget} from '@s-ui/js/lib/react'
import {getCurrentElementFocused} from '@s-ui/js/lib/dom'
const BASE_CLASS = `sui-MoleculeSelect`
const CLASS_FOCUS = `${BASE_CLASS}--focus`
const CLASS_DISABLED = `is-disabled`
const ERROR_STATES = {
ERROR: 'error',
SUCCESS: 'success'
}
const ENABLED_KEYS = ['Enter', 'ArrowDown', 'ArrowUp']
const getErrorStateClass = errorState => {
if (errorState) return `${BASE_CLASS}--${ERROR_STATES.ERROR}`
if (errorState === false) return `${BASE_CLASS}--${ERROR_STATES.SUCCESS}`
return ''
}
const getOptionData = children => {
const optionsData = {}
React.Children.forEach(children, child => {
const {children, value} = child.props
optionsData[value] = children
})
return optionsData
}
const MoleculeSelect = props => {
const {
isOpen,
onToggle,
children,
errorState,
disabled,
keysSelection,
refMoleculeSelect: refMoleculeSelectFromProps
} = props
const refMoleculeSelect = useRef(refMoleculeSelectFromProps)
const refsMoleculeSelectOptions = useRef([])
const [optionsData, setOptionsData] = useState(getOptionData(children))
const [focus, setFocus] = useState(false)
const extendedChildren = React.Children.toArray(children)
.filter(Boolean)
.map((child, index) => {
refsMoleculeSelectOptions.current[index] = React.createRef()
return React.cloneElement(child, {
innerRef: refsMoleculeSelectOptions.current[index],
onSelectKey: keysSelection
})
})
const className = cx(
BASE_CLASS,
{
[CLASS_FOCUS]: focus,
[CLASS_DISABLED]: disabled
},
getErrorStateClass(errorState)
)
useEffect(() => {
setOptionsData(getOptionData(children))
}, [children])
const closeList = ev => {
onToggle(ev, {isOpen: false})
ev.preventDefault()
ev.stopPropagation()
}
const focusFirstOption = (ev, {options}) => {
options[0].focus()
ev.preventDefault()
ev.stopPropagation()
}
const handleToggle = ev => {
onToggle(ev, {})
ev.preventDefault()
ev.stopPropagation()
}
const handleKeyDown = ev => {
ev.persist()
const isEnabledKey = ENABLED_KEYS.includes(ev.key)
const options = refsMoleculeSelectOptions.current.map(getTarget)
const domSourceEvent = ev.target
const domMoleculeSelect = refMoleculeSelect.current
if (!isOpen && isEnabledKey) {
domSourceEvent === domMoleculeSelect && handleToggle(ev)
} else {
const currentElementFocused = getCurrentElementFocused()
const isSomeOptionFocused = [...options].includes(currentElementFocused)
if (ev.key === 'Escape') closeList(ev)
if (ev.key === 'ArrowDown' && !isSomeOptionFocused)
focusFirstOption(ev, {options})
}
}
const handleFocusIn = () => {
!disabled && setFocus(true)
}
const handleFocusOut = ev => {
ev.persist()
const options = refsMoleculeSelectOptions.current.map(getTarget)
const firstOption = options[0]
setTimeout(() => {
const currentElementFocused = getCurrentElementFocused()
const isSomeOptionFocused = [...options].includes(currentElementFocused)
const isOptionListFocused = firstOption
? currentElementFocused.isSameNode(firstOption.parentNode)
: false
if (!isSomeOptionFocused && !isOptionListFocused && isOpen) {
closeList(ev)
}
}, 1)
setFocus(false)
}
const {multiselection, ...propsFromProps} = props
return (
<div
ref={refMoleculeSelect}
tabIndex="0"
className={className}
onKeyDown={handleKeyDown}
onFocus={handleFocusIn}
onBlur={handleFocusOut}
>
{multiselection ? (
<MoleculeSelectMultipleSelection
refMoleculeSelect={refMoleculeSelect}
optionsData={optionsData}
{...propsFromProps}
>
{extendedChildren}
</MoleculeSelectMultipleSelection>
) : (
<MoleculeSelectSingleSelection
refMoleculeSelect={refMoleculeSelect}
optionsData={optionsData}
{...propsFromProps}
>
{extendedChildren}
</MoleculeSelectSingleSelection>
)}
</div>
)
}
MoleculeSelect.propTypes = {
/** children */
children: PropTypes.any,
/** The DOM id global attribute. */
id: PropTypes.string,
/** if select accept single value or multiple values */
multiselection: PropTypes.bool,
/** value selected */
value: PropTypes.any,
/** list of values to be displayed on the select */
options: PropTypes.array,
/** if list of options is displayed or not */
isOpen: PropTypes.bool,
/** callback when arrow up/down is clicked → to show/hide list of options */
onToggle: PropTypes.func,
/** callback to be triggered when value selected changes */
onChange: PropTypes.func,
/** Icon for closing (removing) tags */
iconCloseTag: PropTypes.node,
/** Icon for arrow in select (down direction when closed) */
iconArrowDown: PropTypes.node.isRequired,
/** size (height) of the list */
size: PropTypes.oneOf(Object.values(SIZES)),
/** list of key identifiers that will trigger a selection */
keysSelection: PropTypes.array,
/* object generated w/ Reacte.createRef method to get a DOM reference of internal input */
refMoleculeSelect: PropTypes.object,
/** true = error, false = success, null = neutral */
errorState: PropTypes.bool,
/** This Boolean attribute prevents the user from interacting with the select */
disabled: PropTypes.bool,
/** This Boolean attribute prevents the user from interacting with the input but without disabled styles */
readOnly: PropTypes.bool,
/** Size of the select(input) */
selectSize: PropTypes.oneOf(Object.values(SELECT_SIZES))
}
MoleculeSelect.defaultProps = {
disabled: false,
keysSelection: [' ', 'Enter'],
onChange: () => {},
onToggle: () => {},
readOnly: false,
selectSize: SELECT_SIZES.MEDIUM
}
export default withOpenToggle(MoleculeSelect)
export {SIZES as moleculeSelectDropdownListSizes}
export {SELECT_SIZES as moleculeSelectSizes}