@aerogel/core 0.0.0-next.b58141fee5d2fe7d25debdbca6b1d2bf1c13e48e → 0.0.0-next.b656a964404fbde17d9cce7668722596098e47fd
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.d.ts +2082 -813
- package/dist/aerogel-core.js +3119 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +36 -34
- package/src/bootstrap/bootstrap.test.ts +7 -10
- package/src/bootstrap/index.ts +43 -14
- package/src/bootstrap/options.ts +4 -1
- package/src/components/AppLayout.vue +16 -0
- package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
- package/src/components/{AGAppOverlays.vue → AppOverlays.vue} +5 -6
- package/src/components/AppToasts.vue +16 -0
- package/src/components/contracts/AlertModal.ts +4 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +41 -0
- package/src/components/contracts/DropdownMenu.ts +11 -0
- package/src/components/contracts/ErrorReportModal.ts +29 -0
- package/src/components/contracts/Input.ts +26 -0
- package/src/components/contracts/LoadingModal.ts +18 -0
- package/src/components/contracts/Modal.ts +13 -0
- package/src/components/contracts/PromptModal.ts +28 -0
- package/src/components/contracts/Select.ts +36 -0
- package/src/components/contracts/Toast.ts +13 -0
- package/src/components/contracts/index.ts +9 -0
- package/src/components/contracts/shared.ts +9 -0
- package/src/components/headless/HeadlessButton.vue +50 -0
- package/src/components/headless/HeadlessInput.vue +59 -0
- package/src/components/headless/HeadlessInputDescription.vue +27 -0
- package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +75 -0
- package/src/components/headless/HeadlessInputLabel.vue +18 -0
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/{modals/AGHeadlessModal.vue → HeadlessModal.vue} +17 -19
- package/src/components/headless/HeadlessModalContent.vue +24 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +104 -0
- package/src/components/headless/HeadlessSelectError.vue +25 -0
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +30 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +15 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +18 -3
- package/src/components/index.ts +6 -9
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +13 -0
- package/src/components/ui/Button.vue +98 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +42 -0
- package/src/components/ui/DropdownMenu.vue +33 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +62 -0
- package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +38 -29
- 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 +32 -0
- package/src/components/ui/Markdown.vue +69 -0
- package/src/components/ui/Modal.vue +70 -0
- package/src/components/ui/ModalContext.vue +30 -0
- package/src/components/ui/ProgressBar.vue +50 -0
- package/src/components/ui/PromptModal.vue +35 -0
- package/src/components/ui/Select.vue +21 -0
- package/src/components/ui/SelectLabel.vue +10 -0
- package/src/components/ui/SelectOptions.vue +31 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +31 -0
- package/src/components/ui/Toast.vue +42 -0
- package/src/components/ui/index.ts +27 -0
- package/src/directives/index.ts +13 -5
- package/src/directives/measure.ts +40 -0
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +35 -34
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +19 -29
- package/src/errors/utils.ts +35 -0
- package/src/forms/{Form.test.ts → FormController.test.ts} +33 -4
- package/src/forms/{Form.ts → FormController.ts} +94 -25
- package/src/forms/composition.ts +4 -4
- package/src/forms/index.ts +3 -1
- package/src/forms/utils.ts +36 -5
- package/src/forms/validation.ts +19 -0
- package/src/index.css +46 -0
- package/src/{main.ts → index.ts} +3 -0
- package/src/jobs/Job.ts +147 -0
- package/src/jobs/index.ts +10 -0
- 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 +44 -29
- package/src/lang/index.ts +11 -6
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/plugins/Plugin.ts +2 -1
- package/src/plugins/index.ts +22 -0
- package/src/services/App.state.ts +40 -6
- package/src/services/App.ts +48 -5
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +112 -32
- package/src/services/Service.ts +154 -49
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +18 -6
- package/src/services/store.ts +8 -5
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +19 -7
- package/src/ui/UI.ts +268 -57
- package/src/ui/index.ts +23 -17
- package/src/ui/utils.ts +16 -0
- package/src/utils/classes.ts +49 -0
- package/src/utils/composition/events.ts +3 -2
- package/src/utils/composition/forms.ts +14 -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 +4 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +19 -6
- package/src/utils/vue.ts +28 -118
- package/.eslintrc.js +0 -3
- 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/dist/virtual.d.ts +0 -11
- package/noeldemartin.config.js +0 -5
- package/src/components/AGAppLayout.vue +0 -11
- package/src/components/AGAppSnackbars.vue +0 -13
- package/src/components/basic/AGErrorMessage.vue +0 -16
- package/src/components/basic/AGLink.vue +0 -9
- package/src/components/basic/AGMarkdown.vue +0 -36
- package/src/components/basic/index.ts +0 -5
- package/src/components/constants.ts +0 -8
- package/src/components/forms/AGButton.vue +0 -44
- package/src/components/forms/AGCheckbox.vue +0 -35
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -36
- package/src/components/forms/index.ts +0 -6
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -8
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -54
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +0 -16
- package/src/components/headless/forms/index.ts +0 -6
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
- 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 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
- package/src/components/headless/snackbars/index.ts +0 -25
- package/src/components/modals/AGAlertModal.vue +0 -25
- package/src/components/modals/AGConfirmModal.vue +0 -30
- package/src/components/modals/AGErrorReportModal.ts +0 -20
- package/src/components/modals/AGErrorReportModal.vue +0 -62
- package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
- package/src/components/modals/AGLoadingModal.vue +0 -19
- package/src/components/modals/AGModal.ts +0 -10
- package/src/components/modals/AGModal.vue +0 -37
- 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/index.ts +0 -23
- package/src/components/snackbars/AGSnackbar.vue +0 -42
- package/src/components/snackbars/index.ts +0 -3
- package/src/directives/initial-focus.ts +0 -11
- package/src/types/virtual.d.ts +0 -11
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
package/src/errors/Errors.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
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, { UIComponents } from '
|
|
6
|
-
import { translateWithDefault } from '
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
5
|
+
import UI, { UIComponents } from '@aerogel/core/ui/UI';
|
|
6
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
7
|
+
import { Events } from '@aerogel/core/services';
|
|
8
|
+
import type { ErrorReportModalProps } from '@aerogel/core/components/contracts/ErrorReportModal';
|
|
9
|
+
import type { ModalComponent } from '@aerogel/core/ui/UI.state';
|
|
7
10
|
|
|
8
11
|
import Service from './Errors.state';
|
|
9
|
-
import { Colors } from '@/components/constants';
|
|
10
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
11
13
|
|
|
12
14
|
export class ErrorsService extends Service {
|
|
@@ -23,7 +25,7 @@ export class ErrorsService extends Service {
|
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
|
|
26
|
-
const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
|
|
28
|
+
const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
|
|
27
29
|
|
|
28
30
|
if (reports.length === 0) {
|
|
29
31
|
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
@@ -31,11 +33,19 @@ export class ErrorsService extends Service {
|
|
|
31
33
|
return;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
36
|
+
UI.openModal<ModalComponent<ErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
37
|
+
reports,
|
|
38
|
+
});
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
38
|
-
|
|
42
|
+
await Events.emit('error', { error, message });
|
|
43
|
+
|
|
44
|
+
if (isTesting('unit')) {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isDevelopment()) {
|
|
39
49
|
this.logError(error);
|
|
40
50
|
}
|
|
41
51
|
|
|
@@ -43,7 +53,7 @@ export class ErrorsService extends Service {
|
|
|
43
53
|
throw error;
|
|
44
54
|
}
|
|
45
55
|
|
|
46
|
-
if (!App.isMounted) {
|
|
56
|
+
if (!App.isMounted()) {
|
|
47
57
|
const startupError = await this.createStartupErrorReport(error);
|
|
48
58
|
|
|
49
59
|
if (startupError) {
|
|
@@ -60,19 +70,20 @@ export class ErrorsService extends Service {
|
|
|
60
70
|
date: new Date(),
|
|
61
71
|
};
|
|
62
72
|
|
|
63
|
-
UI.
|
|
73
|
+
UI.toast(
|
|
64
74
|
message ??
|
|
65
75
|
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
66
76
|
{
|
|
67
|
-
|
|
77
|
+
variant: 'danger',
|
|
68
78
|
actions: [
|
|
69
79
|
{
|
|
70
|
-
|
|
80
|
+
label: translateWithDefault('errors.viewDetails', 'View details'),
|
|
71
81
|
dismiss: true,
|
|
72
|
-
|
|
73
|
-
UI.openModal(
|
|
74
|
-
|
|
75
|
-
|
|
82
|
+
click: () =>
|
|
83
|
+
UI.openModal<ModalComponent<ErrorReportModalProps>>(
|
|
84
|
+
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
85
|
+
{ reports: [report] },
|
|
86
|
+
),
|
|
76
87
|
},
|
|
77
88
|
],
|
|
78
89
|
},
|
|
@@ -105,22 +116,6 @@ export class ErrorsService extends Service {
|
|
|
105
116
|
});
|
|
106
117
|
}
|
|
107
118
|
|
|
108
|
-
public getErrorMessage(error: ErrorSource): string {
|
|
109
|
-
if (typeof error === 'string') {
|
|
110
|
-
return error;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (error instanceof Error || error instanceof JSError) {
|
|
114
|
-
return error.message;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (isObject(error)) {
|
|
118
|
-
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
119
|
private logError(error: unknown): void {
|
|
125
120
|
// eslint-disable-next-line no-console
|
|
126
121
|
console.error(error);
|
|
@@ -180,4 +175,10 @@ export class ErrorsService extends Service {
|
|
|
180
175
|
|
|
181
176
|
}
|
|
182
177
|
|
|
183
|
-
export default facade(
|
|
178
|
+
export default facade(ErrorsService);
|
|
179
|
+
|
|
180
|
+
declare module '@aerogel/core/services/Events' {
|
|
181
|
+
export interface EventsPayload {
|
|
182
|
+
error: { error: ErrorSource; message?: string };
|
|
183
|
+
}
|
|
184
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,38 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { App } from 'vue';
|
|
2
2
|
|
|
3
|
-
import { bootServices } from '
|
|
4
|
-
import { definePlugin } from '
|
|
3
|
+
import { bootServices } from '@aerogel/core/services';
|
|
4
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
5
5
|
|
|
6
6
|
import Errors from './Errors';
|
|
7
|
-
import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
7
|
+
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export * from './utils';
|
|
10
|
+
export { Errors };
|
|
11
|
+
export { default as JobCancelledError } from './JobCancelledError';
|
|
12
|
+
export { default as ServiceBootError } from './ServiceBootError';
|
|
13
|
+
export type { ErrorSource, ErrorReport, ErrorReportLog };
|
|
10
14
|
|
|
11
15
|
const services = { $errors: Errors };
|
|
12
16
|
const frameworkHandler: ErrorHandler = (error) => {
|
|
13
|
-
if (!Errors.instance) {
|
|
14
|
-
// eslint-disable-next-line no-console
|
|
15
|
-
console.warn('Errors service hasn\'t been initialized properly!');
|
|
16
|
-
|
|
17
|
-
// eslint-disable-next-line no-console
|
|
18
|
-
console.error(error);
|
|
19
|
-
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
17
|
Errors.report(error);
|
|
24
18
|
|
|
25
19
|
return true;
|
|
26
20
|
};
|
|
27
21
|
|
|
28
|
-
function setUpErrorHandler(baseHandler: ErrorHandler = () => false):
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
);
|
|
22
|
+
function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
|
|
23
|
+
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
24
|
+
|
|
25
|
+
app.config.errorHandler = errorHandler;
|
|
26
|
+
globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
|
|
27
|
+
globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
|
|
36
28
|
}
|
|
37
29
|
|
|
38
30
|
export type ErrorHandler = (error: ErrorSource) => boolean;
|
|
@@ -40,20 +32,18 @@ export type ErrorsServices = typeof services;
|
|
|
40
32
|
|
|
41
33
|
export default definePlugin({
|
|
42
34
|
async install(app, options) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
app.config.errorHandler = errorHandler;
|
|
35
|
+
setUpErrorHandler(app, options.handleError);
|
|
46
36
|
|
|
47
37
|
await bootServices(app, services);
|
|
48
38
|
},
|
|
49
39
|
});
|
|
50
40
|
|
|
51
|
-
declare module '
|
|
52
|
-
interface AerogelOptions {
|
|
41
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
42
|
+
export interface AerogelOptions {
|
|
53
43
|
handleError?(error: ErrorSource): boolean;
|
|
54
44
|
}
|
|
55
45
|
}
|
|
56
46
|
|
|
57
|
-
declare module '
|
|
47
|
+
declare module '@aerogel/core/services' {
|
|
58
48
|
export interface Services extends ErrorsServices {}
|
|
59
49
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { translateWithDefault } from '@aerogel/core/lang/utils';
|
|
3
|
+
import type { ErrorSource } from './Errors.state';
|
|
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
|
+
|
|
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
|
+
|
|
22
|
+
if (typeof error === 'string') {
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
27
|
+
return error.message;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isObject(error)) {
|
|
31
|
+
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
35
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { describe, expect, expectTypeOf, it } from 'vitest';
|
|
2
2
|
|
|
3
|
-
import { useForm } from '
|
|
4
|
-
import {
|
|
5
|
-
import { numberInput, requiredStringInput } from '@/forms/utils';
|
|
3
|
+
import { useForm } from '@aerogel/core/forms/composition';
|
|
4
|
+
import { numberInput, requiredStringInput } from '@aerogel/core/forms/utils';
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
import { FormFieldTypes } from './FormController';
|
|
7
|
+
|
|
8
|
+
describe('FormController', () => {
|
|
8
9
|
|
|
9
10
|
it('defines magic fields', () => {
|
|
10
11
|
const form = useForm({
|
|
@@ -55,4 +56,32 @@ describe('Form', () => {
|
|
|
55
56
|
expect(form.name).toBeNull();
|
|
56
57
|
});
|
|
57
58
|
|
|
59
|
+
it('trims values', () => {
|
|
60
|
+
// Arrange
|
|
61
|
+
const form = useForm({
|
|
62
|
+
trimmed: {
|
|
63
|
+
type: FormFieldTypes.String,
|
|
64
|
+
rules: 'required',
|
|
65
|
+
},
|
|
66
|
+
untrimmed: {
|
|
67
|
+
type: FormFieldTypes.String,
|
|
68
|
+
rules: 'required',
|
|
69
|
+
trim: false,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Act
|
|
74
|
+
form.trimmed = ' ';
|
|
75
|
+
form.untrimmed = ' ';
|
|
76
|
+
|
|
77
|
+
form.submit();
|
|
78
|
+
|
|
79
|
+
// Assert
|
|
80
|
+
expect(form.valid).toBe(false);
|
|
81
|
+
expect(form.submitted).toBe(true);
|
|
82
|
+
expect(form.trimmed).toEqual('');
|
|
83
|
+
expect(form.untrimmed).toEqual(' ');
|
|
84
|
+
expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
|
|
85
|
+
});
|
|
86
|
+
|
|
58
87
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import { validate } from './validation';
|
|
3
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
5
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
6
|
|
|
@@ -7,16 +8,20 @@ export const FormFieldTypes = {
|
|
|
7
8
|
String: 'string',
|
|
8
9
|
Number: 'number',
|
|
9
10
|
Boolean: 'boolean',
|
|
11
|
+
Object: 'object',
|
|
12
|
+
Date: 'date',
|
|
10
13
|
} as const;
|
|
11
14
|
|
|
12
15
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
13
16
|
type: TType;
|
|
17
|
+
trim?: boolean;
|
|
14
18
|
default?: GetFormFieldValue<TType>;
|
|
15
19
|
rules?: TRules;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
19
23
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
24
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
20
25
|
|
|
21
26
|
export type FormData<T> = {
|
|
22
27
|
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
@@ -33,20 +38,29 @@ export type FormErrors<T> = {
|
|
|
33
38
|
export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.String
|
|
34
39
|
? string
|
|
35
40
|
: TType extends typeof FormFieldTypes.Number
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
? number
|
|
42
|
+
: TType extends typeof FormFieldTypes.Boolean
|
|
43
|
+
? boolean
|
|
44
|
+
: TType extends typeof FormFieldTypes.Object
|
|
45
|
+
? object
|
|
46
|
+
: TType extends typeof FormFieldTypes.Date
|
|
47
|
+
? Date
|
|
48
|
+
: never;
|
|
40
49
|
|
|
41
|
-
|
|
50
|
+
const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
|
|
51
|
+
|
|
52
|
+
export type SubmitFormListener = () => unknown;
|
|
53
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
54
|
+
|
|
55
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
42
56
|
|
|
43
57
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
44
58
|
|
|
45
59
|
private _fields: Fields;
|
|
46
60
|
private _data: FormData<Fields>;
|
|
47
|
-
private _valid: ComputedRef<boolean>;
|
|
48
61
|
private _submitted: Ref<boolean>;
|
|
49
62
|
private _errors: FormErrors<Fields>;
|
|
63
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
50
64
|
|
|
51
65
|
constructor(fields: Fields) {
|
|
52
66
|
super();
|
|
@@ -55,13 +69,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
55
69
|
this._submitted = ref(false);
|
|
56
70
|
this._data = this.getInitialData(fields);
|
|
57
71
|
this._errors = this.getInitialErrors(fields);
|
|
58
|
-
|
|
72
|
+
|
|
73
|
+
validForms.set(
|
|
74
|
+
this,
|
|
75
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
76
|
+
);
|
|
59
77
|
|
|
60
78
|
this.errors = readonly(this._errors);
|
|
61
79
|
}
|
|
62
80
|
|
|
63
81
|
public get valid(): boolean {
|
|
64
|
-
return this
|
|
82
|
+
return !!validForms.get(this)?.value;
|
|
65
83
|
}
|
|
66
84
|
|
|
67
85
|
public get submitted(): boolean {
|
|
@@ -69,7 +87,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
69
87
|
}
|
|
70
88
|
|
|
71
89
|
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
72
|
-
|
|
90
|
+
const definition =
|
|
91
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
92
|
+
|
|
93
|
+
this._data[field] =
|
|
94
|
+
definition.type === FormFieldTypes.String && (definition.trim ?? true)
|
|
95
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
96
|
+
: value;
|
|
73
97
|
|
|
74
98
|
if (this._submitted.value) {
|
|
75
99
|
this.validate();
|
|
@@ -80,12 +104,23 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
80
104
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
81
105
|
}
|
|
82
106
|
|
|
107
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
108
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public data(): FormData<Fields> {
|
|
112
|
+
return { ...this._data };
|
|
113
|
+
}
|
|
114
|
+
|
|
83
115
|
public validate(): boolean {
|
|
84
|
-
const errors = Object.entries(this._fields).reduce(
|
|
85
|
-
formErrors[name
|
|
116
|
+
const errors = Object.entries(this._fields).reduce(
|
|
117
|
+
(formErrors, [name, definition]) => {
|
|
118
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
86
119
|
|
|
87
|
-
|
|
88
|
-
|
|
120
|
+
return formErrors;
|
|
121
|
+
},
|
|
122
|
+
{} as Record<string, string[] | null>,
|
|
123
|
+
);
|
|
89
124
|
|
|
90
125
|
this.resetErrors(errors);
|
|
91
126
|
|
|
@@ -102,32 +137,66 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
102
137
|
public submit(): boolean {
|
|
103
138
|
this._submitted.value = true;
|
|
104
139
|
|
|
105
|
-
|
|
140
|
+
const valid = this.validate();
|
|
141
|
+
|
|
142
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
143
|
+
|
|
144
|
+
return valid;
|
|
106
145
|
}
|
|
107
146
|
|
|
108
|
-
|
|
147
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
148
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
149
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
150
|
+
this._listeners[event] ??= [];
|
|
151
|
+
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
this._listeners[event]?.push(listener as any);
|
|
154
|
+
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
156
|
+
return () => this.off(event as any, listener);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
160
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
161
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
162
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public async focus(input: string): Promise<void> {
|
|
166
|
+
await nextTick();
|
|
167
|
+
|
|
168
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
protected override __get(property: string): unknown {
|
|
109
172
|
if (!(property in this._fields)) {
|
|
110
173
|
return super.__get(property);
|
|
111
174
|
}
|
|
112
175
|
|
|
113
|
-
return this.
|
|
176
|
+
return this.getFieldValue(property);
|
|
114
177
|
}
|
|
115
178
|
|
|
116
|
-
protected __set(property: string, value: unknown): void {
|
|
179
|
+
protected override __set(property: string, value: unknown): void {
|
|
117
180
|
if (!(property in this._fields)) {
|
|
118
181
|
super.__set(property, value);
|
|
119
182
|
|
|
120
183
|
return;
|
|
121
184
|
}
|
|
122
185
|
|
|
123
|
-
|
|
186
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
124
187
|
}
|
|
125
188
|
|
|
126
189
|
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
127
190
|
const errors = [];
|
|
191
|
+
const value = this._data[name];
|
|
192
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
193
|
+
|
|
194
|
+
for (const rule of rules) {
|
|
195
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
128
198
|
|
|
129
|
-
|
|
130
|
-
errors.push('required');
|
|
199
|
+
errors.push(...validate(value, rule));
|
|
131
200
|
}
|
|
132
201
|
|
|
133
202
|
return errors.length > 0 ? errors : null;
|
|
@@ -138,10 +207,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
138
207
|
return {} as FormData<Fields>;
|
|
139
208
|
}
|
|
140
209
|
|
|
141
|
-
const data = Object.entries(fields).reduce((
|
|
142
|
-
|
|
210
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
211
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
143
212
|
|
|
144
|
-
return
|
|
213
|
+
return initialData;
|
|
145
214
|
}, {} as FormData<Fields>);
|
|
146
215
|
|
|
147
216
|
return reactive(data) as FormData<Fields>;
|
package/src/forms/composition.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { FormData, FormFieldDefinitions } from '
|
|
1
|
+
import FormController from '@aerogel/core/forms/FormController';
|
|
2
|
+
import type { FormData, FormFieldDefinitions } from '@aerogel/core/forms/FormController';
|
|
3
3
|
|
|
4
|
-
export function useForm<const T extends FormFieldDefinitions>(fields: T):
|
|
5
|
-
return new
|
|
4
|
+
export function useForm<const T extends FormFieldDefinitions>(fields: T): FormController<T> & FormData<T> {
|
|
5
|
+
return new FormController(fields) as FormController<T> & FormData<T>;
|
|
6
6
|
}
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
-
import { FormFieldTypes } from './
|
|
2
|
-
import type { FormFieldDefinition } from './
|
|
1
|
+
import { FormFieldTypes } from './FormController';
|
|
2
|
+
import type { FormFieldDefinition } from './FormController';
|
|
3
3
|
|
|
4
|
-
export function booleanInput(
|
|
4
|
+
export function booleanInput(
|
|
5
|
+
defaultValue?: boolean,
|
|
6
|
+
options: { rules?: string } = {},
|
|
7
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
8
|
return {
|
|
6
9
|
default: defaultValue,
|
|
7
10
|
type: FormFieldTypes.Boolean,
|
|
11
|
+
rules: options.rules,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function dateInput(
|
|
16
|
+
defaultValue?: Date,
|
|
17
|
+
options: { rules?: string } = {},
|
|
18
|
+
): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
19
|
+
return {
|
|
20
|
+
default: defaultValue,
|
|
21
|
+
type: FormFieldTypes.Date,
|
|
22
|
+
rules: options.rules,
|
|
8
23
|
};
|
|
9
24
|
}
|
|
10
25
|
|
|
@@ -18,6 +33,14 @@ export function requiredBooleanInput(
|
|
|
18
33
|
};
|
|
19
34
|
}
|
|
20
35
|
|
|
36
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
37
|
+
return {
|
|
38
|
+
default: defaultValue,
|
|
39
|
+
type: FormFieldTypes.Date,
|
|
40
|
+
rules: 'required',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
21
44
|
export function requiredNumberInput(
|
|
22
45
|
defaultValue?: number,
|
|
23
46
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
|
@@ -38,16 +61,24 @@ export function requiredStringInput(
|
|
|
38
61
|
};
|
|
39
62
|
}
|
|
40
63
|
|
|
41
|
-
export function numberInput(
|
|
64
|
+
export function numberInput(
|
|
65
|
+
defaultValue?: number,
|
|
66
|
+
options: { rules?: string } = {},
|
|
67
|
+
): FormFieldDefinition<typeof FormFieldTypes.Number> {
|
|
42
68
|
return {
|
|
43
69
|
default: defaultValue,
|
|
44
70
|
type: FormFieldTypes.Number,
|
|
71
|
+
rules: options.rules,
|
|
45
72
|
};
|
|
46
73
|
}
|
|
47
74
|
|
|
48
|
-
export function stringInput(
|
|
75
|
+
export function stringInput(
|
|
76
|
+
defaultValue?: string,
|
|
77
|
+
options: { rules?: string } = {},
|
|
78
|
+
): FormFieldDefinition<typeof FormFieldTypes.String> {
|
|
49
79
|
return {
|
|
50
80
|
default: defaultValue,
|
|
51
81
|
type: FormFieldTypes.String,
|
|
82
|
+
rules: options.rules,
|
|
52
83
|
};
|
|
53
84
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
const builtInRules: Record<string, FormFieldValidator> = {
|
|
4
|
+
required: (value) => (value ? undefined : 'required'),
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
|
|
8
|
+
|
|
9
|
+
export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
|
|
10
|
+
|
|
11
|
+
export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
|
|
12
|
+
validators[rule] = validator as FormFieldValidator;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function validate(value: unknown, rule: string): string[] {
|
|
16
|
+
const errors = validators[rule]?.(value);
|
|
17
|
+
|
|
18
|
+
return errors ? arrayFrom(errors) : [];
|
|
19
|
+
}
|
package/src/index.css
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
@import 'tailwindcss';
|
|
2
|
+
|
|
3
|
+
@source './';
|
|
4
|
+
@source '../../plugin-local-first/src';
|
|
5
|
+
@source '../../plugin-solid/src';
|
|
6
|
+
|
|
7
|
+
@plugin '@tailwindcss/forms';
|
|
8
|
+
@plugin '@tailwindcss/typography';
|
|
9
|
+
|
|
10
|
+
@theme {
|
|
11
|
+
--color-background: oklch(1 0 0);
|
|
12
|
+
--color-primary: oklch(0.205 0 0);
|
|
13
|
+
--color-danger: oklch(0.577 0.245 27.325);
|
|
14
|
+
--color-accent: oklch(0.97 0 0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.clickable {
|
|
18
|
+
position: relative;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.clickable::after {
|
|
22
|
+
--clickable-size: 44px;
|
|
23
|
+
--clickable-inset-by: min(0px, calc((100% - var(--clickable-size)) / 2));
|
|
24
|
+
|
|
25
|
+
content: '';
|
|
26
|
+
position: absolute;
|
|
27
|
+
top: var(--clickable-inset-by);
|
|
28
|
+
left: var(--clickable-inset-by);
|
|
29
|
+
right: var(--clickable-inset-by);
|
|
30
|
+
bottom: var(--clickable-inset-by);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
input[type='number'].appearance-textfield {
|
|
34
|
+
appearance: textfield;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
input[type='number'].appearance-textfield::-webkit-outer-spin-button,
|
|
38
|
+
input[type='number'].appearance-textfield::-webkit-inner-spin-button {
|
|
39
|
+
appearance: none;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
button[data-markdown-action] {
|
|
43
|
+
color: var(--tw-prose-links);
|
|
44
|
+
text-decoration: underline;
|
|
45
|
+
font-weight: 500;
|
|
46
|
+
}
|
package/src/{main.ts → index.ts}
RENAMED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export * from './bootstrap';
|
|
2
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
3
4
|
export * from './errors';
|
|
4
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
5
7
|
export * from './lang';
|
|
6
8
|
export * from './plugins';
|
|
7
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
8
11
|
export * from './ui';
|
|
9
12
|
export * from './utils';
|