@xleddyl/443-components-vue 0.16.69 → 0.16.71
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/CATALOG.md +130 -127
- package/README.md +70 -16
- package/dist/components/core/Button.vue +67 -5
- package/dist/components/core/Button.vue.d.ts +9 -4
- package/dist/components/core/Form.vue +8 -2
- package/dist/components/core/Form.vue.d.ts +1 -0
- package/dist/components/core/Input.vue.d.ts +2 -2
- package/dist/components/data/RecordList.vue +29 -7
- package/dist/components/forms/ColorInput.vue.d.ts +2 -2
- package/dist/components/forms/DateInput.vue.d.ts +2 -2
- package/dist/components/forms/Fieldset.vue +16 -2
- package/dist/components/forms/Fieldset.vue.d.ts +3 -3
- package/dist/components/forms/NumberInput.vue.d.ts +2 -2
- package/dist/components/forms/PaletteSelect.vue.d.ts +4 -4
- package/dist/components/forms/PasswordInput.vue.d.ts +2 -2
- package/dist/components/forms/PinInput.vue.d.ts +2 -2
- package/dist/components/forms/Slider.vue.d.ts +2 -2
- package/dist/components/forms/TimeInput.vue.d.ts +2 -2
- package/dist/components/layout/Tabs.vue +10 -1
- package/dist/components/layout/ThemeSwitch.vue.d.ts +4 -0
- package/dist/components/overlays/CommandButton.vue +1 -0
- package/dist/components/overlays/ConfirmDialog.vue.d.ts +8 -2
- package/dist/components/overlays/ConfirmHost.vue.d.ts +8 -2
- package/dist/components/overlays/ContextMenu.vue.d.ts +2 -2
- package/dist/components/overlays/Drawer.vue +9 -1
- package/dist/components/overlays/Drawer.vue.d.ts +9 -0
- package/dist/components/overlays/Modal.vue +8 -0
- package/dist/components/overlays/Modal.vue.d.ts +9 -0
- package/dist/components/overlays/Tooltip.vue +3 -1
- package/dist/components/overlays/types.d.ts +1 -0
- package/dist/components/templates/AuthLogin.vue +4 -2
- package/dist/components/templates/FormDrawer.vue +14 -33
- package/dist/components/templates/FormDrawer.vue.d.ts +5 -1
- package/dist/components/templates/FormModal.vue +153 -0
- package/dist/components/templates/FormModal.vue.d.ts +303 -0
- package/dist/components/templates/FormPage.vue +1 -0
- package/dist/components/templates/SettingsModal.vue +63 -8
- package/dist/components/templates/SettingsModal.vue.d.ts +7 -0
- package/dist/components/templates/index.d.ts +1 -0
- package/dist/components/templates/index.mjs +1 -0
- package/dist/components/templates/types.d.ts +4 -2
- package/dist/composables/templates.d.ts +1 -0
- package/dist/composables/templates.mjs +1 -0
- package/dist/composables/useConfirm.mjs +11 -8
- package/dist/composables/useDirtyGuard.mjs +7 -47
- package/dist/composables/useForm.d.ts +4 -0
- package/dist/composables/useForm.mjs +9 -1
- package/dist/composables/useLeaveGuard.d.ts +19 -0
- package/dist/composables/useLeaveGuard.mjs +52 -0
- package/dist/composables/useRouteRecord.mjs +6 -5
- package/dist/grid/DataGrid.vue +124 -8
- package/dist/grid/DataGrid.vue.d.ts +7 -23
- package/dist/grid/cells/ButtonCell.vue +21 -1
- package/dist/grid/cells/ButtonCell.vue.d.ts +4 -0
- package/dist/grid/data-grid.css +7 -1
- package/dist/grid/types.d.ts +12 -0
- package/dist/internal/forms/form-overlay.d.ts +25 -0
- package/dist/internal/forms/form-overlay.mjs +31 -0
- package/dist/internal/forms/leave.d.ts +30 -0
- package/dist/internal/forms/leave.mjs +135 -0
- package/dist/internal/keys.d.ts +1 -0
- package/dist/internal/keys.mjs +1 -0
- package/dist/internal/layout/query.d.ts +4 -0
- package/dist/internal/layout/query.mjs +3 -0
- package/dist/internal/overlays/DialogFrame.vue +43 -5
- package/dist/internal/overlays/DialogFrame.vue.d.ts +11 -1
- package/dist/internal/overlays/HintTooltip.vue +65 -0
- package/dist/internal/overlays/HintTooltip.vue.d.ts +402 -0
- package/dist/internal/overlays/OverlayView.vue.d.ts +10 -4
- package/dist/styles/data/record-list.css +31 -1
- package/dist/styles/forms/fieldset.css +8 -0
- package/dist/styles/overlays/tooltip.css +11 -0
- package/dist/styles/templates/form-modal.css +16 -0
- package/dist/styles/templates.css +1 -0
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<Modal
|
|
3
|
+
ref="modal"
|
|
3
4
|
v-model:open="open"
|
|
4
5
|
class="k-settings-modal"
|
|
5
6
|
size="xl"
|
|
@@ -8,6 +9,9 @@
|
|
|
8
9
|
:sheet="false"
|
|
9
10
|
:label="titleText"
|
|
10
11
|
:before-close="beforeClose"
|
|
12
|
+
:guard="guard"
|
|
13
|
+
:guard-title="guardTitle"
|
|
14
|
+
:guard-message="guardMessage"
|
|
11
15
|
@open="emit('open')"
|
|
12
16
|
@close="emit('close')"
|
|
13
17
|
@after-leave="emit('afterLeave')"
|
|
@@ -131,7 +135,7 @@
|
|
|
131
135
|
</div>
|
|
132
136
|
<template v-for="tabs in [tabsContent(active, close)]" :key="active.id">
|
|
133
137
|
<div v-if="tabs.length" class="k-settings-modal-tabs">
|
|
134
|
-
<
|
|
138
|
+
<RenderNodes :nodes="tabs" />
|
|
135
139
|
</div>
|
|
136
140
|
</template>
|
|
137
141
|
</header>
|
|
@@ -204,6 +208,7 @@ import {
|
|
|
204
208
|
Text,
|
|
205
209
|
computed,
|
|
206
210
|
nextTick,
|
|
211
|
+
onBeforeUnmount,
|
|
207
212
|
onMounted,
|
|
208
213
|
useId,
|
|
209
214
|
useTemplateRef,
|
|
@@ -215,10 +220,12 @@ import { useLinkResolver } from "../../internal/core/link";
|
|
|
215
220
|
import { useScrollEdges } from "../../internal/core/scroll-edges";
|
|
216
221
|
import CollapsedTrail from "../../internal/layout/CollapsedTrail.vue";
|
|
217
222
|
import {
|
|
223
|
+
routeParam as readRouteParam,
|
|
218
224
|
useDefaultRouteLocation,
|
|
219
225
|
useRouteValue,
|
|
220
226
|
writeRouteValue
|
|
221
227
|
} from "../../internal/layout/query";
|
|
228
|
+
import { runNavigationGuard } from "../../internal/forms/leave";
|
|
222
229
|
import { useRouterLike } from "../../internal/layout/route";
|
|
223
230
|
import Button from "../core/Button.vue";
|
|
224
231
|
import Icon from "../core/Icon.vue";
|
|
@@ -252,7 +259,10 @@ const props = defineProps({
|
|
|
252
259
|
route: { type: [String, Boolean], required: false, default: false },
|
|
253
260
|
routeLocation: { type: String, required: false, default: void 0 },
|
|
254
261
|
accountSection: { type: String, required: false, default: "account" },
|
|
255
|
-
resolvePage: { type: Function, required: false }
|
|
262
|
+
resolvePage: { type: Function, required: false },
|
|
263
|
+
guard: { type: Boolean, required: false, default: true },
|
|
264
|
+
guardTitle: { type: String, required: false },
|
|
265
|
+
guardMessage: { type: String, required: false }
|
|
256
266
|
});
|
|
257
267
|
const slots = defineSlots();
|
|
258
268
|
const emit = defineEmits(["open", "close", "afterLeave"]);
|
|
@@ -311,14 +321,29 @@ const activeId = computed(() => {
|
|
|
311
321
|
});
|
|
312
322
|
const active = computed(() => sectionList.value.find((item) => item.id === activeId.value));
|
|
313
323
|
const currentPage = computed(() => pages.value.at(-1));
|
|
314
|
-
|
|
324
|
+
const modal = useTemplateRef("modal");
|
|
325
|
+
function confirmLeave() {
|
|
326
|
+
return modal.value?.confirmLeave() ?? Promise.resolve(true);
|
|
327
|
+
}
|
|
328
|
+
async function guarded(change) {
|
|
329
|
+
if (!await confirmLeave()) return false;
|
|
330
|
+
change();
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
function setDepth(depth) {
|
|
315
334
|
pages.value = pages.value.slice(0, depth);
|
|
316
335
|
}
|
|
336
|
+
function goTo(depth) {
|
|
337
|
+
if (depth >= pages.value.length) return Promise.resolve(true);
|
|
338
|
+
return guarded(() => setDepth(depth));
|
|
339
|
+
}
|
|
317
340
|
function openPage(page) {
|
|
318
|
-
|
|
341
|
+
return guarded(() => {
|
|
342
|
+
pages.value = [...pages.value, page];
|
|
343
|
+
});
|
|
319
344
|
}
|
|
320
345
|
function back() {
|
|
321
|
-
goTo(pages.value.length - 1);
|
|
346
|
+
return goTo(pages.value.length - 1);
|
|
322
347
|
}
|
|
323
348
|
const trail = computed(() => [
|
|
324
349
|
active.value?.label ?? "",
|
|
@@ -332,12 +357,22 @@ function hasContent(node) {
|
|
|
332
357
|
}
|
|
333
358
|
return true;
|
|
334
359
|
}
|
|
360
|
+
const RenderNodes = (props2) => props2.nodes;
|
|
335
361
|
function tabsContent(item, close) {
|
|
336
362
|
const nodes = slots[`tabs-${item.id}`]?.(slotProps(item, close));
|
|
337
363
|
return (nodes ?? []).some(hasContent) ? nodes : [];
|
|
338
364
|
}
|
|
339
365
|
function slotProps(item, close) {
|
|
340
|
-
return {
|
|
366
|
+
return {
|
|
367
|
+
section: item,
|
|
368
|
+
close,
|
|
369
|
+
page: currentPage.value,
|
|
370
|
+
pages: pages.value,
|
|
371
|
+
openPage,
|
|
372
|
+
back,
|
|
373
|
+
goTo,
|
|
374
|
+
confirmLeave
|
|
375
|
+
};
|
|
341
376
|
}
|
|
342
377
|
const router = useRouterLike();
|
|
343
378
|
const routeKey = computed(
|
|
@@ -360,11 +395,28 @@ function decodeSegment(segment) {
|
|
|
360
395
|
return segment;
|
|
361
396
|
}
|
|
362
397
|
}
|
|
398
|
+
let allowedRoute;
|
|
363
399
|
function writeRoute(value, history) {
|
|
364
400
|
const key = routeKey.value;
|
|
365
401
|
if (!router || !key || value === routeParam.value) return;
|
|
402
|
+
allowedRoute = { value };
|
|
366
403
|
void writeRouteValue(router, key, value, history, location.value);
|
|
367
404
|
}
|
|
405
|
+
let removeRouteGuard;
|
|
406
|
+
onMounted(() => {
|
|
407
|
+
if (!router || !syncing.value) return;
|
|
408
|
+
removeRouteGuard = router.beforeEach((to, from) => {
|
|
409
|
+
const key = routeKey.value;
|
|
410
|
+
const pass = allowedRoute;
|
|
411
|
+
allowedRoute = void 0;
|
|
412
|
+
if (!key || !open.value) return true;
|
|
413
|
+
const before = readRouteParam(from, key, location.value);
|
|
414
|
+
const after = readRouteParam(to, key, location.value);
|
|
415
|
+
if (to.path !== from.path || after === before || after === pass?.value) return true;
|
|
416
|
+
return runNavigationGuard(to, confirmLeave);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
onBeforeUnmount(() => removeRouteGuard?.());
|
|
368
420
|
async function settle() {
|
|
369
421
|
await nextTick();
|
|
370
422
|
applying = false;
|
|
@@ -418,7 +470,7 @@ function applyRoute(value) {
|
|
|
418
470
|
void settle();
|
|
419
471
|
}
|
|
420
472
|
watch(activeId, () => {
|
|
421
|
-
if (!applying)
|
|
473
|
+
if (!applying) setDepth(0);
|
|
422
474
|
});
|
|
423
475
|
watch(routeValue, (value) => {
|
|
424
476
|
if (!applying) writeRoute(value, "push");
|
|
@@ -436,7 +488,10 @@ watch(
|
|
|
436
488
|
() => contentBody.value?.scrollTo({ top: 0 })
|
|
437
489
|
);
|
|
438
490
|
function select(id) {
|
|
439
|
-
|
|
491
|
+
if (id === activeId.value) return;
|
|
492
|
+
void guarded(() => {
|
|
493
|
+
section.value = id;
|
|
494
|
+
});
|
|
440
495
|
}
|
|
441
496
|
function signOut() {
|
|
442
497
|
props.onSignOut?.();
|
|
@@ -43,6 +43,9 @@ declare const __VLS_base: import("vue").DefineComponent<{
|
|
|
43
43
|
routeLocation?: RouteLocation;
|
|
44
44
|
accountSection?: string;
|
|
45
45
|
resolvePage?: SettingsPageResolver;
|
|
46
|
+
guard?: boolean;
|
|
47
|
+
guardTitle?: string;
|
|
48
|
+
guardMessage?: string;
|
|
46
49
|
open?: boolean | undefined;
|
|
47
50
|
section?: string;
|
|
48
51
|
pages?: SettingsModalPage[];
|
|
@@ -295,6 +298,9 @@ declare const __VLS_base: import("vue").DefineComponent<{
|
|
|
295
298
|
routeLocation?: RouteLocation;
|
|
296
299
|
accountSection?: string;
|
|
297
300
|
resolvePage?: SettingsPageResolver;
|
|
301
|
+
guard?: boolean;
|
|
302
|
+
guardTitle?: string;
|
|
303
|
+
guardMessage?: string;
|
|
298
304
|
open?: boolean | undefined;
|
|
299
305
|
section?: string;
|
|
300
306
|
pages?: SettingsModalPage[];
|
|
@@ -523,6 +529,7 @@ declare const __VLS_base: import("vue").DefineComponent<{
|
|
|
523
529
|
"onUpdate:pages"?: ((value: SettingsModalPage[]) => any) | undefined;
|
|
524
530
|
}>, {
|
|
525
531
|
sections: SettingsModalSection[];
|
|
532
|
+
guard: boolean;
|
|
526
533
|
presets: false | readonly ThemePreset[];
|
|
527
534
|
themeLabel: string;
|
|
528
535
|
themeText: string;
|
|
@@ -10,6 +10,7 @@ export { default as DetailPage } from './DetailPage.vue';
|
|
|
10
10
|
export { default as EmptyPage } from './EmptyPage.vue';
|
|
11
11
|
export { default as ErrorPage } from './ErrorPage.vue';
|
|
12
12
|
export { default as FormDrawer } from './FormDrawer.vue';
|
|
13
|
+
export { default as FormModal } from './FormModal.vue';
|
|
13
14
|
export { default as FormPage } from './FormPage.vue';
|
|
14
15
|
export { default as ListPage } from './ListPage.vue';
|
|
15
16
|
export { default as NotFoundPage } from './NotFoundPage.vue';
|
|
@@ -10,6 +10,7 @@ export { default as DetailPage } from "./DetailPage.vue";
|
|
|
10
10
|
export { default as EmptyPage } from "./EmptyPage.vue";
|
|
11
11
|
export { default as ErrorPage } from "./ErrorPage.vue";
|
|
12
12
|
export { default as FormDrawer } from "./FormDrawer.vue";
|
|
13
|
+
export { default as FormModal } from "./FormModal.vue";
|
|
13
14
|
export { default as FormPage } from "./FormPage.vue";
|
|
14
15
|
export { default as ListPage } from "./ListPage.vue";
|
|
15
16
|
export { default as NotFoundPage } from "./NotFoundPage.vue";
|
|
@@ -41,8 +41,10 @@ export interface SettingsSectionSlotProps {
|
|
|
41
41
|
close: () => void;
|
|
42
42
|
page: SettingsModalPage | undefined;
|
|
43
43
|
pages: SettingsModalPage[];
|
|
44
|
-
openPage: (page: SettingsModalPage) =>
|
|
45
|
-
back: () =>
|
|
44
|
+
openPage: (page: SettingsModalPage) => Promise<boolean>;
|
|
45
|
+
back: () => Promise<boolean>;
|
|
46
|
+
goTo: (depth: number) => Promise<boolean>;
|
|
47
|
+
confirmLeave: () => Promise<boolean>;
|
|
46
48
|
}
|
|
47
49
|
export interface SettingsModalAccount {
|
|
48
50
|
name: string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useLocale } from "./useLocale.mjs";
|
|
2
2
|
import { isClient } from "../internal/core/env.mjs";
|
|
3
3
|
import { enqueueDialog, hasConfirmHost, nativeDialogMessage } from "../internal/overlays/confirm.mjs";
|
|
4
|
+
import { askDiscard } from "../internal/forms/leave.mjs";
|
|
4
5
|
async function nativeConfirm(options, fallback) {
|
|
5
6
|
if (!window.confirm(nativeDialogMessage(options) || fallback)) return false;
|
|
6
7
|
try {
|
|
@@ -24,14 +25,16 @@ export function useConfirm() {
|
|
|
24
25
|
return confirm({ tone: "danger", confirmLabel: t("common.delete"), ...options, title });
|
|
25
26
|
}
|
|
26
27
|
function confirmDiscard(options = {}) {
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
return askDiscard(
|
|
29
|
+
() => confirm({
|
|
30
|
+
tone: "danger",
|
|
31
|
+
title: t("dirtyGuard.title"),
|
|
32
|
+
description: t("formDrawer.discardDescription"),
|
|
33
|
+
confirmLabel: t("dirtyGuard.confirm"),
|
|
34
|
+
cancelLabel: t("dirtyGuard.cancel"),
|
|
35
|
+
...options
|
|
36
|
+
})
|
|
37
|
+
);
|
|
35
38
|
}
|
|
36
39
|
return Object.assign(confirm, { delete: confirmDelete, discard: confirmDiscard });
|
|
37
40
|
}
|
|
@@ -1,51 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { useLocale } from "./useLocale.mjs";
|
|
4
|
-
import { useConfirm } from "./useConfirm.mjs";
|
|
1
|
+
import { toValue } from "vue";
|
|
2
|
+
import { useLeaveGuard } from "./useLeaveGuard.mjs";
|
|
5
3
|
export function useDirtyGuard(dirty, options = {}) {
|
|
6
|
-
const
|
|
7
|
-
const {
|
|
8
|
-
const router = useRouterLike();
|
|
9
|
-
let bypass = false;
|
|
10
|
-
let removeGuard;
|
|
11
|
-
const { enabled, ...confirmOptions } = options;
|
|
12
|
-
function isBlocking() {
|
|
13
|
-
if (toValue(enabled) === false) return false;
|
|
14
|
-
return Boolean(toValue(dirty));
|
|
15
|
-
}
|
|
4
|
+
const guard = useLeaveGuard(dirty, options);
|
|
5
|
+
const { enabled: _enabled, ...confirmOptions } = options;
|
|
16
6
|
function confirmLeave() {
|
|
17
|
-
if (!
|
|
18
|
-
return
|
|
19
|
-
tone: "danger",
|
|
20
|
-
...confirmOptions,
|
|
21
|
-
title: confirmOptions.title ?? t("dirtyGuard.title"),
|
|
22
|
-
description: confirmOptions.description ?? t("dirtyGuard.description"),
|
|
23
|
-
confirmLabel: confirmOptions.confirmLabel ?? t("dirtyGuard.confirm"),
|
|
24
|
-
cancelLabel: confirmOptions.cancelLabel ?? t("dirtyGuard.cancel")
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
function allowNext() {
|
|
28
|
-
bypass = true;
|
|
29
|
-
}
|
|
30
|
-
function onBeforeUnload(event) {
|
|
31
|
-
if (!isBlocking() || bypass) return;
|
|
32
|
-
event.preventDefault();
|
|
33
|
-
event.returnValue = "";
|
|
7
|
+
if (toValue(options.enabled) === false || !toValue(dirty)) return Promise.resolve(true);
|
|
8
|
+
return guard.scope.confirmLeave(confirmOptions);
|
|
34
9
|
}
|
|
35
|
-
|
|
36
|
-
window.addEventListener("beforeunload", onBeforeUnload);
|
|
37
|
-
removeGuard = router?.beforeEach(async (to, from) => {
|
|
38
|
-
if (bypass) {
|
|
39
|
-
bypass = false;
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
if (to.path === from.path) return true;
|
|
43
|
-
return confirmLeave();
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
onBeforeUnmount(() => {
|
|
47
|
-
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
48
|
-
removeGuard?.();
|
|
49
|
-
});
|
|
50
|
-
return { confirmLeave, allowNext };
|
|
10
|
+
return { confirmLeave, allowNext: guard.allowNext };
|
|
51
11
|
}
|
|
@@ -60,6 +60,7 @@ export interface FormConfig {
|
|
|
60
60
|
messages?: Partial<FormMessages>;
|
|
61
61
|
translateMessage?: FormTranslateMessage;
|
|
62
62
|
submitError?: FormSubmitErrorResolver;
|
|
63
|
+
guard?: boolean;
|
|
63
64
|
}
|
|
64
65
|
export type FormValidationTrigger = 'submit' | 'blur' | 'input';
|
|
65
66
|
export type FormIssueKey = IssueKeyResolver;
|
|
@@ -80,6 +81,7 @@ export interface UseFormOptions<S extends FormSchema = FormSchema, V extends obj
|
|
|
80
81
|
onSubmit?: (values: V) => unknown;
|
|
81
82
|
onInvalid?: (errors: FormErrors<S>) => unknown;
|
|
82
83
|
submitError?: FormSubmitErrorResolver;
|
|
84
|
+
guard?: boolean;
|
|
83
85
|
}
|
|
84
86
|
export type FormErrors<S extends FormSchema = FormSchema> = Partial<Record<keyof S & string, string>> & FormMessageMap;
|
|
85
87
|
export interface FormContext {
|
|
@@ -118,7 +120,9 @@ export interface UseFormReturn<S extends FormSchema = FormSchema, V extends obje
|
|
|
118
120
|
clearErrors: () => void;
|
|
119
121
|
touch: (name: string) => void;
|
|
120
122
|
reset: (values?: Partial<V>) => void;
|
|
123
|
+
markClean: () => void;
|
|
121
124
|
submit: () => Promise<boolean>;
|
|
125
|
+
guard?: boolean;
|
|
122
126
|
}
|
|
123
127
|
export declare function isEmptyValue(value: unknown): boolean;
|
|
124
128
|
export declare function useForm<S extends FormSchema = FormSchema, V extends object = FormValues<S>>(options: UseFormOptions<S, V>): UseFormReturn<S, V>;
|
|
@@ -421,6 +421,10 @@ export function useForm(options) {
|
|
|
421
421
|
for (const key of Object.keys(touched)) touched[key] = false;
|
|
422
422
|
submitCount.value = 0;
|
|
423
423
|
}
|
|
424
|
+
function markClean() {
|
|
425
|
+
baseline = cloneValue(target());
|
|
426
|
+
baselineVersion.value++;
|
|
427
|
+
}
|
|
424
428
|
async function submit() {
|
|
425
429
|
if (submitting.value) return false;
|
|
426
430
|
submitCount.value++;
|
|
@@ -431,8 +435,10 @@ export function useForm(options) {
|
|
|
431
435
|
return false;
|
|
432
436
|
}
|
|
433
437
|
submitting.value = true;
|
|
438
|
+
const version = baselineVersion.value;
|
|
434
439
|
try {
|
|
435
440
|
await options.onSubmit?.(cloneValue(target()));
|
|
441
|
+
if (options.onSubmit && baselineVersion.value === version) markClean();
|
|
436
442
|
} catch (error) {
|
|
437
443
|
const result = resolveSubmitError(error);
|
|
438
444
|
if (!result) throw error;
|
|
@@ -565,7 +571,9 @@ export function useForm(options) {
|
|
|
565
571
|
touch,
|
|
566
572
|
isRequired: isFieldRequired,
|
|
567
573
|
reset,
|
|
568
|
-
|
|
574
|
+
markClean,
|
|
575
|
+
submit,
|
|
576
|
+
guard: options.guard ?? config?.guard ?? true
|
|
569
577
|
};
|
|
570
578
|
}
|
|
571
579
|
export function useFormContext() {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ComputedRef, MaybeRefOrGetter } from 'vue';
|
|
2
|
+
import type { LeaveScope } from '../internal/forms/leave';
|
|
3
|
+
import type { ConfirmOptions } from './useConfirm';
|
|
4
|
+
export type { LeaveScope } from '../internal/forms/leave';
|
|
5
|
+
export interface LeaveGuardOptions extends ConfirmOptions {
|
|
6
|
+
enabled?: MaybeRefOrGetter<boolean | undefined>;
|
|
7
|
+
}
|
|
8
|
+
export interface LeaveScopeOptions extends ConfirmOptions {
|
|
9
|
+
enabled?: MaybeRefOrGetter<boolean | undefined>;
|
|
10
|
+
}
|
|
11
|
+
export interface LeaveGuard {
|
|
12
|
+
dirty: ComputedRef<boolean>;
|
|
13
|
+
scope: LeaveScope;
|
|
14
|
+
confirmLeave: (options?: ConfirmOptions) => Promise<boolean>;
|
|
15
|
+
allowNext: () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function useLeaveScope(): LeaveScope;
|
|
18
|
+
export declare function provideLeaveScope(options?: LeaveScopeOptions): LeaveScope;
|
|
19
|
+
export declare function useLeaveGuard(dirty?: MaybeRefOrGetter<boolean | undefined>, options?: LeaveGuardOptions): LeaveGuard;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { computed, toValue } from "vue";
|
|
2
|
+
import {
|
|
3
|
+
createLeaveScope,
|
|
4
|
+
injectedLeaveScope,
|
|
5
|
+
provideScope,
|
|
6
|
+
registerLeaveEntry,
|
|
7
|
+
rootLeaveScope
|
|
8
|
+
} from "../internal/forms/leave.mjs";
|
|
9
|
+
import { useConfirm } from "./useConfirm.mjs";
|
|
10
|
+
import { useLocale } from "./useLocale.mjs";
|
|
11
|
+
function copyOptions(options) {
|
|
12
|
+
if (!options) return void 0;
|
|
13
|
+
const { title, description, confirmLabel, cancelLabel, tone, icon, body } = options;
|
|
14
|
+
return { title, description, confirmLabel, cancelLabel, tone, icon, body };
|
|
15
|
+
}
|
|
16
|
+
export function useLeaveScope() {
|
|
17
|
+
const injected = injectedLeaveScope();
|
|
18
|
+
if (injected) return injected;
|
|
19
|
+
const confirm = useConfirm();
|
|
20
|
+
const { t } = useLocale();
|
|
21
|
+
return rootLeaveScope(confirm.discard, () => ({
|
|
22
|
+
title: t("dirtyGuard.title"),
|
|
23
|
+
description: t("dirtyGuard.description")
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
export function provideLeaveScope(options = {}) {
|
|
27
|
+
const parent = useLeaveScope();
|
|
28
|
+
const confirm = useConfirm();
|
|
29
|
+
const { enabled, ...confirmOptions } = options;
|
|
30
|
+
const scope = createLeaveScope(confirm.discard, {
|
|
31
|
+
enabled: () => toValue(enabled) !== false,
|
|
32
|
+
options: () => copyOptions(confirmOptions),
|
|
33
|
+
allowNext: () => parent.allowNext()
|
|
34
|
+
});
|
|
35
|
+
return provideScope(scope, parent);
|
|
36
|
+
}
|
|
37
|
+
export function useLeaveGuard(dirty, options = {}) {
|
|
38
|
+
const scope = useLeaveScope();
|
|
39
|
+
const { enabled, ...confirmOptions } = options;
|
|
40
|
+
if (dirty !== void 0) {
|
|
41
|
+
registerLeaveEntry(scope, {
|
|
42
|
+
dirty: () => toValue(enabled) !== false && Boolean(toValue(dirty)),
|
|
43
|
+
options: () => copyOptions(confirmOptions)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
dirty: computed(() => scope.dirty.value),
|
|
48
|
+
scope,
|
|
49
|
+
confirmLeave: (extra) => scope.confirmLeave({ ...copyOptions(confirmOptions), ...extra }),
|
|
50
|
+
allowNext: () => scope.allowNext()
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
} from "../internal/layout/query.mjs";
|
|
8
8
|
import { useRouterLike } from "../internal/layout/route.mjs";
|
|
9
9
|
import { useRouteState } from "./useRouteState.mjs";
|
|
10
|
+
import { dialogScope, runNavigationGuard } from "../internal/forms/leave.mjs";
|
|
11
|
+
import { modalStack } from "../internal/overlays/dialog.mjs";
|
|
10
12
|
export const NEW_ROUTE_RECORD = "new";
|
|
11
13
|
export function useRouteRecord(key, options = {}) {
|
|
12
14
|
const router = useRouterLike();
|
|
@@ -30,8 +32,7 @@ export function useRouteRecord(key, options = {}) {
|
|
|
30
32
|
param.value = changes[key];
|
|
31
33
|
return Promise.resolve(true);
|
|
32
34
|
}
|
|
33
|
-
if (
|
|
34
|
-
allowed = { value: changes[key] };
|
|
35
|
+
if (routeValue(router.currentRoute?.value) !== changes[key]) allowed = { value: changes[key] };
|
|
35
36
|
const writes = Object.entries(changes).map(
|
|
36
37
|
([name, value]) => writeRouteValue(router, name, value, history, location)
|
|
37
38
|
);
|
|
@@ -62,8 +63,8 @@ export function useRouteRecord(key, options = {}) {
|
|
|
62
63
|
{ immediate: true }
|
|
63
64
|
);
|
|
64
65
|
}
|
|
65
|
-
const beforeLeave = options.beforeLeave;
|
|
66
|
-
if (
|
|
66
|
+
const beforeLeave = options.beforeLeave ?? (() => dialogScope(modalStack[modalStack.length - 1])?.confirmLeave() ?? true);
|
|
67
|
+
if (router && getCurrentInstance()) {
|
|
67
68
|
let removeGuard;
|
|
68
69
|
onMounted(() => {
|
|
69
70
|
removeGuard = router.beforeEach(async (to, from) => {
|
|
@@ -73,7 +74,7 @@ export function useRouteRecord(key, options = {}) {
|
|
|
73
74
|
if (!before) return true;
|
|
74
75
|
const after = routeValue(to);
|
|
75
76
|
if (to.path === from.path && (after === before || after === pass?.value)) return true;
|
|
76
|
-
return
|
|
77
|
+
return runNavigationGuard(to, beforeLeave);
|
|
77
78
|
});
|
|
78
79
|
});
|
|
79
80
|
onBeforeUnmount(() => removeGuard?.());
|