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.
Files changed (68) hide show
  1. package/dist/module.json +1 -1
  2. package/dist/module.mjs +8 -1
  3. package/dist/runtime/form/components/actions/form-actions.vue +13 -127
  4. package/dist/runtime/form/components/page/form-page-actions.d.vue.ts +3 -0
  5. package/dist/runtime/form/components/page/form-page-actions.vue +40 -0
  6. package/dist/runtime/form/components/page/form-page-actions.vue.d.ts +3 -0
  7. package/dist/runtime/form/components/page/form-page-header.d.vue.ts +24 -0
  8. package/dist/runtime/form/components/page/form-page-header.vue +99 -0
  9. package/dist/runtime/form/components/page/form-page-header.vue.d.ts +24 -0
  10. package/dist/runtime/form/components/page/form-page-navigation.d.vue.ts +34 -0
  11. package/dist/runtime/form/components/page/form-page-navigation.vue +229 -0
  12. package/dist/runtime/form/components/page/form-page-navigation.vue.d.ts +34 -0
  13. package/dist/runtime/form/components/page/form-page-section.d.vue.ts +19 -0
  14. package/dist/runtime/form/components/page/form-page-section.vue +114 -0
  15. package/dist/runtime/form/components/page/form-page-section.vue.d.ts +19 -0
  16. package/dist/runtime/form/components/page/form-page-sections.d.vue.ts +21 -0
  17. package/dist/runtime/form/components/page/form-page-sections.vue +75 -0
  18. package/dist/runtime/form/components/page/form-page-sections.vue.d.ts +21 -0
  19. package/dist/runtime/form/components/page/form-page.d.vue.ts +80 -0
  20. package/dist/runtime/form/components/page/form-page.vue +91 -0
  21. package/dist/runtime/form/components/page/form-page.vue.d.ts +80 -0
  22. package/dist/runtime/form/components/renderer/form-field-shell.vue +7 -11
  23. package/dist/runtime/form/components/root/form.vue +25 -194
  24. package/dist/runtime/form/composables/use-form-action-buttons.d.ts +32 -0
  25. package/dist/runtime/form/composables/use-form-action-buttons.js +89 -0
  26. package/dist/runtime/form/composables/use-form-actions.d.ts +8 -5
  27. package/dist/runtime/form/composables/use-form-actions.js +1 -1
  28. package/dist/runtime/form/composables/use-form-page-scroll.d.ts +22 -0
  29. package/dist/runtime/form/composables/use-form-page-scroll.js +178 -0
  30. package/dist/runtime/form/composables/use-form-page.d.ts +114 -0
  31. package/dist/runtime/form/composables/use-form-page.js +83 -0
  32. package/dist/runtime/form/composables/use-form-root.d.ts +34 -0
  33. package/dist/runtime/form/composables/use-form-root.js +171 -0
  34. package/dist/runtime/form/composables/use-form-runtime.js +13 -4
  35. package/dist/runtime/form/composables/use-form.js +2 -22
  36. package/dist/runtime/form/fields/checkbox-card/component.vue +28 -19
  37. package/dist/runtime/form/fields/checkbox-card/types.d.ts +2 -5
  38. package/dist/runtime/form/fields/choice-card/choice-card-label.d.vue.ts +17 -0
  39. package/dist/runtime/form/fields/choice-card/choice-card-label.vue +50 -0
  40. package/dist/runtime/form/fields/choice-card/choice-card-label.vue.d.ts +17 -0
  41. package/dist/runtime/form/fields/choice-card/types.d.ts +31 -0
  42. package/dist/runtime/form/fields/choice-card/types.js +0 -0
  43. package/dist/runtime/form/fields/choice-card/use-choice-card.d.ts +22 -0
  44. package/dist/runtime/form/fields/choice-card/use-choice-card.js +52 -0
  45. package/dist/runtime/form/fields/radio-card/component.vue +17 -18
  46. package/dist/runtime/form/fields/radio-card/types.d.ts +2 -4
  47. package/dist/runtime/form/index.d.ts +1 -1
  48. package/dist/runtime/form/index.js +7 -1
  49. package/dist/runtime/form/schema/index.d.ts +1 -1
  50. package/dist/runtime/form/schema/index.js +1 -0
  51. package/dist/runtime/form/schema/page.d.ts +50 -0
  52. package/dist/runtime/form/schema/page.js +25 -0
  53. package/dist/runtime/form/types/index.d.ts +1 -0
  54. package/dist/runtime/form/types/page.d.ts +172 -0
  55. package/dist/runtime/form/types/page.js +0 -0
  56. package/dist/runtime/form/types/ui.d.ts +60 -0
  57. package/dist/runtime/form/utils/controls.d.ts +15 -0
  58. package/dist/runtime/form/utils/controls.js +38 -0
  59. package/dist/runtime/form/utils/layout.js +3 -1
  60. package/dist/runtime/form/utils/page.d.ts +20 -0
  61. package/dist/runtime/form/utils/page.js +107 -0
  62. package/dist/runtime/form/utils/state.d.ts +2 -0
  63. package/dist/runtime/form/utils/state.js +1 -1
  64. package/dist/runtime/form/utils/ui.js +1 -0
  65. package/dist/runtime/i18n/locales/en.js +18 -0
  66. package/dist/runtime/i18n/locales/fr.js +18 -0
  67. package/dist/runtime/i18n/types.d.ts +29 -0
  68. package/package.json +1 -1
@@ -0,0 +1,114 @@
1
+ import type { FormPageSectionState } from '../types/index.js';
2
+ import type { useFormRoot } from './use-form-root.js';
3
+ /**
4
+ * State shared by a form page and its parts: the visible sections and where each stands, the
5
+ * section in view, the navigation summary, and the actions a part can take on a section.
6
+ */
7
+ export declare function useFormPage(params: {
8
+ root: ReturnType<typeof useFormRoot>;
9
+ /** The page element, where the lookup of the scroll container starts. */
10
+ element: () => HTMLElement | null;
11
+ hash: () => boolean;
12
+ }): {
13
+ active: import("vue").Ref<string | undefined, string | undefined>;
14
+ cancel: () => void;
15
+ dirtyCheck: import("vue").ComputedRef<boolean>;
16
+ items: import("vue").ComputedRef<{
17
+ entry: import("../types/index.js").FormPageSectionEntry;
18
+ section: FormPageSectionState;
19
+ }[]>;
20
+ modified: import("vue").ComputedRef<number>;
21
+ navigationTitle: import("vue").ComputedRef<string | undefined>;
22
+ register: (key: string, element: HTMLElement) => () => void;
23
+ remaining: import("vue").ComputedRef<number>;
24
+ resetSection: (key: string) => Promise<void>;
25
+ root: {
26
+ cancel: () => void;
27
+ contextError: import("vue").ComputedRef<string | undefined>;
28
+ contextLoading: import("vue").ComputedRef<boolean>;
29
+ contextPending: import("vue").ComputedRef<boolean>;
30
+ formUi: {
31
+ controlSize: import("vue").ComputedRef<import("../types/index.js").FormControlSize>;
32
+ density: import("vue").ComputedRef<import("../types/index.js").FormDensity>;
33
+ ui: import("vue").ComputedRef<import("../types/index.js").FormUiConfig>;
34
+ };
35
+ input: import("vue").ComputedRef<import("../../shared/index.js").GenericObject | undefined>;
36
+ refreshContext: () => Promise<void>;
37
+ runtime: import("../types/index.js").FormRuntime;
38
+ schema: import("vue").ComputedRef<{}>;
39
+ submit: () => Promise<void>;
40
+ validationMode: import("vue").ComputedRef<import("../types/index.js").FormValidationMode>;
41
+ };
42
+ scrollTo: (key: string, options?: import("../types/index.js").FormPageScrollOptions) => boolean;
43
+ sections: import("vue").ComputedRef<readonly FormPageSectionState[]>;
44
+ stickyOffset: import("vue").Ref<number, number>;
45
+ };
46
+ export declare function provideFormPage(page: ReturnType<typeof useFormPage>): {
47
+ active: import("vue").Ref<string | undefined, string | undefined>;
48
+ cancel: () => void;
49
+ dirtyCheck: import("vue").ComputedRef<boolean>;
50
+ items: import("vue").ComputedRef<{
51
+ entry: import("../types/index.js").FormPageSectionEntry;
52
+ section: FormPageSectionState;
53
+ }[]>;
54
+ modified: import("vue").ComputedRef<number>;
55
+ navigationTitle: import("vue").ComputedRef<string | undefined>;
56
+ register: (key: string, element: HTMLElement) => () => void;
57
+ remaining: import("vue").ComputedRef<number>;
58
+ resetSection: (key: string) => Promise<void>;
59
+ root: {
60
+ cancel: () => void;
61
+ contextError: import("vue").ComputedRef<string | undefined>;
62
+ contextLoading: import("vue").ComputedRef<boolean>;
63
+ contextPending: import("vue").ComputedRef<boolean>;
64
+ formUi: {
65
+ controlSize: import("vue").ComputedRef<import("../types/index.js").FormControlSize>;
66
+ density: import("vue").ComputedRef<import("../types/index.js").FormDensity>;
67
+ ui: import("vue").ComputedRef<import("../types/index.js").FormUiConfig>;
68
+ };
69
+ input: import("vue").ComputedRef<import("../../shared/index.js").GenericObject | undefined>;
70
+ refreshContext: () => Promise<void>;
71
+ runtime: import("../types/index.js").FormRuntime;
72
+ schema: import("vue").ComputedRef<{}>;
73
+ submit: () => Promise<void>;
74
+ validationMode: import("vue").ComputedRef<import("../types/index.js").FormValidationMode>;
75
+ };
76
+ scrollTo: (key: string, options?: import("../types/index.js").FormPageScrollOptions) => boolean;
77
+ sections: import("vue").ComputedRef<readonly FormPageSectionState[]>;
78
+ stickyOffset: import("vue").Ref<number, number>;
79
+ };
80
+ /** The page a part renders in. Parts only work inside `FormPage`. */
81
+ export declare function useFormPageContext(): {
82
+ active: import("vue").Ref<string | undefined, string | undefined>;
83
+ cancel: () => void;
84
+ dirtyCheck: import("vue").ComputedRef<boolean>;
85
+ items: import("vue").ComputedRef<{
86
+ entry: import("../types/index.js").FormPageSectionEntry;
87
+ section: FormPageSectionState;
88
+ }[]>;
89
+ modified: import("vue").ComputedRef<number>;
90
+ navigationTitle: import("vue").ComputedRef<string | undefined>;
91
+ register: (key: string, element: HTMLElement) => () => void;
92
+ remaining: import("vue").ComputedRef<number>;
93
+ resetSection: (key: string) => Promise<void>;
94
+ root: {
95
+ cancel: () => void;
96
+ contextError: import("vue").ComputedRef<string | undefined>;
97
+ contextLoading: import("vue").ComputedRef<boolean>;
98
+ contextPending: import("vue").ComputedRef<boolean>;
99
+ formUi: {
100
+ controlSize: import("vue").ComputedRef<import("../types/index.js").FormControlSize>;
101
+ density: import("vue").ComputedRef<import("../types/index.js").FormDensity>;
102
+ ui: import("vue").ComputedRef<import("../types/index.js").FormUiConfig>;
103
+ };
104
+ input: import("vue").ComputedRef<import("../../shared/index.js").GenericObject | undefined>;
105
+ refreshContext: () => Promise<void>;
106
+ runtime: import("../types/index.js").FormRuntime;
107
+ schema: import("vue").ComputedRef<{}>;
108
+ submit: () => Promise<void>;
109
+ validationMode: import("vue").ComputedRef<import("../types/index.js").FormValidationMode>;
110
+ };
111
+ scrollTo: (key: string, options?: import("../types/index.js").FormPageScrollOptions) => boolean;
112
+ sections: import("vue").ComputedRef<readonly FormPageSectionState[]>;
113
+ stickyOffset: import("vue").Ref<number, number>;
114
+ };
@@ -0,0 +1,83 @@
1
+ import { computed, inject, provide, ref } from "vue";
2
+ import { getSchemaDirtyCheck } from "../utils/controls.js";
3
+ import {
4
+ getFormPageNavigationTitle,
5
+ getFormPageSections,
6
+ isFormPageSectionVisible,
7
+ resolveFormPageSectionState
8
+ } from "../utils/page.js";
9
+ import { cloneFormValue } from "../utils/path.js";
10
+ import { useFormPageScroll } from "./use-form-page-scroll.js";
11
+ export function useFormPage(params) {
12
+ const { root } = params;
13
+ const { runtime } = root;
14
+ const entries = computed(
15
+ () => getFormPageSections(root.schema.value).filter(
16
+ (entry) => isFormPageSectionVisible(entry, runtime)
17
+ )
18
+ );
19
+ const requiredEnforced = computed(
20
+ () => root.validationMode.value === true || root.validationMode.value === "required"
21
+ );
22
+ const items = computed(
23
+ () => entries.value.map((entry, index) => ({
24
+ entry,
25
+ section: resolveFormPageSectionState({
26
+ entry,
27
+ index,
28
+ input: root.input.value,
29
+ requiredEnforced: requiredEnforced.value,
30
+ runtime
31
+ })
32
+ }))
33
+ );
34
+ const sections = computed(
35
+ () => items.value.map((item) => item.section)
36
+ );
37
+ const dirtyCheck = computed(() => getSchemaDirtyCheck(root.schema.value));
38
+ const navigationTitle = computed(() => getFormPageNavigationTitle(root.schema.value));
39
+ const remaining = computed(
40
+ () => sections.value.filter((section) => !section.optional && section.status !== "complete").length
41
+ );
42
+ const modified = computed(() => sections.value.filter((section) => section.dirty).length);
43
+ const stickyOffset = ref(0);
44
+ const scroll = useFormPageScroll({ hash: params.hash, root: params.element, sections });
45
+ async function resetSection(key) {
46
+ const section = sections.value.find((candidate) => candidate.key === key);
47
+ if (!section) {
48
+ return;
49
+ }
50
+ for (const path of section.dirtyPaths) {
51
+ runtime.setValue(path, cloneFormValue(runtime.getInitialValue(path)));
52
+ runtime.clearError(path);
53
+ }
54
+ await runtime.settleEffects();
55
+ }
56
+ return {
57
+ active: scroll.active,
58
+ cancel: root.cancel,
59
+ dirtyCheck,
60
+ items,
61
+ modified,
62
+ navigationTitle,
63
+ register: scroll.register,
64
+ remaining,
65
+ resetSection,
66
+ root,
67
+ scrollTo: scroll.scrollTo,
68
+ sections,
69
+ stickyOffset
70
+ };
71
+ }
72
+ const formPageKey = Symbol("nuxt-ui-tools-form-page");
73
+ export function provideFormPage(page) {
74
+ provide(formPageKey, page);
75
+ return page;
76
+ }
77
+ export function useFormPageContext() {
78
+ const page = inject(formPageKey, null);
79
+ if (!page) {
80
+ throw new Error("Form page parts must be rendered inside <FormPage>.");
81
+ }
82
+ return page;
83
+ }
@@ -0,0 +1,34 @@
1
+ import type { FormObject, FormRendererController, FormSubmitHandlerResult, FormUiConfig, FormValidationMode, FormValue } from '../types/index.js';
2
+ export interface UseFormRootParams {
3
+ /** Controller from `useForm`. Its schema, input, and policies win over the direct ones. */
4
+ form: () => FormRendererController | undefined;
5
+ schema: () => FormValue;
6
+ input: () => FormObject | undefined;
7
+ syncInput: () => boolean | readonly string[] | undefined;
8
+ validate: () => FormValidationMode | undefined;
9
+ ui: () => FormUiConfig | undefined;
10
+ onSubmitted: (value: FormObject, result: FormSubmitHandlerResult<FormValue>) => void;
11
+ onCancelled: (value: FormObject) => void;
12
+ }
13
+ /**
14
+ * Root of a rendered form. It creates the runtime and binds it to the controller, provides the
15
+ * runtime and the presentation config to the fields, guards navigation away from a dirty form,
16
+ * and owns submit and cancel. `<NutForm>` and `FormPage` both render from it.
17
+ */
18
+ export declare function useFormRoot(params: UseFormRootParams): {
19
+ cancel: () => void;
20
+ contextError: import("vue").ComputedRef<string | undefined>;
21
+ contextLoading: import("vue").ComputedRef<boolean>;
22
+ contextPending: import("vue").ComputedRef<boolean>;
23
+ formUi: {
24
+ controlSize: import("vue").ComputedRef<import("../types/index.js").FormControlSize>;
25
+ density: import("vue").ComputedRef<import("../types/index.js").FormDensity>;
26
+ ui: import("vue").ComputedRef<FormUiConfig>;
27
+ };
28
+ input: import("vue").ComputedRef<import("../../shared/index.js").GenericObject | undefined>;
29
+ refreshContext: () => Promise<void>;
30
+ runtime: import("../types/index.js").FormRuntime;
31
+ schema: import("vue").ComputedRef<{}>;
32
+ submit: () => Promise<void>;
33
+ validationMode: import("vue").ComputedRef<FormValidationMode>;
34
+ };
@@ -0,0 +1,171 @@
1
+ import { useAppConfig } from "nuxt/app";
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, watch } from "vue";
3
+ import { useRouter } from "vue-router";
4
+ import { useUiToolsLocale } from "../../i18n/use-locale.js";
5
+ import {
6
+ getSchemaAutoFocus,
7
+ getSchemaDirtyNavigation,
8
+ getSchemaSyncInput,
9
+ getSchemaUi,
10
+ getSchemaValidationMode
11
+ } from "../utils/controls.js";
12
+ import { isRecord } from "../utils/path.js";
13
+ import { isString, isUndefined, stringArray } from "../utils/predicate.js";
14
+ import { collectFormFieldsPaths } from "../utils/state.js";
15
+ import { resolveFormText } from "../utils/text.js";
16
+ import { mergeFormUi, resolveAppFormUi } from "../utils/ui.js";
17
+ import { provideFormRuntime, useFormRuntime } from "./use-form-runtime.js";
18
+ import { provideFormUi } from "./use-form-ui.js";
19
+ const DEFAULT_DIRTY_NAVIGATION_MESSAGE = "You have unsaved changes. Close this form?";
20
+ export function useFormRoot(params) {
21
+ const router = useRouter();
22
+ const { t } = useUiToolsLocale();
23
+ const appConfig = useAppConfig();
24
+ const schema = computed(() => params.form()?.schema.value ?? params.schema() ?? {});
25
+ const formUi = provideFormUi(
26
+ computed(
27
+ () => mergeFormUi(resolveAppFormUi(appConfig), getSchemaUi(schema.value), params.ui())
28
+ )
29
+ );
30
+ const input = computed(() => params.form()?.input.value ?? params.input());
31
+ const syncInput = computed(
32
+ () => params.form()?.syncInput.value ?? params.syncInput() ?? getSchemaSyncInput(schema.value)
33
+ );
34
+ const validationMode = computed(
35
+ () => params.form()?.validationMode.value ?? params.validate() ?? getSchemaValidationMode(schema.value)
36
+ );
37
+ const runtime = useFormRuntime({ input, schema, syncInput, validationMode });
38
+ provideFormRuntime(runtime);
39
+ watch(
40
+ params.form,
41
+ (controller, previousController) => {
42
+ previousController?.unbind(runtime);
43
+ controller?.bind(runtime);
44
+ },
45
+ { immediate: true }
46
+ );
47
+ onBeforeUnmount(() => params.form()?.unbind(runtime));
48
+ onMounted(async () => {
49
+ await nextTick();
50
+ const target = getSchemaAutoFocus(schema.value);
51
+ if (isString(target)) {
52
+ await runtime.focusField(target);
53
+ return;
54
+ }
55
+ if (target === true) {
56
+ await focusFirstField();
57
+ }
58
+ });
59
+ if (import.meta.client) {
60
+ let handleBeforeUnload = function(event) {
61
+ if (!shouldConfirmDirtyNavigation()) {
62
+ return;
63
+ }
64
+ event.preventDefault();
65
+ event.returnValue = "";
66
+ };
67
+ window.addEventListener("beforeunload", handleBeforeUnload);
68
+ onBeforeUnmount(() => window.removeEventListener("beforeunload", handleBeforeUnload));
69
+ }
70
+ const removeRouteGuard = router.beforeEach((to, from) => {
71
+ if (runtime.actionPending.value === "submit" || isSameDocument(to, from)) {
72
+ return true;
73
+ }
74
+ if (!shouldConfirmDirtyNavigation()) {
75
+ return true;
76
+ }
77
+ return window.confirm(getDirtyNavigationMessage());
78
+ });
79
+ onBeforeUnmount(removeRouteGuard);
80
+ const contextPending = computed(
81
+ () => Object.values(runtime.context).some(
82
+ (resource) => "pending" in resource && resource.pending && isUndefined(resource.value)
83
+ )
84
+ );
85
+ const contextLoading = computed(
86
+ () => Object.values(runtime.context).some((resource) => "loading" in resource && resource.loading)
87
+ );
88
+ const contextError = computed(() => {
89
+ for (const resource of Object.values(runtime.context)) {
90
+ if (!("error" in resource) || !resource.error) {
91
+ continue;
92
+ }
93
+ if (resource.error instanceof Error) {
94
+ return resource.error.message;
95
+ }
96
+ if (isString(resource.error)) {
97
+ return resource.error;
98
+ }
99
+ return t("form.states.contextError.description");
100
+ }
101
+ return void 0;
102
+ });
103
+ async function refreshContext() {
104
+ await Promise.all(
105
+ Object.values(runtime.context).flatMap(
106
+ (resource) => "refresh" in resource ? [resource.refresh()] : []
107
+ )
108
+ );
109
+ }
110
+ async function submit() {
111
+ const controller = params.form();
112
+ const result = controller ? await controller.submitHandler() : await runtime.submitHandler();
113
+ if (result.success) {
114
+ params.onSubmitted(runtime.output.value, result);
115
+ }
116
+ }
117
+ function cancel() {
118
+ if (shouldConfirmDirtyNavigation() && !window.confirm(getDirtyNavigationMessage())) {
119
+ return;
120
+ }
121
+ params.onCancelled(runtime.output.value);
122
+ }
123
+ function shouldConfirmDirtyNavigation() {
124
+ if (!runtime.isDirty.value) {
125
+ return false;
126
+ }
127
+ const config = getSchemaDirtyNavigation(schema.value);
128
+ if (!config) {
129
+ return false;
130
+ }
131
+ if (!isRecord(config)) {
132
+ return config === true;
133
+ }
134
+ const ignored = stringArray(config.ignorePaths);
135
+ return runtime.dirtyPaths.value.some(
136
+ (path) => !ignored.some((ignoredPath) => path === ignoredPath || path.startsWith(`${ignoredPath}.`))
137
+ );
138
+ }
139
+ function getDirtyNavigationMessage() {
140
+ const config = getSchemaDirtyNavigation(schema.value);
141
+ if (!isRecord(config)) {
142
+ return DEFAULT_DIRTY_NAVIGATION_MESSAGE;
143
+ }
144
+ const message = Object.getOwnPropertyDescriptor(config, "message")?.value;
145
+ return resolveFormText(message) ?? DEFAULT_DIRTY_NAVIGATION_MESSAGE;
146
+ }
147
+ async function focusFirstField() {
148
+ const parentPath = runtime.currentStepRoot.value ? [runtime.currentStepRoot.value] : [];
149
+ for (const path of collectFormFieldsPaths(runtime.currentFields.value, parentPath)) {
150
+ if (await runtime.focusField(path)) {
151
+ return;
152
+ }
153
+ }
154
+ }
155
+ return {
156
+ cancel,
157
+ contextError,
158
+ contextLoading,
159
+ contextPending,
160
+ formUi,
161
+ input,
162
+ refreshContext,
163
+ runtime,
164
+ schema,
165
+ submit,
166
+ validationMode
167
+ };
168
+ }
169
+ function isSameDocument(to, from) {
170
+ return to.fullPath.split("#")[0] === from.fullPath.split("#")[0];
171
+ }
@@ -137,7 +137,7 @@ export function useFormRuntime(params) {
137
137
  }
138
138
  return valid;
139
139
  }
140
- const submit = useFormSubmission({
140
+ const submission = useFormSubmission({
141
141
  beforeNext: () => isStepped.value ? runBeforeNext() : Promise.resolve(true),
142
142
  focusFirstInvalid: focus.focusFirstInvalid,
143
143
  getApi: () => createPublicFormApi(runtime),
@@ -146,6 +146,13 @@ export function useFormRuntime(params) {
146
146
  getSchema: () => params.schema.value,
147
147
  validate
148
148
  });
149
+ async function submitHandler(externalSubmitHandler) {
150
+ const result = await submission.submitHandler(externalSubmitHandler);
151
+ if (result.success) {
152
+ state.rebaseline();
153
+ }
154
+ return result;
155
+ }
149
156
  let hasInitializedInput = !isUndefined(params.input?.value);
150
157
  state.initialize(params.input?.value);
151
158
  let pendingInput;
@@ -221,7 +228,9 @@ export function useFormRuntime(params) {
221
228
  const isLastStep = computed(() => currentStepIndex.value >= steps.value.length - 1);
222
229
  const canGoPrevious = computed(() => isStepped.value && !isFirstStep.value);
223
230
  const canGoNext = computed(() => isStepped.value && !isLastStep.value);
224
- const actionPending = computed(() => navigationActionPending.value ?? submit.actionPending.value);
231
+ const actionPending = computed(
232
+ () => navigationActionPending.value ?? submission.actionPending.value
233
+ );
225
234
  async function runBeforeNext() {
226
235
  const handler = getSchemaLifecycleHandler(params.schema.value, "onBeforeNext");
227
236
  if (!handler) {
@@ -433,10 +442,10 @@ export function useFormRuntime(params) {
433
442
  state: state.state,
434
443
  steps,
435
444
  submit: async () => {
436
- const result = await submit.submitHandler();
445
+ const result = await submitHandler();
437
446
  return result.success;
438
447
  },
439
- submitHandler: submit.submitHandler,
448
+ submitHandler,
440
449
  trackEffect: effects.track,
441
450
  validate,
442
451
  validateCurrentStep
@@ -1,6 +1,6 @@
1
1
  import { computed, shallowRef, toValue } from "vue";
2
- import { isRecord } from "../utils/path.js";
3
- import { isBoolean, isUndefined, stringArray } from "../utils/predicate.js";
2
+ import { getSchemaSyncInput, getSchemaValidationMode } from "../utils/controls.js";
3
+ import { isUndefined } from "../utils/predicate.js";
4
4
  export function useForm(params) {
5
5
  const runtime = shallowRef(null);
6
6
  const schema = computed(() => toValue(params.schema));
@@ -151,23 +151,3 @@ export function useForm(params) {
151
151
  validationMode
152
152
  };
153
153
  }
154
- function getSchemaSyncInput(schema) {
155
- const controls = getSchemaControls(schema);
156
- const value = controls ? Object.getOwnPropertyDescriptor(controls, "syncInput")?.value : void 0;
157
- if (isBoolean(value)) {
158
- return value;
159
- }
160
- return stringArray(value);
161
- }
162
- function getSchemaValidationMode(schema) {
163
- const controls = getSchemaControls(schema);
164
- const value = controls ? Object.getOwnPropertyDescriptor(controls, "validate")?.value : void 0;
165
- return value === false || value === "required" || value === "validators" ? value : true;
166
- }
167
- function getSchemaControls(schema) {
168
- if (!isRecord(schema)) {
169
- return;
170
- }
171
- const controls = Object.getOwnPropertyDescriptor(schema, "controls")?.value;
172
- return isRecord(controls) ? controls : void 0;
173
- }
@@ -5,18 +5,20 @@ import FormFieldShell from "../../components/renderer/form-field-shell.vue";
5
5
  import { useFieldControl } from "../../composables/use-field-control";
6
6
  import { formOptionKey } from "../../utils/options";
7
7
  import { isBoolean, isNumber, isString, isUndefined } from "../../utils/predicate";
8
- import { mergeFormUiClass } from "../../utils/ui";
9
- import { CARD_SELECTED_RING } from "../card-selection";
8
+ import ChoiceCardLabel from "../choice-card/choice-card-label.vue";
9
+ import { CHOICE_CARD_PROPS, useChoiceCard } from "../choice-card/use-choice-card";
10
10
  const props = defineProps({
11
11
  field: { type: Object, required: true },
12
12
  path: { type: Array, required: true }
13
13
  });
14
14
  const { fieldProps, form, controlProps, disabled, handleBlur, options } = useFieldControl(
15
15
  () => props.field,
16
- () => props.path
16
+ () => props.path,
17
+ { omit: CHOICE_CARD_PROPS }
17
18
  );
18
- const valueByKey = computed(
19
- () => new Map(options.items.value.map((item) => [formOptionKey(item.value), item.value]))
19
+ const cards = useChoiceCard({ props: () => fieldProps.value, ui: () => controlProps.value.ui });
20
+ const optionByKey = computed(
21
+ () => new Map(options.items.value.map((item) => [formOptionKey(item.value), item]))
20
22
  );
21
23
  const model = computed({
22
24
  get: () => {
@@ -26,22 +28,17 @@ const model = computed({
26
28
  set: (value) => form.setValue(
27
29
  props.path,
28
30
  value.flatMap((key) => {
29
- const optionValue = valueByKey.value.get(key);
31
+ const optionValue = optionByKey.value.get(key)?.value;
30
32
  return isUndefined(optionValue) ? [] : [optionValue];
31
33
  })
32
34
  )
33
35
  });
34
36
  const items = computed(
35
- () => options.items.value.map((item) => ({ ...item, value: formOptionKey(item.value) }))
37
+ () => options.items.value.map(({ icon: _icon, ...item }) => ({
38
+ ...item,
39
+ value: formOptionKey(item.value)
40
+ }))
36
41
  );
37
- const groupUi = computed(() => ({
38
- ...controlProps.value.ui,
39
- fieldset: mergeFormUiClass(
40
- controlProps.value.ui?.fieldset,
41
- fieldProps.value.orientation === "horizontal" ? "flex-wrap" : void 0
42
- ),
43
- item: mergeFormUiClass(CARD_SELECTED_RING, controlProps.value.ui?.item)
44
- }));
45
42
  function isOptionValue(value) {
46
43
  return isString(value) || isNumber(value) || isBoolean(value);
47
44
  }
@@ -56,12 +53,24 @@ function isOptionValue(value) {
56
53
  value-key="value"
57
54
  label-key="label"
58
55
  description-key="description"
59
- :orientation="fieldProps.orientation"
60
- :ui="groupUi"
61
56
  variant="card"
62
- :indicator="fieldProps.indicator"
57
+ :orientation="cards.orientation.value"
58
+ :indicator="cards.indicator.value"
59
+ :ui="cards.ui.value"
60
+ :style="cards.style.value"
63
61
  :disabled="disabled"
64
62
  @blur="handleBlur"
65
- />
63
+ >
64
+ <template #label="{ item }">
65
+ <ChoiceCardLabel
66
+ :label="optionByKey.get(item.value)?.label ?? item.label"
67
+ :icon="optionByKey.get(item.value)?.icon"
68
+ :tile="cards.tile.value"
69
+ :corner="cards.corner.value"
70
+ :selected="model.includes(item.value)"
71
+ :ui="cards.ui.value"
72
+ />
73
+ </template>
74
+ </UCheckboxGroup>
66
75
  </FormFieldShell>
67
76
  </template>
@@ -1,11 +1,8 @@
1
1
  import type { FormStatefulFieldBase } from '../../types/field-base.js';
2
2
  import type { FieldOptionValue, NullableValue } from '../../types/field-output-utils.js';
3
3
  import type { FormOptionConfig, FormOptionItem, FormOptionValue, FormOptionsSource } from '../../types/options.js';
4
- export interface FormCheckboxCardProps {
5
- orientation?: 'horizontal' | 'vertical';
6
- indicator?: 'start' | 'end' | 'hidden';
7
- }
8
- export interface FormCheckboxCardField<TContext = NonNullable<unknown>, TDeps = NonNullable<unknown>, TValue extends FormOptionValue = FormOptionValue, TOption extends FormOptionItem<TValue> = FormOptionItem<TValue>> extends FormStatefulFieldBase<'checkbox-card', readonly TValue[] | null, TContext, TDeps, FormCheckboxCardProps> {
4
+ import type { FormChoiceCardProps } from '../choice-card/types.js';
5
+ export interface FormCheckboxCardField<TContext = NonNullable<unknown>, TDeps = NonNullable<unknown>, TValue extends FormOptionValue = FormOptionValue, TOption extends FormOptionItem<TValue> = FormOptionItem<TValue>> extends FormStatefulFieldBase<'checkbox-card', readonly TValue[] | null, TContext, TDeps, FormChoiceCardProps> {
9
6
  options: FormOptionConfig<TOption, TContext, TDeps, readonly TValue[] | null> | FormOptionsSource<TOption, TContext, TDeps, readonly TValue[] | null>;
10
7
  }
11
8
  export type CheckboxCardFieldOutput<TField> = readonly FieldOptionValue<TField>[] | NullableValue;
@@ -0,0 +1,17 @@
1
+ import type { FormControlUi } from '../../types/index.js';
2
+ /**
3
+ * Content of a card's label: the option icon, inline or in a tile above the label, and the check
4
+ * a selected card shows in its corner. Styled through `ui.tile`, `ui.tileIcon`, `ui.optionIcon`,
5
+ * `ui.check`, and `ui.checkIcon`.
6
+ */
7
+ type __VLS_Props = {
8
+ label?: string;
9
+ icon?: string;
10
+ tile: boolean;
11
+ corner: boolean;
12
+ selected: boolean;
13
+ ui?: FormControlUi;
14
+ };
15
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
16
+ declare const _default: typeof __VLS_export;
17
+ export default _default;
@@ -0,0 +1,50 @@
1
+ <script setup>
2
+ import UIcon from "@nuxt/ui/components/Icon.vue";
3
+ import { mergeFormUiClass } from "../../utils/ui";
4
+ defineProps({
5
+ label: { type: String, required: false },
6
+ icon: { type: String, required: false },
7
+ tile: { type: Boolean, required: true },
8
+ corner: { type: Boolean, required: true },
9
+ selected: { type: Boolean, required: true },
10
+ ui: { type: Object, required: false }
11
+ });
12
+ </script>
13
+
14
+ <template>
15
+ <span
16
+ v-if="tile && icon"
17
+ :data-selected="selected || void 0"
18
+ :class="
19
+ mergeFormUiClass(
20
+ 'grid size-8 place-items-center rounded-lg bg-elevated text-muted transition-colors data-[selected]:bg-primary/10 data-[selected]:text-primary',
21
+ ui?.tile
22
+ )
23
+ "
24
+ data-choice-tile
25
+ >
26
+ <UIcon :name="icon" :class="mergeFormUiClass('size-4', ui?.tileIcon)" aria-hidden="true" />
27
+ </span>
28
+ <span v-if="!tile && icon" class="inline-flex items-center gap-2">
29
+ <UIcon
30
+ :name="icon"
31
+ :class="mergeFormUiClass('size-4 shrink-0', ui?.optionIcon)"
32
+ aria-hidden="true"
33
+ />
34
+ <span>{{ label }}</span>
35
+ </span>
36
+ <span v-else class="block">{{ label }}</span>
37
+ <span
38
+ v-if="corner && selected"
39
+ aria-hidden="true"
40
+ :class="
41
+ mergeFormUiClass(
42
+ 'absolute end-3 top-3 grid size-4 place-items-center rounded-[4px] bg-primary text-inverted',
43
+ ui?.check
44
+ )
45
+ "
46
+ data-choice-check
47
+ >
48
+ <UIcon name="i-lucide-check" :class="mergeFormUiClass('size-2.5', ui?.checkIcon)" />
49
+ </span>
50
+ </template>
@@ -0,0 +1,17 @@
1
+ import type { FormControlUi } from '../../types/index.js';
2
+ /**
3
+ * Content of a card's label: the option icon, inline or in a tile above the label, and the check
4
+ * a selected card shows in its corner. Styled through `ui.tile`, `ui.tileIcon`, `ui.optionIcon`,
5
+ * `ui.check`, and `ui.checkIcon`.
6
+ */
7
+ type __VLS_Props = {
8
+ label?: string;
9
+ icon?: string;
10
+ tile: boolean;
11
+ corner: boolean;
12
+ selected: boolean;
13
+ ui?: FormControlUi;
14
+ };
15
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
16
+ declare const _default: typeof __VLS_export;
17
+ export default _default;