@destyler/dynamic 0.0.3 → 0.0.5

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,511 +1,27 @@
1
1
  'use strict';
2
2
 
3
- const vue = require('vue');
4
- const primitive = require('@destyler/primitive');
5
- const composition = require('@destyler/composition');
6
- const shared = require('@destyler/shared');
7
- const core = require('@vueuse/core');
8
- const collection = require('@destyler/collection');
9
- const component = require('@destyler/visually-hidden/dist/component');
10
- const directives = require('@destyler/directives');
11
-
12
- const destylerDynamicRootProps = {
13
- as: {
14
- type: [String, Object],
15
- required: false,
16
- default: "div"
17
- },
18
- asChild: {
19
- type: Boolean,
20
- required: false,
21
- default: false
22
- },
23
- modelValue: {
24
- type: Array,
25
- required: false,
26
- default: () => []
27
- },
28
- defaultValue: {
29
- type: Array,
30
- required: false
31
- },
32
- addOnPaste: {
33
- type: Boolean,
34
- required: false
35
- },
36
- duplicate: {
37
- type: Boolean,
38
- required: false
39
- },
40
- disabled: {
41
- type: Boolean,
42
- required: false
43
- },
44
- delimiter: {
45
- type: String,
46
- required: false,
47
- default: ","
48
- },
49
- dir: {
50
- type: String,
51
- required: false
52
- },
53
- max: {
54
- type: Number,
55
- required: false,
56
- default: 0
57
- },
58
- required: {
59
- type: Boolean,
60
- required: false
61
- },
62
- name: {
63
- type: String,
64
- required: false
65
- },
66
- id: {
67
- type: String,
68
- required: false
69
- }
70
- };
71
- const [injectDynamicRootContext, provideDynamicRootContext] = shared.createContext("DestylerDynamicRoot");
72
- const DestylerDynamicRoot = vue.defineComponent({
73
- name: "DestylerDynamicRoot",
74
- props: destylerDynamicRootProps,
75
- emits: ["update:modelValue", "invalid"],
76
- setup(props, { emit }) {
77
- const { addOnPaste, disabled, delimiter, max, id, dir: propDir } = vue.toRefs(props);
78
- const dir = composition.useDirection(propDir);
79
- const modelValue = composition.useVModel(props, "modelValue", emit, {
80
- defaultValue: props.defaultValue,
81
- passive: true,
82
- deep: true
83
- });
84
- const { forwardRef, currentElement } = composition.useForwardExpose();
85
- const { focused } = core.useFocusWithin(currentElement);
86
- const isFormControl = composition.useFormControl(currentElement);
87
- const { getItems } = collection.createCollection();
88
- const selectedElement = vue.ref();
89
- const isInvalidInput = vue.ref(false);
90
- provideDynamicRootContext({
91
- modelValue,
92
- onAddValue: (payload) => {
93
- if (modelValue.value.length >= max.value && !!max.value) {
94
- emit("invalid", payload);
95
- return false;
96
- }
97
- if (props.duplicate) {
98
- modelValue.value.push(payload);
99
- return true;
100
- } else {
101
- const exist = modelValue.value.includes(payload);
102
- if (!exist) {
103
- modelValue.value.push(payload);
104
- return true;
105
- } else {
106
- isInvalidInput.value = true;
107
- }
108
- }
109
- emit("invalid", payload);
110
- return false;
111
- },
112
- onRemoveValue: (index) => {
113
- if (index !== -1)
114
- modelValue.value.splice(index, 1);
115
- },
116
- onInputKeydown: (event) => {
117
- const target = event.target;
118
- const collection = getItems().map((i) => i.ref).filter((i) => i.dataset.disabled !== "");
119
- if (!collection.length)
120
- return;
121
- const lastTag = collection.at(-1);
122
- switch (event.key) {
123
- case "Delete":
124
- case "Backspace": {
125
- if (target.selectionStart !== 0 || target.selectionEnd !== 0)
126
- break;
127
- if (selectedElement.value) {
128
- const index = collection.findIndex((i) => i === selectedElement.value);
129
- modelValue.value.splice(index, 1);
130
- selectedElement.value = selectedElement.value === lastTag ? collection.at(index - 1) : collection.at(index + 1);
131
- event.preventDefault();
132
- } else if (event.key === "Backspace") {
133
- selectedElement.value = lastTag;
134
- event.preventDefault();
135
- }
136
- break;
137
- }
138
- case "Home":
139
- case "End":
140
- case "ArrowRight":
141
- case "ArrowLeft": {
142
- const isArrowRight = event.key === "ArrowRight" && dir.value === "ltr" || event.key === "ArrowLeft" && dir.value === "rtl";
143
- const isArrowLeft = !isArrowRight;
144
- if (target.selectionStart !== 0 || target.selectionEnd !== 0)
145
- break;
146
- if (isArrowLeft && !selectedElement.value) {
147
- selectedElement.value = lastTag;
148
- event.preventDefault();
149
- } else if (isArrowRight && lastTag && selectedElement.value === lastTag) {
150
- selectedElement.value = void 0;
151
- event.preventDefault();
152
- } else if (selectedElement.value) {
153
- const el = composition.useArrowNavigation(event, selectedElement.value, void 0, {
154
- itemsArray: collection,
155
- loop: false,
156
- dir: dir.value
157
- });
158
- if (el)
159
- selectedElement.value = el;
160
- event.preventDefault();
161
- }
162
- break;
163
- }
164
- case "ArrowUp":
165
- case "ArrowDown": {
166
- if (selectedElement.value)
167
- event.preventDefault();
168
- break;
169
- }
170
- default: {
171
- selectedElement.value = void 0;
172
- }
173
- }
174
- },
175
- selectedElement,
176
- isInvalidInput,
177
- addOnPaste,
178
- dir,
179
- disabled,
180
- delimiter,
181
- max,
182
- id
183
- });
184
- return {
185
- forwardRef,
186
- dir,
187
- isInvalidInput,
188
- disabled,
189
- focused,
190
- modelValue,
191
- isFormControl
192
- };
193
- },
194
- render() {
195
- return vue.h(collection.DestylerCollectionSlot, {}, () => vue.h(primitive.DestylerPrimitive, {
196
- "ref": (el) => this.forwardRef(el),
197
- "dir": this.dir,
198
- "as": this.$props.as,
199
- "asChild": this.$props.asChild,
200
- "data-invalid": this.isInvalidInput ? "" : void 0,
201
- "data-disabled": this.disabled ? "" : void 0,
202
- "data-focused": this.focused ? "" : void 0
203
- }, () => [
204
- this.$slots.default?.({ modelValue: this.modelValue }),
205
- this.isFormControl && this.$props.name ? vue.h(component.DestylerVisuallyhiddenInput, {
206
- name: this.$props.name,
207
- value: this.modelValue,
208
- required: this.required,
209
- disabled: this.disabled
210
- }) : null
211
- ]));
212
- }
213
- });
214
-
215
- const destylerDynamicClearProps = {
216
- as: {
217
- type: [String, Object],
218
- required: false,
219
- default: "button"
220
- },
221
- asChild: {
222
- type: Boolean,
223
- required: false,
224
- default: false
225
- }
226
- };
227
- const DestylerDynamicClear = vue.defineComponent({
228
- name: "DestylerDynamicClear",
229
- props: destylerDynamicClearProps,
230
- setup() {
231
- composition.useForwardExpose();
232
- const context = injectDynamicRootContext();
233
- function handleCancel() {
234
- if (context.disabled.value)
235
- return;
236
- context.modelValue.value = [];
237
- }
238
- return {
239
- context,
240
- handleCancel
241
- };
242
- },
243
- render() {
244
- return vue.h(primitive.DestylerPrimitive, vue.mergeProps(this.$props, {
245
- "type": this.$props.as === "button" ? "button" : void 0,
246
- "data-disabled": this.context.disabled.value ? "" : void 0,
247
- "onClick": () => {
248
- this.handleCancel();
249
- }
250
- }), () => this.$slots.default?.());
251
- }
252
- });
253
-
254
- const destylerDynamicInputProps = {
255
- as: {
256
- type: [String, Object],
257
- required: false,
258
- default: "input"
259
- },
260
- asChild: {
261
- type: Boolean,
262
- required: false,
263
- default: false
264
- },
265
- placeholder: {
266
- type: String,
267
- required: false
268
- },
269
- autoFocus: {
270
- type: Boolean,
271
- required: false
272
- },
273
- maxLength: {
274
- type: Number,
275
- required: false
276
- }
277
- };
278
- const DestylerDynamicInput = vue.defineComponent({
279
- name: "DestylerDynamicInput",
280
- props: destylerDynamicInputProps,
281
- setup(props) {
282
- const context = injectDynamicRootContext();
283
- const { forwardRef, currentElement } = composition.useForwardExpose();
284
- async function handleEnter(event) {
285
- await vue.nextTick();
286
- if (event.defaultPrevented)
287
- return;
288
- const target = event.target;
289
- if (!target.value)
290
- return;
291
- const isAdded = context.onAddValue(target.value);
292
- if (isAdded)
293
- target.value = "";
294
- event.preventDefault();
295
- }
296
- function handleInput(event) {
297
- context.isInvalidInput.value = false;
298
- const delimiter = context.delimiter.value;
299
- if (delimiter === event.data) {
300
- const target = event.target;
301
- target.value = target.value.replaceAll(delimiter, "");
302
- const isAdded = context.onAddValue(target.value);
303
- if (isAdded)
304
- target.value = "";
305
- }
306
- }
307
- function handlePaste(event) {
308
- if (context.addOnPaste.value) {
309
- event.preventDefault();
310
- const clipboardData = event.clipboardData;
311
- if (!clipboardData)
312
- return;
313
- const value = clipboardData.getData("text");
314
- if (context.delimiter.value) {
315
- const splittedValue = value.split(context.delimiter.value);
316
- splittedValue.forEach((v) => {
317
- context.onAddValue(v);
318
- });
319
- } else {
320
- context.onAddValue(value);
321
- }
322
- }
323
- }
324
- vue.onMounted(() => {
325
- const inputEl = currentElement.value.nodeName === "INPUT" ? currentElement.value : currentElement.value.querySelector("input");
326
- if (!inputEl)
327
- return;
328
- setTimeout(() => {
329
- if (props.autoFocus)
330
- inputEl?.focus();
331
- }, 1);
332
- });
333
- return {
334
- forwardRef,
335
- context,
336
- handleInput,
337
- handlePaste,
338
- handleEnter
339
- };
340
- },
341
- render() {
342
- return vue.withDirectives(vue.h(primitive.DestylerPrimitive, {
343
- "ref": (el) => this.forwardRef(el),
344
- "type": "text",
345
- "autocomplete": "off",
346
- "autocorrect": "off",
347
- "autocapitalize": "off",
348
- "as": this.$props.as,
349
- "asChild": this.$props.asChild,
350
- "maxlength": this.$props.maxLength,
351
- "placeholder": this.$props.placeholder,
352
- "disabled": this.context.disabled.value,
353
- "data-invalid": this.context.isInvalidInput.value ? "" : void 0,
354
- "onInput": (event) => {
355
- this.handleInput(event);
356
- },
357
- "onPaste": (event) => {
358
- this.handlePaste(event);
359
- },
360
- "onKeydown": (event) => {
361
- if (event.key === "Enter")
362
- this.handleEnter(event);
363
- else
364
- this.context.onInputKeydown(event);
365
- }
366
- }, () => this.$slots.default?.()), [
367
- [directives.BindOnceDirective, { id: this.context.id?.value }]
368
- ]);
369
- }
370
- });
371
-
372
- const destylerDynamicItemProps = {
373
- as: {
374
- type: [String, Object],
375
- required: false,
376
- default: "div"
377
- },
378
- asChild: {
379
- type: Boolean,
380
- required: false,
381
- default: false
382
- },
383
- value: {
384
- type: String,
385
- required: true
386
- },
387
- disabled: {
388
- type: Boolean,
389
- required: false
390
- }
391
- };
392
- const [injectDynamicItemContext, provideDynamicItemContext] = shared.createContext("DestylerDynamicItem");
393
- const DestylerDynamicItem = vue.defineComponent({
394
- name: "DestylerDynamicItem",
395
- props: destylerDynamicItemProps,
396
- setup(props) {
397
- const { value } = vue.toRefs(props);
398
- const context = injectDynamicRootContext();
399
- const { forwardRef, currentElement } = composition.useForwardExpose();
400
- const isSelected = vue.computed(() => context.selectedElement.value === currentElement.value);
401
- const textId = composition.useId();
402
- const disabled = vue.computed(() => props.disabled || context.disabled.value);
403
- provideDynamicItemContext({
404
- value,
405
- isSelected,
406
- disabled,
407
- textId
408
- });
409
- return {
410
- forwardRef,
411
- textId,
412
- isSelected,
413
- disabled
414
- };
415
- },
416
- render() {
417
- return vue.h(collection.DestylerCollectionItem, {}, () => vue.h(primitive.DestylerPrimitive, {
418
- "ref": (el) => this.forwardRef(el),
419
- "as": this.$props.as,
420
- "asChild": this.$props.asChild,
421
- "aria-labelledby": this.textId,
422
- "aria-current": this.isSelected,
423
- "data-disabled": this.disabled ? "" : void 0,
424
- "data-state": this.isSelected ? "active" : "inactive"
425
- }, () => this.$slots.default?.()));
426
- }
427
- });
428
-
429
- const destylerDynamicItemDeleteProps = {
430
- as: {
431
- type: [String, Object],
432
- required: false,
433
- default: "button"
434
- },
435
- asChild: {
436
- type: Boolean,
437
- required: false,
438
- default: false
439
- }
440
- };
441
- const DestylerDynamicItemDelete = vue.defineComponent({
442
- name: "DestylerDynamicItemDelete",
443
- props: destylerDynamicItemDeleteProps,
444
- setup() {
445
- composition.useForwardExpose();
446
- const context = injectDynamicRootContext();
447
- const itemContext = injectDynamicItemContext();
448
- const disabled = vue.computed(() => itemContext.disabled?.value || context.disabled.value);
449
- function handleDelete() {
450
- if (disabled.value)
451
- return;
452
- const index = context.modelValue.value.findIndex((i) => i === itemContext.value.value);
453
- context.onRemoveValue(index);
454
- }
455
- return {
456
- context,
457
- itemContext,
458
- disabled,
459
- handleDelete
460
- };
461
- },
462
- render() {
463
- return vue.h(primitive.DestylerPrimitive, vue.mergeProps(this.$props, {
464
- "tabindex": "-1",
465
- "aria-labelledby": this.itemContext.textId,
466
- "aria-current": this.itemContext.isSelected.value,
467
- "data-state": this.itemContext.isSelected.value ? "active" : "inactive",
468
- "data-disabled": this.disabled ? "" : void 0,
469
- "type": this.$props.as === "button" ? "button" : void 0,
470
- "onClick": () => {
471
- this.handleDelete();
472
- }
473
- }), () => this.$slots.default?.());
474
- }
475
- });
476
-
477
- const destylerDynamicItemTextProps = {
478
- as: {
479
- type: [String, Object],
480
- required: false,
481
- default: "span"
482
- },
483
- asChild: {
484
- type: Boolean,
485
- required: false,
486
- default: false
487
- }
488
- };
489
- const DestylerDynamicItemText = vue.defineComponent({
490
- name: "DestylerDynamicItemText",
491
- props: destylerDynamicItemTextProps,
492
- setup() {
493
- const itemContext = injectDynamicItemContext();
494
- composition.useForwardExpose();
495
- return {
496
- itemContext
497
- };
498
- },
499
- render() {
500
- return vue.withDirectives(vue.h(primitive.DestylerPrimitive, this.$props, () => this.$slots.default ? this.$slots.default?.() : this.itemContext.value.value), [
501
- [directives.BindOnceDirective, { id: this.itemContext.textId }]
502
- ]);
503
- }
504
- });
505
-
506
- exports.DestylerDynamicClear = DestylerDynamicClear;
507
- exports.DestylerDynamicInput = DestylerDynamicInput;
508
- exports.DestylerDynamicItem = DestylerDynamicItem;
509
- exports.DestylerDynamicItemDelete = DestylerDynamicItemDelete;
510
- exports.DestylerDynamicItemText = DestylerDynamicItemText;
511
- exports.DestylerDynamicRoot = DestylerDynamicRoot;
3
+ const index = require('./component.cjs');
4
+ require('vue');
5
+ require('@destyler/primitive');
6
+ require('@destyler/composition');
7
+ require('@destyler/shared');
8
+ require('@vueuse/core');
9
+ require('@destyler/collection');
10
+ require('@destyler/collection/composition');
11
+ require('@destyler/visually-hidden/component');
12
+ require('@destyler/directives');
13
+
14
+
15
+
16
+ exports.DynamicClear = index.DynamicClear;
17
+ exports.DynamicInput = index.DynamicInput;
18
+ exports.DynamicItem = index.DynamicItem;
19
+ exports.DynamicItemDelete = index.DynamicItemDelete;
20
+ exports.DynamicItemText = index.DynamicItemText;
21
+ exports.DynamicRoot = index.DynamicRoot;
22
+ exports.dynamicClearProps = index.dynamicClearProps;
23
+ exports.dynamicInputProps = index.dynamicInputProps;
24
+ exports.dynamicItemDeleteProps = index.dynamicItemDeleteProps;
25
+ exports.dynamicItemProps = index.dynamicItemProps;
26
+ exports.dynamicItemTextProps = index.dynamicItemTextProps;
27
+ exports.dynamicRootProps = index.dynamicRootProps;