@aerogel/core 0.0.0-next.7035064d9ec6a82a936ee8dfcc4b58ed2e25a399 → 0.0.0-next.73a6df428477c011a1aebe0a932ebef0aa5e1b16

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