@aerogel/core 0.0.0-next.f1f5a990033d966dc0bb12d251110fbc9350dcc7 → 0.0.0-next.f8c757d83e1e0d001a2836fa45aba318ec17b9b9
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 +2144 -1268
- package/dist/aerogel-core.js +3234 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +30 -37
- package/src/bootstrap/bootstrap.test.ts +4 -8
- package/src/bootstrap/index.ts +27 -14
- package/src/bootstrap/options.ts +4 -1
- package/src/components/{AGAppLayout.vue → AppLayout.vue} +4 -4
- package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
- package/src/components/{AGAppOverlays.vue → AppOverlays.vue} +5 -10
- 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 +18 -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 +30 -0
- package/src/components/contracts/Select.ts +44 -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 +51 -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/{forms/AGHeadlessInputLabel.vue → HeadlessInputLabel.vue} +3 -7
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/{modals/AGHeadlessModal.vue → HeadlessModal.vue} +18 -18
- package/src/components/headless/HeadlessModalContent.vue +24 -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 +113 -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 +37 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +19 -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 +27 -0
- package/src/components/ui/DropdownMenuOption.vue +14 -0
- package/src/components/ui/DropdownMenuOptions.vue +27 -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} +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 +32 -0
- package/src/components/ui/Markdown.vue +69 -0
- package/src/components/ui/Modal.vue +75 -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 +25 -0
- package/src/components/ui/SelectLabel.vue +17 -0
- package/src/components/ui/SelectOption.vue +29 -0
- package/src/components/ui/SelectOptions.vue +30 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/{lib/AGStartupCrash.vue → ui/StartupCrash.vue} +8 -8
- package/src/components/ui/Toast.vue +42 -0
- package/src/components/ui/index.ts +30 -0
- package/src/directives/index.ts +11 -5
- package/src/directives/measure.ts +34 -6
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +29 -33
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +10 -16
- 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} +86 -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 +1 -1
- package/src/plugins/index.ts +10 -7
- package/src/services/App.state.ts +37 -5
- package/src/services/App.ts +35 -6
- 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 +150 -49
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +17 -5
- 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 +218 -70
- package/src/ui/index.ts +19 -16
- package/src/ui/utils.ts +16 -0
- package/src/utils/classes.ts +49 -0
- package/src/utils/composition/events.ts +2 -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 -1
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +19 -6
- package/src/utils/vue.ts +28 -127
- 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/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 -35
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -36
- package/src/components/forms/AGSelect.story.vue +0 -28
- package/src/components/forms/AGSelect.vue +0 -53
- 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 -28
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -57
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -39
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -76
- 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 -14
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -34
- 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 -36
- 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/index.ts +0 -26
- 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/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/directives/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { Directive } from 'vue';
|
|
2
2
|
|
|
3
|
-
import { definePlugin } from '
|
|
3
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
4
4
|
|
|
5
|
-
import initialFocus from './initial-focus';
|
|
6
5
|
import measure from './measure';
|
|
7
6
|
|
|
8
7
|
const builtInDirectives: Record<string, Directive> = {
|
|
9
|
-
|
|
10
|
-
'measure': measure,
|
|
8
|
+
measure: measure,
|
|
11
9
|
};
|
|
12
10
|
|
|
11
|
+
export * from './measure';
|
|
12
|
+
|
|
13
13
|
export default definePlugin({
|
|
14
14
|
install(app, options) {
|
|
15
15
|
const directives = {
|
|
@@ -23,8 +23,14 @@ export default definePlugin({
|
|
|
23
23
|
},
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
declare module '
|
|
26
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
27
27
|
export interface AerogelOptions {
|
|
28
28
|
directives?: Record<string, Directive>;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
declare module 'vue' {
|
|
33
|
+
interface ComponentCustomDirectives {
|
|
34
|
+
measure: Directive<string, string>;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,12 +1,40 @@
|
|
|
1
|
-
import { defineDirective } from '
|
|
1
|
+
import { defineDirective } from '@aerogel/core/utils/vue';
|
|
2
|
+
import { tap } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
const resizeObservers: WeakMap<HTMLElement, ResizeObserver> = new WeakMap();
|
|
5
|
+
|
|
6
|
+
export interface ElementSize {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type MeasureDirectiveListener = (size: ElementSize) => unknown;
|
|
2
12
|
|
|
3
13
|
export default defineDirective({
|
|
4
|
-
mounted(element: HTMLElement, { value }
|
|
5
|
-
|
|
14
|
+
mounted(element: HTMLElement, { value }) {
|
|
15
|
+
// TODO replace with argument when typed properly
|
|
16
|
+
const modifiers = { css: true, watch: true };
|
|
17
|
+
|
|
18
|
+
const listener = typeof value === 'function' ? (value as MeasureDirectiveListener) : null;
|
|
19
|
+
const update = () => {
|
|
20
|
+
const sizes = element.getBoundingClientRect();
|
|
6
21
|
|
|
7
|
-
|
|
8
|
-
|
|
22
|
+
if (modifiers.css) {
|
|
23
|
+
element.style.setProperty('--width', `${sizes.width}px`);
|
|
24
|
+
element.style.setProperty('--height', `${sizes.height}px`);
|
|
25
|
+
}
|
|
9
26
|
|
|
10
|
-
|
|
27
|
+
listener?.({ width: sizes.width, height: sizes.height });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
if (modifiers.watch) {
|
|
31
|
+
resizeObservers.set(element, tap(new ResizeObserver(update)).observe(element));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
update();
|
|
35
|
+
},
|
|
36
|
+
unmounted(element) {
|
|
37
|
+
resizeObservers.get(element)?.unobserve(element);
|
|
38
|
+
resizeObservers.delete(element);
|
|
11
39
|
},
|
|
12
40
|
});
|
package/src/errors/Errors.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
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
|
-
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
11
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
12
|
-
import type { ModalComponent } from '@/ui/UI.state';
|
|
13
13
|
|
|
14
14
|
export class ErrorsService extends Service {
|
|
15
15
|
|
|
@@ -33,13 +33,19 @@ export class ErrorsService extends Service {
|
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
UI.openModal<ModalComponent<
|
|
36
|
+
UI.openModal<ModalComponent<ErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
37
37
|
reports,
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
42
|
-
|
|
42
|
+
await Events.emit('error', { error, message });
|
|
43
|
+
|
|
44
|
+
if (isTesting('unit')) {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isDevelopment()) {
|
|
43
49
|
this.logError(error);
|
|
44
50
|
}
|
|
45
51
|
|
|
@@ -47,7 +53,7 @@ export class ErrorsService extends Service {
|
|
|
47
53
|
throw error;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
|
-
if (!App.isMounted) {
|
|
56
|
+
if (!App.isMounted()) {
|
|
51
57
|
const startupError = await this.createStartupErrorReport(error);
|
|
52
58
|
|
|
53
59
|
if (startupError) {
|
|
@@ -64,17 +70,17 @@ export class ErrorsService extends Service {
|
|
|
64
70
|
date: new Date(),
|
|
65
71
|
};
|
|
66
72
|
|
|
67
|
-
UI.
|
|
73
|
+
UI.toast(
|
|
68
74
|
message ??
|
|
69
75
|
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
70
76
|
{
|
|
71
|
-
|
|
77
|
+
variant: 'danger',
|
|
72
78
|
actions: [
|
|
73
79
|
{
|
|
74
|
-
|
|
80
|
+
label: translateWithDefault('errors.viewDetails', 'View details'),
|
|
75
81
|
dismiss: true,
|
|
76
|
-
|
|
77
|
-
UI.openModal<ModalComponent<
|
|
82
|
+
click: () =>
|
|
83
|
+
UI.openModal<ModalComponent<ErrorReportModalProps>>(
|
|
78
84
|
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
79
85
|
{ reports: [report] },
|
|
80
86
|
),
|
|
@@ -110,22 +116,6 @@ export class ErrorsService extends Service {
|
|
|
110
116
|
});
|
|
111
117
|
}
|
|
112
118
|
|
|
113
|
-
public getErrorMessage(error: ErrorSource): string {
|
|
114
|
-
if (typeof error === 'string') {
|
|
115
|
-
return error;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (error instanceof Error || error instanceof JSError) {
|
|
119
|
-
return error.message;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (isObject(error)) {
|
|
123
|
-
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
119
|
private logError(error: unknown): void {
|
|
130
120
|
// eslint-disable-next-line no-console
|
|
131
121
|
console.error(error);
|
|
@@ -185,4 +175,10 @@ export class ErrorsService extends Service {
|
|
|
185
175
|
|
|
186
176
|
}
|
|
187
177
|
|
|
188
|
-
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,25 +1,19 @@
|
|
|
1
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;
|
|
@@ -44,12 +38,12 @@ export default definePlugin({
|
|
|
44
38
|
},
|
|
45
39
|
});
|
|
46
40
|
|
|
47
|
-
declare module '
|
|
41
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
48
42
|
export interface AerogelOptions {
|
|
49
43
|
handleError?(error: ErrorSource): boolean;
|
|
50
44
|
}
|
|
51
45
|
}
|
|
52
46
|
|
|
53
|
-
declare module '
|
|
47
|
+
declare module '@aerogel/core/services' {
|
|
54
48
|
export interface Services extends ErrorsServices {}
|
|
55
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
|
|
|
@@ -8,16 +9,19 @@ export const FormFieldTypes = {
|
|
|
8
9
|
Number: 'number',
|
|
9
10
|
Boolean: 'boolean',
|
|
10
11
|
Object: 'object',
|
|
12
|
+
Date: 'date',
|
|
11
13
|
} as const;
|
|
12
14
|
|
|
13
15
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
14
16
|
type: TType;
|
|
17
|
+
trim?: boolean;
|
|
15
18
|
default?: GetFormFieldValue<TType>;
|
|
16
19
|
rules?: TRules;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
20
23
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
24
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
21
25
|
|
|
22
26
|
export type FormData<T> = {
|
|
23
27
|
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
@@ -34,16 +38,21 @@ export type FormErrors<T> = {
|
|
|
34
38
|
export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.String
|
|
35
39
|
? string
|
|
36
40
|
: TType extends typeof FormFieldTypes.Number
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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;
|
|
43
49
|
|
|
44
|
-
const validForms: WeakMap<
|
|
50
|
+
const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
|
|
45
51
|
|
|
46
|
-
export
|
|
52
|
+
export type SubmitFormListener = () => unknown;
|
|
53
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
54
|
+
|
|
55
|
+
export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
47
56
|
|
|
48
57
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
49
58
|
|
|
@@ -51,6 +60,7 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
51
60
|
private _data: FormData<Fields>;
|
|
52
61
|
private _submitted: Ref<boolean>;
|
|
53
62
|
private _errors: FormErrors<Fields>;
|
|
63
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
54
64
|
|
|
55
65
|
constructor(fields: Fields) {
|
|
56
66
|
super();
|
|
@@ -77,7 +87,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
80
|
-
|
|
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;
|
|
81
97
|
|
|
82
98
|
if (this._submitted.value) {
|
|
83
99
|
this.validate();
|
|
@@ -88,12 +104,23 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
88
104
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
89
105
|
}
|
|
90
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
|
+
|
|
91
115
|
public validate(): boolean {
|
|
92
|
-
const errors = Object.entries(this._fields).reduce(
|
|
93
|
-
formErrors[name
|
|
116
|
+
const errors = Object.entries(this._fields).reduce(
|
|
117
|
+
(formErrors, [name, definition]) => {
|
|
118
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
94
119
|
|
|
95
|
-
|
|
96
|
-
|
|
120
|
+
return formErrors;
|
|
121
|
+
},
|
|
122
|
+
{} as Record<string, string[] | null>,
|
|
123
|
+
);
|
|
97
124
|
|
|
98
125
|
this.resetErrors(errors);
|
|
99
126
|
|
|
@@ -110,32 +137,66 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
110
137
|
public submit(): boolean {
|
|
111
138
|
this._submitted.value = true;
|
|
112
139
|
|
|
113
|
-
|
|
140
|
+
const valid = this.validate();
|
|
141
|
+
|
|
142
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
143
|
+
|
|
144
|
+
return valid;
|
|
114
145
|
}
|
|
115
146
|
|
|
116
|
-
|
|
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 {
|
|
117
172
|
if (!(property in this._fields)) {
|
|
118
173
|
return super.__get(property);
|
|
119
174
|
}
|
|
120
175
|
|
|
121
|
-
return this.
|
|
176
|
+
return this.getFieldValue(property);
|
|
122
177
|
}
|
|
123
178
|
|
|
124
|
-
protected __set(property: string, value: unknown): void {
|
|
179
|
+
protected override __set(property: string, value: unknown): void {
|
|
125
180
|
if (!(property in this._fields)) {
|
|
126
181
|
super.__set(property, value);
|
|
127
182
|
|
|
128
183
|
return;
|
|
129
184
|
}
|
|
130
185
|
|
|
131
|
-
|
|
186
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
132
187
|
}
|
|
133
188
|
|
|
134
189
|
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
135
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
|
+
}
|
|
136
198
|
|
|
137
|
-
|
|
138
|
-
errors.push('required');
|
|
199
|
+
errors.push(...validate(value, rule));
|
|
139
200
|
}
|
|
140
201
|
|
|
141
202
|
return errors.length > 0 ? errors : null;
|
|
@@ -146,10 +207,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
146
207
|
return {} as FormData<Fields>;
|
|
147
208
|
}
|
|
148
209
|
|
|
149
|
-
const data = Object.entries(fields).reduce((
|
|
150
|
-
|
|
210
|
+
const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
|
|
211
|
+
initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
151
212
|
|
|
152
|
-
return
|
|
213
|
+
return initialData;
|
|
153
214
|
}, {} as FormData<Fields>);
|
|
154
215
|
|
|
155
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
|
+
}
|