react-animated-select 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -18
- package/dist/index.cjs.js +9 -9
- package/dist/index.css +1 -1
- package/dist/index.es.js +420 -395
- package/package.json +1 -1
- package/src/getText.jsx +11 -0
- package/src/option.jsx +6 -3
- package/src/select.css +2 -2
- package/src/select.jsx +6 -4
- package/src/slideLeft.jsx +1 -1
- package/src/useSelect.jsx +21 -11
- package/src/useSelectLogic.jsx +262 -241
package/src/useSelectLogic.jsx
CHANGED
|
@@ -1,242 +1,263 @@
|
|
|
1
|
-
import {useState, useMemo, useCallback, useId} from 'react'
|
|
2
|
-
import {makeId} from './makeId'
|
|
3
|
-
|
|
4
|
-
export function useSelectLogic({
|
|
5
|
-
options = [],
|
|
6
|
-
jsxOptions = [],
|
|
7
|
-
|
|
8
|
-
value,
|
|
9
|
-
defaultValue = undefined,
|
|
10
|
-
onChange,
|
|
11
|
-
|
|
12
|
-
disabled = false,
|
|
13
|
-
loading = false,
|
|
14
|
-
error = false,
|
|
15
|
-
|
|
16
|
-
placeholder = 'Choose option',
|
|
17
|
-
emptyText = 'No options',
|
|
18
|
-
disabledText = 'Disabled',
|
|
19
|
-
loadingText = 'Loading',
|
|
20
|
-
errorText = 'Failed to load',
|
|
21
|
-
disabledOption = 'Disabled option',
|
|
22
|
-
emptyOption = 'Empty option',
|
|
23
|
-
invalidOption = 'Invalid option',
|
|
24
|
-
setVisibility
|
|
25
|
-
}) {
|
|
26
|
-
|
|
27
|
-
const stableId = useId()
|
|
28
|
-
|
|
29
|
-
// controlled select check
|
|
30
|
-
const isControlled = value !== undefined
|
|
31
|
-
|
|
32
|
-
// filled in value of the select by the user via prop
|
|
33
|
-
const [internalValue, setInternalValue] = useState(defaultValue)
|
|
34
|
-
|
|
35
|
-
// value chosenness status
|
|
36
|
-
const selectedValue = isControlled ? value : internalValue
|
|
37
|
-
|
|
38
|
-
const isOptionObject = (obj) => {
|
|
39
|
-
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return false
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
'
|
|
43
|
-
'
|
|
44
|
-
'
|
|
45
|
-
'
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
label: '0'
|
|
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
|
-
flat.push({
|
|
105
|
-
key,
|
|
106
|
-
value,
|
|
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
|
-
flat.push({
|
|
133
|
-
key:
|
|
134
|
-
value:
|
|
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
|
-
|
|
1
|
+
import {useState, useMemo, useCallback, useId} from 'react'
|
|
2
|
+
import {makeId} from './makeId'
|
|
3
|
+
|
|
4
|
+
export function useSelectLogic({
|
|
5
|
+
options = [],
|
|
6
|
+
jsxOptions = [],
|
|
7
|
+
|
|
8
|
+
value,
|
|
9
|
+
defaultValue = undefined,
|
|
10
|
+
onChange,
|
|
11
|
+
|
|
12
|
+
disabled = false,
|
|
13
|
+
loading = false,
|
|
14
|
+
error = false,
|
|
15
|
+
|
|
16
|
+
placeholder = 'Choose option',
|
|
17
|
+
emptyText = 'No options',
|
|
18
|
+
disabledText = 'Disabled',
|
|
19
|
+
loadingText = 'Loading',
|
|
20
|
+
errorText = 'Failed to load',
|
|
21
|
+
disabledOption = 'Disabled option',
|
|
22
|
+
emptyOption = 'Empty option',
|
|
23
|
+
invalidOption = 'Invalid option',
|
|
24
|
+
setVisibility
|
|
25
|
+
}) {
|
|
26
|
+
|
|
27
|
+
const stableId = useId()
|
|
28
|
+
|
|
29
|
+
// controlled select check
|
|
30
|
+
const isControlled = value !== undefined
|
|
31
|
+
|
|
32
|
+
// filled in value of the select by the user via prop
|
|
33
|
+
const [internalValue, setInternalValue] = useState(defaultValue)
|
|
34
|
+
|
|
35
|
+
// value chosenness status
|
|
36
|
+
const selectedValue = isControlled ? value : internalValue
|
|
37
|
+
|
|
38
|
+
const isOptionObject = (obj) => {
|
|
39
|
+
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return false
|
|
40
|
+
return (
|
|
41
|
+
'id' in obj ||
|
|
42
|
+
'value' in obj ||
|
|
43
|
+
'name' in obj ||
|
|
44
|
+
'label' in obj ||
|
|
45
|
+
'disabled' in obj
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// normalization list of options; converting an object or an unsuitable array into a suitable one
|
|
50
|
+
const normalizedPropOptions = useMemo(() => {
|
|
51
|
+
if (!options) return []
|
|
52
|
+
|
|
53
|
+
const flat = []
|
|
54
|
+
|
|
55
|
+
const push = (key, value, originalItem) => {
|
|
56
|
+
if (typeof value === 'number' && value === 0) {
|
|
57
|
+
flat.push({
|
|
58
|
+
key: '0',
|
|
59
|
+
value: 0,
|
|
60
|
+
userId: originalItem?.id ?? 0,
|
|
61
|
+
label: '0',
|
|
62
|
+
original: originalItem
|
|
63
|
+
})
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (typeof value == 'boolean') {
|
|
68
|
+
flat.push({
|
|
69
|
+
key: `bool-${flat.length}`,
|
|
70
|
+
value,
|
|
71
|
+
userId: originalItem?.id ?? `bool-${flat.length}`,
|
|
72
|
+
label: String(value),
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
original: originalItem
|
|
75
|
+
})
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (value === '' || value === null || value === undefined) {
|
|
80
|
+
flat.push({
|
|
81
|
+
key: `empty-${flat.length}`,
|
|
82
|
+
value: null,
|
|
83
|
+
userId: originalItem?.id ?? `empty-${flat.length}`,
|
|
84
|
+
disabled: true,
|
|
85
|
+
label: emptyOption,
|
|
86
|
+
original: originalItem
|
|
87
|
+
})
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof value == 'function') {
|
|
92
|
+
flat.push({
|
|
93
|
+
key: `invalid-${flat.length}`,
|
|
94
|
+
value: null,
|
|
95
|
+
userId: originalItem?.id ?? `invalid-${flat.length}`,
|
|
96
|
+
disabled: true,
|
|
97
|
+
label: invalidOption,
|
|
98
|
+
original: originalItem
|
|
99
|
+
})
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
104
|
+
flat.push({
|
|
105
|
+
key: value.id ?? value.value ?? value.name ?? key,
|
|
106
|
+
value,
|
|
107
|
+
userId: value.id ?? value.value ?? value.name ?? key,
|
|
108
|
+
disabled: !!value.disabled,
|
|
109
|
+
label: value.name ?? value.label ?? key,
|
|
110
|
+
original: originalItem
|
|
111
|
+
})
|
|
112
|
+
} else {
|
|
113
|
+
flat.push({
|
|
114
|
+
key,
|
|
115
|
+
value,
|
|
116
|
+
userId: key,
|
|
117
|
+
label: String(value ?? key),
|
|
118
|
+
original: originalItem
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (Array.isArray(options)) {
|
|
124
|
+
for (const item of options) {
|
|
125
|
+
if (
|
|
126
|
+
item &&
|
|
127
|
+
typeof item == 'object' &&
|
|
128
|
+
!Array.isArray(item) &&
|
|
129
|
+
Object.keys(item).length == 1 &&
|
|
130
|
+
item.disabled == true
|
|
131
|
+
) {
|
|
132
|
+
flat.push({
|
|
133
|
+
key: `disabled-${flat.length}`,
|
|
134
|
+
value: null,
|
|
135
|
+
userId: item?.id ?? item ?? null,
|
|
136
|
+
disabled: true,
|
|
137
|
+
label: disabledOption,
|
|
138
|
+
original: item
|
|
139
|
+
})
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (isOptionObject(item)) {
|
|
144
|
+
flat.push({
|
|
145
|
+
key: item.id ?? item.value ?? item.name ?? `opt-${flat.length}`,
|
|
146
|
+
value: item.value ?? item.id ?? item,
|
|
147
|
+
userId: item?.id ?? item?.value ?? item?.name ?? item?.label ?? item ?? null,
|
|
148
|
+
disabled: !!item.disabled,
|
|
149
|
+
label: item.name ?? item.label ?? String(item.id ?? item.value),
|
|
150
|
+
original: item
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
else if (item && typeof item == 'object') {
|
|
154
|
+
for (const [k, v] of Object.entries(item)) {
|
|
155
|
+
push(k, v, v)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
push(item, item, item)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (typeof options == 'object') {
|
|
164
|
+
for (const [k, v] of Object.entries(options)) {
|
|
165
|
+
push(k, v, v)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const used = new Map()
|
|
170
|
+
|
|
171
|
+
const makeUnique = (base) => {
|
|
172
|
+
const n = used.get(base) || 0
|
|
173
|
+
used.set(base, n + 1)
|
|
174
|
+
return n == 0 ? base : `${base}-${n}`
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return flat.map((item, i) => {
|
|
178
|
+
const baseId = makeId(item.key ?? item.label ?? i, 'invalid-option', stableId)
|
|
179
|
+
return {
|
|
180
|
+
id: makeUnique(baseId),
|
|
181
|
+
userId: item.userId ?? item.key ?? i,
|
|
182
|
+
name: String(item.label),
|
|
183
|
+
raw: item.value,
|
|
184
|
+
original: item.original,
|
|
185
|
+
disabled: item.disabled,
|
|
186
|
+
type: typeof item.value === 'boolean' ? 'boolean' : 'normal'
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
}, [options, stableId])
|
|
191
|
+
|
|
192
|
+
const normalizedJsxOptions = useMemo(() => {
|
|
193
|
+
return jsxOptions.map((opt, index) => {
|
|
194
|
+
const uniqueId = `jsx-${stableId.replace(/:/g, '')}-${opt.id}`
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
id: uniqueId,
|
|
198
|
+
userId: opt.id,
|
|
199
|
+
value: opt.value,
|
|
200
|
+
raw: opt.value,
|
|
201
|
+
original: opt.value,
|
|
202
|
+
name: opt.label,
|
|
203
|
+
jsx: opt.jsx,
|
|
204
|
+
disabled: opt.disabled,
|
|
205
|
+
className: opt.className,
|
|
206
|
+
type: typeof opt.value === 'boolean' ? 'boolean' : 'normal'
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
}, [jsxOptions, stableId])
|
|
210
|
+
|
|
211
|
+
const normalizedOptions = useMemo(() => {
|
|
212
|
+
return [...normalizedPropOptions, ...normalizedJsxOptions]
|
|
213
|
+
}, [normalizedPropOptions, normalizedJsxOptions])
|
|
214
|
+
|
|
215
|
+
// option availability status
|
|
216
|
+
const hasOptions = normalizedOptions.length > 0
|
|
217
|
+
|
|
218
|
+
// select activity status
|
|
219
|
+
const active = useMemo(() => (
|
|
220
|
+
!error && !loading && !disabled && hasOptions
|
|
221
|
+
), [error, loading, disabled, hasOptions])
|
|
222
|
+
|
|
223
|
+
const selected = useMemo(() => {
|
|
224
|
+
const current = isControlled ? value : internalValue
|
|
225
|
+
if (current === undefined || current === null) return null
|
|
226
|
+
|
|
227
|
+
return normalizedOptions.find(o =>
|
|
228
|
+
o.id === String(current) ||
|
|
229
|
+
o.userId === String(current) ||
|
|
230
|
+
o.raw === current ||
|
|
231
|
+
o.original === current ||
|
|
232
|
+
(typeof current === 'object' && current !== null && o.id === String(current.id || current.value))
|
|
233
|
+
) ?? null
|
|
234
|
+
}, [isControlled, value, internalValue, normalizedOptions])
|
|
235
|
+
|
|
236
|
+
// option selection function
|
|
237
|
+
const selectOption = useCallback((option, e) => {
|
|
238
|
+
if (option.disabled) {
|
|
239
|
+
e.stopPropagation()
|
|
240
|
+
e.preventDefault()
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!isControlled) {
|
|
245
|
+
setInternalValue(option?.original)
|
|
246
|
+
}
|
|
247
|
+
onChange?.(option?.original, option?.userId)
|
|
248
|
+
|
|
249
|
+
setVisibility(false)
|
|
250
|
+
}, [isControlled, onChange, setVisibility])
|
|
251
|
+
|
|
252
|
+
// clear function of selected option
|
|
253
|
+
const clear = useCallback((e) => {
|
|
254
|
+
e.preventDefault()
|
|
255
|
+
e.stopPropagation()
|
|
256
|
+
if (!isControlled) {
|
|
257
|
+
setInternalValue(null)
|
|
258
|
+
}
|
|
259
|
+
onChange?.(null, null)
|
|
260
|
+
}, [isControlled, onChange])
|
|
261
|
+
|
|
262
|
+
return {normalizedOptions, selected, selectOption, clear, hasOptions, active, selectedValue, placeholder, emptyText, disabledText, loadingText, errorText, disabledOption, emptyOption, invalidOption, disabled, loading, error}
|
|
242
263
|
}
|