@brightlocal/ui-components 0.2.0 → 0.3.1
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/dist/alert.cjs +1 -1
- package/dist/alert.d.ts +10 -1
- package/dist/alert.d.ts.map +1 -1
- package/dist/alert.js +6 -2
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +6 -2
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +89 -73
- package/dist/centred-layout.cjs +1 -1
- package/dist/centred-layout.d.ts +49 -25
- package/dist/centred-layout.d.ts.map +1 -1
- package/dist/centred-layout.js +57 -32
- package/dist/checkbox.d.ts.map +1 -1
- package/dist/chip.cjs +1 -0
- package/dist/chip.d.ts +61 -0
- package/dist/chip.d.ts.map +1 -0
- package/dist/chip.js +165 -0
- package/dist/combobox.cjs +1 -1
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +5 -2
- package/dist/date-picker.cjs +1 -1
- package/dist/date-picker.d.ts +89 -52
- package/dist/date-picker.d.ts.map +1 -1
- package/dist/date-picker.js +228 -210
- package/dist/dropdown-menu.d.ts.map +1 -1
- package/dist/header.cjs +1 -0
- package/dist/header.d.ts +42 -0
- package/dist/header.d.ts.map +1 -0
- package/dist/header.js +22 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +426 -396
- package/dist/input-chip.cjs +1 -0
- package/dist/input-chip.d.ts +117 -0
- package/dist/input-chip.d.ts.map +1 -0
- package/dist/input-chip.js +284 -0
- package/dist/input-group.d.ts.map +1 -1
- package/dist/input-otp.cjs +1 -1
- package/dist/input-otp.d.ts +43 -36
- package/dist/input-otp.d.ts.map +1 -1
- package/dist/input-otp.js +140 -119
- package/dist/input-password.cjs +1 -1
- package/dist/input-password.d.ts +147 -19
- package/dist/input-password.d.ts.map +1 -1
- package/dist/input-password.js +197 -92
- package/dist/input.cjs +1 -1
- package/dist/input.js +1 -1
- package/dist/progress.cjs +1 -1
- package/dist/progress.d.ts +10 -0
- package/dist/progress.d.ts.map +1 -1
- package/dist/progress.js +45 -36
- package/dist/select.d.ts.map +1 -1
- package/dist/slider.cjs +1 -1
- package/dist/slider.d.ts +31 -3
- package/dist/slider.d.ts.map +1 -1
- package/dist/slider.js +53 -43
- package/dist/textarea.d.ts.map +1 -1
- package/package.json +23 -24
- package/src/components/alert.tsx +10 -1
- package/src/components/card.tsx +66 -38
- package/src/components/centred-layout.tsx +115 -38
- package/src/components/checkbox.tsx +2 -2
- package/src/components/chip.tsx +249 -0
- package/src/components/combobox.tsx +17 -13
- package/src/components/date-picker.tsx +410 -266
- package/src/components/dropdown-menu.tsx +6 -1
- package/src/components/header.tsx +69 -0
- package/src/components/input-chip.tsx +548 -0
- package/src/components/input-group.tsx +4 -1
- package/src/components/input-otp.tsx +159 -91
- package/src/components/input-password.tsx +469 -129
- package/src/components/input.tsx +1 -1
- package/src/components/progress.tsx +21 -2
- package/src/components/select.tsx +1 -3
- package/src/components/slider.tsx +85 -15
- package/src/components/textarea.tsx +4 -1
- package/src/index.ts +3 -0
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
Check,
|
|
7
|
+
ChevronRight,
|
|
8
|
+
Circle,
|
|
9
|
+
ChevronsUpDown,
|
|
10
|
+
} from "@brightlocal/icons";
|
|
6
11
|
|
|
7
12
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
8
13
|
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Header provides a semantic header container for top-level page navigation.
|
|
7
|
+
*
|
|
8
|
+
* It serves as a flexible wrapper for brand elements (Logo) and navigation
|
|
9
|
+
* components (NavigationMenu, links, etc.). The component uses flex layout
|
|
10
|
+
* for content alignment and can be customized via className.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Basic usage with Logo
|
|
14
|
+
* <Header dataHook="main-header">
|
|
15
|
+
* <Logo dataHook="header-logo" />
|
|
16
|
+
* </Header>
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // With navigation (future use)
|
|
20
|
+
* <Header dataHook="main-header">
|
|
21
|
+
* <Logo dataHook="header-logo" />
|
|
22
|
+
* <NavigationMenu dataHook="header-nav" />
|
|
23
|
+
* </Header>
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
interface HeaderProps extends React.ComponentProps<"header"> {
|
|
27
|
+
/**
|
|
28
|
+
* Data hook for automated testing (required)
|
|
29
|
+
*/
|
|
30
|
+
dataHook: string;
|
|
31
|
+
/**
|
|
32
|
+
* Tracking element identifier for analytics
|
|
33
|
+
*/
|
|
34
|
+
trackingEl?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Tracking label for analytics context
|
|
37
|
+
*/
|
|
38
|
+
trackingLabel?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Header component for top-level page navigation and branding.
|
|
43
|
+
*/
|
|
44
|
+
const Header = React.forwardRef<HTMLElement, HeaderProps>(
|
|
45
|
+
(
|
|
46
|
+
{ className, dataHook, trackingEl, trackingLabel, children, ...props },
|
|
47
|
+
ref
|
|
48
|
+
) => {
|
|
49
|
+
return (
|
|
50
|
+
<header
|
|
51
|
+
ref={ref}
|
|
52
|
+
className={cn("flex items-center", className)}
|
|
53
|
+
data-hook={dataHook}
|
|
54
|
+
data-slot="header"
|
|
55
|
+
data-tracking-el={trackingEl}
|
|
56
|
+
data-tracking-label={trackingLabel}
|
|
57
|
+
{...props}
|
|
58
|
+
>
|
|
59
|
+
{children}
|
|
60
|
+
</header>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
Header.displayName = "Header";
|
|
66
|
+
|
|
67
|
+
export { Header };
|
|
68
|
+
|
|
69
|
+
export type { HeaderProps };
|
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
5
|
+
|
|
6
|
+
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
7
|
+
import { Chip } from "@brightlocal/ui-components/chip";
|
|
8
|
+
|
|
9
|
+
/* -------------------------------------------------------------------------------------------------
|
|
10
|
+
* InputChip Context
|
|
11
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
12
|
+
|
|
13
|
+
interface InputChipContextValue {
|
|
14
|
+
value: string[];
|
|
15
|
+
onValueChange: (value: string[]) => void;
|
|
16
|
+
inputValue: string;
|
|
17
|
+
setInputValue: (value: string) => void;
|
|
18
|
+
disabled: boolean;
|
|
19
|
+
addItem: (item: string) => void;
|
|
20
|
+
removeItem: (index: number) => void;
|
|
21
|
+
/** Ref to the input element for focus management */
|
|
22
|
+
inputRef: React.RefObject<HTMLInputElement | null>;
|
|
23
|
+
/** Index of the currently focused chip (-1 means input is focused) */
|
|
24
|
+
focusedChipIndex: number;
|
|
25
|
+
/** Set the focused chip index */
|
|
26
|
+
setFocusedChipIndex: (index: number) => void;
|
|
27
|
+
/** Refs for chip elements for programmatic focus */
|
|
28
|
+
chipRefs: React.RefObject<(HTMLDivElement | null)[]>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const InputChipContext = React.createContext<InputChipContextValue | null>(
|
|
32
|
+
null
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
function useInputChipContext() {
|
|
36
|
+
const context = React.useContext(InputChipContext);
|
|
37
|
+
if (!context) {
|
|
38
|
+
throw new Error("InputChip components must be used within an <InputChip>");
|
|
39
|
+
}
|
|
40
|
+
return context;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* -------------------------------------------------------------------------------------------------
|
|
44
|
+
* InputChip (Context provider)
|
|
45
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
46
|
+
|
|
47
|
+
export interface InputChipProps {
|
|
48
|
+
/** Controlled value - array of chip values */
|
|
49
|
+
value?: string[];
|
|
50
|
+
/** Default value for uncontrolled mode */
|
|
51
|
+
defaultValue?: string[];
|
|
52
|
+
/** Callback when value changes */
|
|
53
|
+
onValueChange?: (value: string[]) => void;
|
|
54
|
+
/** Disabled state */
|
|
55
|
+
disabled?: boolean;
|
|
56
|
+
/** Allow duplicate values (default: false) */
|
|
57
|
+
allowDuplicates?: boolean;
|
|
58
|
+
/** Children */
|
|
59
|
+
children: React.ReactNode;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* InputChip context provider.
|
|
64
|
+
* Manages state for chip values and input.
|
|
65
|
+
*
|
|
66
|
+
* @example Chips inside input
|
|
67
|
+
* ```tsx
|
|
68
|
+
* <InputChip defaultValue={["React", "TypeScript"]}>
|
|
69
|
+
* <InputChipInput dataHook="tags" placeholder="Add...">
|
|
70
|
+
* <InputChipItems />
|
|
71
|
+
* </InputChipInput>
|
|
72
|
+
* </InputChip>
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example Chips outside input
|
|
76
|
+
* ```tsx
|
|
77
|
+
* <InputChip defaultValue={["React", "TypeScript"]}>
|
|
78
|
+
* <InputChipInput dataHook="tags" placeholder="Add..." />
|
|
79
|
+
* <InputChipItems />
|
|
80
|
+
* </InputChip>
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
function InputChip({
|
|
84
|
+
value: controlledValue,
|
|
85
|
+
defaultValue = [],
|
|
86
|
+
onValueChange: controlledOnValueChange,
|
|
87
|
+
disabled = false,
|
|
88
|
+
allowDuplicates = false,
|
|
89
|
+
children,
|
|
90
|
+
}: InputChipProps) {
|
|
91
|
+
const [uncontrolledValue, setUncontrolledValue] =
|
|
92
|
+
React.useState<string[]>(defaultValue);
|
|
93
|
+
const [inputValue, setInputValue] = React.useState("");
|
|
94
|
+
const [focusedChipIndex, setFocusedChipIndex] = React.useState(-1);
|
|
95
|
+
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
96
|
+
const chipRefs = React.useRef<(HTMLDivElement | null)[]>([]);
|
|
97
|
+
|
|
98
|
+
const isControlled = controlledValue !== undefined;
|
|
99
|
+
const value = isControlled ? controlledValue : uncontrolledValue;
|
|
100
|
+
|
|
101
|
+
// Cleanup when chips are removed: trim refs and reset stale focus index
|
|
102
|
+
React.useEffect(() => {
|
|
103
|
+
if (chipRefs.current.length > value.length) {
|
|
104
|
+
chipRefs.current.length = value.length;
|
|
105
|
+
}
|
|
106
|
+
// Reset focus index if it's out of bounds (e.g., chip was removed externally)
|
|
107
|
+
if (focusedChipIndex >= value.length) {
|
|
108
|
+
setFocusedChipIndex(-1);
|
|
109
|
+
}
|
|
110
|
+
}, [value.length, focusedChipIndex]);
|
|
111
|
+
|
|
112
|
+
const onValueChange = React.useCallback(
|
|
113
|
+
(newValue: string[]) => {
|
|
114
|
+
if (!isControlled) {
|
|
115
|
+
setUncontrolledValue(newValue);
|
|
116
|
+
}
|
|
117
|
+
controlledOnValueChange?.(newValue);
|
|
118
|
+
},
|
|
119
|
+
[isControlled, controlledOnValueChange]
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const addItem = React.useCallback(
|
|
123
|
+
(item: string) => {
|
|
124
|
+
if (disabled) return;
|
|
125
|
+
const trimmed = item.trim();
|
|
126
|
+
if (trimmed && (allowDuplicates || !value.includes(trimmed))) {
|
|
127
|
+
onValueChange([...value, trimmed]);
|
|
128
|
+
setInputValue("");
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
[value, onValueChange, allowDuplicates, disabled]
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const removeItem = React.useCallback(
|
|
135
|
+
(index: number) => {
|
|
136
|
+
if (disabled) return;
|
|
137
|
+
onValueChange(value.filter((_, i) => i !== index));
|
|
138
|
+
},
|
|
139
|
+
[value, onValueChange, disabled]
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const contextValue = React.useMemo<InputChipContextValue>(
|
|
143
|
+
() => ({
|
|
144
|
+
value,
|
|
145
|
+
onValueChange,
|
|
146
|
+
inputValue,
|
|
147
|
+
setInputValue,
|
|
148
|
+
disabled,
|
|
149
|
+
addItem,
|
|
150
|
+
removeItem,
|
|
151
|
+
inputRef,
|
|
152
|
+
focusedChipIndex,
|
|
153
|
+
setFocusedChipIndex,
|
|
154
|
+
chipRefs,
|
|
155
|
+
}),
|
|
156
|
+
[
|
|
157
|
+
value,
|
|
158
|
+
onValueChange,
|
|
159
|
+
inputValue,
|
|
160
|
+
setInputValue,
|
|
161
|
+
disabled,
|
|
162
|
+
addItem,
|
|
163
|
+
removeItem,
|
|
164
|
+
inputRef,
|
|
165
|
+
focusedChipIndex,
|
|
166
|
+
setFocusedChipIndex,
|
|
167
|
+
chipRefs,
|
|
168
|
+
]
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<InputChipContext.Provider value={contextValue}>
|
|
173
|
+
{children}
|
|
174
|
+
</InputChipContext.Provider>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* -------------------------------------------------------------------------------------------------
|
|
179
|
+
* InputChipInput (Visual container with input)
|
|
180
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
181
|
+
|
|
182
|
+
const inputChipInputVariants = cva(
|
|
183
|
+
[
|
|
184
|
+
// Layout
|
|
185
|
+
"relative flex w-full flex-wrap items-center gap-1",
|
|
186
|
+
// Appearance
|
|
187
|
+
"min-h-9 rounded-sm border border-input bg-background",
|
|
188
|
+
"px-3 py-2",
|
|
189
|
+
// Focus-within state (when input inside is focused)
|
|
190
|
+
"has-[:focus-visible]:border-ring has-[:focus-visible]:bg-background",
|
|
191
|
+
"has-[:focus-visible]:shadow-default",
|
|
192
|
+
// Disabled state
|
|
193
|
+
"has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50",
|
|
194
|
+
// Transition
|
|
195
|
+
"transition-[color,box-shadow,border-color]",
|
|
196
|
+
"duration-150",
|
|
197
|
+
"ease-in-out",
|
|
198
|
+
],
|
|
199
|
+
{
|
|
200
|
+
variants: {
|
|
201
|
+
error: {
|
|
202
|
+
true: [
|
|
203
|
+
"border-red-600/40 dark:border-red-400/60",
|
|
204
|
+
"has-[:focus-visible]:border-destructive has-[:focus-visible]:shadow-destructive",
|
|
205
|
+
],
|
|
206
|
+
false: "",
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
defaultVariants: {
|
|
210
|
+
error: false,
|
|
211
|
+
},
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const inputStyles = [
|
|
216
|
+
// Reset and base
|
|
217
|
+
"flex-1 min-w-20",
|
|
218
|
+
"bg-transparent",
|
|
219
|
+
"border-0 outline-none",
|
|
220
|
+
// Typography
|
|
221
|
+
"text-sm font-sans",
|
|
222
|
+
"text-foreground",
|
|
223
|
+
// Placeholder
|
|
224
|
+
"placeholder:text-muted-foreground",
|
|
225
|
+
// Focus - no visible focus ring since container handles it
|
|
226
|
+
"focus:outline-none focus-visible:outline-none",
|
|
227
|
+
// Disabled
|
|
228
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
229
|
+
];
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Props for the InputChipInput component
|
|
233
|
+
*/
|
|
234
|
+
export interface InputChipInputProps
|
|
235
|
+
extends
|
|
236
|
+
Omit<React.HTMLAttributes<HTMLDivElement>, "onChange">,
|
|
237
|
+
VariantProps<typeof inputChipInputVariants> {
|
|
238
|
+
/**
|
|
239
|
+
* Data hook for automated testing (required)
|
|
240
|
+
*/
|
|
241
|
+
dataHook: string;
|
|
242
|
+
/**
|
|
243
|
+
* Tracking element identifier for analytics
|
|
244
|
+
*/
|
|
245
|
+
trackingEl?: string;
|
|
246
|
+
/**
|
|
247
|
+
* Tracking label for analytics context
|
|
248
|
+
*/
|
|
249
|
+
trackingLabel?: string;
|
|
250
|
+
/**
|
|
251
|
+
* Placeholder text for the input
|
|
252
|
+
*/
|
|
253
|
+
placeholder?: string;
|
|
254
|
+
/**
|
|
255
|
+
* Whether the input has an error state
|
|
256
|
+
*/
|
|
257
|
+
error?: boolean;
|
|
258
|
+
/**
|
|
259
|
+
* ID for the input element (for label association)
|
|
260
|
+
*/
|
|
261
|
+
inputId?: string;
|
|
262
|
+
/**
|
|
263
|
+
* Accessible label for the input
|
|
264
|
+
*/
|
|
265
|
+
"aria-label"?: string;
|
|
266
|
+
/**
|
|
267
|
+
* ID of element that labels the input
|
|
268
|
+
*/
|
|
269
|
+
"aria-labelledby"?: string;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Input container with bordered styling.
|
|
274
|
+
* Place Chip components or InputChipItems inside as children.
|
|
275
|
+
*/
|
|
276
|
+
const InputChipInput = React.forwardRef<HTMLDivElement, InputChipInputProps>(
|
|
277
|
+
(
|
|
278
|
+
{
|
|
279
|
+
className,
|
|
280
|
+
error = false,
|
|
281
|
+
dataHook,
|
|
282
|
+
trackingEl,
|
|
283
|
+
trackingLabel,
|
|
284
|
+
placeholder,
|
|
285
|
+
inputId,
|
|
286
|
+
"aria-label": ariaLabel,
|
|
287
|
+
"aria-labelledby": ariaLabelledby,
|
|
288
|
+
children,
|
|
289
|
+
...props
|
|
290
|
+
},
|
|
291
|
+
ref
|
|
292
|
+
) => {
|
|
293
|
+
const {
|
|
294
|
+
value,
|
|
295
|
+
inputValue,
|
|
296
|
+
setInputValue,
|
|
297
|
+
disabled,
|
|
298
|
+
addItem,
|
|
299
|
+
inputRef,
|
|
300
|
+
setFocusedChipIndex,
|
|
301
|
+
chipRefs,
|
|
302
|
+
} = useInputChipContext();
|
|
303
|
+
|
|
304
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
305
|
+
setInputValue(e.target.value);
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
309
|
+
if (e.key === "Enter" && inputValue.trim()) {
|
|
310
|
+
e.preventDefault();
|
|
311
|
+
addItem(inputValue);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Arrow left navigation: move focus to last chip when input is empty
|
|
315
|
+
if (e.key === "ArrowLeft" && !inputValue && value.length > 0) {
|
|
316
|
+
e.preventDefault();
|
|
317
|
+
const lastIndex = value.length - 1;
|
|
318
|
+
setFocusedChipIndex(lastIndex);
|
|
319
|
+
chipRefs.current[lastIndex]?.focus();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Backspace: move focus to last chip when input is empty
|
|
323
|
+
if (e.key === "Backspace" && !inputValue && value.length > 0) {
|
|
324
|
+
e.preventDefault();
|
|
325
|
+
const lastIndex = value.length - 1;
|
|
326
|
+
setFocusedChipIndex(lastIndex);
|
|
327
|
+
chipRefs.current[lastIndex]?.focus();
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<div
|
|
333
|
+
ref={ref}
|
|
334
|
+
className={cn(inputChipInputVariants({ error }), className)}
|
|
335
|
+
data-hook={dataHook}
|
|
336
|
+
data-slot="input-chip"
|
|
337
|
+
data-tracking-el={trackingEl}
|
|
338
|
+
data-tracking-label={trackingLabel}
|
|
339
|
+
{...props}
|
|
340
|
+
>
|
|
341
|
+
{children}
|
|
342
|
+
<input
|
|
343
|
+
ref={inputRef as React.RefObject<HTMLInputElement>}
|
|
344
|
+
aria-label={ariaLabel}
|
|
345
|
+
aria-labelledby={ariaLabelledby}
|
|
346
|
+
className={cn(inputStyles)}
|
|
347
|
+
data-slot="input-chip-input"
|
|
348
|
+
disabled={disabled}
|
|
349
|
+
id={inputId}
|
|
350
|
+
placeholder={placeholder}
|
|
351
|
+
type="text"
|
|
352
|
+
value={inputValue}
|
|
353
|
+
onChange={handleChange}
|
|
354
|
+
onKeyDown={handleKeyDown}
|
|
355
|
+
/>
|
|
356
|
+
</div>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
);
|
|
360
|
+
InputChipInput.displayName = "InputChipInput";
|
|
361
|
+
|
|
362
|
+
/* -------------------------------------------------------------------------------------------------
|
|
363
|
+
* InputChip Items (Auto-renders chips from value)
|
|
364
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
365
|
+
|
|
366
|
+
export interface InputChipItemsProps {
|
|
367
|
+
/** Custom className for the container */
|
|
368
|
+
className?: string;
|
|
369
|
+
/** Data hook prefix for chips */
|
|
370
|
+
dataHookPrefix?: string;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Auto-renders chips from the InputChip value array.
|
|
375
|
+
* Must be used within an InputChip component.
|
|
376
|
+
*/
|
|
377
|
+
function InputChipItems({
|
|
378
|
+
className,
|
|
379
|
+
dataHookPrefix = "chip",
|
|
380
|
+
}: InputChipItemsProps) {
|
|
381
|
+
const {
|
|
382
|
+
value,
|
|
383
|
+
disabled,
|
|
384
|
+
removeItem,
|
|
385
|
+
inputRef,
|
|
386
|
+
setFocusedChipIndex,
|
|
387
|
+
chipRefs,
|
|
388
|
+
} = useInputChipContext();
|
|
389
|
+
|
|
390
|
+
// Track pending focus to apply after DOM updates (avoids race conditions with rapid deletions)
|
|
391
|
+
const pendingFocusRef = React.useRef<number | "input" | null>(null);
|
|
392
|
+
|
|
393
|
+
// Apply pending focus after value changes
|
|
394
|
+
React.useEffect(() => {
|
|
395
|
+
if (pendingFocusRef.current === null) return;
|
|
396
|
+
|
|
397
|
+
if (pendingFocusRef.current === "input") {
|
|
398
|
+
inputRef.current?.focus();
|
|
399
|
+
} else {
|
|
400
|
+
const targetIndex = pendingFocusRef.current;
|
|
401
|
+
// Ensure index is still valid after state update
|
|
402
|
+
if (targetIndex >= 0 && targetIndex < value.length) {
|
|
403
|
+
chipRefs.current[targetIndex]?.focus();
|
|
404
|
+
} else if (value.length > 0) {
|
|
405
|
+
// Fallback to last chip if index is out of bounds
|
|
406
|
+
chipRefs.current[value.length - 1]?.focus();
|
|
407
|
+
} else {
|
|
408
|
+
inputRef.current?.focus();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
pendingFocusRef.current = null;
|
|
412
|
+
}, [value.length, inputRef, chipRefs]);
|
|
413
|
+
|
|
414
|
+
const handleChipKeyDown = (
|
|
415
|
+
e: React.KeyboardEvent<HTMLDivElement>,
|
|
416
|
+
index: number
|
|
417
|
+
) => {
|
|
418
|
+
// Prevent keyboard interaction when disabled
|
|
419
|
+
if (disabled) return;
|
|
420
|
+
|
|
421
|
+
switch (e.key) {
|
|
422
|
+
case "ArrowLeft":
|
|
423
|
+
e.preventDefault();
|
|
424
|
+
if (index > 0) {
|
|
425
|
+
// Move to previous chip
|
|
426
|
+
setFocusedChipIndex(index - 1);
|
|
427
|
+
chipRefs.current[index - 1]?.focus();
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
|
|
431
|
+
case "ArrowRight":
|
|
432
|
+
e.preventDefault();
|
|
433
|
+
if (index < value.length - 1) {
|
|
434
|
+
// Move to next chip
|
|
435
|
+
setFocusedChipIndex(index + 1);
|
|
436
|
+
chipRefs.current[index + 1]?.focus();
|
|
437
|
+
} else {
|
|
438
|
+
// Move to input
|
|
439
|
+
setFocusedChipIndex(-1);
|
|
440
|
+
inputRef.current?.focus();
|
|
441
|
+
}
|
|
442
|
+
break;
|
|
443
|
+
|
|
444
|
+
case "Backspace":
|
|
445
|
+
case "Delete": {
|
|
446
|
+
e.preventDefault();
|
|
447
|
+
// Determine focus target after removal
|
|
448
|
+
const newLength = value.length - 1;
|
|
449
|
+
if (newLength === 0) {
|
|
450
|
+
// No chips left, focus input
|
|
451
|
+
setFocusedChipIndex(-1);
|
|
452
|
+
pendingFocusRef.current = "input";
|
|
453
|
+
removeItem(index);
|
|
454
|
+
} else if (index >= newLength) {
|
|
455
|
+
// Removed last chip, focus new last chip
|
|
456
|
+
const newIndex = newLength - 1;
|
|
457
|
+
setFocusedChipIndex(newIndex);
|
|
458
|
+
pendingFocusRef.current = newIndex;
|
|
459
|
+
removeItem(index);
|
|
460
|
+
} else {
|
|
461
|
+
// Focus the chip that takes removed chip's position
|
|
462
|
+
setFocusedChipIndex(index);
|
|
463
|
+
pendingFocusRef.current = index;
|
|
464
|
+
removeItem(index);
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
case "Home":
|
|
470
|
+
e.preventDefault();
|
|
471
|
+
if (value.length > 0) {
|
|
472
|
+
setFocusedChipIndex(0);
|
|
473
|
+
chipRefs.current[0]?.focus();
|
|
474
|
+
}
|
|
475
|
+
break;
|
|
476
|
+
|
|
477
|
+
case "End":
|
|
478
|
+
e.preventDefault();
|
|
479
|
+
setFocusedChipIndex(-1);
|
|
480
|
+
inputRef.current?.focus();
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
const handleChipFocus = (index: number) => {
|
|
486
|
+
setFocusedChipIndex(index);
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
const handleChipBlur = (e: React.FocusEvent<HTMLDivElement>) => {
|
|
490
|
+
// Only reset if focus is leaving the chip area entirely
|
|
491
|
+
const relatedTarget = e.relatedTarget as HTMLElement | null;
|
|
492
|
+
const isWithinChipArea =
|
|
493
|
+
!!relatedTarget &&
|
|
494
|
+
(relatedTarget.closest('[data-slot="chip"]') !== null ||
|
|
495
|
+
relatedTarget.dataset.slot === "input-chip-input");
|
|
496
|
+
|
|
497
|
+
if (!isWithinChipArea) {
|
|
498
|
+
setFocusedChipIndex(-1);
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
if (value.length === 0) {
|
|
503
|
+
return null;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return (
|
|
507
|
+
<div
|
|
508
|
+
className={cn("contents", className)}
|
|
509
|
+
data-slot="input-chip-items"
|
|
510
|
+
>
|
|
511
|
+
{value.map((item, index) => (
|
|
512
|
+
<Chip
|
|
513
|
+
key={`${item}-${index}`}
|
|
514
|
+
ref={(el) => {
|
|
515
|
+
chipRefs.current[index] = el;
|
|
516
|
+
}}
|
|
517
|
+
aria-label={`${item}, press Delete to remove`}
|
|
518
|
+
dataHook={`${dataHookPrefix}-${index}`}
|
|
519
|
+
disabled={disabled}
|
|
520
|
+
role="group"
|
|
521
|
+
tabIndex={-1}
|
|
522
|
+
onBlur={handleChipBlur}
|
|
523
|
+
onFocus={() => handleChipFocus(index)}
|
|
524
|
+
onKeyDown={(e) => handleChipKeyDown(e, index)}
|
|
525
|
+
onRemove={() => removeItem(index)}
|
|
526
|
+
>
|
|
527
|
+
{item}
|
|
528
|
+
</Chip>
|
|
529
|
+
))}
|
|
530
|
+
</div>
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/* -------------------------------------------------------------------------------------------------
|
|
535
|
+
* Exports
|
|
536
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
537
|
+
|
|
538
|
+
// Keep for backwards compatibility
|
|
539
|
+
const inputChipVariants = inputChipInputVariants;
|
|
540
|
+
|
|
541
|
+
export {
|
|
542
|
+
InputChip,
|
|
543
|
+
InputChipInput,
|
|
544
|
+
InputChipItems,
|
|
545
|
+
inputChipVariants,
|
|
546
|
+
inputChipInputVariants,
|
|
547
|
+
useInputChipContext,
|
|
548
|
+
};
|
|
@@ -54,7 +54,10 @@ interface InputGroupProps
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const InputGroup = React.forwardRef<HTMLDivElement, InputGroupProps>(
|
|
57
|
-
(
|
|
57
|
+
(
|
|
58
|
+
{ className, size, error, dataHook, trackingEl, trackingLabel, ...props },
|
|
59
|
+
ref
|
|
60
|
+
) => {
|
|
58
61
|
return (
|
|
59
62
|
<div
|
|
60
63
|
ref={ref}
|