@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e
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.cjs.js +1 -1
- package/dist/aerogel-core.d.ts +426 -82
- package/dist/aerogel-core.esm.js +1 -1
- package/package.json +6 -8
- package/src/bootstrap/bootstrap.test.ts +0 -56
- package/src/bootstrap/index.ts +9 -25
- package/src/bootstrap/options.ts +5 -1
- package/src/components/basic/AGMarkdown.vue +20 -5
- package/src/components/forms/AGButton.vue +26 -3
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/index.ts +2 -1
- package/src/components/headless/forms/AGHeadlessButton.vue +3 -4
- package/src/components/headless/forms/AGHeadlessInput.ts +2 -2
- package/src/components/headless/forms/AGHeadlessInput.vue +5 -5
- package/src/components/headless/forms/AGHeadlessInputError.vue +9 -5
- package/src/components/headless/forms/AGHeadlessInputInput.vue +20 -4
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +16 -0
- package/src/components/headless/forms/index.ts +6 -4
- package/src/components/headless/modals/AGHeadlessModal.vue +5 -1
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
- package/src/components/index.ts +2 -1
- package/src/components/modals/AGAlertModal.vue +13 -2
- package/src/components/modals/AGConfirmModal.vue +30 -0
- package/src/components/modals/AGLoadingModal.vue +19 -0
- package/src/components/modals/AGModal.ts +4 -0
- package/src/components/modals/AGModal.vue +20 -2
- package/src/components/modals/index.ts +4 -1
- package/src/directives/index.ts +5 -3
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +132 -0
- package/src/errors/index.ts +21 -0
- package/src/forms/Form.test.ts +21 -0
- package/src/forms/Form.ts +38 -16
- package/src/forms/utils.ts +17 -0
- package/src/globals.ts +6 -0
- package/src/lang/Lang.ts +47 -8
- package/src/lang/index.ts +17 -76
- package/src/lang/utils.ts +4 -0
- package/src/main.ts +4 -0
- package/src/plugins/Plugin.ts +7 -0
- package/src/plugins/index.ts +7 -0
- package/src/services/App.state.ts +13 -0
- package/src/services/App.ts +17 -0
- package/src/services/Service.ts +151 -28
- package/src/services/index.ts +29 -7
- package/src/services/store.ts +27 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +3 -6
- package/src/ui/UI.ts +35 -2
- package/src/ui/index.ts +20 -14
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/index.ts +3 -0
- package/tsconfig.json +1 -10
- package/vite.config.ts +2 -6
- package/src/bootstrap/hooks.ts +0 -19
- package/src/lang/helpers.ts +0 -5
- package/src/models/index.ts +0 -18
- package/src/routing/index.ts +0 -33
- package/src/testing/stubs/lang/en.yaml +0 -1
- package/src/testing/stubs/models/User.ts +0 -3
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<DialogPanel>
|
|
3
3
|
<slot />
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
<template v-if="childModal">
|
|
6
|
+
<div class="pointer-events-none fixed inset-0 z-50 bg-black/30" />
|
|
7
|
+
<AGModalContext :child-index="modal.childIndex + 1" :modal="childModal" />
|
|
8
|
+
</template>
|
|
5
9
|
</DialogPanel>
|
|
6
10
|
</template>
|
|
7
11
|
|
|
@@ -15,6 +19,10 @@ import type { IAGModalContext } from '@/components/modals/AGModalContext';
|
|
|
15
19
|
|
|
16
20
|
import AGModalContext from '../../modals/AGModalContext.vue';
|
|
17
21
|
|
|
18
|
-
const modal = injectReactiveOrFail<IAGModalContext>(
|
|
22
|
+
const modal = injectReactiveOrFail<IAGModalContext>(
|
|
23
|
+
'modal',
|
|
24
|
+
'could not obtain modal reference from <AGHeadlessModalPanel>, ' +
|
|
25
|
+
'did you render this component manually? Show it using $ui.openModal() instead',
|
|
26
|
+
);
|
|
19
27
|
const childModal = computed(() => UI.modals[modal.childIndex] ?? null);
|
|
20
28
|
</script>
|
package/src/components/index.ts
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<AGModal>
|
|
3
|
+
<AGMarkdown
|
|
4
|
+
v-if="title"
|
|
5
|
+
:text="title"
|
|
6
|
+
as="h2"
|
|
7
|
+
class="font-semibold"
|
|
8
|
+
raw
|
|
9
|
+
inline
|
|
10
|
+
/>
|
|
3
11
|
<AGMarkdown :text="message" />
|
|
4
12
|
</AGModal>
|
|
5
13
|
</template>
|
|
6
14
|
|
|
7
15
|
<script setup lang="ts">
|
|
8
|
-
import { requiredStringProp } from '@/utils/vue';
|
|
16
|
+
import { requiredStringProp, stringProp } from '@/utils/vue';
|
|
9
17
|
|
|
10
18
|
import AGModal from './AGModal.vue';
|
|
11
19
|
|
|
12
20
|
import AGMarkdown from '../basic/AGMarkdown.vue';
|
|
13
21
|
|
|
14
|
-
defineProps({
|
|
22
|
+
defineProps({
|
|
23
|
+
title: stringProp(),
|
|
24
|
+
message: requiredStringProp(),
|
|
25
|
+
});
|
|
15
26
|
</script>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<AGModal v-slot="{ close }: IAGModalDefaultSlotProps" :cancellable="false">
|
|
3
|
+
<AGMarkdown v-if="title" :text="title" as="h1" />
|
|
4
|
+
<AGMarkdown :text="message" />
|
|
5
|
+
|
|
6
|
+
<div class="mt-2 flex flex-row-reverse gap-2">
|
|
7
|
+
<AGButton @click="close(true)">
|
|
8
|
+
{{ $td('ui.ok', 'OK') }}
|
|
9
|
+
</AGButton>
|
|
10
|
+
<AGButton secondary @click="close()">
|
|
11
|
+
{{ $td('ui.cancel', 'Cancel') }}
|
|
12
|
+
</AGButton>
|
|
13
|
+
</div>
|
|
14
|
+
</AGModal>
|
|
15
|
+
</template>
|
|
16
|
+
|
|
17
|
+
<script setup lang="ts">
|
|
18
|
+
import { requiredStringProp, stringProp } from '@/utils/vue';
|
|
19
|
+
|
|
20
|
+
import AGModal from './AGModal.vue';
|
|
21
|
+
import type { IAGModalDefaultSlotProps } from './AGModal';
|
|
22
|
+
|
|
23
|
+
import AGButton from '../forms/AGButton.vue';
|
|
24
|
+
import AGMarkdown from '../basic/AGMarkdown.vue';
|
|
25
|
+
|
|
26
|
+
defineProps({
|
|
27
|
+
title: stringProp(),
|
|
28
|
+
message: requiredStringProp(),
|
|
29
|
+
});
|
|
30
|
+
</script>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<AGModal :cancellable="false">
|
|
3
|
+
<AGMarkdown :text="renderedMessage" />
|
|
4
|
+
</AGModal>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { computed } from 'vue';
|
|
9
|
+
|
|
10
|
+
import { stringProp } from '@/utils/vue';
|
|
11
|
+
import { translateWithDefault } from '@/lang/utils';
|
|
12
|
+
|
|
13
|
+
import AGModal from './AGModal.vue';
|
|
14
|
+
|
|
15
|
+
import AGMarkdown from '../basic/AGMarkdown.vue';
|
|
16
|
+
|
|
17
|
+
const props = defineProps({ message: stringProp() });
|
|
18
|
+
const renderedMessage = computed(() => props.message ?? translateWithDefault('ui.loading', 'Loading...'));
|
|
19
|
+
</script>
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<AGHeadlessModal
|
|
2
|
+
<AGHeadlessModal
|
|
3
|
+
ref="$headlessModal"
|
|
4
|
+
v-slot="{ close }: IAGHeadlessModalDefaultSlotProps"
|
|
5
|
+
:cancellable="cancellable"
|
|
6
|
+
class="relative z-50"
|
|
7
|
+
>
|
|
3
8
|
<div class="fixed inset-0 flex items-center justify-center">
|
|
4
9
|
<AGHeadlessModalPanel class="flex max-h-full max-w-full flex-col overflow-hidden bg-white">
|
|
5
10
|
<div class="flex max-h-full flex-col overflow-auto p-4">
|
|
@@ -11,8 +16,21 @@
|
|
|
11
16
|
</template>
|
|
12
17
|
|
|
13
18
|
<script setup lang="ts">
|
|
14
|
-
import
|
|
19
|
+
import { computed, ref } from 'vue';
|
|
20
|
+
|
|
21
|
+
import { booleanProp } from '@/utils';
|
|
22
|
+
import type { IAGHeadlessModal, IAGHeadlessModalDefaultSlotProps } from '@/components/headless/modals/AGHeadlessModal';
|
|
23
|
+
|
|
24
|
+
import type { IAGModal } from './AGModal';
|
|
15
25
|
|
|
16
26
|
import AGHeadlessModal from '../headless/modals/AGHeadlessModal.vue';
|
|
17
27
|
import AGHeadlessModalPanel from '../headless/modals/AGHeadlessModalPanel.vue';
|
|
28
|
+
|
|
29
|
+
const $headlessModal = ref<IAGHeadlessModal>();
|
|
30
|
+
|
|
31
|
+
defineProps({ cancellable: booleanProp(true) });
|
|
32
|
+
defineExpose<IAGModal>({
|
|
33
|
+
close: async () => $headlessModal.value?.close(),
|
|
34
|
+
cancellable: computed(() => !!$headlessModal.value?.cancellable),
|
|
35
|
+
});
|
|
18
36
|
</script>
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import AGAlertModal from './AGAlertModal.vue';
|
|
2
|
+
import AGConfirmModal from './AGConfirmModal.vue';
|
|
3
|
+
import AGLoadingModal from './AGLoadingModal.vue';
|
|
1
4
|
import AGModal from './AGModal.vue';
|
|
2
5
|
import AGModalContext from './AGModalContext.vue';
|
|
3
6
|
import { IAGModal } from './AGModal';
|
|
4
7
|
|
|
5
|
-
export { AGModal, AGModalContext, IAGModal };
|
|
8
|
+
export { AGAlertModal, AGConfirmModal, AGModal, AGModalContext, AGLoadingModal, IAGModal };
|
package/src/directives/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Directive } from 'vue';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { definePlugin } from '@/plugins';
|
|
4
4
|
|
|
5
5
|
import initialFocus from './initial-focus';
|
|
6
6
|
|
|
@@ -8,6 +8,8 @@ const directives: Record<string, Directive> = {
|
|
|
8
8
|
'initial-focus': initialFocus,
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
export default
|
|
12
|
-
|
|
11
|
+
export default definePlugin({
|
|
12
|
+
install(app) {
|
|
13
|
+
Object.entries(directives).forEach(([name, directive]) => app.directive(name, directive));
|
|
14
|
+
},
|
|
13
15
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { JSError } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import { defineServiceState } from '@/services';
|
|
4
|
+
|
|
5
|
+
export type ErrorSource = string | Error | JSError | unknown;
|
|
6
|
+
|
|
7
|
+
export interface ErrorReport {
|
|
8
|
+
title: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
details?: string;
|
|
11
|
+
error?: Error | JSError | unknown;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ErrorReportLog {
|
|
15
|
+
report: ErrorReport;
|
|
16
|
+
seen: boolean;
|
|
17
|
+
date: Date;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default defineServiceState({
|
|
21
|
+
name: 'errors',
|
|
22
|
+
initialState: {
|
|
23
|
+
logs: [] as ErrorReportLog[],
|
|
24
|
+
startupErrors: [] as ErrorReport[],
|
|
25
|
+
},
|
|
26
|
+
computed: {
|
|
27
|
+
hasErrors: ({ logs }) => logs.length > 0,
|
|
28
|
+
hasNewErrors: ({ logs }) => logs.some((error) => !error.seen),
|
|
29
|
+
hasStartupErrors: ({ startupErrors }) => startupErrors.length > 0,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { JSError, facade, isObject } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
import ServiceBootError from '@/errors/ServiceBootError';
|
|
5
|
+
import UI from '@/ui/UI';
|
|
6
|
+
import { translate } from '@/lang/utils';
|
|
7
|
+
|
|
8
|
+
import Service from './Errors.state';
|
|
9
|
+
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
10
|
+
|
|
11
|
+
export class ErrorsService extends Service {
|
|
12
|
+
|
|
13
|
+
public forceReporting: boolean = false;
|
|
14
|
+
private enabled: boolean = true;
|
|
15
|
+
|
|
16
|
+
public enable(): void {
|
|
17
|
+
this.enabled = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public disable(): void {
|
|
21
|
+
this.enabled = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
|
|
25
|
+
const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
|
|
26
|
+
|
|
27
|
+
// TODO open errors modal
|
|
28
|
+
reports;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
32
|
+
if (App.isDevelopment || App.isTesting) {
|
|
33
|
+
this.logError(error);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!this.enabled) {
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!App.isMounted) {
|
|
41
|
+
const startupError = await this.createStartupErrorReport(error);
|
|
42
|
+
|
|
43
|
+
if (startupError) {
|
|
44
|
+
this.setState({ startupErrors: this.startupErrors.concat(startupError) });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const report = await this.createErrorReport(error);
|
|
51
|
+
const log: ErrorReportLog = {
|
|
52
|
+
report,
|
|
53
|
+
seen: false,
|
|
54
|
+
date: new Date(),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// TODO open error snackbar
|
|
58
|
+
UI.alert(message ?? 'Something went wrong, but it\'s not your fault! (look at the console for details)');
|
|
59
|
+
|
|
60
|
+
this.setState({ logs: [log].concat(this.logs) });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public see(report: ErrorReport): void {
|
|
64
|
+
this.setState({
|
|
65
|
+
logs: this.logs.map((log) => {
|
|
66
|
+
if (log.report !== report) {
|
|
67
|
+
return log;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
...log,
|
|
72
|
+
seen: true,
|
|
73
|
+
};
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public seeAll(): void {
|
|
79
|
+
this.setState({
|
|
80
|
+
logs: this.logs.map((log) => ({
|
|
81
|
+
...log,
|
|
82
|
+
seen: true,
|
|
83
|
+
})),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private logError(error: unknown): void {
|
|
88
|
+
// eslint-disable-next-line no-console
|
|
89
|
+
console.error(error);
|
|
90
|
+
|
|
91
|
+
if (isObject(error) && error.cause) {
|
|
92
|
+
this.logError(error.cause);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
|
|
97
|
+
if (typeof error === 'string') {
|
|
98
|
+
return { title: error };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
102
|
+
return this.createErrorReportFromError(error);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
title: translate('errors.unknown'),
|
|
107
|
+
error,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async createStartupErrorReport(error: ErrorSource): Promise<ErrorReport | null> {
|
|
112
|
+
if (error instanceof ServiceBootError) {
|
|
113
|
+
// Ignore second-order boot errors in order to have a cleaner startup crash screen.
|
|
114
|
+
return error.cause instanceof ServiceBootError ? null : this.createErrorReport(error.cause);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return this.createErrorReport(error);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private createErrorReportFromError(error: Error | JSError, defaults: Partial<ErrorReport> = {}): ErrorReport {
|
|
121
|
+
return {
|
|
122
|
+
title: error.name,
|
|
123
|
+
description: error.message,
|
|
124
|
+
details: error.stack,
|
|
125
|
+
error,
|
|
126
|
+
...defaults,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export default facade(new ErrorsService());
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { bootServices } from '@/services';
|
|
2
|
+
import { definePlugin } from '@/plugins';
|
|
3
|
+
|
|
4
|
+
import Errors from './Errors';
|
|
5
|
+
import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
6
|
+
|
|
7
|
+
export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
|
|
8
|
+
|
|
9
|
+
const services = { $errors: Errors };
|
|
10
|
+
|
|
11
|
+
export type ErrorsServices = typeof services;
|
|
12
|
+
|
|
13
|
+
export default definePlugin({
|
|
14
|
+
async install(app) {
|
|
15
|
+
await bootServices(app, services);
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
declare module '@/services' {
|
|
20
|
+
export interface Services extends ErrorsServices {}
|
|
21
|
+
}
|
package/src/forms/Form.test.ts
CHANGED
|
@@ -34,4 +34,25 @@ describe('Form', () => {
|
|
|
34
34
|
expect(form.errors.name).toEqual(['required']);
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
+
it('resets form', () => {
|
|
38
|
+
// Arrange
|
|
39
|
+
const form = useForm({
|
|
40
|
+
name: {
|
|
41
|
+
type: FormFieldTypes.String,
|
|
42
|
+
rules: 'required',
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
form.name = 'Foo bar';
|
|
47
|
+
form.submit();
|
|
48
|
+
|
|
49
|
+
// Act
|
|
50
|
+
form.reset();
|
|
51
|
+
|
|
52
|
+
// Assert
|
|
53
|
+
expect(form.valid).toBe(true);
|
|
54
|
+
expect(form.submitted).toBe(false);
|
|
55
|
+
expect(form.name).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
|
|
37
58
|
});
|
package/src/forms/Form.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { MagicObject } from '@noeldemartin/utils';
|
|
2
|
-
import { reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { computed, reactive, readonly, ref } from 'vue';
|
|
3
3
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
|
-
import type { DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
4
|
+
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
5
|
|
|
6
6
|
export const FormFieldTypes = {
|
|
7
7
|
String: 'string',
|
|
8
8
|
Number: 'number',
|
|
9
|
+
Boolean: 'boolean',
|
|
9
10
|
} as const;
|
|
10
11
|
|
|
11
12
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
@@ -18,7 +19,7 @@ export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
|
18
19
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
19
20
|
|
|
20
21
|
export type FormData<T> = {
|
|
21
|
-
[k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
22
|
+
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
22
23
|
? TRules extends 'required'
|
|
23
24
|
? GetFormFieldValue<TType>
|
|
24
25
|
: GetFormFieldValue<TType> | null
|
|
@@ -33,6 +34,8 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
33
34
|
? string
|
|
34
35
|
: TType extends typeof FormFieldTypes.Number
|
|
35
36
|
? number
|
|
37
|
+
: TType extends typeof FormFieldTypes.Boolean
|
|
38
|
+
? boolean
|
|
36
39
|
: never;
|
|
37
40
|
|
|
38
41
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
@@ -41,7 +44,7 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
41
44
|
|
|
42
45
|
private _fields: Fields;
|
|
43
46
|
private _data: FormData<Fields>;
|
|
44
|
-
private _valid:
|
|
47
|
+
private _valid: ComputedRef<boolean>;
|
|
45
48
|
private _submitted: Ref<boolean>;
|
|
46
49
|
private _errors: FormErrors<Fields>;
|
|
47
50
|
|
|
@@ -50,9 +53,9 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
50
53
|
|
|
51
54
|
this._fields = fields;
|
|
52
55
|
this._submitted = ref(false);
|
|
53
|
-
this._valid = ref(true);
|
|
54
56
|
this._data = this.getInitialData(fields);
|
|
55
57
|
this._errors = this.getInitialErrors(fields);
|
|
58
|
+
this._valid = computed(() => !Object.values(this._errors).some((error) => error !== null));
|
|
56
59
|
|
|
57
60
|
this.errors = readonly(this._errors);
|
|
58
61
|
}
|
|
@@ -78,15 +81,22 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
public validate(): boolean {
|
|
81
|
-
const errors = Object.entries(this._fields).reduce((
|
|
82
|
-
|
|
84
|
+
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
85
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
83
86
|
|
|
84
|
-
return
|
|
87
|
+
return formErrors;
|
|
85
88
|
}, {} as Record<string, string[] | null>);
|
|
86
89
|
|
|
87
|
-
|
|
90
|
+
this.resetErrors(errors);
|
|
88
91
|
|
|
89
|
-
return
|
|
92
|
+
return this.valid;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public reset(): void {
|
|
96
|
+
this._submitted.value = false;
|
|
97
|
+
|
|
98
|
+
this.resetData();
|
|
99
|
+
this.resetErrors();
|
|
90
100
|
}
|
|
91
101
|
|
|
92
102
|
public submit(): boolean {
|
|
@@ -128,10 +138,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
128
138
|
return {} as FormData<Fields>;
|
|
129
139
|
}
|
|
130
140
|
|
|
131
|
-
const data = Object.entries(fields).reduce((
|
|
132
|
-
|
|
141
|
+
const data = Object.entries(fields).reduce((formData, [name, definition]) => {
|
|
142
|
+
formData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
133
143
|
|
|
134
|
-
return
|
|
144
|
+
return formData;
|
|
135
145
|
}, {} as FormData<Fields>);
|
|
136
146
|
|
|
137
147
|
return reactive(data) as FormData<Fields>;
|
|
@@ -142,13 +152,25 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
142
152
|
return {} as FormErrors<Fields>;
|
|
143
153
|
}
|
|
144
154
|
|
|
145
|
-
const errors = Object.keys(fields).reduce((
|
|
146
|
-
|
|
155
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
156
|
+
formErrors[name as keyof Fields] = null;
|
|
147
157
|
|
|
148
|
-
return
|
|
158
|
+
return formErrors;
|
|
149
159
|
}, {} as FormErrors<Fields>);
|
|
150
160
|
|
|
151
161
|
return reactive(errors) as FormErrors<Fields>;
|
|
152
162
|
}
|
|
153
163
|
|
|
164
|
+
private resetData(): void {
|
|
165
|
+
for (const [name, field] of Object.entries(this._fields)) {
|
|
166
|
+
this._data[name as keyof Fields] = (field.default ?? null) as FormData<Fields>[keyof Fields];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private resetErrors(errors?: Record<string, string[] | null>): void {
|
|
171
|
+
Object.keys(this._errors).forEach((key) => delete this._errors[key as keyof Fields]);
|
|
172
|
+
|
|
173
|
+
errors && Object.assign(this._errors, errors);
|
|
174
|
+
}
|
|
175
|
+
|
|
154
176
|
}
|
package/src/forms/utils.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { FormFieldTypes } from './Form';
|
|
2
2
|
import type { FormFieldDefinition } from './Form';
|
|
3
3
|
|
|
4
|
+
export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
|
+
return {
|
|
6
|
+
default: defaultValue,
|
|
7
|
+
type: FormFieldTypes.Boolean,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function requiredBooleanInput(
|
|
12
|
+
defaultValue?: boolean,
|
|
13
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
|
|
14
|
+
return {
|
|
15
|
+
default: defaultValue,
|
|
16
|
+
type: FormFieldTypes.Boolean,
|
|
17
|
+
rules: 'required',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
export function requiredNumberInput(
|
|
5
22
|
defaultValue?: number,
|
|
6
23
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
package/src/globals.ts
ADDED
package/src/lang/Lang.ts
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { facade } from '@noeldemartin/utils';
|
|
3
|
-
import type { Composer } from 'vue-i18n';
|
|
1
|
+
import { facade, toString } from '@noeldemartin/utils';
|
|
4
2
|
|
|
3
|
+
import App from '@/services/App';
|
|
5
4
|
import Service from '@/services/Service';
|
|
6
5
|
|
|
6
|
+
export interface LangProvider {
|
|
7
|
+
translate(key: string, parameters?: Record<string, unknown>): string;
|
|
8
|
+
}
|
|
9
|
+
|
|
7
10
|
export class LangService extends Service {
|
|
8
11
|
|
|
9
|
-
private
|
|
12
|
+
private provider: LangProvider;
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
|
|
17
|
+
this.provider = {
|
|
18
|
+
translate: (key) => {
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
App.isDevelopment && console.warn('Lang provider is missing');
|
|
21
|
+
|
|
22
|
+
return key;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
10
26
|
|
|
11
|
-
public
|
|
12
|
-
this.
|
|
27
|
+
public setProvider(provider: LangProvider): void {
|
|
28
|
+
this.provider = provider;
|
|
13
29
|
}
|
|
14
30
|
|
|
15
|
-
public translate(key: string, parameters
|
|
16
|
-
return this.
|
|
31
|
+
public translate(key: string, parameters?: Record<string, unknown>): string {
|
|
32
|
+
return this.provider.translate(key, parameters) ?? key;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public translateWithDefault(key: string, defaultMessage: string): string;
|
|
36
|
+
public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
|
|
37
|
+
public translateWithDefault(
|
|
38
|
+
key: string,
|
|
39
|
+
defaultMessageOrParameters?: string | Record<string, unknown>,
|
|
40
|
+
defaultMessage?: string,
|
|
41
|
+
): string {
|
|
42
|
+
defaultMessage ??= defaultMessageOrParameters as string;
|
|
43
|
+
|
|
44
|
+
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
|
|
45
|
+
const message = this.provider.translate(key, parameters) ?? key;
|
|
46
|
+
|
|
47
|
+
if (message === key) {
|
|
48
|
+
return Object.entries(parameters).reduce(
|
|
49
|
+
(renderedMessage, [name, value]) =>
|
|
50
|
+
renderedMessage.replace(new RegExp(`\\{\\s*${name}\\s*\\}`, 'g'), toString(value)),
|
|
51
|
+
defaultMessage,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return message;
|
|
17
56
|
}
|
|
18
57
|
|
|
19
58
|
}
|