@diceui/tags-input 0.7.2 → 0.7.3

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