@diceui/tags-input 0.0.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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/index.cjs +684 -0
- package/dist/index.d.cts +126 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.js +651 -0
- package/package.json +62 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var reactPrimitive = require('@radix-ui/react-primitive');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var shared = require('@diceui/shared');
|
|
6
|
+
|
|
7
|
+
function _interopNamespace(e) {
|
|
8
|
+
if (e && e.__esModule) return e;
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
26
|
+
|
|
27
|
+
var ROOT_NAME = "TagsInputRoot";
|
|
28
|
+
var [TagsInputProvider, useTagsInput] = shared.createContext(ROOT_NAME);
|
|
29
|
+
var TagsInputRoot = React__namespace.forwardRef((props, ref) => {
|
|
30
|
+
const {
|
|
31
|
+
value: valueProp,
|
|
32
|
+
defaultValue,
|
|
33
|
+
onValueChange,
|
|
34
|
+
onValidate,
|
|
35
|
+
onInvalid,
|
|
36
|
+
displayValue = (value2) => value2.toString(),
|
|
37
|
+
addOnPaste = false,
|
|
38
|
+
addOnTab = false,
|
|
39
|
+
disabled = false,
|
|
40
|
+
editable = false,
|
|
41
|
+
loop = false,
|
|
42
|
+
blurBehavior,
|
|
43
|
+
delimiter = ",",
|
|
44
|
+
max = Number.POSITIVE_INFINITY,
|
|
45
|
+
required = false,
|
|
46
|
+
name,
|
|
47
|
+
children,
|
|
48
|
+
dir: dirProp,
|
|
49
|
+
id: idProp,
|
|
50
|
+
...rootProps
|
|
51
|
+
} = props;
|
|
52
|
+
const [value = [], setValue] = shared.useControllableState({
|
|
53
|
+
prop: valueProp,
|
|
54
|
+
defaultProp: defaultValue,
|
|
55
|
+
onChange: onValueChange
|
|
56
|
+
});
|
|
57
|
+
const [highlightedValue, setHighlightedValue] = React__namespace.useState(null);
|
|
58
|
+
const [editingValue, setEditingValue] = React__namespace.useState(
|
|
59
|
+
null
|
|
60
|
+
);
|
|
61
|
+
const [isInvalidInput, setIsInvalidInput] = React__namespace.useState(false);
|
|
62
|
+
const collectionRef = React__namespace.useRef(null);
|
|
63
|
+
const inputRef = React__namespace.useRef(null);
|
|
64
|
+
const id = shared.useId();
|
|
65
|
+
const inputId = shared.useId();
|
|
66
|
+
const labelId = shared.useId();
|
|
67
|
+
const dir = shared.useDirection(dirProp);
|
|
68
|
+
const { getEnabledItems } = shared.useCollection({
|
|
69
|
+
ref: collectionRef
|
|
70
|
+
});
|
|
71
|
+
const { isFormControl, onTriggerChange } = shared.useFormControl();
|
|
72
|
+
const composedRef = shared.useComposedRefs(
|
|
73
|
+
ref,
|
|
74
|
+
collectionRef,
|
|
75
|
+
(node) => onTriggerChange(node)
|
|
76
|
+
);
|
|
77
|
+
const onItemAdd = React__namespace.useCallback(
|
|
78
|
+
(textValue, options) => {
|
|
79
|
+
if (addOnPaste && options?.viaPaste) {
|
|
80
|
+
const splitValues = textValue.split(delimiter).map((v) => v.trim()).filter(Boolean);
|
|
81
|
+
if (value.length + splitValues.length > max && max > 0) {
|
|
82
|
+
onInvalid?.(textValue);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
let newValues2 = [];
|
|
86
|
+
for (const v of splitValues) {
|
|
87
|
+
if (value.includes(v)) {
|
|
88
|
+
onInvalid?.(v);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
newValues2 = [...new Set(splitValues.filter((v) => !value.includes(v)))];
|
|
92
|
+
const validValues = newValues2.filter(
|
|
93
|
+
(v) => !onValidate || onValidate(v)
|
|
94
|
+
);
|
|
95
|
+
if (validValues.length === 0) return false;
|
|
96
|
+
setValue([...value, ...validValues]);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (value.length >= max && max > 0) {
|
|
100
|
+
onInvalid?.(textValue);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const trimmedValue = textValue.trim();
|
|
104
|
+
if (onValidate && !onValidate(trimmedValue)) {
|
|
105
|
+
setIsInvalidInput(true);
|
|
106
|
+
onInvalid?.(trimmedValue);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const exists = value.some((v) => {
|
|
110
|
+
const valueToCompare = v;
|
|
111
|
+
return valueToCompare === trimmedValue;
|
|
112
|
+
});
|
|
113
|
+
if (exists) {
|
|
114
|
+
setIsInvalidInput(true);
|
|
115
|
+
onInvalid?.(trimmedValue);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
const newValue = trimmedValue;
|
|
119
|
+
const newValues = [...value, newValue];
|
|
120
|
+
setValue(newValues);
|
|
121
|
+
setHighlightedValue(null);
|
|
122
|
+
setEditingValue(null);
|
|
123
|
+
setIsInvalidInput(false);
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
[value, max, addOnPaste, delimiter, setValue, onInvalid, onValidate]
|
|
127
|
+
);
|
|
128
|
+
const onItemUpdate = React__namespace.useCallback(
|
|
129
|
+
(index, newTextValue) => {
|
|
130
|
+
if (index !== -1) {
|
|
131
|
+
const trimmedValue = newTextValue.trim();
|
|
132
|
+
const exists = value.some((v, i) => {
|
|
133
|
+
if (i === index) return false;
|
|
134
|
+
const valueToCompare = v;
|
|
135
|
+
return valueToCompare === trimmedValue;
|
|
136
|
+
});
|
|
137
|
+
if (exists) {
|
|
138
|
+
setIsInvalidInput(true);
|
|
139
|
+
onInvalid?.(trimmedValue);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (onValidate && !onValidate(trimmedValue)) {
|
|
143
|
+
setIsInvalidInput(true);
|
|
144
|
+
onInvalid?.(trimmedValue);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const updatedValue = displayValue(trimmedValue);
|
|
148
|
+
const newValues = [...value];
|
|
149
|
+
newValues[index] = updatedValue;
|
|
150
|
+
setValue(newValues);
|
|
151
|
+
setHighlightedValue(updatedValue);
|
|
152
|
+
setEditingValue(null);
|
|
153
|
+
setIsInvalidInput(false);
|
|
154
|
+
requestAnimationFrame(() => inputRef.current?.focus());
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
[value, setValue, displayValue, onInvalid, onValidate]
|
|
158
|
+
);
|
|
159
|
+
const onItemRemove = React__namespace.useCallback(
|
|
160
|
+
(index) => {
|
|
161
|
+
if (index !== -1) {
|
|
162
|
+
const newValues = [...value];
|
|
163
|
+
newValues.splice(index, 1);
|
|
164
|
+
setValue(newValues);
|
|
165
|
+
setHighlightedValue(null);
|
|
166
|
+
setEditingValue(null);
|
|
167
|
+
inputRef.current?.focus();
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
[value, setValue]
|
|
171
|
+
);
|
|
172
|
+
const onItemLeave = React__namespace.useCallback(() => {
|
|
173
|
+
setHighlightedValue(null);
|
|
174
|
+
setEditingValue(null);
|
|
175
|
+
inputRef.current?.focus();
|
|
176
|
+
}, []);
|
|
177
|
+
const onInputKeydown = React__namespace.useCallback(
|
|
178
|
+
(event) => {
|
|
179
|
+
const target = event.target;
|
|
180
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
181
|
+
const isArrowLeft = event.key === "ArrowLeft" && dir === "ltr" || event.key === "ArrowRight" && dir === "rtl";
|
|
182
|
+
const isArrowRight = event.key === "ArrowRight" && dir === "ltr" || event.key === "ArrowLeft" && dir === "rtl";
|
|
183
|
+
if (target.value && target.selectionStart !== 0) {
|
|
184
|
+
setHighlightedValue(null);
|
|
185
|
+
setEditingValue(null);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const findNextEnabledValue = (currentValue, direction) => {
|
|
189
|
+
const collectionElement = collectionRef.current;
|
|
190
|
+
if (!collectionElement) return null;
|
|
191
|
+
const enabledItems = getEnabledItems();
|
|
192
|
+
const enabledValues = enabledItems.map((_, index) => value[index]);
|
|
193
|
+
if (enabledValues.length === 0) return null;
|
|
194
|
+
if (currentValue === null) {
|
|
195
|
+
return direction === "prev" ? enabledValues[enabledValues.length - 1] ?? null : enabledValues[0] ?? null;
|
|
196
|
+
}
|
|
197
|
+
const currentIndex = enabledValues.indexOf(currentValue);
|
|
198
|
+
if (direction === "next") {
|
|
199
|
+
return currentIndex >= enabledValues.length - 1 ? loop ? enabledValues[0] ?? null : null : enabledValues[currentIndex + 1] ?? null;
|
|
200
|
+
}
|
|
201
|
+
return currentIndex <= 0 ? loop ? enabledValues[enabledValues.length - 1] ?? null : null : enabledValues[currentIndex - 1] ?? null;
|
|
202
|
+
};
|
|
203
|
+
switch (event.key) {
|
|
204
|
+
case "Delete":
|
|
205
|
+
case "Backspace": {
|
|
206
|
+
if (target.selectionStart !== 0 || target.selectionEnd !== 0) break;
|
|
207
|
+
if (highlightedValue !== null) {
|
|
208
|
+
const index = value.indexOf(highlightedValue);
|
|
209
|
+
const newValue = findNextEnabledValue(highlightedValue, "prev");
|
|
210
|
+
onItemRemove(index);
|
|
211
|
+
setHighlightedValue(newValue);
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
} else if (event.key === "Backspace" && value.length > 0) {
|
|
214
|
+
const lastValue = findNextEnabledValue(null, "prev");
|
|
215
|
+
setHighlightedValue(lastValue);
|
|
216
|
+
event.preventDefault();
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case "Enter": {
|
|
221
|
+
if (highlightedValue !== null && editable && !disabled) {
|
|
222
|
+
setEditingValue(highlightedValue);
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
case "ArrowLeft":
|
|
229
|
+
case "ArrowRight": {
|
|
230
|
+
if (target.selectionStart === 0 && isArrowLeft && highlightedValue === null && value.length > 0) {
|
|
231
|
+
const lastValue = findNextEnabledValue(null, "prev");
|
|
232
|
+
setHighlightedValue(lastValue);
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
} else if (highlightedValue !== null) {
|
|
235
|
+
const nextValue = findNextEnabledValue(
|
|
236
|
+
highlightedValue,
|
|
237
|
+
isArrowLeft ? "prev" : "next"
|
|
238
|
+
);
|
|
239
|
+
if (nextValue !== null) {
|
|
240
|
+
setHighlightedValue(nextValue);
|
|
241
|
+
event.preventDefault();
|
|
242
|
+
} else if (isArrowRight) {
|
|
243
|
+
setHighlightedValue(null);
|
|
244
|
+
requestAnimationFrame(() => target.setSelectionRange(0, 0));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "Home": {
|
|
250
|
+
if (highlightedValue !== null) {
|
|
251
|
+
const firstValue = findNextEnabledValue(null, "next");
|
|
252
|
+
setHighlightedValue(firstValue);
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
case "End": {
|
|
258
|
+
if (highlightedValue !== null) {
|
|
259
|
+
const lastValue = findNextEnabledValue(null, "prev");
|
|
260
|
+
setHighlightedValue(lastValue);
|
|
261
|
+
event.preventDefault();
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case "Escape": {
|
|
266
|
+
setHighlightedValue(null);
|
|
267
|
+
setEditingValue(null);
|
|
268
|
+
requestAnimationFrame(() => target.setSelectionRange(0, 0));
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
[
|
|
274
|
+
highlightedValue,
|
|
275
|
+
value,
|
|
276
|
+
onItemRemove,
|
|
277
|
+
dir,
|
|
278
|
+
editable,
|
|
279
|
+
disabled,
|
|
280
|
+
loop,
|
|
281
|
+
getEnabledItems
|
|
282
|
+
]
|
|
283
|
+
);
|
|
284
|
+
const getIsClickedInEmptyRoot = React__namespace.useCallback((target) => {
|
|
285
|
+
return collectionRef.current?.contains(target) && !target.hasAttribute(shared.DATA_ITEM_ATTR) && target.tagName !== "INPUT";
|
|
286
|
+
}, []);
|
|
287
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
288
|
+
TagsInputProvider,
|
|
289
|
+
{
|
|
290
|
+
value,
|
|
291
|
+
onValueChange: setValue,
|
|
292
|
+
onItemAdd,
|
|
293
|
+
onItemRemove,
|
|
294
|
+
onItemUpdate,
|
|
295
|
+
onInputKeydown,
|
|
296
|
+
highlightedValue,
|
|
297
|
+
setHighlightedValue,
|
|
298
|
+
editingValue,
|
|
299
|
+
setEditingValue,
|
|
300
|
+
displayValue,
|
|
301
|
+
onItemLeave,
|
|
302
|
+
inputRef,
|
|
303
|
+
isInvalidInput,
|
|
304
|
+
addOnPaste,
|
|
305
|
+
addOnTab,
|
|
306
|
+
disabled,
|
|
307
|
+
editable,
|
|
308
|
+
loop,
|
|
309
|
+
blurBehavior,
|
|
310
|
+
delimiter,
|
|
311
|
+
max,
|
|
312
|
+
dir,
|
|
313
|
+
id,
|
|
314
|
+
inputId,
|
|
315
|
+
labelId
|
|
316
|
+
},
|
|
317
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
318
|
+
reactPrimitive.Primitive.div,
|
|
319
|
+
{
|
|
320
|
+
id,
|
|
321
|
+
"data-invalid": isInvalidInput ? "" : void 0,
|
|
322
|
+
"data-disabled": disabled ? "" : void 0,
|
|
323
|
+
dir,
|
|
324
|
+
...rootProps,
|
|
325
|
+
ref: composedRef,
|
|
326
|
+
onClick: shared.composeEventHandlers(rootProps.onClick, (event) => {
|
|
327
|
+
const target = event.target;
|
|
328
|
+
if (!(target instanceof HTMLElement)) return;
|
|
329
|
+
if (getIsClickedInEmptyRoot(target) && document.activeElement !== inputRef.current) {
|
|
330
|
+
event.currentTarget.focus();
|
|
331
|
+
inputRef.current?.focus();
|
|
332
|
+
}
|
|
333
|
+
}),
|
|
334
|
+
onMouseDown: shared.composeEventHandlers(rootProps.onMouseDown, (event) => {
|
|
335
|
+
const target = event.target;
|
|
336
|
+
if (!(target instanceof HTMLElement)) return;
|
|
337
|
+
if (getIsClickedInEmptyRoot(target)) {
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
}
|
|
340
|
+
}),
|
|
341
|
+
onBlur: shared.composeEventHandlers(rootProps.onBlur, (event) => {
|
|
342
|
+
if (event.relatedTarget !== inputRef.current && !collectionRef.current?.contains(event.relatedTarget)) {
|
|
343
|
+
requestAnimationFrame(() => setHighlightedValue(null));
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
},
|
|
347
|
+
typeof children === "function" ? /* @__PURE__ */ React__namespace.createElement(React__namespace.Fragment, null, children({ value })) : children,
|
|
348
|
+
isFormControl && name && /* @__PURE__ */ React__namespace.createElement(
|
|
349
|
+
shared.BubbleInput,
|
|
350
|
+
{
|
|
351
|
+
type: "hidden",
|
|
352
|
+
control: collectionRef.current,
|
|
353
|
+
name,
|
|
354
|
+
value,
|
|
355
|
+
required,
|
|
356
|
+
disabled
|
|
357
|
+
}
|
|
358
|
+
)
|
|
359
|
+
)
|
|
360
|
+
);
|
|
361
|
+
});
|
|
362
|
+
TagsInputRoot.displayName = ROOT_NAME;
|
|
363
|
+
var Root = TagsInputRoot;
|
|
364
|
+
var LABEL_NAME = "TagsInputLabel";
|
|
365
|
+
var TagsInputLabel = React__namespace.forwardRef(
|
|
366
|
+
(props, ref) => {
|
|
367
|
+
const context = useTagsInput(LABEL_NAME);
|
|
368
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
369
|
+
reactPrimitive.Primitive.label,
|
|
370
|
+
{
|
|
371
|
+
id: context.labelId,
|
|
372
|
+
htmlFor: context.inputId,
|
|
373
|
+
...props,
|
|
374
|
+
ref
|
|
375
|
+
}
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
);
|
|
379
|
+
TagsInputLabel.displayName = LABEL_NAME;
|
|
380
|
+
var Label = TagsInputLabel;
|
|
381
|
+
var ITEM_NAME = "TagsInputItem";
|
|
382
|
+
var [TagsInputItemProvider, useTagsInputItem] = shared.createContext(ITEM_NAME);
|
|
383
|
+
var TagsInputItem = React__namespace.forwardRef(
|
|
384
|
+
(props, ref) => {
|
|
385
|
+
const { value, disabled, ...itemProps } = props;
|
|
386
|
+
const pointerTypeRef = React__namespace.useRef("touch");
|
|
387
|
+
const context = useTagsInput(ITEM_NAME);
|
|
388
|
+
const id = shared.useId();
|
|
389
|
+
const textId = `${id}text`;
|
|
390
|
+
const isHighlighted = value === context.highlightedValue;
|
|
391
|
+
const isEditing = value === context.editingValue;
|
|
392
|
+
const itemDisabled = disabled || context.disabled;
|
|
393
|
+
const displayValue = context.displayValue(value);
|
|
394
|
+
function onSelect() {
|
|
395
|
+
context.setHighlightedValue(value);
|
|
396
|
+
context.inputRef.current?.focus();
|
|
397
|
+
}
|
|
398
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
399
|
+
TagsInputItemProvider,
|
|
400
|
+
{
|
|
401
|
+
id,
|
|
402
|
+
value,
|
|
403
|
+
isHighlighted,
|
|
404
|
+
isEditing,
|
|
405
|
+
disabled: itemDisabled,
|
|
406
|
+
textId,
|
|
407
|
+
displayValue
|
|
408
|
+
},
|
|
409
|
+
/* @__PURE__ */ React__namespace.createElement(
|
|
410
|
+
reactPrimitive.Primitive.div,
|
|
411
|
+
{
|
|
412
|
+
...{ [shared.DATA_ITEM_ATTR]: "" },
|
|
413
|
+
"aria-labelledby": textId,
|
|
414
|
+
"aria-current": isHighlighted,
|
|
415
|
+
"aria-disabled": itemDisabled,
|
|
416
|
+
"data-state": isHighlighted ? "active" : "inactive",
|
|
417
|
+
"data-highlighted": isHighlighted ? "" : void 0,
|
|
418
|
+
"data-editing": isEditing ? "" : void 0,
|
|
419
|
+
"data-editable": context.editable ? "" : void 0,
|
|
420
|
+
"data-disabled": itemDisabled ? "" : void 0,
|
|
421
|
+
id,
|
|
422
|
+
...itemProps,
|
|
423
|
+
ref,
|
|
424
|
+
onClick: shared.composeEventHandlers(itemProps.onClick, (event) => {
|
|
425
|
+
event.stopPropagation();
|
|
426
|
+
if (!isEditing && pointerTypeRef.current !== "mouse") {
|
|
427
|
+
onSelect();
|
|
428
|
+
}
|
|
429
|
+
}),
|
|
430
|
+
onDoubleClick: shared.composeEventHandlers(itemProps.onDoubleClick, () => {
|
|
431
|
+
if (context.editable && !itemDisabled) {
|
|
432
|
+
requestAnimationFrame(() => context.setEditingValue(value));
|
|
433
|
+
}
|
|
434
|
+
}),
|
|
435
|
+
onPointerUp: shared.composeEventHandlers(itemProps.onPointerUp, () => {
|
|
436
|
+
if (pointerTypeRef.current === "mouse") onSelect();
|
|
437
|
+
}),
|
|
438
|
+
onPointerDown: shared.composeEventHandlers(
|
|
439
|
+
itemProps.onPointerDown,
|
|
440
|
+
(event) => pointerTypeRef.current = event.pointerType
|
|
441
|
+
),
|
|
442
|
+
onPointerMove: shared.composeEventHandlers(
|
|
443
|
+
itemProps.onPointerMove,
|
|
444
|
+
(event) => {
|
|
445
|
+
pointerTypeRef.current = event.pointerType;
|
|
446
|
+
if (disabled) {
|
|
447
|
+
context.onItemLeave();
|
|
448
|
+
} else if (pointerTypeRef.current === "mouse") {
|
|
449
|
+
event.currentTarget.focus({ preventScroll: true });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
),
|
|
453
|
+
onPointerLeave: shared.composeEventHandlers(
|
|
454
|
+
itemProps.onPointerLeave,
|
|
455
|
+
(event) => {
|
|
456
|
+
if (event.currentTarget === document.activeElement) {
|
|
457
|
+
context.onItemLeave();
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
)
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
);
|
|
466
|
+
TagsInputItem.displayName = ITEM_NAME;
|
|
467
|
+
var Item = TagsInputItem;
|
|
468
|
+
var INPUT_NAME = "TagsInputInput";
|
|
469
|
+
var TagsInputInput = React__namespace.forwardRef(
|
|
470
|
+
(props, ref) => {
|
|
471
|
+
const { autoFocus, ...inputProps } = props;
|
|
472
|
+
const context = useTagsInput(INPUT_NAME);
|
|
473
|
+
function onTab(event) {
|
|
474
|
+
if (!context.addOnTab) return;
|
|
475
|
+
onCustomKeydown(event);
|
|
476
|
+
}
|
|
477
|
+
function onCustomKeydown(event) {
|
|
478
|
+
if (event.defaultPrevented) return;
|
|
479
|
+
const value = event.currentTarget.value;
|
|
480
|
+
if (!value) return;
|
|
481
|
+
const isAdded = context.onItemAdd(value);
|
|
482
|
+
if (isAdded) {
|
|
483
|
+
event.currentTarget.value = "";
|
|
484
|
+
context.setHighlightedValue(null);
|
|
485
|
+
}
|
|
486
|
+
event.preventDefault();
|
|
487
|
+
}
|
|
488
|
+
React__namespace.useEffect(() => {
|
|
489
|
+
if (!autoFocus) return;
|
|
490
|
+
const animationFrameId = requestAnimationFrame(
|
|
491
|
+
() => context.inputRef.current?.focus()
|
|
492
|
+
);
|
|
493
|
+
return () => cancelAnimationFrame(animationFrameId);
|
|
494
|
+
}, [autoFocus, context.inputRef]);
|
|
495
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
496
|
+
reactPrimitive.Primitive.input,
|
|
497
|
+
{
|
|
498
|
+
type: "text",
|
|
499
|
+
id: context.inputId,
|
|
500
|
+
autoCapitalize: "off",
|
|
501
|
+
autoComplete: "off",
|
|
502
|
+
autoCorrect: "off",
|
|
503
|
+
spellCheck: "false",
|
|
504
|
+
autoFocus,
|
|
505
|
+
"aria-labelledby": context.labelId,
|
|
506
|
+
"data-invalid": context.isInvalidInput ? "" : void 0,
|
|
507
|
+
disabled: context.disabled,
|
|
508
|
+
...inputProps,
|
|
509
|
+
ref: shared.composeRefs(context.inputRef, ref),
|
|
510
|
+
onBlur: shared.composeEventHandlers(inputProps.onBlur, (event) => {
|
|
511
|
+
if (context.blurBehavior === "add") {
|
|
512
|
+
const value = event.target.value;
|
|
513
|
+
if (value) {
|
|
514
|
+
const isAdded = context.onItemAdd(value);
|
|
515
|
+
if (isAdded) event.target.value = "";
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (context.blurBehavior === "clear") {
|
|
519
|
+
event.target.value = "";
|
|
520
|
+
}
|
|
521
|
+
}),
|
|
522
|
+
onChange: shared.composeEventHandlers(inputProps.onChange, (event) => {
|
|
523
|
+
const target = event.target;
|
|
524
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
525
|
+
const delimiter = context.delimiter;
|
|
526
|
+
if (delimiter === target.value.slice(-1)) {
|
|
527
|
+
const value = target.value.slice(0, -1);
|
|
528
|
+
target.value = "";
|
|
529
|
+
if (value) {
|
|
530
|
+
context.onItemAdd(value);
|
|
531
|
+
context.setHighlightedValue(null);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}),
|
|
535
|
+
onKeyDown: shared.composeEventHandlers(inputProps.onKeyDown, (event) => {
|
|
536
|
+
if (event.key === "Enter") onCustomKeydown(event);
|
|
537
|
+
if (event.key === "Tab") onTab(event);
|
|
538
|
+
context.onInputKeydown(event);
|
|
539
|
+
if (event.key.length === 1) context.setHighlightedValue(null);
|
|
540
|
+
}),
|
|
541
|
+
onPaste: shared.composeEventHandlers(inputProps.onPaste, (event) => {
|
|
542
|
+
if (context.addOnPaste) {
|
|
543
|
+
event.preventDefault();
|
|
544
|
+
const value = event.clipboardData.getData("text");
|
|
545
|
+
context.onItemAdd(value, { viaPaste: true });
|
|
546
|
+
context.setHighlightedValue(null);
|
|
547
|
+
}
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
);
|
|
553
|
+
TagsInputInput.displayName = INPUT_NAME;
|
|
554
|
+
var Input = TagsInputInput;
|
|
555
|
+
var ITEM_DELETE_NAME = "TagsInputItemDelete";
|
|
556
|
+
var TagsInputItemDelete = React__namespace.forwardRef((props, ref) => {
|
|
557
|
+
const context = useTagsInput(ITEM_DELETE_NAME);
|
|
558
|
+
const itemContext = useTagsInputItem(ITEM_DELETE_NAME);
|
|
559
|
+
const disabled = itemContext.disabled || context.disabled;
|
|
560
|
+
if (itemContext.isEditing) return null;
|
|
561
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
562
|
+
reactPrimitive.Primitive.button,
|
|
563
|
+
{
|
|
564
|
+
type: "button",
|
|
565
|
+
tabIndex: -1,
|
|
566
|
+
"aria-labelledby": itemContext.textId,
|
|
567
|
+
"aria-controls": itemContext.id,
|
|
568
|
+
"aria-current": itemContext.isHighlighted,
|
|
569
|
+
"data-state": itemContext.isHighlighted ? "active" : "inactive",
|
|
570
|
+
"data-disabled": disabled ? "" : void 0,
|
|
571
|
+
...props,
|
|
572
|
+
ref,
|
|
573
|
+
onClick: shared.composeEventHandlers(props.onClick, () => {
|
|
574
|
+
if (disabled) return;
|
|
575
|
+
const index = context.value.findIndex((i) => i === itemContext.value);
|
|
576
|
+
context.onItemRemove(index);
|
|
577
|
+
})
|
|
578
|
+
}
|
|
579
|
+
);
|
|
580
|
+
});
|
|
581
|
+
TagsInputItemDelete.displayName = ITEM_DELETE_NAME;
|
|
582
|
+
var Delete = TagsInputItemDelete;
|
|
583
|
+
var ITEM_TEXT_NAME = "TagsInputItemText";
|
|
584
|
+
var TagsInputItemText = React__namespace.forwardRef((props, ref) => {
|
|
585
|
+
const { children, ...itemTextProps } = props;
|
|
586
|
+
const context = useTagsInput(ITEM_TEXT_NAME);
|
|
587
|
+
const itemContext = useTagsInputItem(ITEM_TEXT_NAME);
|
|
588
|
+
const [editValue, setEditValue] = React__namespace.useState(itemContext.displayValue);
|
|
589
|
+
if (itemContext.isEditing && context.editable && !itemContext.disabled) {
|
|
590
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
591
|
+
reactPrimitive.Primitive.input,
|
|
592
|
+
{
|
|
593
|
+
type: "text",
|
|
594
|
+
autoCapitalize: "off",
|
|
595
|
+
autoComplete: "off",
|
|
596
|
+
autoCorrect: "off",
|
|
597
|
+
spellCheck: "false",
|
|
598
|
+
autoFocus: true,
|
|
599
|
+
"aria-describedby": itemContext.textId,
|
|
600
|
+
value: editValue,
|
|
601
|
+
onInput: (event) => {
|
|
602
|
+
const target = event.target;
|
|
603
|
+
if (!(target instanceof HTMLInputElement)) return;
|
|
604
|
+
target.style.width = "0";
|
|
605
|
+
target.style.width = `${target.scrollWidth + 4}px`;
|
|
606
|
+
},
|
|
607
|
+
onChange: (event) => setEditValue(event.target.value),
|
|
608
|
+
onKeyDown: (event) => {
|
|
609
|
+
if (event.key === "Enter") {
|
|
610
|
+
const index = context.value.findIndex(
|
|
611
|
+
(v) => v === itemContext.value
|
|
612
|
+
);
|
|
613
|
+
context.onItemUpdate(index, editValue);
|
|
614
|
+
} else if (event.key === "Escape") {
|
|
615
|
+
setEditValue(itemContext.displayValue);
|
|
616
|
+
context.setEditingValue(null);
|
|
617
|
+
context.inputRef.current?.focus();
|
|
618
|
+
}
|
|
619
|
+
event.stopPropagation();
|
|
620
|
+
},
|
|
621
|
+
onFocus: (event) => {
|
|
622
|
+
event.target.select();
|
|
623
|
+
event.target.style.width = "0";
|
|
624
|
+
event.target.style.width = `${event.target.scrollWidth + 4}px`;
|
|
625
|
+
},
|
|
626
|
+
onBlur: () => {
|
|
627
|
+
setEditValue(itemContext.displayValue);
|
|
628
|
+
context.setEditingValue(null);
|
|
629
|
+
},
|
|
630
|
+
style: {
|
|
631
|
+
outline: "none",
|
|
632
|
+
background: "inherit",
|
|
633
|
+
border: "none",
|
|
634
|
+
font: "inherit",
|
|
635
|
+
color: "inherit",
|
|
636
|
+
padding: 0,
|
|
637
|
+
minWidth: "1ch"
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
return /* @__PURE__ */ React__namespace.createElement(reactPrimitive.Primitive.span, { id: itemContext.textId, ...itemTextProps, ref }, children ?? itemContext.displayValue);
|
|
643
|
+
});
|
|
644
|
+
var Text = TagsInputItemText;
|
|
645
|
+
var CLEAR_NAME = "TagsInputClear";
|
|
646
|
+
var TagsInputClear = React__namespace.forwardRef(
|
|
647
|
+
(props, ref) => {
|
|
648
|
+
const { forceMount, ...clearProps } = props;
|
|
649
|
+
const context = useTagsInput(CLEAR_NAME);
|
|
650
|
+
return /* @__PURE__ */ React__namespace.createElement(shared.Presence, { present: forceMount || context.value.length > 0 }, /* @__PURE__ */ React__namespace.createElement(
|
|
651
|
+
reactPrimitive.Primitive.button,
|
|
652
|
+
{
|
|
653
|
+
type: "button",
|
|
654
|
+
"aria-disabled": context.disabled,
|
|
655
|
+
"data-state": context.value.length > 0 ? "visible" : "invisible",
|
|
656
|
+
"data-disabled": context.disabled ? "" : void 0,
|
|
657
|
+
...clearProps,
|
|
658
|
+
ref,
|
|
659
|
+
onClick: shared.composeEventHandlers(props.onClick, () => {
|
|
660
|
+
if (context.disabled) return;
|
|
661
|
+
context.onValueChange([]);
|
|
662
|
+
context.inputRef.current?.focus();
|
|
663
|
+
})
|
|
664
|
+
}
|
|
665
|
+
));
|
|
666
|
+
}
|
|
667
|
+
);
|
|
668
|
+
TagsInputClear.displayName = CLEAR_NAME;
|
|
669
|
+
var Clear = TagsInputClear;
|
|
670
|
+
|
|
671
|
+
exports.Clear = Clear;
|
|
672
|
+
exports.Delete = Delete;
|
|
673
|
+
exports.Input = Input;
|
|
674
|
+
exports.Item = Item;
|
|
675
|
+
exports.Label = Label;
|
|
676
|
+
exports.Root = Root;
|
|
677
|
+
exports.TagsInputClear = TagsInputClear;
|
|
678
|
+
exports.TagsInputInput = TagsInputInput;
|
|
679
|
+
exports.TagsInputItem = TagsInputItem;
|
|
680
|
+
exports.TagsInputItemDelete = TagsInputItemDelete;
|
|
681
|
+
exports.TagsInputItemText = TagsInputItemText;
|
|
682
|
+
exports.TagsInputLabel = TagsInputLabel;
|
|
683
|
+
exports.TagsInputRoot = TagsInputRoot;
|
|
684
|
+
exports.Text = Text;
|