@bitrix24/b24ui-nuxt 0.5.8 → 0.5.10
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/.nuxt/b24ui/navigation-menu.ts +0 -2
- package/.nuxt/b24ui/popover.ts +1 -1
- package/.nuxt/b24ui/textarea.ts +43 -0
- package/dist/meta.d.mts +5090 -4900
- package/dist/meta.mjs +5090 -4900
- package/dist/module.json +5 -5
- package/dist/module.mjs +11 -7
- package/dist/runtime/components/Advice.vue +47 -54
- package/dist/runtime/components/Advice.vue.d.ts +170 -0
- package/dist/runtime/components/Alert.vue +71 -96
- package/dist/runtime/components/Alert.vue.d.ts +464 -0
- package/dist/runtime/components/App.vue +37 -34
- package/dist/runtime/components/App.vue.d.ts +23 -0
- package/dist/runtime/components/Avatar.vue +69 -81
- package/dist/runtime/components/Avatar.vue.d.ts +281 -0
- package/dist/runtime/components/AvatarGroup.vue +53 -76
- package/dist/runtime/components/AvatarGroup.vue.d.ts +204 -0
- package/dist/runtime/components/Badge.vue +83 -83
- package/dist/runtime/components/Badge.vue.d.ts +517 -0
- package/dist/runtime/components/Button.vue +219 -149
- package/dist/runtime/components/Button.vue.d.ts +640 -0
- package/dist/runtime/components/ButtonGroup.vue +35 -51
- package/dist/runtime/components/ButtonGroup.vue.d.ts +116 -0
- package/dist/runtime/components/Calendar.vue +186 -152
- package/dist/runtime/components/Calendar.vue.d.ts +437 -0
- package/dist/runtime/components/Checkbox.vue +84 -73
- package/dist/runtime/components/Checkbox.vue.d.ts +354 -0
- package/dist/runtime/components/Chip.vue +59 -74
- package/dist/runtime/components/Chip.vue.d.ts +271 -0
- package/dist/runtime/components/Collapsible.vue +44 -41
- package/dist/runtime/components/Collapsible.vue.d.ts +118 -0
- package/dist/runtime/components/Container.vue +18 -27
- package/dist/runtime/components/Container.vue.d.ts +27 -0
- package/dist/runtime/components/Countdown.vue +198 -378
- package/dist/runtime/components/Countdown.vue.d.ts +356 -0
- package/dist/runtime/components/DescriptionList.vue +102 -149
- package/dist/runtime/components/DescriptionList.vue.d.ts +379 -0
- package/dist/runtime/components/DropdownMenu.vue +83 -139
- package/dist/runtime/components/DropdownMenu.vue.d.ts +533 -0
- package/dist/runtime/components/DropdownMenuContent.vue +137 -81
- package/dist/runtime/components/DropdownMenuContent.vue.d.ts +228 -0
- package/dist/runtime/components/Form.vue +162 -216
- package/dist/runtime/components/Form.vue.d.ts +55 -0
- package/dist/runtime/components/FormField.vue +76 -80
- package/dist/runtime/components/FormField.vue.d.ts +282 -0
- package/dist/runtime/components/Input.vue +166 -185
- package/dist/runtime/components/Input.vue.d.ts +755 -0
- package/dist/runtime/components/InputMenu.vue +300 -381
- package/dist/runtime/components/InputMenu.vue.d.ts +1504 -0
- package/dist/runtime/components/InputNumber.vue +180 -177
- package/dist/runtime/components/InputNumber.vue.d.ts +658 -0
- package/dist/runtime/components/Kbd.vue +33 -45
- package/dist/runtime/components/Kbd.vue.d.ts +109 -0
- package/dist/runtime/components/Link.vue +179 -173
- package/dist/runtime/components/Link.vue.d.ts +129 -0
- package/dist/runtime/components/LinkBase.vue +64 -42
- package/dist/runtime/components/LinkBase.vue.d.ts +48 -0
- package/dist/runtime/components/Modal.vue +105 -127
- package/dist/runtime/components/Modal.vue.d.ts +327 -0
- package/dist/runtime/components/ModalDialogClose.vue +4 -8
- package/dist/runtime/components/ModalDialogClose.vue.d.ts +10 -0
- package/dist/runtime/components/Navbar.vue +24 -33
- package/dist/runtime/components/Navbar.vue.d.ts +101 -0
- package/dist/runtime/components/NavbarDivider.vue +24 -33
- package/dist/runtime/components/NavbarDivider.vue.d.ts +101 -0
- package/dist/runtime/components/NavbarSection.vue +24 -33
- package/dist/runtime/components/NavbarSection.vue.d.ts +101 -0
- package/dist/runtime/components/NavbarSpacer.vue +24 -33
- package/dist/runtime/components/NavbarSpacer.vue.d.ts +101 -0
- package/dist/runtime/components/NavigationMenu.vue +152 -216
- package/dist/runtime/components/NavigationMenu.vue.d.ts +824 -0
- package/dist/runtime/components/OverlayProvider.vue +13 -17
- package/dist/runtime/components/OverlayProvider.vue.d.ts +2 -0
- package/dist/runtime/components/Popover.vue +81 -81
- package/dist/runtime/components/Popover.vue.d.ts +147 -0
- package/dist/runtime/components/Progress.vue +109 -136
- package/dist/runtime/components/Progress.vue.d.ts +592 -0
- package/dist/runtime/components/RadioGroup.vue +120 -134
- package/dist/runtime/components/RadioGroup.vue.d.ts +723 -0
- package/dist/runtime/components/Range.vue +94 -85
- package/dist/runtime/components/Range.vue.d.ts +417 -0
- package/dist/runtime/components/Select.vue +212 -260
- package/dist/runtime/components/Select.vue.d.ts +1200 -0
- package/dist/runtime/components/SelectMenu.vue +272 -366
- package/dist/runtime/components/SelectMenu.vue.d.ts +1298 -0
- package/dist/runtime/components/Separator.vue +61 -71
- package/dist/runtime/components/Separator.vue.d.ts +400 -0
- package/dist/runtime/components/Sidebar.vue +24 -33
- package/dist/runtime/components/Sidebar.vue.d.ts +101 -0
- package/dist/runtime/components/SidebarBody.vue +30 -38
- package/dist/runtime/components/SidebarBody.vue.d.ts +90 -0
- package/dist/runtime/components/SidebarFooter.vue +24 -33
- package/dist/runtime/components/SidebarFooter.vue.d.ts +101 -0
- package/dist/runtime/components/SidebarHeader.vue +24 -33
- package/dist/runtime/components/SidebarHeader.vue.d.ts +101 -0
- package/dist/runtime/components/SidebarHeading.vue +24 -33
- package/dist/runtime/components/SidebarHeading.vue.d.ts +101 -0
- package/dist/runtime/components/SidebarLayout.vue +40 -70
- package/dist/runtime/components/SidebarLayout.vue.d.ts +222 -0
- package/dist/runtime/components/SidebarSection.vue +24 -33
- package/dist/runtime/components/SidebarSection.vue.d.ts +101 -0
- package/dist/runtime/components/SidebarSpacer.vue +24 -33
- package/dist/runtime/components/SidebarSpacer.vue.d.ts +101 -0
- package/dist/runtime/components/Skeleton.vue +17 -22
- package/dist/runtime/components/Skeleton.vue.d.ts +26 -0
- package/dist/runtime/components/Slideover.vue +108 -131
- package/dist/runtime/components/Slideover.vue.d.ts +360 -0
- package/dist/runtime/components/StackedLayout.vue +40 -73
- package/dist/runtime/components/StackedLayout.vue.d.ts +192 -0
- package/dist/runtime/components/Switch.vue +100 -95
- package/dist/runtime/components/Switch.vue.d.ts +587 -0
- package/dist/runtime/components/Tabs.vue +83 -105
- package/dist/runtime/components/Tabs.vue.d.ts +453 -0
- package/dist/runtime/components/Textarea.vue +215 -203
- package/dist/runtime/components/Textarea.vue.d.ts +601 -0
- package/dist/runtime/components/Toast.vue +94 -105
- package/dist/runtime/components/Toast.vue.d.ts +438 -0
- package/dist/runtime/components/Toaster.vue +94 -111
- package/dist/runtime/components/Toaster.vue.d.ts +219 -0
- package/dist/runtime/components/Tooltip.vue +78 -64
- package/dist/runtime/components/Tooltip.vue.d.ts +186 -0
- package/dist/runtime/components/content/TableWrapper.vue +58 -70
- package/dist/runtime/components/content/TableWrapper.vue.d.ts +237 -0
- package/dist/runtime/composables/useAvatarGroup.d.ts +1 -1
- package/dist/runtime/composables/useButtonGroup.d.ts +2 -2
- package/dist/runtime/composables/useComponentIcons.d.ts +3 -3
- package/dist/runtime/composables/useFormField.d.ts +1 -1
- package/dist/runtime/prose/A.vue +18 -23
- package/dist/runtime/prose/A.vue.d.ts +84 -0
- package/dist/runtime/prose/Blockquote.vue +18 -23
- package/dist/runtime/prose/Blockquote.vue.d.ts +84 -0
- package/dist/runtime/prose/Code.vue +23 -31
- package/dist/runtime/prose/Code.vue.d.ts +97 -0
- package/dist/runtime/prose/Em.vue +18 -23
- package/dist/runtime/prose/Em.vue.d.ts +84 -0
- package/dist/runtime/prose/H1.vue +18 -23
- package/dist/runtime/prose/H1.vue.d.ts +97 -0
- package/dist/runtime/prose/H2.vue +18 -23
- package/dist/runtime/prose/H2.vue.d.ts +123 -0
- package/dist/runtime/prose/H3.vue +18 -23
- package/dist/runtime/prose/H3.vue.d.ts +123 -0
- package/dist/runtime/prose/H4.vue +18 -23
- package/dist/runtime/prose/H4.vue.d.ts +123 -0
- package/dist/runtime/prose/H5.vue +18 -23
- package/dist/runtime/prose/H5.vue.d.ts +123 -0
- package/dist/runtime/prose/H6.vue +18 -23
- package/dist/runtime/prose/H6.vue.d.ts +123 -0
- package/dist/runtime/prose/Hr.vue +18 -19
- package/dist/runtime/prose/Hr.vue.d.ts +74 -0
- package/dist/runtime/prose/Img.vue +18 -23
- package/dist/runtime/prose/Img.vue.d.ts +77 -0
- package/dist/runtime/prose/Li.vue +18 -23
- package/dist/runtime/prose/Li.vue.d.ts +84 -0
- package/dist/runtime/prose/Ol.vue +18 -23
- package/dist/runtime/prose/Ol.vue.d.ts +84 -0
- package/dist/runtime/prose/P.vue +18 -23
- package/dist/runtime/prose/P.vue.d.ts +84 -0
- package/dist/runtime/prose/Pre.vue +28 -33
- package/dist/runtime/prose/Pre.vue.d.ts +117 -0
- package/dist/runtime/prose/Strong.vue +18 -23
- package/dist/runtime/prose/Strong.vue.d.ts +84 -0
- package/dist/runtime/prose/Table.vue +44 -54
- package/dist/runtime/prose/Table.vue.d.ts +144 -0
- package/dist/runtime/prose/Tbody.vue +18 -23
- package/dist/runtime/prose/Tbody.vue.d.ts +84 -0
- package/dist/runtime/prose/Td.vue +18 -23
- package/dist/runtime/prose/Td.vue.d.ts +84 -0
- package/dist/runtime/prose/Th.vue +18 -23
- package/dist/runtime/prose/Th.vue.d.ts +84 -0
- package/dist/runtime/prose/Thead.vue +18 -23
- package/dist/runtime/prose/Thead.vue.d.ts +84 -0
- package/dist/runtime/prose/Tr.vue +18 -23
- package/dist/runtime/prose/Tr.vue.d.ts +84 -0
- package/dist/runtime/prose/Ul.vue +18 -23
- package/dist/runtime/prose/Ul.vue.d.ts +84 -0
- package/dist/runtime/vue/components/Link.vue +201 -202
- package/dist/runtime/vue/components/Link.vue.d.ts +129 -0
- package/dist/runtime/vue/stubs.d.ts +2 -2
- package/dist/shared/{b24ui-nuxt.DBFS6LH7.mjs → b24ui-nuxt.CS9Lf0os.mjs} +54 -4
- package/dist/types.d.mts +3 -5
- package/dist/unplugin.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +62 -35
- package/dist/meta.cjs +0 -71922
- package/dist/meta.d.cts +0 -71920
- package/dist/meta.d.ts +0 -71920
- package/dist/module.cjs +0 -59
- package/dist/module.d.cts +0 -15
- package/dist/module.d.ts +0 -15
- package/dist/shared/b24ui-nuxt.B9fPD5yO.cjs +0 -7671
- package/dist/types.d.ts +0 -7
- package/dist/unplugin.cjs +0 -236
- package/dist/unplugin.d.cts +0 -33
- package/dist/unplugin.d.ts +0 -33
- package/dist/vite.cjs +0 -21
- package/dist/vite.d.cts +0 -14
- package/dist/vite.d.ts +0 -14
|
@@ -1,285 +1,231 @@
|
|
|
1
|
-
<script
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import type { DeepReadonly } from 'vue'
|
|
8
|
-
|
|
9
|
-
const appConfigForm = _appConfig as AppConfig & { b24ui: { form: Partial<typeof theme> } }
|
|
10
|
-
|
|
11
|
-
const form = tv({ extend: tv(theme), ...(appConfigForm.b24ui?.form || {}) })
|
|
12
|
-
|
|
13
|
-
export interface FormProps<T extends object> {
|
|
14
|
-
id?: string | number
|
|
15
|
-
/** Schema to validate the form state. Supports Standard Schema objects, Yup, Joi, and Superstructs. */
|
|
16
|
-
schema?: FormSchema<T>
|
|
17
|
-
/** An object representing the current state of the form. */
|
|
18
|
-
state: Partial<T>
|
|
19
|
-
/**
|
|
20
|
-
* Custom validation function to validate the form state.
|
|
21
|
-
* @param state - The current state of the form.
|
|
22
|
-
* @returns A promise that resolves to an array of FormError objects, or an array of FormError objects directly.
|
|
23
|
-
*/
|
|
24
|
-
validate?: (state: Partial<T>) => Promise<FormError[]> | FormError[]
|
|
25
|
-
/**
|
|
26
|
-
* The list of input events that trigger the form validation.
|
|
27
|
-
* @defaultValue `['blur', 'change', 'input']`
|
|
28
|
-
*/
|
|
29
|
-
validateOn?: FormInputEvents[]
|
|
30
|
-
/** Disable all inputs inside the form. */
|
|
31
|
-
disabled?: boolean
|
|
32
|
-
/**
|
|
33
|
-
* Delay in milliseconds before validating the form on input events.
|
|
34
|
-
* @defaultValue `300`
|
|
35
|
-
*/
|
|
36
|
-
validateOnInputDelay?: number
|
|
37
|
-
/**
|
|
38
|
-
* If true, schema transformations will be applied to the state on submit.
|
|
39
|
-
* @defaultValue `true`
|
|
40
|
-
*/
|
|
41
|
-
transform?: boolean
|
|
42
|
-
class?: any
|
|
43
|
-
onSubmit?: ((event: FormSubmitEvent<T>) => void | Promise<void>) | (() => void | Promise<void>)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface FormEmits<T extends object> {
|
|
47
|
-
(e: 'submit', payload: FormSubmitEvent<T>): void
|
|
48
|
-
(e: 'error', payload: FormErrorEvent): void
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface FormSlots {
|
|
52
|
-
default(props?: { errors: FormError[] }): any
|
|
53
|
-
}
|
|
1
|
+
<script>
|
|
2
|
+
import _appConfig from "#build/app.config";
|
|
3
|
+
import theme from "#build/b24ui/form";
|
|
4
|
+
import { tv } from "../utils/tv";
|
|
5
|
+
const appConfigForm = _appConfig;
|
|
6
|
+
const form = tv({ extend: tv(theme), ...appConfigForm.b24ui?.form || {} });
|
|
54
7
|
</script>
|
|
55
8
|
|
|
56
|
-
<script
|
|
57
|
-
import { provide, inject, nextTick, ref, onUnmounted, onMounted, computed, useId, readonly } from
|
|
58
|
-
import { useEventBus } from
|
|
59
|
-
import { formOptionsInjectionKey, formInputsInjectionKey, formBusInjectionKey, formLoadingInjectionKey } from
|
|
60
|
-
import { validateSchema } from
|
|
61
|
-
import { FormValidationException } from
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
9
|
+
<script setup>
|
|
10
|
+
import { provide, inject, nextTick, ref, onUnmounted, onMounted, computed, useId, readonly } from "vue";
|
|
11
|
+
import { useEventBus } from "@vueuse/core";
|
|
12
|
+
import { formOptionsInjectionKey, formInputsInjectionKey, formBusInjectionKey, formLoadingInjectionKey } from "../composables/useFormField";
|
|
13
|
+
import { validateSchema } from "../utils/form";
|
|
14
|
+
import { FormValidationException } from "../types/form";
|
|
15
|
+
const props = defineProps({
|
|
16
|
+
id: {
|
|
17
|
+
type: [String, Number],
|
|
18
|
+
required: false
|
|
66
19
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
20
|
+
schema: {
|
|
21
|
+
type: null,
|
|
22
|
+
required: false
|
|
23
|
+
},
|
|
24
|
+
state: {
|
|
25
|
+
type: Object,
|
|
26
|
+
required: true
|
|
27
|
+
},
|
|
28
|
+
validate: {
|
|
29
|
+
type: Function,
|
|
30
|
+
required: false
|
|
31
|
+
},
|
|
32
|
+
validateOn: {
|
|
33
|
+
type: Array,
|
|
34
|
+
required: false,
|
|
35
|
+
default() {
|
|
36
|
+
return ["input", "blur", "change"];
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
disabled: {
|
|
40
|
+
type: Boolean,
|
|
41
|
+
required: false
|
|
42
|
+
},
|
|
43
|
+
validateOnInputDelay: {
|
|
44
|
+
type: Number,
|
|
45
|
+
required: false,
|
|
46
|
+
default: 300
|
|
47
|
+
},
|
|
48
|
+
transform: {
|
|
49
|
+
type: Boolean,
|
|
50
|
+
required: false,
|
|
51
|
+
default: true
|
|
52
|
+
},
|
|
53
|
+
class: {
|
|
54
|
+
type: null,
|
|
55
|
+
required: false
|
|
56
|
+
},
|
|
57
|
+
onSubmit: {
|
|
58
|
+
type: Function,
|
|
59
|
+
required: false
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const emits = defineEmits(["submit", "error"]);
|
|
63
|
+
defineSlots();
|
|
64
|
+
const formId = props.id ?? useId();
|
|
65
|
+
const bus = useEventBus(`form-${formId}`);
|
|
76
66
|
const parentBus = inject(
|
|
77
67
|
formBusInjectionKey,
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const nestedForms = ref<Map<string | number, { validate: typeof _validate }>>(new Map())
|
|
84
|
-
|
|
68
|
+
void 0
|
|
69
|
+
);
|
|
70
|
+
provide(formBusInjectionKey, bus);
|
|
71
|
+
const nestedForms = ref(/* @__PURE__ */ new Map());
|
|
85
72
|
onMounted(async () => {
|
|
86
73
|
bus.on(async (event) => {
|
|
87
|
-
if (event.type ===
|
|
88
|
-
nestedForms.value.set(event.formId, { validate: event.validate })
|
|
89
|
-
} else if (event.type ===
|
|
90
|
-
nestedForms.value.delete(event.formId)
|
|
74
|
+
if (event.type === "attach") {
|
|
75
|
+
nestedForms.value.set(event.formId, { validate: event.validate });
|
|
76
|
+
} else if (event.type === "detach") {
|
|
77
|
+
nestedForms.value.delete(event.formId);
|
|
91
78
|
} else if (props.validateOn?.includes(event.type) && !loading.value) {
|
|
92
|
-
if (event.type !==
|
|
93
|
-
await _validate({ name: event.name, silent: true, nested: false })
|
|
79
|
+
if (event.type !== "input") {
|
|
80
|
+
await _validate({ name: event.name, silent: true, nested: false });
|
|
94
81
|
} else if (event.eager || blurredFields.has(event.name)) {
|
|
95
|
-
await _validate({ name: event.name, silent: true, nested: false })
|
|
82
|
+
await _validate({ name: event.name, silent: true, nested: false });
|
|
96
83
|
}
|
|
97
84
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
blurredFields.add(event.name)
|
|
85
|
+
if (event.type === "blur") {
|
|
86
|
+
blurredFields.add(event.name);
|
|
101
87
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
touchedFields.add(event.name)
|
|
88
|
+
if (event.type === "change" || event.type === "input" || event.type === "blur" || event.type === "focus") {
|
|
89
|
+
touchedFields.add(event.name);
|
|
105
90
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
dirtyFields.add(event.name)
|
|
91
|
+
if (event.type === "change" || event.type === "input") {
|
|
92
|
+
dirtyFields.add(event.name);
|
|
109
93
|
}
|
|
110
|
-
})
|
|
111
|
-
})
|
|
112
|
-
|
|
94
|
+
});
|
|
95
|
+
});
|
|
113
96
|
onUnmounted(() => {
|
|
114
|
-
bus.reset()
|
|
115
|
-
})
|
|
116
|
-
|
|
97
|
+
bus.reset();
|
|
98
|
+
});
|
|
117
99
|
onMounted(async () => {
|
|
118
100
|
if (parentBus) {
|
|
119
|
-
await nextTick()
|
|
120
|
-
parentBus.emit({ type:
|
|
101
|
+
await nextTick();
|
|
102
|
+
parentBus.emit({ type: "attach", validate: _validate, formId });
|
|
121
103
|
}
|
|
122
|
-
})
|
|
123
|
-
|
|
104
|
+
});
|
|
124
105
|
onUnmounted(() => {
|
|
125
106
|
if (parentBus) {
|
|
126
|
-
parentBus.emit({ type:
|
|
107
|
+
parentBus.emit({ type: "detach", formId });
|
|
127
108
|
}
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const blurredFields = new Set<keyof T>()
|
|
139
|
-
|
|
140
|
-
function resolveErrorIds(errs: FormError[]): FormErrorWithId[] {
|
|
141
|
-
return errs.map(err => ({
|
|
109
|
+
});
|
|
110
|
+
const errors = ref([]);
|
|
111
|
+
provide("form-errors", errors);
|
|
112
|
+
const inputs = ref({});
|
|
113
|
+
provide(formInputsInjectionKey, inputs);
|
|
114
|
+
const dirtyFields = /* @__PURE__ */ new Set();
|
|
115
|
+
const touchedFields = /* @__PURE__ */ new Set();
|
|
116
|
+
const blurredFields = /* @__PURE__ */ new Set();
|
|
117
|
+
function resolveErrorIds(errs) {
|
|
118
|
+
return errs.map((err) => ({
|
|
142
119
|
...err,
|
|
143
|
-
id: err?.name ? inputs.value[err.name]?.id :
|
|
144
|
-
}))
|
|
120
|
+
id: err?.name ? inputs.value[err.name]?.id : void 0
|
|
121
|
+
}));
|
|
145
122
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
async function getErrors(): Promise<FormErrorWithId[]> {
|
|
150
|
-
let errs = props.validate ? (await props.validate(props.state)) ?? [] : []
|
|
151
|
-
|
|
123
|
+
const transformedState = ref(null);
|
|
124
|
+
async function getErrors() {
|
|
125
|
+
let errs = props.validate ? await props.validate(props.state) ?? [] : [];
|
|
152
126
|
if (props.schema) {
|
|
153
|
-
const { errors, result } = await validateSchema(props.state, props.schema
|
|
154
|
-
if (
|
|
155
|
-
errs = errs.concat(
|
|
127
|
+
const { errors: errors2, result } = await validateSchema(props.state, props.schema);
|
|
128
|
+
if (errors2) {
|
|
129
|
+
errs = errs.concat(errors2);
|
|
156
130
|
} else {
|
|
157
|
-
transformedState.value = result
|
|
131
|
+
transformedState.value = result;
|
|
158
132
|
}
|
|
159
133
|
}
|
|
160
|
-
|
|
161
|
-
return resolveErrorIds(errs)
|
|
134
|
+
return resolveErrorIds(errs);
|
|
162
135
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return error
|
|
174
|
-
})
|
|
175
|
-
)
|
|
176
|
-
: []
|
|
177
|
-
|
|
136
|
+
async function _validate(opts = { silent: false, nested: true, transform: false }) {
|
|
137
|
+
const names = opts.name && !Array.isArray(opts.name) ? [opts.name] : opts.name;
|
|
138
|
+
const nestedValidatePromises = !names && opts.nested ? Array.from(nestedForms.value.values()).map(
|
|
139
|
+
({ validate }) => validate(opts).then(() => void 0).catch((error) => {
|
|
140
|
+
if (!(error instanceof FormValidationException)) {
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
return error;
|
|
144
|
+
})
|
|
145
|
+
) : [];
|
|
178
146
|
if (names) {
|
|
179
|
-
const otherErrors = errors.value.filter(error => !names.some((name) => {
|
|
180
|
-
const pattern = inputs.value?.[name]?.pattern
|
|
181
|
-
return name === error.name ||
|
|
182
|
-
}))
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
errors.value = otherErrors.concat(pathErrors)
|
|
147
|
+
const otherErrors = errors.value.filter((error) => !names.some((name) => {
|
|
148
|
+
const pattern = inputs.value?.[name]?.pattern;
|
|
149
|
+
return name === error.name || pattern && error.name?.match(pattern);
|
|
150
|
+
}));
|
|
151
|
+
const pathErrors = (await getErrors()).filter((error) => names.some((name) => {
|
|
152
|
+
const pattern = inputs.value?.[name]?.pattern;
|
|
153
|
+
return name === error.name || pattern && error.name?.match(pattern);
|
|
154
|
+
}));
|
|
155
|
+
errors.value = otherErrors.concat(pathErrors);
|
|
190
156
|
} else {
|
|
191
|
-
errors.value = await getErrors()
|
|
157
|
+
errors.value = await getErrors();
|
|
192
158
|
}
|
|
193
|
-
|
|
194
|
-
const childErrors = (await Promise.all(nestedValidatePromises)).filter(val => val !== undefined)
|
|
195
|
-
|
|
159
|
+
const childErrors = (await Promise.all(nestedValidatePromises)).filter((val) => val !== void 0);
|
|
196
160
|
if (errors.value.length + childErrors.length > 0) {
|
|
197
|
-
if (opts.silent) return false
|
|
198
|
-
throw new FormValidationException(formId, errors.value, childErrors)
|
|
161
|
+
if (opts.silent) return false;
|
|
162
|
+
throw new FormValidationException(formId, errors.value, childErrors);
|
|
199
163
|
}
|
|
200
|
-
|
|
201
164
|
if (opts.transform) {
|
|
202
|
-
Object.assign(props.state, transformedState.value)
|
|
165
|
+
Object.assign(props.state, transformedState.value);
|
|
203
166
|
}
|
|
204
|
-
|
|
205
|
-
return props.state as T
|
|
167
|
+
return props.state;
|
|
206
168
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
loading.value = true
|
|
213
|
-
|
|
214
|
-
const event = payload as FormSubmitEvent<any>
|
|
215
|
-
|
|
169
|
+
const loading = ref(false);
|
|
170
|
+
provide(formLoadingInjectionKey, readonly(loading));
|
|
171
|
+
async function onSubmitWrapper(payload) {
|
|
172
|
+
loading.value = true;
|
|
173
|
+
const event = payload;
|
|
216
174
|
try {
|
|
217
|
-
event.data = await _validate({ nested: true, transform: props.transform })
|
|
218
|
-
await props.onSubmit?.(event)
|
|
219
|
-
dirtyFields.clear()
|
|
175
|
+
event.data = await _validate({ nested: true, transform: props.transform });
|
|
176
|
+
await props.onSubmit?.(event);
|
|
177
|
+
dirtyFields.clear();
|
|
220
178
|
} catch (error) {
|
|
221
179
|
if (!(error instanceof FormValidationException)) {
|
|
222
|
-
throw error
|
|
180
|
+
throw error;
|
|
223
181
|
}
|
|
224
|
-
|
|
225
|
-
const errorEvent: FormErrorEvent = {
|
|
182
|
+
const errorEvent = {
|
|
226
183
|
...event,
|
|
227
184
|
errors: error.errors,
|
|
228
185
|
children: error.children
|
|
229
|
-
}
|
|
230
|
-
emits(
|
|
186
|
+
};
|
|
187
|
+
emits("error", errorEvent);
|
|
231
188
|
} finally {
|
|
232
|
-
loading.value = false
|
|
189
|
+
loading.value = false;
|
|
233
190
|
}
|
|
234
191
|
}
|
|
235
|
-
|
|
236
|
-
const disabled = computed(() => props.disabled || loading.value)
|
|
237
|
-
|
|
192
|
+
const disabled = computed(() => props.disabled || loading.value);
|
|
238
193
|
provide(formOptionsInjectionKey, computed(() => ({
|
|
239
194
|
disabled: disabled.value,
|
|
240
195
|
validateOnInputDelay: props.validateOnInputDelay
|
|
241
|
-
})))
|
|
242
|
-
|
|
243
|
-
defineExpose<Form<T>>({
|
|
196
|
+
})));
|
|
197
|
+
defineExpose({
|
|
244
198
|
validate: _validate,
|
|
245
199
|
errors,
|
|
246
|
-
|
|
247
|
-
setErrors(errs: FormError[], name?: keyof T) {
|
|
200
|
+
setErrors(errs, name) {
|
|
248
201
|
if (name) {
|
|
249
|
-
errors.value = errors.value
|
|
250
|
-
.filter(error => error.name !== name)
|
|
251
|
-
.concat(resolveErrorIds(errs))
|
|
202
|
+
errors.value = errors.value.filter((error) => error.name !== name).concat(resolveErrorIds(errs));
|
|
252
203
|
} else {
|
|
253
|
-
errors.value = resolveErrorIds(errs)
|
|
204
|
+
errors.value = resolveErrorIds(errs);
|
|
254
205
|
}
|
|
255
206
|
},
|
|
256
|
-
|
|
257
207
|
async submit() {
|
|
258
|
-
await onSubmitWrapper(new Event(
|
|
208
|
+
await onSubmitWrapper(new Event("submit"));
|
|
259
209
|
},
|
|
260
|
-
|
|
261
|
-
getErrors(name?: keyof T) {
|
|
210
|
+
getErrors(name) {
|
|
262
211
|
if (name) {
|
|
263
|
-
return errors.value.filter(err => err.name === name)
|
|
212
|
+
return errors.value.filter((err) => err.name === name);
|
|
264
213
|
}
|
|
265
|
-
return errors.value
|
|
214
|
+
return errors.value;
|
|
266
215
|
},
|
|
267
|
-
|
|
268
|
-
clear(name?: string) {
|
|
216
|
+
clear(name) {
|
|
269
217
|
if (name) {
|
|
270
|
-
errors.value = errors.value.filter(err => err.name !== name)
|
|
218
|
+
errors.value = errors.value.filter((err) => err.name !== name);
|
|
271
219
|
} else {
|
|
272
|
-
errors.value = []
|
|
220
|
+
errors.value = [];
|
|
273
221
|
}
|
|
274
222
|
},
|
|
275
|
-
|
|
276
223
|
disabled,
|
|
277
224
|
dirty: computed(() => !!dirtyFields.size),
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
})
|
|
225
|
+
dirtyFields: readonly(dirtyFields),
|
|
226
|
+
blurredFields: readonly(blurredFields),
|
|
227
|
+
touchedFields: readonly(touchedFields)
|
|
228
|
+
});
|
|
283
229
|
</script>
|
|
284
230
|
|
|
285
231
|
<template>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { FormSchema, FormError, FormInputEvents, FormErrorEvent, FormSubmitEvent, Form } from '../types/form';
|
|
2
|
+
export interface FormProps<T extends object> {
|
|
3
|
+
id?: string | number;
|
|
4
|
+
/** Schema to validate the form state. Supports Standard Schema objects, Yup, Joi, and Superstructs. */
|
|
5
|
+
schema?: FormSchema<T>;
|
|
6
|
+
/** An object representing the current state of the form. */
|
|
7
|
+
state: Partial<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Custom validation function to validate the form state.
|
|
10
|
+
* @param state - The current state of the form.
|
|
11
|
+
* @returns A promise that resolves to an array of FormError objects, or an array of FormError objects directly.
|
|
12
|
+
*/
|
|
13
|
+
validate?: (state: Partial<T>) => Promise<FormError[]> | FormError[];
|
|
14
|
+
/**
|
|
15
|
+
* The list of input events that trigger the form validation.
|
|
16
|
+
* @defaultValue `['blur', 'change', 'input']`
|
|
17
|
+
*/
|
|
18
|
+
validateOn?: FormInputEvents[];
|
|
19
|
+
/** Disable all inputs inside the form. */
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Delay in milliseconds before validating the form on input events.
|
|
23
|
+
* @defaultValue `300`
|
|
24
|
+
*/
|
|
25
|
+
validateOnInputDelay?: number;
|
|
26
|
+
/**
|
|
27
|
+
* If true, schema transformations will be applied to the state on submit.
|
|
28
|
+
* @defaultValue `true`
|
|
29
|
+
*/
|
|
30
|
+
transform?: boolean;
|
|
31
|
+
class?: any;
|
|
32
|
+
onSubmit?: ((event: FormSubmitEvent<T>) => void | Promise<void>) | (() => void | Promise<void>);
|
|
33
|
+
}
|
|
34
|
+
export interface FormEmits<T extends object> {
|
|
35
|
+
(e: 'submit', payload: FormSubmitEvent<T>): void;
|
|
36
|
+
(e: 'error', payload: FormErrorEvent): void;
|
|
37
|
+
}
|
|
38
|
+
export interface FormSlots {
|
|
39
|
+
default(props?: {
|
|
40
|
+
errors: FormError[];
|
|
41
|
+
}): any;
|
|
42
|
+
}
|
|
43
|
+
declare const _default: <T extends object>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
|
|
44
|
+
props: __VLS_PrettifyLocal<any & FormProps<T> & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
|
|
45
|
+
expose(exposed: import("vue").ShallowUnwrapRef<Form<T>>): void;
|
|
46
|
+
attrs: any;
|
|
47
|
+
slots: Readonly<FormSlots> & FormSlots;
|
|
48
|
+
emit: FormEmits<T>;
|
|
49
|
+
}>) => import("vue").VNode & {
|
|
50
|
+
__ctx?: Awaited<typeof __VLS_setup>;
|
|
51
|
+
};
|
|
52
|
+
export default _default;
|
|
53
|
+
type __VLS_PrettifyLocal<T> = {
|
|
54
|
+
[K in keyof T]: T[K];
|
|
55
|
+
} & {};
|