nuxt-ui-tools 1.3.0 → 1.4.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.
- package/dist/module.json +1 -1
- package/dist/module.mjs +8 -1
- package/dist/runtime/form/components/actions/form-actions.vue +13 -127
- package/dist/runtime/form/components/page/form-page-actions.d.vue.ts +3 -0
- package/dist/runtime/form/components/page/form-page-actions.vue +40 -0
- package/dist/runtime/form/components/page/form-page-actions.vue.d.ts +3 -0
- package/dist/runtime/form/components/page/form-page-header.d.vue.ts +24 -0
- package/dist/runtime/form/components/page/form-page-header.vue +99 -0
- package/dist/runtime/form/components/page/form-page-header.vue.d.ts +24 -0
- package/dist/runtime/form/components/page/form-page-navigation.d.vue.ts +34 -0
- package/dist/runtime/form/components/page/form-page-navigation.vue +229 -0
- package/dist/runtime/form/components/page/form-page-navigation.vue.d.ts +34 -0
- package/dist/runtime/form/components/page/form-page-section.d.vue.ts +19 -0
- package/dist/runtime/form/components/page/form-page-section.vue +114 -0
- package/dist/runtime/form/components/page/form-page-section.vue.d.ts +19 -0
- package/dist/runtime/form/components/page/form-page-sections.d.vue.ts +21 -0
- package/dist/runtime/form/components/page/form-page-sections.vue +75 -0
- package/dist/runtime/form/components/page/form-page-sections.vue.d.ts +21 -0
- package/dist/runtime/form/components/page/form-page.d.vue.ts +80 -0
- package/dist/runtime/form/components/page/form-page.vue +91 -0
- package/dist/runtime/form/components/page/form-page.vue.d.ts +80 -0
- package/dist/runtime/form/components/renderer/form-field-shell.vue +7 -11
- package/dist/runtime/form/components/root/form.vue +25 -194
- package/dist/runtime/form/composables/use-form-action-buttons.d.ts +32 -0
- package/dist/runtime/form/composables/use-form-action-buttons.js +89 -0
- package/dist/runtime/form/composables/use-form-actions.d.ts +8 -5
- package/dist/runtime/form/composables/use-form-actions.js +1 -1
- package/dist/runtime/form/composables/use-form-page-scroll.d.ts +22 -0
- package/dist/runtime/form/composables/use-form-page-scroll.js +178 -0
- package/dist/runtime/form/composables/use-form-page.d.ts +114 -0
- package/dist/runtime/form/composables/use-form-page.js +83 -0
- package/dist/runtime/form/composables/use-form-root.d.ts +34 -0
- package/dist/runtime/form/composables/use-form-root.js +171 -0
- package/dist/runtime/form/composables/use-form-runtime.js +13 -4
- package/dist/runtime/form/composables/use-form.js +2 -22
- package/dist/runtime/form/fields/checkbox-card/component.vue +28 -19
- package/dist/runtime/form/fields/checkbox-card/types.d.ts +2 -5
- package/dist/runtime/form/fields/choice-card/choice-card-label.d.vue.ts +17 -0
- package/dist/runtime/form/fields/choice-card/choice-card-label.vue +50 -0
- package/dist/runtime/form/fields/choice-card/choice-card-label.vue.d.ts +17 -0
- package/dist/runtime/form/fields/choice-card/types.d.ts +31 -0
- package/dist/runtime/form/fields/choice-card/types.js +0 -0
- package/dist/runtime/form/fields/choice-card/use-choice-card.d.ts +22 -0
- package/dist/runtime/form/fields/choice-card/use-choice-card.js +52 -0
- package/dist/runtime/form/fields/radio-card/component.vue +17 -18
- package/dist/runtime/form/fields/radio-card/types.d.ts +2 -4
- package/dist/runtime/form/index.d.ts +1 -1
- package/dist/runtime/form/index.js +7 -1
- package/dist/runtime/form/schema/index.d.ts +1 -1
- package/dist/runtime/form/schema/index.js +1 -0
- package/dist/runtime/form/schema/page.d.ts +50 -0
- package/dist/runtime/form/schema/page.js +25 -0
- package/dist/runtime/form/types/index.d.ts +1 -0
- package/dist/runtime/form/types/page.d.ts +172 -0
- package/dist/runtime/form/types/page.js +0 -0
- package/dist/runtime/form/types/ui.d.ts +60 -0
- package/dist/runtime/form/utils/controls.d.ts +15 -0
- package/dist/runtime/form/utils/controls.js +38 -0
- package/dist/runtime/form/utils/layout.js +3 -1
- package/dist/runtime/form/utils/page.d.ts +20 -0
- package/dist/runtime/form/utils/page.js +107 -0
- package/dist/runtime/form/utils/state.d.ts +2 -0
- package/dist/runtime/form/utils/state.js +1 -1
- package/dist/runtime/form/utils/ui.js +1 -0
- package/dist/runtime/i18n/locales/en.js +18 -0
- package/dist/runtime/i18n/locales/fr.js +18 -0
- package/dist/runtime/i18n/types.d.ts +29 -0
- package/package.json +1 -1
|
@@ -4,27 +4,17 @@ import UBadge from "@nuxt/ui/components/Badge.vue";
|
|
|
4
4
|
import UButton from "@nuxt/ui/components/Button.vue";
|
|
5
5
|
import USkeleton from "@nuxt/ui/components/Skeleton.vue";
|
|
6
6
|
import { useScrollShadow } from "@nuxt/ui/composables/useScrollShadow";
|
|
7
|
-
import {
|
|
8
|
-
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
|
9
|
-
import { useRouter } from "vue-router";
|
|
7
|
+
import { computed, nextTick, ref, watch } from "vue";
|
|
10
8
|
import { useUiToolsLocale } from "#ui-tools/i18n";
|
|
11
9
|
import { useFormActions } from "../../composables/use-form-actions";
|
|
12
10
|
import { useFormGridLayout } from "../../composables/use-form-layout";
|
|
13
|
-
import {
|
|
14
|
-
import { provideFormUi } from "../../composables/use-form-ui";
|
|
11
|
+
import { useFormRoot } from "../../composables/use-form-root";
|
|
15
12
|
import { getFormHeader } from "../../utils/overlay";
|
|
16
13
|
import { isRecord } from "../../utils/path";
|
|
17
|
-
import {
|
|
18
|
-
isBoolean,
|
|
19
|
-
isFunction,
|
|
20
|
-
isNumber,
|
|
21
|
-
isString,
|
|
22
|
-
isUndefined,
|
|
23
|
-
stringArray
|
|
24
|
-
} from "../../utils/predicate";
|
|
14
|
+
import { isFunction, isNumber, isString } from "../../utils/predicate";
|
|
25
15
|
import { getSchemaFields, getSchemaSteps, isSteppedSchema } from "../../utils/state";
|
|
26
16
|
import { resolveFormText } from "../../utils/text";
|
|
27
|
-
import {
|
|
17
|
+
import { mergeFormUiClass } from "../../utils/ui";
|
|
28
18
|
import FormActions from "../actions/form-actions.vue";
|
|
29
19
|
import FormFieldRenderer from "../renderer/form-field-renderer.vue";
|
|
30
20
|
import FormDirectionalTransition from "../utils/form-directional-transition.vue";
|
|
@@ -38,68 +28,28 @@ const props = defineProps({
|
|
|
38
28
|
showCloseButton: { type: Boolean, required: false },
|
|
39
29
|
ui: { type: Object, required: false }
|
|
40
30
|
});
|
|
41
|
-
const router = useRouter();
|
|
42
31
|
const emit = defineEmits(["submit", "cancel"]);
|
|
43
32
|
const { t } = useUiToolsLocale();
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const syncInputRef = computed(
|
|
53
|
-
() => props.form?.syncInput.value ?? props.syncInput ?? getSchemaSyncInput(schemaRef.value)
|
|
54
|
-
);
|
|
55
|
-
const validationModeRef = computed(
|
|
56
|
-
() => props.form?.validationMode.value ?? props.validate ?? getSchemaValidationMode(schemaRef.value)
|
|
57
|
-
);
|
|
58
|
-
const runtime = useFormRuntime({
|
|
59
|
-
input: inputRef,
|
|
33
|
+
const {
|
|
34
|
+
cancel,
|
|
35
|
+
contextError,
|
|
36
|
+
contextLoading,
|
|
37
|
+
contextPending,
|
|
38
|
+
formUi,
|
|
39
|
+
refreshContext,
|
|
40
|
+
runtime,
|
|
60
41
|
schema: schemaRef,
|
|
61
|
-
|
|
62
|
-
|
|
42
|
+
submit
|
|
43
|
+
} = useFormRoot({
|
|
44
|
+
form: () => props.form,
|
|
45
|
+
input: () => props.input,
|
|
46
|
+
onCancelled: (value) => emit("cancel", value),
|
|
47
|
+
onSubmitted: (value, result) => emit("submit", value, result),
|
|
48
|
+
schema: () => props.schema,
|
|
49
|
+
syncInput: () => props.syncInput,
|
|
50
|
+
ui: () => props.ui,
|
|
51
|
+
validate: () => props.validate
|
|
63
52
|
});
|
|
64
|
-
provideFormRuntime(runtime);
|
|
65
|
-
watch(
|
|
66
|
-
() => props.form,
|
|
67
|
-
(controller, previousController) => {
|
|
68
|
-
previousController?.unbind(runtime);
|
|
69
|
-
controller?.bind(runtime);
|
|
70
|
-
},
|
|
71
|
-
{ immediate: true }
|
|
72
|
-
);
|
|
73
|
-
onBeforeUnmount(() => props.form?.unbind(runtime));
|
|
74
|
-
onMounted(async () => {
|
|
75
|
-
await nextTick();
|
|
76
|
-
const target = getSchemaAutoFocus(schemaRef.value);
|
|
77
|
-
if (isString(target)) {
|
|
78
|
-
await runtime.focusField(target);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (target === true) {
|
|
82
|
-
await focusFirstRenderedField();
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
if (import.meta.client) {
|
|
86
|
-
let handleBeforeUnload = function(event) {
|
|
87
|
-
if (!shouldConfirmDirtyNavigation()) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
event.preventDefault();
|
|
91
|
-
event.returnValue = "";
|
|
92
|
-
};
|
|
93
|
-
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
94
|
-
onBeforeUnmount(() => window.removeEventListener("beforeunload", handleBeforeUnload));
|
|
95
|
-
}
|
|
96
|
-
const removeRouteGuard = router.beforeEach(() => {
|
|
97
|
-
if (!shouldConfirmDirtyNavigation()) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
return window.confirm(getDirtyNavigationMessage());
|
|
101
|
-
});
|
|
102
|
-
onBeforeUnmount(removeRouteGuard);
|
|
103
53
|
const viewportRef = ref(null);
|
|
104
54
|
const viewportShadow = useScrollShadow(viewportRef, { size: 20 });
|
|
105
55
|
const displayedStepIndex = ref(runtime.currentStepIndex.value);
|
|
@@ -114,9 +64,6 @@ watch(
|
|
|
114
64
|
});
|
|
115
65
|
}
|
|
116
66
|
);
|
|
117
|
-
const parentPath = computed(
|
|
118
|
-
() => runtime.currentStepRoot.value ? [runtime.currentStepRoot.value] : []
|
|
119
|
-
);
|
|
120
67
|
const displayedStep = computed(
|
|
121
68
|
() => isSteppedSchema(schemaRef.value) ? getSchemaSteps(schemaRef.value)[displayedStepIndex.value] : void 0
|
|
122
69
|
);
|
|
@@ -167,56 +114,10 @@ const footerClass = computed(
|
|
|
167
114
|
formUi.ui.value.root?.ui?.footer
|
|
168
115
|
)
|
|
169
116
|
);
|
|
170
|
-
const actions = useFormActions({
|
|
171
|
-
|
|
172
|
-
() =>
|
|
173
|
-
(resource) => "pending" in resource && resource.pending && isUndefined(resource.value)
|
|
174
|
-
)
|
|
175
|
-
);
|
|
176
|
-
const contextLoading = computed(
|
|
177
|
-
() => Object.values(runtime.context).some((resource) => "loading" in resource && resource.loading)
|
|
178
|
-
);
|
|
179
|
-
const contextError = computed(() => {
|
|
180
|
-
for (const resource of Object.values(runtime.context)) {
|
|
181
|
-
if (!("error" in resource) || !resource.error) {
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (resource.error instanceof Error) {
|
|
185
|
-
return resource.error.message;
|
|
186
|
-
}
|
|
187
|
-
if (isString(resource.error)) {
|
|
188
|
-
return resource.error;
|
|
189
|
-
}
|
|
190
|
-
return t("form.states.contextError.description");
|
|
191
|
-
}
|
|
192
|
-
return void 0;
|
|
117
|
+
const actions = useFormActions({
|
|
118
|
+
runtime,
|
|
119
|
+
slot: () => shell.value === "inline" ? "left" : "right"
|
|
193
120
|
});
|
|
194
|
-
async function submit() {
|
|
195
|
-
if (props.form) {
|
|
196
|
-
const result2 = await props.form.submitHandler();
|
|
197
|
-
if (result2.success) {
|
|
198
|
-
emit("submit", runtime.output.value, result2);
|
|
199
|
-
}
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const result = await runtime.submitHandler();
|
|
203
|
-
if (result.success) {
|
|
204
|
-
emit("submit", runtime.output.value, result);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
async function refreshContext() {
|
|
208
|
-
await Promise.all(
|
|
209
|
-
Object.values(runtime.context).flatMap(
|
|
210
|
-
(resource) => "refresh" in resource ? [resource.refresh()] : []
|
|
211
|
-
)
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
function cancel() {
|
|
215
|
-
if (shouldConfirmDirtyNavigation() && !window.confirm(getDirtyNavigationMessage())) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
emit("cancel", runtime.output.value);
|
|
219
|
-
}
|
|
220
121
|
defineExpose({
|
|
221
122
|
actionPending: runtime.actionPending,
|
|
222
123
|
dirtyPaths: runtime.dirtyPaths,
|
|
@@ -268,13 +169,6 @@ function headerDisplayed(display, currentShell) {
|
|
|
268
169
|
function isRenderShell(value) {
|
|
269
170
|
return value === "inline" || value === "drawer" || value === "modal" || value === "fullscreen";
|
|
270
171
|
}
|
|
271
|
-
function getSchemaUi(schema) {
|
|
272
|
-
if (!isRecord(schema)) {
|
|
273
|
-
return void 0;
|
|
274
|
-
}
|
|
275
|
-
const value = Object.getOwnPropertyDescriptor(schema, "ui")?.value;
|
|
276
|
-
return isRecord(value) ? value : void 0;
|
|
277
|
-
}
|
|
278
172
|
function getSchemaShowStepper(schema) {
|
|
279
173
|
if (!isRecord(schema)) {
|
|
280
174
|
return true;
|
|
@@ -282,69 +176,6 @@ function getSchemaShowStepper(schema) {
|
|
|
282
176
|
const value = Object.getOwnPropertyDescriptor(schema, "showStepper")?.value;
|
|
283
177
|
return value !== false;
|
|
284
178
|
}
|
|
285
|
-
function getSchemaSyncInput(schema) {
|
|
286
|
-
const controls = getSchemaControls(schema);
|
|
287
|
-
const value = controls ? Object.getOwnPropertyDescriptor(controls, "syncInput")?.value : void 0;
|
|
288
|
-
if (isBoolean(value)) {
|
|
289
|
-
return value;
|
|
290
|
-
}
|
|
291
|
-
return stringArray(value);
|
|
292
|
-
}
|
|
293
|
-
function getSchemaValidationMode(schema) {
|
|
294
|
-
const controls = getSchemaControls(schema);
|
|
295
|
-
const value = controls ? Object.getOwnPropertyDescriptor(controls, "validate")?.value : void 0;
|
|
296
|
-
return value === false || value === "required" || value === "validators" ? value : true;
|
|
297
|
-
}
|
|
298
|
-
function getSchemaControls(schema) {
|
|
299
|
-
if (!isRecord(schema)) {
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
const controls = Object.getOwnPropertyDescriptor(schema, "controls")?.value;
|
|
303
|
-
return isRecord(controls) ? controls : void 0;
|
|
304
|
-
}
|
|
305
|
-
function getSchemaAutoFocus(schema) {
|
|
306
|
-
const controls = getSchemaControls(schema);
|
|
307
|
-
const value = controls ? Object.getOwnPropertyDescriptor(controls, "autoFocus")?.value : void 0;
|
|
308
|
-
return isString(value) || isBoolean(value) ? value : false;
|
|
309
|
-
}
|
|
310
|
-
function dirtyNavigationConfig() {
|
|
311
|
-
const controls = getSchemaControls(schemaRef.value);
|
|
312
|
-
return controls ? Object.getOwnPropertyDescriptor(controls, "confirmNavOnDirty")?.value : void 0;
|
|
313
|
-
}
|
|
314
|
-
function shouldConfirmDirtyNavigation() {
|
|
315
|
-
if (!runtime.isDirty.value) {
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
const config = dirtyNavigationConfig();
|
|
319
|
-
if (!config) {
|
|
320
|
-
return false;
|
|
321
|
-
}
|
|
322
|
-
if (!isRecord(config)) {
|
|
323
|
-
return config === true;
|
|
324
|
-
}
|
|
325
|
-
const ignored = stringArray(config.ignorePaths);
|
|
326
|
-
return runtime.dirtyPaths.value.some(
|
|
327
|
-
(path) => !ignored.some((ignoredPath) => path === ignoredPath || path.startsWith(`${ignoredPath}.`))
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
function getDirtyNavigationMessage() {
|
|
331
|
-
const config = dirtyNavigationConfig();
|
|
332
|
-
if (!isRecord(config)) {
|
|
333
|
-
return "You have unsaved changes. Close this form?";
|
|
334
|
-
}
|
|
335
|
-
const message = Object.getOwnPropertyDescriptor(config, "message")?.value;
|
|
336
|
-
return resolveFormText(message) ?? "You have unsaved changes. Close this form?";
|
|
337
|
-
}
|
|
338
|
-
async function focusFirstRenderedField() {
|
|
339
|
-
const paths = runtime.currentFields.value.map(
|
|
340
|
-
(field) => `${parentPath.value.join(".")}${parentPath.value.length ? "." : ""}${field.key}`
|
|
341
|
-
);
|
|
342
|
-
for (const path of paths) {
|
|
343
|
-
if (await runtime.focusField(path)) {
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
179
|
</script>
|
|
349
180
|
|
|
350
181
|
<template>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FormAction, FormRuntime } from '../types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Turns form actions into Nuxt UI buttons: visibility, disabled and loading states, links, and
|
|
4
|
+
* what a click runs. The form footer and the form page actions render from it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useFormActionButtons(params: {
|
|
7
|
+
runtime: FormRuntime;
|
|
8
|
+
actions: () => readonly FormAction[];
|
|
9
|
+
onCancel: () => void;
|
|
10
|
+
}): {
|
|
11
|
+
button: (action: FormAction) => {
|
|
12
|
+
readonly color: import("../types/index.js").FormActionColor | undefined;
|
|
13
|
+
readonly disabled: boolean;
|
|
14
|
+
readonly icon: string | undefined;
|
|
15
|
+
readonly label: string | undefined;
|
|
16
|
+
readonly loading: boolean;
|
|
17
|
+
readonly size: "xs" | "sm" | "md" | "lg" | "xl";
|
|
18
|
+
readonly to: string | undefined;
|
|
19
|
+
readonly trailingIcon: string | undefined;
|
|
20
|
+
readonly type: "button" | "submit";
|
|
21
|
+
readonly variant: import("../types/index.js").FormActionVariant | undefined;
|
|
22
|
+
};
|
|
23
|
+
fills: typeof fills;
|
|
24
|
+
run: (action: FormAction) => Promise<void>;
|
|
25
|
+
slot: typeof slot;
|
|
26
|
+
visible: import("vue").ComputedRef<FormAction[]>;
|
|
27
|
+
};
|
|
28
|
+
/** Footer group of an action at the current breakpoint. */
|
|
29
|
+
declare function slot(action: FormAction): "left" | "right";
|
|
30
|
+
/** True when an action fills its row at the current breakpoint. */
|
|
31
|
+
declare function fills(action: FormAction): boolean;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { getResponsiveValue } from "../../shared/composables/use-responsive-value.js";
|
|
3
|
+
import { createPublicFormApi } from "../utils/api.js";
|
|
4
|
+
import { invokeFormFunction, isString } from "../utils/predicate.js";
|
|
5
|
+
import { resolveFormText } from "../utils/text.js";
|
|
6
|
+
import { useFormUi } from "./use-form-ui.js";
|
|
7
|
+
export function useFormActionButtons(params) {
|
|
8
|
+
const formUi = useFormUi();
|
|
9
|
+
const context = computed(() => ({
|
|
10
|
+
actionPending: params.runtime.actionPending.value,
|
|
11
|
+
api: createPublicFormApi(params.runtime),
|
|
12
|
+
currentStep: params.runtime.currentStepIndex.value,
|
|
13
|
+
isFirstStep: params.runtime.isFirstStep.value,
|
|
14
|
+
isLastStep: params.runtime.isLastStep.value,
|
|
15
|
+
isMultiStep: params.runtime.isStepped.value
|
|
16
|
+
}));
|
|
17
|
+
const visible = computed(
|
|
18
|
+
() => params.actions().filter((action) => action.condition?.(context.value) ?? true)
|
|
19
|
+
);
|
|
20
|
+
async function run(action) {
|
|
21
|
+
if (action.key === "submit") {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (action.key === "next") {
|
|
25
|
+
await params.runtime.nextStep();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (action.key === "previous") {
|
|
29
|
+
await params.runtime.previousStep();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (action.key === "reset") {
|
|
33
|
+
await params.runtime.reset();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (action.key === "cancel") {
|
|
37
|
+
params.onCancel();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if ("action" in action) {
|
|
41
|
+
await action.action?.(context.value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function link(action) {
|
|
45
|
+
if (!("link" in action)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (isString(action.link)) {
|
|
49
|
+
return action.link;
|
|
50
|
+
}
|
|
51
|
+
const resolved = invokeFormFunction(action.link, [context.value]);
|
|
52
|
+
return isString(resolved) ? resolved : void 0;
|
|
53
|
+
}
|
|
54
|
+
function loading(action) {
|
|
55
|
+
const pending = params.runtime.actionPending.value;
|
|
56
|
+
return pending !== null && isBuiltInActionKey(action.key) && action.key === pending;
|
|
57
|
+
}
|
|
58
|
+
function disabled(action) {
|
|
59
|
+
if (action.disabled?.(context.value)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return params.runtime.actionPending.value !== null && !loading(action);
|
|
63
|
+
}
|
|
64
|
+
function button(action) {
|
|
65
|
+
const primary = action.type === "primary";
|
|
66
|
+
return {
|
|
67
|
+
color: action.color ?? (primary ? "primary" : void 0),
|
|
68
|
+
disabled: disabled(action),
|
|
69
|
+
icon: action.icon === false ? void 0 : action.icon,
|
|
70
|
+
label: resolveFormText(action.label),
|
|
71
|
+
loading: loading(action),
|
|
72
|
+
size: action.size ?? formUi.controlSize.value,
|
|
73
|
+
to: link(action),
|
|
74
|
+
trailingIcon: action.trailingIcon === false ? void 0 : action.trailingIcon,
|
|
75
|
+
type: action.key === "submit" ? "submit" : "button",
|
|
76
|
+
variant: action.variant ?? (primary ? "solid" : void 0)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return { button, fills, run, slot, visible };
|
|
80
|
+
}
|
|
81
|
+
function slot(action) {
|
|
82
|
+
return getResponsiveValue(action.slot ?? "right") === "left" ? "left" : "right";
|
|
83
|
+
}
|
|
84
|
+
function fills(action) {
|
|
85
|
+
return getResponsiveValue(action.width ?? "fit") === "fill";
|
|
86
|
+
}
|
|
87
|
+
function isBuiltInActionKey(value) {
|
|
88
|
+
return value === "reset" || value === "cancel" || value === "submit" || value === "previous" || value === "next";
|
|
89
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export declare function useFormActions(params: {
|
|
1
|
+
import type { FormAction, FormActionSlot, FormRuntime } from '../types/index.js';
|
|
2
|
+
interface UseFormActionsParams {
|
|
4
3
|
runtime: FormRuntime;
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/** Slot the built-in actions take unless an authored action sets its own. */
|
|
5
|
+
slot: () => FormActionSlot;
|
|
6
|
+
}
|
|
7
|
+
/** Resolves the actions of the current schema or step, completing built-in actions. */
|
|
8
|
+
export declare function useFormActions(params: UseFormActionsParams): import("vue").ComputedRef<readonly FormAction[]>;
|
|
9
|
+
export {};
|
|
@@ -28,7 +28,7 @@ function normalizeFormAction(action, params, t) {
|
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
function getBaseFormAction(key, params, t) {
|
|
31
|
-
const slot = params.
|
|
31
|
+
const slot = params.slot();
|
|
32
32
|
if (key === "submit") {
|
|
33
33
|
return {
|
|
34
34
|
condition: (context) => context.isMultiStep ? context.isLastStep : true,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ComputedRef } from 'vue';
|
|
2
|
+
import type { FormPageScrollOptions, FormPageSectionState } from '../types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Scroll behavior of a form page: which section is in view (a scrollspy on the nearest scrolling
|
|
5
|
+
* ancestor), scrolling to a section, and the URL hash that records and restores it.
|
|
6
|
+
*
|
|
7
|
+
* A section counts as reached once its top passes the line its `scroll-margin-top` defines, the
|
|
8
|
+
* same offset `scrollIntoView` honors, so the pinned header and the scrollspy agree. The section
|
|
9
|
+
* of an entry clicked in the navigation, and the one holding the field that takes focus (such as
|
|
10
|
+
* the first invalid field on submit), stay current through the scroll they cause, until the page
|
|
11
|
+
* is scrolled again.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useFormPageScroll(params: {
|
|
14
|
+
sections: ComputedRef<readonly FormPageSectionState[]>;
|
|
15
|
+
/** Element the scroll container is looked up from: the page itself, or an ancestor. */
|
|
16
|
+
root: () => HTMLElement | null;
|
|
17
|
+
hash: () => boolean;
|
|
18
|
+
}): {
|
|
19
|
+
active: import("vue").Ref<string | undefined, string | undefined>;
|
|
20
|
+
register: (key: string, element: HTMLElement) => () => void;
|
|
21
|
+
scrollTo: (key: string, options?: FormPageScrollOptions) => boolean;
|
|
22
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { nextTick, onBeforeUnmount, onMounted, ref } from "vue";
|
|
2
|
+
const SCROLL_SETTLE_MS = 120;
|
|
3
|
+
const ACTIVATION_SLACK = 8;
|
|
4
|
+
export function useFormPageScroll(params) {
|
|
5
|
+
const elements = /* @__PURE__ */ new Map();
|
|
6
|
+
const active = ref();
|
|
7
|
+
let scroller;
|
|
8
|
+
let focusRoot = null;
|
|
9
|
+
let target;
|
|
10
|
+
let pendingHash;
|
|
11
|
+
let settleTimer;
|
|
12
|
+
let holdFrame = 0;
|
|
13
|
+
let frame = 0;
|
|
14
|
+
function register(key, element) {
|
|
15
|
+
elements.set(key, element);
|
|
16
|
+
if (pendingHash === key) {
|
|
17
|
+
pendingHash = void 0;
|
|
18
|
+
void nextTick(() => scrollTo(key, { behavior: "instant", hash: false }));
|
|
19
|
+
} else {
|
|
20
|
+
schedule();
|
|
21
|
+
}
|
|
22
|
+
return () => {
|
|
23
|
+
if (elements.get(key) === element) {
|
|
24
|
+
elements.delete(key);
|
|
25
|
+
}
|
|
26
|
+
schedule();
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function scrollTo(key, options = {}) {
|
|
30
|
+
const element = elements.get(key);
|
|
31
|
+
if (!element) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
hold(key);
|
|
35
|
+
element.scrollIntoView?.({
|
|
36
|
+
behavior: options.behavior ?? (prefersReducedMotion() ? "instant" : "smooth"),
|
|
37
|
+
block: "start"
|
|
38
|
+
});
|
|
39
|
+
if (options.focus) {
|
|
40
|
+
element.querySelector("[data-form-page-section-title]")?.focus({ preventScroll: true });
|
|
41
|
+
}
|
|
42
|
+
if (options.hash ?? params.hash()) {
|
|
43
|
+
writeHash(key);
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
function hold(key) {
|
|
48
|
+
target = key;
|
|
49
|
+
active.value = key;
|
|
50
|
+
clearTimeout(settleTimer);
|
|
51
|
+
cancelAnimationFrame(holdFrame);
|
|
52
|
+
holdFrame = requestAnimationFrame(() => {
|
|
53
|
+
holdFrame = requestAnimationFrame(() => {
|
|
54
|
+
holdFrame = 0;
|
|
55
|
+
settleSoon();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function settleSoon() {
|
|
60
|
+
clearTimeout(settleTimer);
|
|
61
|
+
settleTimer = setTimeout(() => {
|
|
62
|
+
target = void 0;
|
|
63
|
+
}, SCROLL_SETTLE_MS);
|
|
64
|
+
}
|
|
65
|
+
function onScroll() {
|
|
66
|
+
if (target) {
|
|
67
|
+
settleSoon();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
schedule();
|
|
71
|
+
}
|
|
72
|
+
function onFocusIn(event) {
|
|
73
|
+
const section = event.target instanceof Element ? event.target.closest("[data-form-page-section]") : null;
|
|
74
|
+
const key = section?.dataset.formPageSection;
|
|
75
|
+
if (key && elements.get(key) === section) {
|
|
76
|
+
hold(key);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function schedule() {
|
|
80
|
+
if (frame) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
frame = requestAnimationFrame(() => {
|
|
84
|
+
frame = 0;
|
|
85
|
+
update();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function update() {
|
|
89
|
+
if (target) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const next = findActive();
|
|
93
|
+
if (next !== void 0) {
|
|
94
|
+
active.value = next;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function findActive() {
|
|
98
|
+
const keys = params.sections.value.map((section) => section.key).filter((key) => elements.has(key));
|
|
99
|
+
if (!keys.length) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
if (isScrolledToEnd()) {
|
|
103
|
+
return keys.at(-1);
|
|
104
|
+
}
|
|
105
|
+
const line = scroller instanceof HTMLElement ? scroller.getBoundingClientRect().top : 0;
|
|
106
|
+
let current = keys[0];
|
|
107
|
+
for (const key of keys) {
|
|
108
|
+
const element = elements.get(key);
|
|
109
|
+
if (!element) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const margin = Number.parseFloat(getComputedStyle(element).scrollMarginTop) || 0;
|
|
113
|
+
if (element.getBoundingClientRect().top - margin > line + ACTIVATION_SLACK) {
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
current = key;
|
|
117
|
+
}
|
|
118
|
+
return current;
|
|
119
|
+
}
|
|
120
|
+
function isScrolledToEnd() {
|
|
121
|
+
const box = scroller instanceof HTMLElement ? scroller : document.scrollingElement;
|
|
122
|
+
if (!box || box.scrollHeight <= box.clientHeight) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return box.scrollTop + box.clientHeight >= box.scrollHeight - 1;
|
|
126
|
+
}
|
|
127
|
+
function onHashChange() {
|
|
128
|
+
const key = readHash();
|
|
129
|
+
if (key && elements.has(key)) {
|
|
130
|
+
scrollTo(key, { hash: false });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
onMounted(() => {
|
|
134
|
+
focusRoot = params.root();
|
|
135
|
+
focusRoot?.addEventListener("focusin", onFocusIn);
|
|
136
|
+
scroller = findScroller(focusRoot);
|
|
137
|
+
scroller.addEventListener("scroll", onScroll, { passive: true });
|
|
138
|
+
window.addEventListener("hashchange", onHashChange);
|
|
139
|
+
const key = params.hash() ? readHash() : void 0;
|
|
140
|
+
if (key && elements.has(key)) {
|
|
141
|
+
void nextTick(() => scrollTo(key, { behavior: "instant", hash: false }));
|
|
142
|
+
} else {
|
|
143
|
+
pendingHash = key;
|
|
144
|
+
schedule();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
onBeforeUnmount(() => {
|
|
148
|
+
focusRoot?.removeEventListener("focusin", onFocusIn);
|
|
149
|
+
scroller?.removeEventListener("scroll", onScroll);
|
|
150
|
+
window.removeEventListener("hashchange", onHashChange);
|
|
151
|
+
clearTimeout(settleTimer);
|
|
152
|
+
cancelAnimationFrame(holdFrame);
|
|
153
|
+
if (frame) {
|
|
154
|
+
cancelAnimationFrame(frame);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
return { active, register, scrollTo };
|
|
158
|
+
}
|
|
159
|
+
function findScroller(element) {
|
|
160
|
+
for (let node = element; node; node = node.parentElement) {
|
|
161
|
+
if (/auto|scroll/u.test(getComputedStyle(node).overflowY)) {
|
|
162
|
+
return node;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return window;
|
|
166
|
+
}
|
|
167
|
+
function prefersReducedMotion() {
|
|
168
|
+
return window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
169
|
+
}
|
|
170
|
+
function readHash() {
|
|
171
|
+
const hash = window.location.hash.slice(1);
|
|
172
|
+
return hash ? decodeURIComponent(hash) : void 0;
|
|
173
|
+
}
|
|
174
|
+
function writeHash(key) {
|
|
175
|
+
const url = new URL(window.location.href);
|
|
176
|
+
url.hash = key;
|
|
177
|
+
window.history.replaceState(window.history.state, "", url);
|
|
178
|
+
}
|