knt-shared 1.0.0

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.
@@ -0,0 +1,734 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const vue = require("vue");
4
+ const webVue = require("@arco-design/web-vue");
5
+ const componentMap = /* @__PURE__ */ new Map();
6
+ componentMap.set("Input", webVue.Input);
7
+ componentMap.set("InputNumber", webVue.InputNumber);
8
+ componentMap.set("InputPassword", webVue.InputPassword);
9
+ componentMap.set("Textarea", webVue.Textarea);
10
+ componentMap.set("AutoComplete", webVue.AutoComplete);
11
+ componentMap.set("Select", webVue.Select);
12
+ componentMap.set("Radio", webVue.Radio);
13
+ componentMap.set("RadioGroup", webVue.RadioGroup);
14
+ componentMap.set("Checkbox", webVue.Checkbox);
15
+ componentMap.set("CheckboxGroup", webVue.CheckboxGroup);
16
+ componentMap.set("Cascader", webVue.Cascader);
17
+ componentMap.set("TreeSelect", webVue.TreeSelect);
18
+ componentMap.set("DatePicker", webVue.DatePicker);
19
+ componentMap.set("TimePicker", webVue.TimePicker);
20
+ componentMap.set("RangePicker", webVue.RangePicker);
21
+ componentMap.set("Switch", webVue.Switch);
22
+ componentMap.set("Slider", webVue.Slider);
23
+ componentMap.set("Rate", webVue.Rate);
24
+ componentMap.set("Upload", webVue.Upload);
25
+ componentMap.set("Mention", webVue.Mention);
26
+ componentMap.set("Transfer", webVue.Transfer);
27
+ function registerComponent(name, component) {
28
+ componentMap.set(name, component);
29
+ }
30
+ function getComponent(name) {
31
+ if (typeof name === "string") {
32
+ return componentMap.get(name);
33
+ }
34
+ return name;
35
+ }
36
+ const componentsNeedPlaceholder = /* @__PURE__ */ new Set([
37
+ "Input",
38
+ "InputPassword",
39
+ "Textarea",
40
+ "AutoComplete"
41
+ ]);
42
+ const componentsNeedSelectPlaceholder = /* @__PURE__ */ new Set([
43
+ "Select",
44
+ "DatePicker",
45
+ "TimePicker",
46
+ "RangePicker",
47
+ "Cascader",
48
+ "TreeSelect"
49
+ ]);
50
+ function getPlaceholder(component, label) {
51
+ if (componentsNeedPlaceholder.has(component)) {
52
+ return `请输入${label}`;
53
+ }
54
+ if (componentsNeedSelectPlaceholder.has(component)) {
55
+ return `请选择${label}`;
56
+ }
57
+ return "";
58
+ }
59
+ const _sfc_main = /* @__PURE__ */ vue.defineComponent({
60
+ ...{
61
+ name: "BasicForm",
62
+ inheritAttrs: false
63
+ },
64
+ __name: "BasicForm",
65
+ props: {
66
+ schemas: { default: () => [] },
67
+ layout: { default: "horizontal" },
68
+ size: {},
69
+ disabled: { type: Boolean, default: false },
70
+ labelAlign: { default: "right" },
71
+ autoLabelWidth: { type: Boolean },
72
+ labelWidth: {},
73
+ labelColProps: {},
74
+ wrapperColProps: {},
75
+ baseColProps: { default: () => ({ span: 24 }) },
76
+ baseFormItemProps: {},
77
+ actionColOptions: { default: () => ({ span: 24 }) },
78
+ autoSetPlaceHolder: { type: Boolean, default: true },
79
+ showSubmitButton: { type: Boolean, default: true },
80
+ showResetButton: { type: Boolean, default: true },
81
+ submitButtonText: { default: "提交" },
82
+ resetButtonText: { default: "重置" },
83
+ showActionButtons: { type: Boolean, default: true }
84
+ },
85
+ emits: ["register", "submit", "reset", "validate"],
86
+ setup(__props, { expose: __expose, emit: __emit }) {
87
+ const props = __props;
88
+ const emit = __emit;
89
+ const formRef = vue.ref();
90
+ const formModel = vue.reactive({});
91
+ const submitLoading = vue.ref(false);
92
+ const internalSchemas = vue.ref([]);
93
+ const dynamicProps = vue.ref({});
94
+ const getProps = vue.computed(() => {
95
+ return { ...props, ...dynamicProps.value };
96
+ });
97
+ const getBindValue = vue.computed(() => {
98
+ const propsData = vue.unref(getProps);
99
+ return {
100
+ layout: propsData.layout,
101
+ size: propsData.size,
102
+ disabled: propsData.disabled,
103
+ labelAlign: propsData.labelAlign,
104
+ autoLabelWidth: propsData.autoLabelWidth,
105
+ labelWidth: propsData.labelWidth,
106
+ labelColProps: propsData.labelColProps,
107
+ wrapperColProps: propsData.wrapperColProps
108
+ };
109
+ });
110
+ const getRow = vue.computed(() => {
111
+ return {
112
+ gutter: 24
113
+ };
114
+ });
115
+ const getActionProps = vue.computed(() => {
116
+ const propsData = vue.unref(getProps);
117
+ return {
118
+ showActionButtons: propsData.showActionButtons !== false,
119
+ showSubmitButton: propsData.showSubmitButton !== false,
120
+ showResetButton: propsData.showResetButton !== false,
121
+ submitButtonText: propsData.submitButtonText || "提交",
122
+ resetButtonText: propsData.resetButtonText || "重置",
123
+ actionColOptions: propsData.actionColOptions || { span: 24 }
124
+ };
125
+ });
126
+ const initFormModel = () => {
127
+ if (!props.schemas) return;
128
+ props.schemas.forEach((schema) => {
129
+ if (schema.defaultValue !== void 0) {
130
+ formModel[schema.field] = schema.defaultValue;
131
+ }
132
+ });
133
+ };
134
+ const getVisibleSchemas = () => {
135
+ const schemas = internalSchemas.value.length > 0 ? internalSchemas.value : props.schemas || [];
136
+ return schemas.filter((schema) => {
137
+ if (schema.show === void 0) return true;
138
+ if (typeof schema.show === "boolean") return schema.show;
139
+ return schema.show(formModel);
140
+ });
141
+ };
142
+ const getColProps = (schema) => {
143
+ const propsData = vue.unref(getProps);
144
+ return schema.colProps || propsData.baseColProps || { span: 24 };
145
+ };
146
+ const getFormItemBindValue = (schema) => {
147
+ const propsData = vue.unref(getProps);
148
+ let disabled = propsData.disabled;
149
+ if (schema.disabled !== void 0) {
150
+ disabled = typeof schema.disabled === "boolean" ? schema.disabled : schema.disabled(formModel);
151
+ }
152
+ return {
153
+ field: schema.field,
154
+ label: schema.label,
155
+ labelColProps: schema.labelColProps || propsData.labelColProps,
156
+ wrapperColProps: schema.wrapperColProps || propsData.wrapperColProps,
157
+ rules: schema.rules,
158
+ disabled,
159
+ help: schema.help,
160
+ extra: schema.extra,
161
+ required: schema.required,
162
+ ...propsData.baseFormItemProps || {},
163
+ ...schema.formItemProps || {}
164
+ };
165
+ };
166
+ const getPlaceholderText = (schema) => {
167
+ var _a;
168
+ const propsData = vue.unref(getProps);
169
+ if ((_a = schema.componentProps) == null ? void 0 : _a.placeholder) {
170
+ return schema.componentProps.placeholder;
171
+ }
172
+ if (!propsData.autoSetPlaceHolder) {
173
+ return "";
174
+ }
175
+ const componentType = typeof schema.component === "string" ? schema.component : "";
176
+ return getPlaceholder(componentType, schema.label);
177
+ };
178
+ const handleSubmit = async (values) => {
179
+ var _a;
180
+ try {
181
+ submitLoading.value = true;
182
+ const errors = await ((_a = formRef.value) == null ? void 0 : _a.validate());
183
+ if (!errors) {
184
+ emit("submit", values);
185
+ }
186
+ } finally {
187
+ submitLoading.value = false;
188
+ }
189
+ };
190
+ const handleReset = () => {
191
+ var _a;
192
+ (_a = formRef.value) == null ? void 0 : _a.resetFields();
193
+ emit("reset");
194
+ };
195
+ const getFieldsValue = () => {
196
+ return { ...formModel };
197
+ };
198
+ const setFieldsValue = (values) => {
199
+ Object.keys(values).forEach((key) => {
200
+ if (key in formModel) {
201
+ formModel[key] = values[key];
202
+ }
203
+ });
204
+ };
205
+ const resetFields = () => {
206
+ var _a;
207
+ (_a = formRef.value) == null ? void 0 : _a.resetFields();
208
+ };
209
+ const validate = async () => {
210
+ var _a;
211
+ return (_a = formRef.value) == null ? void 0 : _a.validate();
212
+ };
213
+ const clearValidate = (field) => {
214
+ var _a;
215
+ (_a = formRef.value) == null ? void 0 : _a.clearValidate(field);
216
+ };
217
+ const updateSchema = (schema) => {
218
+ const schemas = Array.isArray(schema) ? schema : [schema];
219
+ schemas.forEach((item) => {
220
+ const index = internalSchemas.value.findIndex((s) => s.field === item.field);
221
+ if (index !== -1) {
222
+ internalSchemas.value[index] = { ...internalSchemas.value[index], ...item };
223
+ }
224
+ });
225
+ };
226
+ const removeSchema = (field) => {
227
+ const fields = Array.isArray(field) ? field : [field];
228
+ fields.forEach((f) => {
229
+ const index = internalSchemas.value.findIndex((s) => s.field === f);
230
+ if (index !== -1) {
231
+ internalSchemas.value.splice(index, 1);
232
+ delete formModel[f];
233
+ }
234
+ });
235
+ };
236
+ const getSchema = (field) => {
237
+ const schemas = internalSchemas.value.length > 0 ? internalSchemas.value : props.schemas || [];
238
+ if (!field) return schemas;
239
+ return schemas.find((s) => s.field === field);
240
+ };
241
+ const resetSchema = (schemas) => {
242
+ internalSchemas.value = [...schemas];
243
+ initFormModel();
244
+ };
245
+ const setProps = (formProps) => {
246
+ dynamicProps.value = { ...dynamicProps.value, ...formProps };
247
+ if (formProps.schemas && Array.isArray(formProps.schemas)) {
248
+ internalSchemas.value = [...formProps.schemas];
249
+ initFormModel();
250
+ }
251
+ };
252
+ const scrollToField = (name, options) => {
253
+ vue.nextTick(() => {
254
+ const element = document.querySelector(`[data-field="${name}"]`);
255
+ if (element) {
256
+ element.scrollIntoView(options);
257
+ }
258
+ });
259
+ };
260
+ __expose({
261
+ formRef,
262
+ formModel,
263
+ handleSubmit,
264
+ getVisibleSchemas,
265
+ getColProps,
266
+ getFormItemBindValue,
267
+ getPlaceholderText,
268
+ getFieldsValue,
269
+ setFieldsValue,
270
+ resetFields,
271
+ validate,
272
+ clearValidate,
273
+ updateSchema,
274
+ removeSchema,
275
+ getSchema,
276
+ resetSchema,
277
+ setProps,
278
+ scrollToField
279
+ });
280
+ const stopSchemasWatch = vue.watch(
281
+ () => props.schemas,
282
+ (newSchemas) => {
283
+ if (newSchemas) {
284
+ internalSchemas.value = [...newSchemas];
285
+ initFormModel();
286
+ }
287
+ },
288
+ { immediate: true, deep: true }
289
+ );
290
+ vue.onMounted(() => {
291
+ vue.nextTick(() => {
292
+ emit("register", {
293
+ formRef,
294
+ formModel,
295
+ getFieldsValue,
296
+ setFieldsValue,
297
+ resetFields,
298
+ validate,
299
+ clearValidate,
300
+ updateSchema,
301
+ removeSchema,
302
+ getSchema,
303
+ resetSchema,
304
+ setProps,
305
+ scrollToField,
306
+ handleSubmit
307
+ });
308
+ });
309
+ });
310
+ vue.onBeforeUnmount(() => {
311
+ stopSchemasWatch();
312
+ Object.keys(formModel).forEach((key) => {
313
+ delete formModel[key];
314
+ });
315
+ internalSchemas.value = [];
316
+ dynamicProps.value = {};
317
+ });
318
+ return (_ctx, _cache) => {
319
+ const _component_a_form_item = vue.resolveComponent("a-form-item");
320
+ const _component_a_col = vue.resolveComponent("a-col");
321
+ const _component_a_button = vue.resolveComponent("a-button");
322
+ const _component_a_space = vue.resolveComponent("a-space");
323
+ const _component_a_row = vue.resolveComponent("a-row");
324
+ const _component_a_form = vue.resolveComponent("a-form");
325
+ return vue.openBlock(), vue.createBlock(_component_a_form, vue.mergeProps({
326
+ ref_key: "formRef",
327
+ ref: formRef
328
+ }, getBindValue.value, {
329
+ model: formModel,
330
+ onSubmit: handleSubmit
331
+ }), {
332
+ default: vue.withCtx(() => [
333
+ vue.createVNode(_component_a_row, vue.normalizeProps(vue.guardReactiveProps(getRow.value)), {
334
+ default: vue.withCtx(() => [
335
+ (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(getVisibleSchemas(), (schema) => {
336
+ return vue.openBlock(), vue.createBlock(_component_a_col, vue.mergeProps({
337
+ key: schema.key || schema.field,
338
+ ref_for: true
339
+ }, getColProps(schema)), {
340
+ default: vue.withCtx(() => [
341
+ schema.render ? (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(schema.render(schema, formModel)), { key: 0 })) : (vue.openBlock(), vue.createBlock(_component_a_form_item, vue.mergeProps({
342
+ key: 1,
343
+ ref_for: true
344
+ }, getFormItemBindValue(schema)), {
345
+ default: vue.withCtx(() => [
346
+ schema.renderComponentContent ? (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(schema.renderComponentContent(schema, formModel)), { key: 0 })) : (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(getComponent)(schema.component)), vue.mergeProps({
347
+ key: 1,
348
+ modelValue: formModel[schema.field],
349
+ "onUpdate:modelValue": ($event) => formModel[schema.field] = $event,
350
+ placeholder: getPlaceholderText(schema)
351
+ }, { ref_for: true }, schema.componentProps), null, 16, ["modelValue", "onUpdate:modelValue", "placeholder"]))
352
+ ]),
353
+ _: 2
354
+ }, 1040))
355
+ ]),
356
+ _: 2
357
+ }, 1040);
358
+ }), 128)),
359
+ getActionProps.value.showActionButtons ? (vue.openBlock(), vue.createBlock(_component_a_col, vue.normalizeProps(vue.mergeProps({ key: 0 }, getActionProps.value.actionColOptions)), {
360
+ default: vue.withCtx(() => [
361
+ vue.createVNode(_component_a_form_item, null, {
362
+ default: vue.withCtx(() => [
363
+ vue.createVNode(_component_a_space, null, {
364
+ default: vue.withCtx(() => [
365
+ getActionProps.value.showSubmitButton ? (vue.openBlock(), vue.createBlock(_component_a_button, {
366
+ key: 0,
367
+ type: "primary",
368
+ "html-type": "submit",
369
+ loading: submitLoading.value
370
+ }, {
371
+ default: vue.withCtx(() => [
372
+ vue.createTextVNode(vue.toDisplayString(getActionProps.value.submitButtonText), 1)
373
+ ]),
374
+ _: 1
375
+ }, 8, ["loading"])) : vue.createCommentVNode("", true),
376
+ getActionProps.value.showResetButton ? (vue.openBlock(), vue.createBlock(_component_a_button, {
377
+ key: 1,
378
+ onClick: handleReset
379
+ }, {
380
+ default: vue.withCtx(() => [
381
+ vue.createTextVNode(vue.toDisplayString(getActionProps.value.resetButtonText), 1)
382
+ ]),
383
+ _: 1
384
+ })) : vue.createCommentVNode("", true),
385
+ vue.renderSlot(_ctx.$slots, "actions")
386
+ ]),
387
+ _: 3
388
+ })
389
+ ]),
390
+ _: 3
391
+ })
392
+ ]),
393
+ _: 3
394
+ }, 16)) : vue.createCommentVNode("", true)
395
+ ]),
396
+ _: 3
397
+ }, 16)
398
+ ]),
399
+ _: 3
400
+ }, 16, ["model"]);
401
+ };
402
+ }
403
+ });
404
+ function useForm(options = {}) {
405
+ const formRef = vue.ref(null);
406
+ const formModel = vue.reactive({});
407
+ const loadedRef = vue.ref(false);
408
+ const propsRef = vue.ref({});
409
+ const initFormModel = () => {
410
+ var _a, _b;
411
+ const schemas = vue.unref(((_b = (_a = formRef.value) == null ? void 0 : _a.getSchema) == null ? void 0 : _b.call(_a)) || options.schemas || []);
412
+ schemas.forEach((schema) => {
413
+ if (schema.defaultValue !== void 0) {
414
+ formModel[schema.field] = schema.defaultValue;
415
+ }
416
+ });
417
+ };
418
+ const register = async (formInstance) => {
419
+ await vue.nextTick();
420
+ formRef.value = formInstance;
421
+ loadedRef.value = true;
422
+ if (options && Object.keys(options).length > 0) {
423
+ await setProps(options);
424
+ }
425
+ initFormModel();
426
+ };
427
+ const getForm = () => {
428
+ const form = vue.unref(formRef);
429
+ if (!form) {
430
+ console.error("表单实例尚未注册,请确保组件已挂载并正确使用 @register");
431
+ }
432
+ return form;
433
+ };
434
+ const getFieldsValue = () => {
435
+ const form = getForm();
436
+ if (!form) return {};
437
+ if (form.getFieldsValue) {
438
+ return form.getFieldsValue();
439
+ }
440
+ return { ...formModel };
441
+ };
442
+ const setFieldsValue = async (values) => {
443
+ const form = getForm();
444
+ if (!form) return;
445
+ if (form.setFieldsValue) {
446
+ await form.setFieldsValue(values);
447
+ } else {
448
+ Object.keys(values).forEach((key) => {
449
+ formModel[key] = values[key];
450
+ });
451
+ }
452
+ };
453
+ const resetFields = async () => {
454
+ var _a;
455
+ const form = getForm();
456
+ if (!form) return;
457
+ if (form.resetFields) {
458
+ await form.resetFields();
459
+ } else if ((_a = form.formRef) == null ? void 0 : _a.value) {
460
+ await form.formRef.value.resetFields();
461
+ }
462
+ };
463
+ const validate = async (nameList) => {
464
+ var _a;
465
+ const form = getForm();
466
+ if (!form) return Promise.reject("表单实例不存在");
467
+ if (form.validate) {
468
+ return await form.validate(nameList);
469
+ } else if ((_a = form.formRef) == null ? void 0 : _a.value) {
470
+ return await form.formRef.value.validate(nameList);
471
+ }
472
+ return Promise.resolve(void 0);
473
+ };
474
+ const validateFields = async (nameList) => {
475
+ return await validate(nameList);
476
+ };
477
+ const clearValidate = async (field) => {
478
+ var _a;
479
+ const form = getForm();
480
+ if (!form) return;
481
+ if (form.clearValidate) {
482
+ await form.clearValidate(field);
483
+ } else if ((_a = form.formRef) == null ? void 0 : _a.value) {
484
+ await form.formRef.value.clearValidate(field);
485
+ }
486
+ };
487
+ const submit = async () => {
488
+ const form = getForm();
489
+ if (!form) return;
490
+ if (form.handleSubmit) {
491
+ return await form.handleSubmit();
492
+ }
493
+ await validate();
494
+ return getFieldsValue();
495
+ };
496
+ const updateSchema = async (schema) => {
497
+ const form = getForm();
498
+ if (!form) return;
499
+ const schemaList = Array.isArray(schema) ? schema : [schema];
500
+ if (form.updateSchema) {
501
+ await form.updateSchema(schemaList);
502
+ }
503
+ };
504
+ const resetSchema = async (schemas) => {
505
+ const form = getForm();
506
+ if (!form) return;
507
+ if (form.resetSchema) {
508
+ await form.resetSchema(schemas);
509
+ }
510
+ };
511
+ const removeSchemaByField = async (field) => {
512
+ const form = getForm();
513
+ if (!form) return;
514
+ const fields = Array.isArray(field) ? field : [field];
515
+ if (form.removeSchema) {
516
+ await form.removeSchema(fields);
517
+ }
518
+ fields.forEach((f) => {
519
+ if (f in formModel) {
520
+ delete formModel[f];
521
+ }
522
+ });
523
+ };
524
+ const appendSchemaByField = async (schema, prefixField, first = false) => {
525
+ var _a;
526
+ const form = getForm();
527
+ if (!form) return;
528
+ const schemas = ((_a = form.getSchema) == null ? void 0 : _a.call(form)) || [];
529
+ const schemaList = Array.isArray(schema) ? schema : [schema];
530
+ if (prefixField) {
531
+ const index = schemas.findIndex((s) => s.field === prefixField);
532
+ if (index !== -1) {
533
+ const insertIndex = first ? index : index + 1;
534
+ schemas.splice(insertIndex, 0, ...schemaList);
535
+ }
536
+ } else {
537
+ if (first) {
538
+ schemas.unshift(...schemaList);
539
+ } else {
540
+ schemas.push(...schemaList);
541
+ }
542
+ }
543
+ if (form.updateSchema) {
544
+ await form.updateSchema(schemas);
545
+ }
546
+ schemaList.forEach((s) => {
547
+ if (s.defaultValue !== void 0) {
548
+ formModel[s.field] = s.defaultValue;
549
+ }
550
+ });
551
+ };
552
+ const getSchema = (field) => {
553
+ const form = getForm();
554
+ if (!form) return void 0;
555
+ if (form.getSchema) {
556
+ return form.getSchema(field);
557
+ }
558
+ return void 0;
559
+ };
560
+ const setProps = async (formProps) => {
561
+ propsRef.value = { ...propsRef.value, ...formProps };
562
+ const form = getForm();
563
+ if (!form) {
564
+ return;
565
+ }
566
+ if (form.setProps) {
567
+ await form.setProps(formProps);
568
+ }
569
+ };
570
+ const scrollToField = async (name, options2) => {
571
+ const form = getForm();
572
+ if (!form) return;
573
+ if (form.scrollToField) {
574
+ await form.scrollToField(name, options2);
575
+ }
576
+ };
577
+ const formMethods = {
578
+ getFieldsValue,
579
+ setFieldsValue,
580
+ resetFields,
581
+ validate,
582
+ validateFields,
583
+ clearValidate,
584
+ submit,
585
+ updateSchema,
586
+ resetSchema,
587
+ removeSchemaByField,
588
+ appendSchemaByField,
589
+ getSchema,
590
+ setProps,
591
+ scrollToField,
592
+ formModel,
593
+ formRef,
594
+ getForm
595
+ };
596
+ return [register, formMethods];
597
+ }
598
+ function createFormSchema(schemas) {
599
+ return schemas.map((schema, index) => ({
600
+ key: schema.key || `${schema.field}_${index}`,
601
+ ...schema
602
+ }));
603
+ }
604
+ function mergeFormSchemas(...schemas) {
605
+ return schemas.flat();
606
+ }
607
+ function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
608
+ const d = typeof date === "number" ? new Date(date) : date;
609
+ const year = d.getFullYear();
610
+ const month = String(d.getMonth() + 1).padStart(2, "0");
611
+ const day = String(d.getDate()).padStart(2, "0");
612
+ const hours = String(d.getHours()).padStart(2, "0");
613
+ const minutes = String(d.getMinutes()).padStart(2, "0");
614
+ const seconds = String(d.getSeconds()).padStart(2, "0");
615
+ return format.replace("YYYY", String(year)).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
616
+ }
617
+ function formatNumber(num) {
618
+ return num.toLocaleString("zh-CN");
619
+ }
620
+ function formatFileSize(bytes) {
621
+ if (bytes === 0) return "0 B";
622
+ const k = 1024;
623
+ const sizes = ["B", "KB", "MB", "GB", "TB"];
624
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
625
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
626
+ }
627
+ function isValidEmail(email) {
628
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
629
+ return emailRegex.test(email);
630
+ }
631
+ function isValidPhone(phone) {
632
+ const phoneRegex = /^1[3-9]\d{9}$/;
633
+ return phoneRegex.test(phone);
634
+ }
635
+ function isValidUrl(url) {
636
+ try {
637
+ new URL(url);
638
+ return true;
639
+ } catch {
640
+ return false;
641
+ }
642
+ }
643
+ function isValidIdCard(idCard) {
644
+ const idCardRegex = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
645
+ return idCardRegex.test(idCard);
646
+ }
647
+ function useDebounce(value, delay = 300) {
648
+ const debouncedValue = vue.ref(value.value);
649
+ let timer = null;
650
+ const updateValue = (newValue) => {
651
+ if (timer) {
652
+ clearTimeout(timer);
653
+ }
654
+ timer = setTimeout(() => {
655
+ debouncedValue.value = newValue;
656
+ }, delay);
657
+ };
658
+ vue.watch(
659
+ value,
660
+ (newValue) => {
661
+ updateValue(newValue);
662
+ },
663
+ { immediate: true }
664
+ );
665
+ vue.onBeforeUnmount(() => {
666
+ if (timer) {
667
+ clearTimeout(timer);
668
+ }
669
+ });
670
+ return debouncedValue;
671
+ }
672
+ function useLocalStorage(key, initialValue) {
673
+ let initialStoredValue;
674
+ try {
675
+ const item = window.localStorage.getItem(key);
676
+ initialStoredValue = item ? JSON.parse(item) : initialValue;
677
+ } catch (error) {
678
+ console.error(`Error reading localStorage key "${key}":`, error);
679
+ initialStoredValue = initialValue;
680
+ }
681
+ const storedValue = vue.ref(initialStoredValue);
682
+ const setValue = (value) => {
683
+ try {
684
+ const valueToStore = value instanceof Function ? value(storedValue.value) : value;
685
+ storedValue.value = valueToStore;
686
+ window.localStorage.setItem(key, JSON.stringify(valueToStore));
687
+ } catch (error) {
688
+ console.error(`Error setting localStorage key "${key}":`, error);
689
+ }
690
+ };
691
+ vue.watch(
692
+ storedValue,
693
+ (newValue) => {
694
+ try {
695
+ window.localStorage.setItem(key, JSON.stringify(newValue));
696
+ } catch (error) {
697
+ console.error(`Error syncing localStorage key "${key}":`, error);
698
+ }
699
+ },
700
+ { deep: true }
701
+ );
702
+ return [storedValue, setValue];
703
+ }
704
+ function useToggle(initialValue = false) {
705
+ const value = vue.ref(initialValue);
706
+ const toggle = () => {
707
+ value.value = !value.value;
708
+ };
709
+ const setTrue = () => {
710
+ value.value = true;
711
+ };
712
+ const setFalse = () => {
713
+ value.value = false;
714
+ };
715
+ return [value, toggle, setTrue, setFalse];
716
+ }
717
+ exports.BasicForm = _sfc_main;
718
+ exports.componentMap = componentMap;
719
+ exports.createFormSchema = createFormSchema;
720
+ exports.formatDate = formatDate;
721
+ exports.formatFileSize = formatFileSize;
722
+ exports.formatNumber = formatNumber;
723
+ exports.getComponent = getComponent;
724
+ exports.isValidEmail = isValidEmail;
725
+ exports.isValidIdCard = isValidIdCard;
726
+ exports.isValidPhone = isValidPhone;
727
+ exports.isValidUrl = isValidUrl;
728
+ exports.mergeFormSchemas = mergeFormSchemas;
729
+ exports.registerComponent = registerComponent;
730
+ exports.useDebounce = useDebounce;
731
+ exports.useForm = useForm;
732
+ exports.useLocalStorage = useLocalStorage;
733
+ exports.useToggle = useToggle;
734
+ //# sourceMappingURL=index.cjs.js.map