@aerogel/core 0.0.0-next.a3194a101ceda87a7033fa5e283e046bad6f4bfc → 0.0.0-next.a3d64a2a6fe8f788cc0fdd63d5ad52404d5058d4
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/aerogel-core.css +1 -0
- package/dist/aerogel-core.d.ts +2283 -1657
- package/dist/aerogel-core.js +3712 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +32 -37
- package/src/bootstrap/bootstrap.test.ts +4 -8
- package/src/bootstrap/index.ts +25 -16
- package/src/bootstrap/options.ts +1 -1
- package/src/components/AppLayout.vue +14 -0
- package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
- package/src/components/AppOverlays.vue +9 -0
- package/src/components/AppToasts.vue +16 -0
- package/src/components/contracts/AlertModal.ts +19 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +48 -0
- package/src/components/contracts/DropdownMenu.ts +25 -0
- package/src/components/contracts/ErrorReportModal.ts +33 -0
- package/src/components/contracts/Input.ts +26 -0
- package/src/components/contracts/LoadingModal.ts +26 -0
- package/src/components/contracts/Modal.ts +21 -0
- package/src/components/contracts/PromptModal.ts +34 -0
- package/src/components/contracts/Select.ts +45 -0
- package/src/components/contracts/Toast.ts +15 -0
- package/src/components/contracts/index.ts +11 -0
- package/src/components/headless/HeadlessButton.vue +51 -0
- package/src/components/headless/HeadlessInput.vue +59 -0
- package/src/components/headless/{forms/AGHeadlessInputDescription.vue → HeadlessInputDescription.vue} +7 -8
- package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +86 -0
- package/src/components/headless/{forms/AGHeadlessInputLabel.vue → HeadlessInputLabel.vue} +3 -7
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/HeadlessModal.vue +57 -0
- package/src/components/headless/HeadlessModalContent.vue +30 -0
- package/src/components/headless/HeadlessModalDescription.vue +12 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +120 -0
- package/src/components/headless/{forms/AGHeadlessSelectError.vue → HeadlessSelectError.vue} +5 -6
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +42 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/HeadlessSwitch.vue +96 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +20 -3
- package/src/components/index.ts +6 -9
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +17 -0
- package/src/components/ui/Button.vue +115 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +50 -0
- package/src/components/ui/DropdownMenu.vue +32 -0
- package/src/components/ui/DropdownMenuOption.vue +22 -0
- package/src/components/ui/DropdownMenuOptions.vue +44 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorLogs.vue +19 -0
- package/src/components/ui/ErrorLogsModal.vue +48 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +73 -0
- package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +34 -27
- package/src/components/ui/ErrorReportModalTitle.vue +24 -0
- package/src/components/ui/Form.vue +24 -0
- package/src/components/ui/Input.vue +56 -0
- package/src/components/ui/Link.vue +12 -0
- package/src/components/ui/LoadingModal.vue +34 -0
- package/src/components/ui/Markdown.vue +97 -0
- package/src/components/ui/Modal.vue +131 -0
- package/src/components/ui/ModalContext.vue +31 -0
- package/src/components/ui/ProgressBar.vue +51 -0
- package/src/components/ui/PromptModal.vue +38 -0
- package/src/components/ui/Select.vue +27 -0
- package/src/components/ui/SelectLabel.vue +21 -0
- package/src/components/ui/SelectOption.vue +29 -0
- package/src/components/ui/SelectOptions.vue +35 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/Setting.vue +31 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/{lib/AGStartupCrash.vue → ui/StartupCrash.vue} +8 -8
- package/src/components/ui/Switch.vue +11 -0
- package/src/components/ui/TextArea.vue +56 -0
- package/src/components/ui/Toast.vue +46 -0
- package/src/components/ui/index.ts +35 -0
- package/src/directives/index.ts +9 -5
- package/src/directives/measure.ts +33 -8
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +29 -27
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +15 -8
- package/src/errors/settings/Debug.vue +32 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +17 -1
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/FormController.ts +255 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +76 -20
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/jobs/Job.ts +144 -2
- package/src/jobs/index.ts +4 -1
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/lang/DefaultLangProvider.ts +46 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +48 -21
- package/src/lang/index.ts +12 -6
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/plugins/Plugin.ts +1 -1
- package/src/plugins/index.ts +10 -7
- package/src/services/App.state.ts +36 -3
- package/src/services/App.ts +19 -3
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +8 -8
- package/src/services/Events.ts +16 -12
- package/src/services/Service.ts +125 -54
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +18 -7
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +8 -3
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +14 -12
- package/src/ui/UI.ts +252 -122
- package/src/ui/index.ts +28 -28
- package/src/ui/utils.ts +16 -0
- package/src/utils/classes.ts +41 -0
- package/src/utils/composition/events.ts +4 -6
- package/src/utils/composition/forms.ts +20 -4
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +33 -0
- package/src/utils/index.ts +5 -1
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +53 -6
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +38 -132
- package/dist/aerogel-core.cjs.js +0 -2
- package/dist/aerogel-core.cjs.js.map +0 -1
- package/dist/aerogel-core.esm.js +0 -2
- package/dist/aerogel-core.esm.js.map +0 -1
- package/histoire.config.ts +0 -7
- package/noeldemartin.config.js +0 -5
- package/postcss.config.js +0 -6
- package/src/assets/histoire.css +0 -3
- package/src/components/AGAppLayout.vue +0 -16
- package/src/components/AGAppOverlays.vue +0 -41
- package/src/components/AGAppSnackbars.vue +0 -13
- package/src/components/constants.ts +0 -8
- package/src/components/forms/AGButton.vue +0 -44
- package/src/components/forms/AGCheckbox.vue +0 -41
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -40
- package/src/components/forms/AGSelect.story.vue +0 -46
- package/src/components/forms/AGSelect.vue +0 -60
- package/src/components/forms/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -30
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -58
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -49
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -42
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -77
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +0 -4
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +0 -39
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +0 -3
- package/src/components/headless/forms/index.ts +0 -15
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -34
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -86
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
- package/src/components/headless/modals/index.ts +0 -4
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
- package/src/components/headless/snackbars/index.ts +0 -40
- package/src/components/lib/AGErrorMessage.vue +0 -16
- package/src/components/lib/AGLink.vue +0 -9
- package/src/components/lib/AGMarkdown.vue +0 -41
- package/src/components/lib/AGMeasured.vue +0 -15
- package/src/components/lib/index.ts +0 -5
- package/src/components/modals/AGAlertModal.ts +0 -15
- package/src/components/modals/AGAlertModal.vue +0 -14
- package/src/components/modals/AGConfirmModal.ts +0 -27
- package/src/components/modals/AGConfirmModal.vue +0 -26
- package/src/components/modals/AGErrorReportModal.ts +0 -46
- package/src/components/modals/AGErrorReportModal.vue +0 -54
- package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
- package/src/components/modals/AGLoadingModal.ts +0 -23
- package/src/components/modals/AGLoadingModal.vue +0 -15
- package/src/components/modals/AGModal.ts +0 -10
- package/src/components/modals/AGModal.vue +0 -39
- package/src/components/modals/AGModalContext.ts +0 -8
- package/src/components/modals/AGModalContext.vue +0 -22
- package/src/components/modals/AGModalTitle.vue +0 -9
- package/src/components/modals/AGPromptModal.ts +0 -30
- package/src/components/modals/AGPromptModal.vue +0 -34
- package/src/components/modals/index.ts +0 -17
- package/src/components/snackbars/AGSnackbar.vue +0 -36
- package/src/components/snackbars/index.ts +0 -3
- package/src/components/utils.ts +0 -10
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -58
- package/src/forms/Form.ts +0 -185
- package/src/forms/composition.ts +0 -6
- package/src/main.histoire.ts +0 -1
- package/src/utils/tailwindcss.test.ts +0 -26
- package/src/utils/tailwindcss.ts +0 -7
- package/tailwind.config.js +0 -4
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
- /package/src/{main.ts → index.ts} +0 -0
package/src/errors/Errors.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
1
|
+
import { JSError, facade, isDevelopment, isObject, isTesting, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import App from '
|
|
4
|
-
import ServiceBootError from '
|
|
5
|
-
import UI
|
|
6
|
-
import { translateWithDefault } from '
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
5
|
+
import UI from '@aerogel/core/ui/UI';
|
|
6
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
7
|
+
import { Events } from '@aerogel/core/services';
|
|
7
8
|
|
|
8
9
|
import Service from './Errors.state';
|
|
9
|
-
import { Colors } from '@/components/constants';
|
|
10
|
-
import { Events } from '@/services';
|
|
11
|
-
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
12
10
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
13
|
-
import type { ModalComponent } from '@/ui/UI.state';
|
|
14
11
|
|
|
15
12
|
export class ErrorsService extends Service {
|
|
16
13
|
|
|
@@ -25,28 +22,33 @@ export class ErrorsService extends Service {
|
|
|
25
22
|
this.enabled = false;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (
|
|
25
|
+
public async inspect(error: ErrorSource | ErrorReport, reports?: ErrorReport[]): Promise<void>;
|
|
26
|
+
public async inspect(reports: ErrorReport[]): Promise<void>;
|
|
27
|
+
public async inspect(errorOrReports: ErrorSource | ErrorReport[], _reports?: ErrorReport[]): Promise<void> {
|
|
28
|
+
if (Array.isArray(errorOrReports) && errorOrReports.length === 0) {
|
|
32
29
|
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
33
30
|
|
|
34
31
|
return;
|
|
35
32
|
}
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const report = Array.isArray(errorOrReports)
|
|
35
|
+
? (errorOrReports[0] as ErrorReport)
|
|
36
|
+
: this.isErrorReport(errorOrReports)
|
|
37
|
+
? errorOrReports
|
|
38
|
+
: await this.createErrorReport(errorOrReports);
|
|
39
|
+
const reports = Array.isArray(errorOrReports) ? (errorOrReports as ErrorReport[]) : (_reports ?? [report]);
|
|
40
|
+
|
|
41
|
+
UI.modal(UI.requireComponent('error-report-modal'), { report, reports });
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
43
45
|
await Events.emit('error', { error, message });
|
|
44
46
|
|
|
45
|
-
if (
|
|
47
|
+
if (isTesting('unit')) {
|
|
46
48
|
throw error;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
if (
|
|
51
|
+
if (isDevelopment()) {
|
|
50
52
|
this.logError(error);
|
|
51
53
|
}
|
|
52
54
|
|
|
@@ -71,20 +73,16 @@ export class ErrorsService extends Service {
|
|
|
71
73
|
date: new Date(),
|
|
72
74
|
};
|
|
73
75
|
|
|
74
|
-
UI.
|
|
76
|
+
UI.toast(
|
|
75
77
|
message ??
|
|
76
78
|
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
77
79
|
{
|
|
78
|
-
|
|
80
|
+
variant: 'danger',
|
|
79
81
|
actions: [
|
|
80
82
|
{
|
|
81
|
-
|
|
83
|
+
label: translateWithDefault('errors.viewDetails', 'View details'),
|
|
82
84
|
dismiss: true,
|
|
83
|
-
|
|
84
|
-
UI.openModal<ModalComponent<AGErrorReportModalProps>>(
|
|
85
|
-
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
86
|
-
{ reports: [report] },
|
|
87
|
-
),
|
|
85
|
+
click: () => UI.modal(UI.requireComponent('error-report-modal'), { report, reports: [report] }),
|
|
88
86
|
},
|
|
89
87
|
],
|
|
90
88
|
},
|
|
@@ -126,6 +124,10 @@ export class ErrorsService extends Service {
|
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
126
|
|
|
127
|
+
private isErrorReport(error: unknown): error is ErrorReport {
|
|
128
|
+
return isObject(error) && 'title' in error;
|
|
129
|
+
}
|
|
130
|
+
|
|
129
131
|
private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
|
|
130
132
|
if (typeof error === 'string') {
|
|
131
133
|
return { title: error };
|
|
@@ -178,7 +180,7 @@ export class ErrorsService extends Service {
|
|
|
178
180
|
|
|
179
181
|
export default facade(ErrorsService);
|
|
180
182
|
|
|
181
|
-
declare module '
|
|
183
|
+
declare module '@aerogel/core/services/Events' {
|
|
182
184
|
export interface EventsPayload {
|
|
183
185
|
error: { error: ErrorSource; message?: string };
|
|
184
186
|
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import type { App } from 'vue';
|
|
1
|
+
import type { App as AppInstance } from 'vue';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
|
+
import { bootServices } from '@aerogel/core/services';
|
|
5
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
5
6
|
|
|
6
7
|
import Errors from './Errors';
|
|
7
|
-
import
|
|
8
|
+
import settings from './settings';
|
|
9
|
+
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
8
10
|
|
|
9
11
|
export * from './utils';
|
|
10
|
-
export { Errors
|
|
12
|
+
export { Errors };
|
|
13
|
+
export { default as JobCancelledError } from './JobCancelledError';
|
|
14
|
+
export { default as ServiceBootError } from './ServiceBootError';
|
|
15
|
+
export type { ErrorSource, ErrorReport, ErrorReportLog };
|
|
11
16
|
|
|
12
17
|
const services = { $errors: Errors };
|
|
13
18
|
const frameworkHandler: ErrorHandler = (error) => {
|
|
@@ -16,7 +21,7 @@ const frameworkHandler: ErrorHandler = (error) => {
|
|
|
16
21
|
return true;
|
|
17
22
|
};
|
|
18
23
|
|
|
19
|
-
function setUpErrorHandler(app:
|
|
24
|
+
function setUpErrorHandler(app: AppInstance, baseHandler: ErrorHandler = () => false): void {
|
|
20
25
|
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
21
26
|
|
|
22
27
|
app.config.errorHandler = errorHandler;
|
|
@@ -31,16 +36,18 @@ export default definePlugin({
|
|
|
31
36
|
async install(app, options) {
|
|
32
37
|
setUpErrorHandler(app, options.handleError);
|
|
33
38
|
|
|
39
|
+
settings.forEach((setting) => App.addSetting(setting));
|
|
40
|
+
|
|
34
41
|
await bootServices(app, services);
|
|
35
42
|
},
|
|
36
43
|
});
|
|
37
44
|
|
|
38
|
-
declare module '
|
|
45
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
39
46
|
export interface AerogelOptions {
|
|
40
47
|
handleError?(error: ErrorSource): boolean;
|
|
41
48
|
}
|
|
42
49
|
}
|
|
43
50
|
|
|
44
|
-
declare module '
|
|
51
|
+
declare module '@aerogel/core/services' {
|
|
45
52
|
export interface Services extends ErrorsServices {}
|
|
46
53
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<Setting
|
|
3
|
+
title-id="debug-setting"
|
|
4
|
+
:title="$td('settings.debug', 'Debugging')"
|
|
5
|
+
:description="$td('settings.debugDescription', 'Enable debugging with [Eruda](https://eruda.liriliri.io/).')"
|
|
6
|
+
>
|
|
7
|
+
<Switch v-model="enabled" aria-labelledby="debug-setting" />
|
|
8
|
+
</Setting>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script setup lang="ts">
|
|
12
|
+
import { ref, watchEffect } from 'vue';
|
|
13
|
+
import type Eruda from 'eruda';
|
|
14
|
+
|
|
15
|
+
import Setting from '@aerogel/core/components/ui/Setting.vue';
|
|
16
|
+
import Switch from '@aerogel/core/components/ui/Switch.vue';
|
|
17
|
+
|
|
18
|
+
let eruda: typeof Eruda | null = null;
|
|
19
|
+
const enabled = ref(false);
|
|
20
|
+
|
|
21
|
+
watchEffect(async () => {
|
|
22
|
+
if (!enabled.value) {
|
|
23
|
+
eruda?.destroy();
|
|
24
|
+
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
eruda ??= (await import('eruda')).default;
|
|
29
|
+
|
|
30
|
+
eruda.init();
|
|
31
|
+
});
|
|
32
|
+
</script>
|
package/src/errors/utils.ts
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
-
import { translateWithDefault } from '
|
|
2
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
3
3
|
import type { ErrorSource } from './Errors.state';
|
|
4
4
|
|
|
5
|
+
const handlers: ErrorHandler[] = [];
|
|
6
|
+
|
|
7
|
+
export type ErrorHandler = (error: ErrorSource) => string | undefined;
|
|
8
|
+
|
|
9
|
+
export function registerErrorHandler(handler: ErrorHandler): void {
|
|
10
|
+
handlers.push(handler);
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
export function getErrorMessage(error: ErrorSource): string {
|
|
14
|
+
for (const handler of handlers) {
|
|
15
|
+
const result = handler(error);
|
|
16
|
+
|
|
17
|
+
if (result) {
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
6
22
|
if (typeof error === 'string') {
|
|
7
23
|
return error;
|
|
8
24
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from 'vitest';
|
|
2
|
+
import { tt } from '@noeldemartin/testing';
|
|
3
|
+
import type { Equals } from '@noeldemartin/utils';
|
|
4
|
+
import type { Expect } from '@noeldemartin/testing';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
enumInput,
|
|
8
|
+
numberInput,
|
|
9
|
+
objectInput,
|
|
10
|
+
requiredObjectInput,
|
|
11
|
+
requiredStringInput,
|
|
12
|
+
stringInput,
|
|
13
|
+
} from '@aerogel/core/forms/utils';
|
|
14
|
+
import { useForm } from '@aerogel/core/utils/composition/forms';
|
|
15
|
+
|
|
16
|
+
describe('FormController', () => {
|
|
17
|
+
|
|
18
|
+
it('defines magic fields', () => {
|
|
19
|
+
const form = useForm({
|
|
20
|
+
name: requiredStringInput(),
|
|
21
|
+
age: numberInput(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expectTypeOf(form.name).toEqualTypeOf<string>();
|
|
25
|
+
expectTypeOf(form.age).toEqualTypeOf<number | null>();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('validates required fields', () => {
|
|
29
|
+
// Arrange
|
|
30
|
+
const form = useForm({
|
|
31
|
+
name: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
rules: 'required',
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Act
|
|
38
|
+
form.submit();
|
|
39
|
+
|
|
40
|
+
// Assert
|
|
41
|
+
expect(form.valid).toBe(false);
|
|
42
|
+
expect(form.submitted).toBe(true);
|
|
43
|
+
expect(form.errors.name).toEqual(['required']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('resets form', () => {
|
|
47
|
+
// Arrange
|
|
48
|
+
const form = useForm({
|
|
49
|
+
name: {
|
|
50
|
+
type: 'string',
|
|
51
|
+
rules: 'required',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
form.name = 'Foo bar';
|
|
56
|
+
form.submit();
|
|
57
|
+
|
|
58
|
+
// Act
|
|
59
|
+
form.reset();
|
|
60
|
+
|
|
61
|
+
// Assert
|
|
62
|
+
expect(form.valid).toBe(true);
|
|
63
|
+
expect(form.submitted).toBe(false);
|
|
64
|
+
expect(form.name).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('trims values', () => {
|
|
68
|
+
// Arrange
|
|
69
|
+
const form = useForm({
|
|
70
|
+
trimmed: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
rules: 'required',
|
|
73
|
+
},
|
|
74
|
+
untrimmed: {
|
|
75
|
+
type: 'string',
|
|
76
|
+
rules: 'required',
|
|
77
|
+
trim: false,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Act
|
|
82
|
+
form.trimmed = ' ';
|
|
83
|
+
form.untrimmed = ' ';
|
|
84
|
+
|
|
85
|
+
form.submit();
|
|
86
|
+
|
|
87
|
+
// Assert
|
|
88
|
+
expect(form.valid).toBe(false);
|
|
89
|
+
expect(form.submitted).toBe(true);
|
|
90
|
+
expect(form.trimmed).toEqual('');
|
|
91
|
+
expect(form.untrimmed).toEqual(' ');
|
|
92
|
+
expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('infers field types', () => {
|
|
96
|
+
const form = useForm({
|
|
97
|
+
one: stringInput(),
|
|
98
|
+
two: requiredStringInput(),
|
|
99
|
+
three: objectInput(),
|
|
100
|
+
four: requiredObjectInput<{ foo: string; bar?: number }>(),
|
|
101
|
+
five: enumInput(['foo', 'bar']),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
tt<
|
|
105
|
+
| Expect<Equals<typeof form.one, string | null>>
|
|
106
|
+
| Expect<Equals<typeof form.two, string>>
|
|
107
|
+
| Expect<Equals<typeof form.three, object | null>>
|
|
108
|
+
| Expect<Equals<typeof form.four, { foo: string; bar?: number }>>
|
|
109
|
+
| Expect<Equals<typeof form.five, 'foo' | 'bar' | null>>
|
|
110
|
+
>();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
4
|
+
|
|
5
|
+
import { validate, validateType } from './validation';
|
|
6
|
+
|
|
7
|
+
export const __valueType: unique symbol = Symbol();
|
|
8
|
+
|
|
9
|
+
export interface FormFieldDefinition<
|
|
10
|
+
TType extends FormFieldType = FormFieldType,
|
|
11
|
+
TRules extends string = string,
|
|
12
|
+
TValueType = unknown,
|
|
13
|
+
> {
|
|
14
|
+
type: TType;
|
|
15
|
+
trim?: boolean;
|
|
16
|
+
default?: GetFormFieldValue<TType>;
|
|
17
|
+
rules?: TRules;
|
|
18
|
+
values?: readonly TValueType[];
|
|
19
|
+
[__valueType]?: TValueType;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FormFieldType = 'string' | 'enum' | 'number' | 'boolean' | 'object' | 'date';
|
|
23
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
24
|
+
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
25
|
+
|
|
26
|
+
export type FormData<T> = {
|
|
27
|
+
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules, infer TValueType>
|
|
28
|
+
? TRules extends 'required'
|
|
29
|
+
? GetFormFieldValue<TType, TValueType>
|
|
30
|
+
: GetFormFieldValue<TType, TValueType> | null
|
|
31
|
+
: never;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FormErrors<T> = {
|
|
35
|
+
[k in keyof T]: string[] | null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type GetFormFieldValue<TType, TValueType = unknown> = TType extends 'string'
|
|
39
|
+
? string
|
|
40
|
+
: TType extends 'number'
|
|
41
|
+
? number
|
|
42
|
+
: TType extends 'boolean'
|
|
43
|
+
? boolean
|
|
44
|
+
: TType extends 'enum'
|
|
45
|
+
? TValueType
|
|
46
|
+
: TType extends 'object'
|
|
47
|
+
? TValueType extends object
|
|
48
|
+
? TValueType
|
|
49
|
+
: object
|
|
50
|
+
: TType extends 'date'
|
|
51
|
+
? Date
|
|
52
|
+
: never;
|
|
53
|
+
|
|
54
|
+
const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
|
|
55
|
+
|
|
56
|
+
export type SubmitFormListener = () => unknown;
|
|
57
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
58
|
+
|
|
59
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
60
|
+
|
|
61
|
+
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
62
|
+
|
|
63
|
+
private _fields: Fields;
|
|
64
|
+
private _data: FormData<Fields>;
|
|
65
|
+
private _submitted: Ref<boolean>;
|
|
66
|
+
private _errors: FormErrors<Fields>;
|
|
67
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
68
|
+
|
|
69
|
+
constructor(fields: Fields) {
|
|
70
|
+
super();
|
|
71
|
+
|
|
72
|
+
this._fields = fields;
|
|
73
|
+
this._submitted = ref(false);
|
|
74
|
+
this._data = this.getInitialData(fields);
|
|
75
|
+
this._errors = this.getInitialErrors(fields);
|
|
76
|
+
|
|
77
|
+
validForms.set(
|
|
78
|
+
this,
|
|
79
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
this.errors = readonly(this._errors);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get valid(): boolean {
|
|
86
|
+
return !!validForms.get(this)?.value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public get submitted(): boolean {
|
|
90
|
+
return this._submitted.value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
94
|
+
const definition =
|
|
95
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
96
|
+
|
|
97
|
+
this._data[field] =
|
|
98
|
+
definition.type === 'string' && (definition.trim ?? true)
|
|
99
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
100
|
+
: value;
|
|
101
|
+
|
|
102
|
+
if (this._submitted.value) {
|
|
103
|
+
this.validate();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getFieldValue<T extends keyof Fields>(field: T): GetFormFieldValue<Fields[T]['type']> {
|
|
108
|
+
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
112
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getFieldType<T extends keyof Fields>(field: T): FormFieldType | null {
|
|
116
|
+
return this._fields[field]?.type ?? null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public data(): FormData<Fields> {
|
|
120
|
+
return { ...this._data };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public validate(): boolean {
|
|
124
|
+
const errors = Object.entries(this._fields).reduce(
|
|
125
|
+
(formErrors, [name, definition]) => {
|
|
126
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
127
|
+
|
|
128
|
+
return formErrors;
|
|
129
|
+
},
|
|
130
|
+
{} as Record<string, string[] | null>,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
this.resetErrors(errors);
|
|
134
|
+
|
|
135
|
+
return this.valid;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
|
|
139
|
+
this._submitted.value = false;
|
|
140
|
+
|
|
141
|
+
options.keepData || this.resetData();
|
|
142
|
+
options.keepErrors || this.resetErrors();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public submit(): boolean {
|
|
146
|
+
this._submitted.value = true;
|
|
147
|
+
|
|
148
|
+
const valid = this.validate();
|
|
149
|
+
|
|
150
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
151
|
+
|
|
152
|
+
return valid;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
156
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
157
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
158
|
+
this._listeners[event] ??= [];
|
|
159
|
+
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
+
this._listeners[event]?.push(listener as any);
|
|
162
|
+
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
164
|
+
return () => this.off(event as any, listener);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
168
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
169
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
170
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public async focus(input: string): Promise<void> {
|
|
174
|
+
await nextTick();
|
|
175
|
+
|
|
176
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
protected override __get(property: string): unknown {
|
|
180
|
+
if (!(property in this._fields)) {
|
|
181
|
+
return super.__get(property);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return this.getFieldValue(property);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
protected override __set(property: string, value: unknown): void {
|
|
188
|
+
if (!(property in this._fields)) {
|
|
189
|
+
super.__set(property, value);
|
|
190
|
+
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
198
|
+
const errors = [];
|
|
199
|
+
const value = this._data[name];
|
|
200
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
201
|
+
|
|
202
|
+
errors.push(...validateType(value, definition));
|
|
203
|
+
|
|
204
|
+
for (const rule of rules) {
|
|
205
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
errors.push(...validate(value, rule));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return errors.length > 0 ? errors : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private getInitialData(fields: Fields): FormData<Fields> {
|
|
216
|
+
if (this.static().isConjuring()) {
|
|
217
|
+
return {} as FormData<Fields>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
221
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
222
|
+
|
|
223
|
+
return initialData;
|
|
224
|
+
}, {} as FormData<Fields>);
|
|
225
|
+
|
|
226
|
+
return reactive(data) as FormData<Fields>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private getInitialErrors(fields: Fields): FormErrors<Fields> {
|
|
230
|
+
if (this.static().isConjuring()) {
|
|
231
|
+
return {} as FormErrors<Fields>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
235
|
+
formErrors[name as keyof Fields] = null;
|
|
236
|
+
|
|
237
|
+
return formErrors;
|
|
238
|
+
}, {} as FormErrors<Fields>);
|
|
239
|
+
|
|
240
|
+
return reactive(errors) as FormErrors<Fields>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private resetData(): void {
|
|
244
|
+
for (const [name, field] of Object.entries(this._fields)) {
|
|
245
|
+
this._data[name as keyof Fields] = (field.default ?? null) as FormData<Fields>[keyof Fields];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private resetErrors(errors?: Record<string, string[] | null>): void {
|
|
250
|
+
Object.keys(this._errors).forEach((key) => delete this._errors[key as keyof Fields]);
|
|
251
|
+
|
|
252
|
+
errors && Object.assign(this._errors, errors);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
}
|
package/src/forms/index.ts
CHANGED