@brightlocal/ui-components 1.2.3 → 1.2.4
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/component-meta.json +474 -112
- package/dist/blocks/input-list.cjs +2 -2
- package/dist/blocks/input-list.d.ts.map +1 -1
- package/dist/blocks/input-list.js +142 -166
- package/package.json +1 -1
- package/src/blocks/input-list.tsx +94 -124
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
|
+
import { motion, AnimatePresence } from "framer-motion";
|
|
4
5
|
import { X } from "@brightlocal/icons";
|
|
5
6
|
|
|
6
7
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
@@ -119,45 +120,31 @@ function InputList({
|
|
|
119
120
|
className,
|
|
120
121
|
}: InputListProps) {
|
|
121
122
|
const [inputValue, setInputValue] = React.useState("");
|
|
122
|
-
const [removingIndices, setRemovingIndices] = React.useState<Set<number>>(
|
|
123
|
-
() => new Set()
|
|
124
|
-
);
|
|
125
|
-
const [addingIndices, setAddingIndices] = React.useState<Set<number>>(
|
|
126
|
-
() => new Set()
|
|
127
|
-
);
|
|
128
123
|
|
|
129
|
-
//
|
|
130
|
-
React.useEffect(() => {
|
|
131
|
-
if (addingIndices.size === 0) return;
|
|
132
|
-
const frame = requestAnimationFrame(() => {
|
|
133
|
-
setAddingIndices(new Set());
|
|
134
|
-
});
|
|
135
|
-
return () => cancelAnimationFrame(frame);
|
|
136
|
-
}, [addingIndices]);
|
|
137
|
-
const valueRef = React.useRef(value);
|
|
138
|
-
React.useEffect(() => {
|
|
139
|
-
valueRef.current = value;
|
|
140
|
-
}, [value]);
|
|
141
|
-
const timersRef = React.useRef(new Set<ReturnType<typeof setTimeout>>());
|
|
142
|
-
React.useEffect(() => {
|
|
143
|
-
const timers = timersRef.current;
|
|
144
|
-
return () => {
|
|
145
|
-
timers.forEach(clearTimeout);
|
|
146
|
-
timers.clear();
|
|
147
|
-
};
|
|
148
|
-
}, []);
|
|
149
|
-
|
|
150
|
-
// Stable keys so remaining items don't remount (and re-animate) on removal
|
|
124
|
+
// Stable keys so AnimatePresence can track items across additions/removals
|
|
151
125
|
const nextIdRef = React.useRef(value.length);
|
|
152
126
|
const [stableKeys, setStableKeys] = React.useState<number[]>(() =>
|
|
153
127
|
value.map((_, i) => i)
|
|
154
128
|
);
|
|
129
|
+
|
|
130
|
+
// Sync keys when value changes externally (not via addItems/removeItem).
|
|
131
|
+
// Simple appends preserve existing keys; other changes (removal, reorder)
|
|
132
|
+
// regenerate all keys to avoid incorrect exit animations.
|
|
155
133
|
React.useEffect(() => {
|
|
156
134
|
setStableKeys((prev) => {
|
|
157
|
-
if (prev.length
|
|
158
|
-
|
|
135
|
+
if (prev.length === value.length) return prev;
|
|
136
|
+
if (value.length > prev.length) {
|
|
137
|
+
// Append: preserve existing keys, generate new ones
|
|
138
|
+
return [
|
|
139
|
+
...prev,
|
|
140
|
+
...Array.from(
|
|
141
|
+
{ length: value.length - prev.length },
|
|
142
|
+
() => nextIdRef.current++
|
|
143
|
+
),
|
|
144
|
+
];
|
|
159
145
|
}
|
|
160
|
-
|
|
146
|
+
// Removal or reorder: regenerate all keys
|
|
147
|
+
return value.map(() => nextIdRef.current++);
|
|
161
148
|
});
|
|
162
149
|
}, [value]);
|
|
163
150
|
|
|
@@ -173,13 +160,6 @@ function InputList({
|
|
|
173
160
|
maxItems !== undefined ? maxItems - value.length : Infinity;
|
|
174
161
|
const toAdd = trimmed.slice(0, available);
|
|
175
162
|
if (toAdd.length) {
|
|
176
|
-
const prefersReducedMotion = window.matchMedia(
|
|
177
|
-
"(prefers-reduced-motion: reduce)"
|
|
178
|
-
).matches;
|
|
179
|
-
if (!prefersReducedMotion) {
|
|
180
|
-
const startIndex = value.length;
|
|
181
|
-
setAddingIndices(new Set(toAdd.map((_, i) => startIndex + i)));
|
|
182
|
-
}
|
|
183
163
|
setStableKeys((prev) => [
|
|
184
164
|
...prev,
|
|
185
165
|
...toAdd.map(() => nextIdRef.current++),
|
|
@@ -193,30 +173,10 @@ function InputList({
|
|
|
193
173
|
|
|
194
174
|
const removeItem = React.useCallback(
|
|
195
175
|
(index: number) => {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
).matches;
|
|
199
|
-
|
|
200
|
-
if (prefersReducedMotion) {
|
|
201
|
-
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
202
|
-
onValueChange(valueRef.current.filter((_, i) => i !== index));
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
setRemovingIndices((prev) => new Set(prev).add(index));
|
|
207
|
-
const timer = setTimeout(() => {
|
|
208
|
-
timersRef.current.delete(timer);
|
|
209
|
-
setRemovingIndices((prev) => {
|
|
210
|
-
const next = new Set(prev);
|
|
211
|
-
next.delete(index);
|
|
212
|
-
return next;
|
|
213
|
-
});
|
|
214
|
-
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
215
|
-
onValueChange(valueRef.current.filter((_, i) => i !== index));
|
|
216
|
-
}, 200);
|
|
217
|
-
timersRef.current.add(timer);
|
|
176
|
+
setStableKeys((prev) => prev.filter((_, i) => i !== index));
|
|
177
|
+
onValueChange(value.filter((_, i) => i !== index));
|
|
218
178
|
},
|
|
219
|
-
[onValueChange]
|
|
179
|
+
[value, onValueChange]
|
|
220
180
|
);
|
|
221
181
|
|
|
222
182
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
@@ -252,55 +212,63 @@ function InputList({
|
|
|
252
212
|
</ul>
|
|
253
213
|
) : value.length > 0 ? (
|
|
254
214
|
<ul
|
|
255
|
-
className="flex flex-col"
|
|
215
|
+
className="flex flex-col gap-2"
|
|
256
216
|
data-hook={`${dataHook}-list`}
|
|
257
217
|
data-slot="list"
|
|
258
218
|
>
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const isCollapsed = isAdding || isRemoving;
|
|
263
|
-
return (
|
|
264
|
-
<li
|
|
219
|
+
<AnimatePresence initial={false}>
|
|
220
|
+
{value.map((item, index) => (
|
|
221
|
+
<motion.li
|
|
265
222
|
key={stableKeys[index]}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
isCollapsed
|
|
269
|
-
? "mb-0 grid-rows-[0fr] opacity-0"
|
|
270
|
-
: "mb-2 grid-rows-[1fr] opacity-100 last:mb-0",
|
|
271
|
-
isRemoving ? "duration-200" : "duration-300"
|
|
272
|
-
)}
|
|
223
|
+
layout
|
|
224
|
+
animate={{ opacity: 1, scale: 1 }}
|
|
273
225
|
data-hook={`${dataHook}-item-${index}`}
|
|
274
226
|
data-slot="item"
|
|
227
|
+
exit={{
|
|
228
|
+
opacity: 0,
|
|
229
|
+
scale: 0.8,
|
|
230
|
+
transition: {
|
|
231
|
+
opacity: { duration: 0.15 },
|
|
232
|
+
scale: { duration: 0.12 },
|
|
233
|
+
},
|
|
234
|
+
}}
|
|
235
|
+
initial={{ opacity: 0, scale: 0.8 }}
|
|
236
|
+
transition={{
|
|
237
|
+
opacity: { duration: 0.15 },
|
|
238
|
+
scale: { duration: 0.15 },
|
|
239
|
+
layout: {
|
|
240
|
+
type: "spring",
|
|
241
|
+
stiffness: 500,
|
|
242
|
+
damping: 35,
|
|
243
|
+
},
|
|
244
|
+
}}
|
|
275
245
|
>
|
|
276
|
-
<div className="
|
|
277
|
-
<
|
|
278
|
-
<
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
<
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
</ItemActions>
|
|
299
|
-
</div>
|
|
246
|
+
<div className="bg-card flex items-center gap-4 rounded-lg py-2 pr-2 pl-4">
|
|
247
|
+
<ItemContent>
|
|
248
|
+
<ItemTitle>{item}</ItemTitle>
|
|
249
|
+
</ItemContent>
|
|
250
|
+
<ItemActions>
|
|
251
|
+
<button
|
|
252
|
+
aria-label={`Remove ${item}`}
|
|
253
|
+
className={cn(
|
|
254
|
+
"inline-flex size-9 cursor-pointer items-center justify-center rounded-full",
|
|
255
|
+
"text-muted-foreground",
|
|
256
|
+
"focus-visible:shadow-default outline-none",
|
|
257
|
+
"transition-colors duration-150 ease-in-out",
|
|
258
|
+
"disabled:pointer-events-none disabled:opacity-50"
|
|
259
|
+
)}
|
|
260
|
+
data-hook={`${dataHook}-remove-${index}`}
|
|
261
|
+
disabled={disabled}
|
|
262
|
+
type="button"
|
|
263
|
+
onClick={() => removeItem(index)}
|
|
264
|
+
>
|
|
265
|
+
<X />
|
|
266
|
+
</button>
|
|
267
|
+
</ItemActions>
|
|
300
268
|
</div>
|
|
301
|
-
</li>
|
|
302
|
-
)
|
|
303
|
-
|
|
269
|
+
</motion.li>
|
|
270
|
+
))}
|
|
271
|
+
</AnimatePresence>
|
|
304
272
|
</ul>
|
|
305
273
|
) : null}
|
|
306
274
|
|
|
@@ -327,34 +295,36 @@ function InputList({
|
|
|
327
295
|
</Button>
|
|
328
296
|
</div>
|
|
329
297
|
|
|
330
|
-
{/*
|
|
331
|
-
{
|
|
332
|
-
<FieldDescription
|
|
333
|
-
data-slot="input-list-description"
|
|
334
|
-
dataHook={`${dataHook}-description`}
|
|
335
|
-
>
|
|
336
|
-
{description}
|
|
337
|
-
</FieldDescription>
|
|
338
|
-
)}
|
|
339
|
-
|
|
340
|
-
{/* Count indicator — always shown when maxItems is set */}
|
|
341
|
-
{maxItems !== undefined && (
|
|
342
|
-
<FieldDescription
|
|
343
|
-
data-slot="input-list-count"
|
|
344
|
-
dataHook={`${dataHook}-count`}
|
|
345
|
-
>
|
|
346
|
-
{value.length}/{maxItems}
|
|
347
|
-
</FieldDescription>
|
|
348
|
-
)}
|
|
349
|
-
|
|
350
|
-
{/* Error message */}
|
|
351
|
-
{error && errorMessage && (
|
|
298
|
+
{/* Error message replaces description and counter */}
|
|
299
|
+
{error && errorMessage ? (
|
|
352
300
|
<FieldError
|
|
353
301
|
data-slot="input-list-error"
|
|
354
302
|
dataHook={`${dataHook}-error`}
|
|
355
303
|
>
|
|
356
304
|
{errorMessage}
|
|
357
305
|
</FieldError>
|
|
306
|
+
) : (
|
|
307
|
+
<>
|
|
308
|
+
{/* Description */}
|
|
309
|
+
{description && (
|
|
310
|
+
<FieldDescription
|
|
311
|
+
data-slot="input-list-description"
|
|
312
|
+
dataHook={`${dataHook}-description`}
|
|
313
|
+
>
|
|
314
|
+
{description}
|
|
315
|
+
</FieldDescription>
|
|
316
|
+
)}
|
|
317
|
+
|
|
318
|
+
{/* Count indicator — always shown when maxItems is set */}
|
|
319
|
+
{maxItems !== undefined && (
|
|
320
|
+
<FieldDescription
|
|
321
|
+
data-slot="input-list-count"
|
|
322
|
+
dataHook={`${dataHook}-count`}
|
|
323
|
+
>
|
|
324
|
+
{value.length}/{maxItems}
|
|
325
|
+
</FieldDescription>
|
|
326
|
+
)}
|
|
327
|
+
</>
|
|
358
328
|
)}
|
|
359
329
|
</div>
|
|
360
330
|
</div>
|