@aerogel/core 0.0.0-next.d824b40e5d06757cd9f47c9f771d916185df4f05 → 0.0.0-next.e4c0d5bd2801fbe93545477ea515af53df69b522

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 (188) hide show
  1. package/dist/aerogel-core.css +1 -0
  2. package/dist/aerogel-core.d.ts +2226 -912
  3. package/dist/aerogel-core.js +3432 -0
  4. package/dist/aerogel-core.js.map +1 -0
  5. package/package.json +37 -34
  6. package/src/bootstrap/bootstrap.test.ts +7 -10
  7. package/src/bootstrap/index.ts +43 -15
  8. package/src/bootstrap/options.ts +4 -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/HeadlessInputDescription.vue +27 -0
  28. package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
  29. package/src/components/headless/HeadlessInputInput.vue +75 -0
  30. package/src/components/headless/HeadlessInputLabel.vue +18 -0
  31. package/src/components/headless/HeadlessInputTextArea.vue +40 -0
  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/HeadlessSelectError.vue +25 -0
  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/HeadlessToast.vue +18 -0
  45. package/src/components/headless/HeadlessToastAction.vue +13 -0
  46. package/src/components/headless/index.ts +19 -3
  47. package/src/components/index.ts +6 -9
  48. package/src/components/ui/AdvancedOptions.vue +18 -0
  49. package/src/components/ui/AlertModal.vue +17 -0
  50. package/src/components/ui/Button.vue +100 -0
  51. package/src/components/ui/Checkbox.vue +56 -0
  52. package/src/components/ui/ConfirmModal.vue +50 -0
  53. package/src/components/ui/DropdownMenu.vue +32 -0
  54. package/src/components/ui/DropdownMenuOption.vue +22 -0
  55. package/src/components/ui/DropdownMenuOptions.vue +44 -0
  56. package/src/components/ui/EditableContent.vue +82 -0
  57. package/src/components/ui/ErrorLogs.vue +19 -0
  58. package/src/components/ui/ErrorLogsModal.vue +48 -0
  59. package/src/components/ui/ErrorMessage.vue +15 -0
  60. package/src/components/ui/ErrorReportModal.vue +73 -0
  61. package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +38 -29
  62. package/src/components/ui/ErrorReportModalTitle.vue +24 -0
  63. package/src/components/ui/Form.vue +24 -0
  64. package/src/components/ui/Input.vue +56 -0
  65. package/src/components/ui/Link.vue +12 -0
  66. package/src/components/ui/LoadingModal.vue +34 -0
  67. package/src/components/ui/Markdown.vue +97 -0
  68. package/src/components/ui/Modal.vue +123 -0
  69. package/src/components/ui/ModalContext.vue +31 -0
  70. package/src/components/ui/ProgressBar.vue +51 -0
  71. package/src/components/ui/PromptModal.vue +38 -0
  72. package/src/components/ui/Select.vue +27 -0
  73. package/src/components/ui/SelectLabel.vue +17 -0
  74. package/src/components/ui/SelectOption.vue +29 -0
  75. package/src/components/ui/SelectOptions.vue +35 -0
  76. package/src/components/ui/SelectTrigger.vue +29 -0
  77. package/src/components/ui/SettingsModal.vue +15 -0
  78. package/src/components/ui/StartupCrash.vue +31 -0
  79. package/src/components/ui/Toast.vue +46 -0
  80. package/src/components/ui/index.ts +32 -0
  81. package/src/directives/index.ts +13 -5
  82. package/src/directives/measure.ts +46 -0
  83. package/src/errors/Errors.state.ts +1 -1
  84. package/src/errors/Errors.ts +41 -37
  85. package/src/errors/JobCancelledError.ts +3 -0
  86. package/src/errors/index.ts +19 -29
  87. package/src/errors/utils.ts +35 -0
  88. package/src/forms/FormController.test.ts +110 -0
  89. package/src/forms/FormController.ts +246 -0
  90. package/src/forms/index.ts +3 -2
  91. package/src/forms/utils.ts +51 -20
  92. package/src/forms/validation.ts +19 -0
  93. package/src/index.css +73 -0
  94. package/src/{main.ts → index.ts} +3 -0
  95. package/src/jobs/Job.ts +147 -0
  96. package/src/jobs/index.ts +10 -0
  97. package/src/jobs/listeners.ts +3 -0
  98. package/src/jobs/status.ts +4 -0
  99. package/src/lang/DefaultLangProvider.ts +46 -0
  100. package/src/lang/Lang.state.ts +11 -0
  101. package/src/lang/Lang.ts +44 -29
  102. package/src/lang/index.ts +12 -6
  103. package/src/lang/settings/Language.vue +48 -0
  104. package/src/lang/settings/index.ts +10 -0
  105. package/src/plugins/Plugin.ts +1 -1
  106. package/src/plugins/index.ts +10 -7
  107. package/src/services/App.state.ts +39 -7
  108. package/src/services/App.ts +49 -6
  109. package/src/services/Cache.ts +43 -0
  110. package/src/services/Events.test.ts +39 -0
  111. package/src/services/Events.ts +110 -36
  112. package/src/services/Service.ts +154 -49
  113. package/src/services/Storage.ts +20 -0
  114. package/src/services/index.ts +18 -6
  115. package/src/services/store.ts +8 -5
  116. package/src/services/utils.ts +18 -0
  117. package/src/testing/index.ts +26 -0
  118. package/src/testing/setup.ts +11 -0
  119. package/src/ui/UI.state.ts +14 -12
  120. package/src/ui/UI.ts +314 -95
  121. package/src/ui/index.ts +32 -27
  122. package/src/ui/utils.ts +16 -0
  123. package/src/utils/classes.ts +41 -0
  124. package/src/utils/composition/events.ts +4 -5
  125. package/src/utils/composition/forms.ts +20 -4
  126. package/src/utils/composition/persistent.test.ts +33 -0
  127. package/src/utils/composition/persistent.ts +11 -0
  128. package/src/utils/composition/state.test.ts +47 -0
  129. package/src/utils/composition/state.ts +33 -0
  130. package/src/utils/index.ts +5 -0
  131. package/src/utils/markdown.test.ts +50 -0
  132. package/src/utils/markdown.ts +53 -6
  133. package/src/utils/types.ts +3 -0
  134. package/src/utils/vue.ts +38 -123
  135. package/.eslintrc.js +0 -3
  136. package/dist/aerogel-core.cjs.js +0 -2
  137. package/dist/aerogel-core.cjs.js.map +0 -1
  138. package/dist/aerogel-core.esm.js +0 -2
  139. package/dist/aerogel-core.esm.js.map +0 -1
  140. package/dist/virtual.d.ts +0 -11
  141. package/noeldemartin.config.js +0 -5
  142. package/src/components/AGAppLayout.vue +0 -11
  143. package/src/components/AGAppOverlays.vue +0 -37
  144. package/src/components/AGAppSnackbars.vue +0 -13
  145. package/src/components/basic/AGErrorMessage.vue +0 -16
  146. package/src/components/basic/AGLink.vue +0 -9
  147. package/src/components/basic/AGMarkdown.vue +0 -36
  148. package/src/components/basic/index.ts +0 -5
  149. package/src/components/constants.ts +0 -8
  150. package/src/components/forms/AGButton.vue +0 -44
  151. package/src/components/forms/AGCheckbox.vue +0 -35
  152. package/src/components/forms/AGForm.vue +0 -26
  153. package/src/components/forms/AGInput.vue +0 -36
  154. package/src/components/forms/index.ts +0 -6
  155. package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
  156. package/src/components/headless/forms/AGHeadlessInput.ts +0 -8
  157. package/src/components/headless/forms/AGHeadlessInput.vue +0 -54
  158. package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
  159. package/src/components/headless/forms/AGHeadlessInputLabel.vue +0 -16
  160. package/src/components/headless/forms/index.ts +0 -6
  161. package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
  162. package/src/components/headless/modals/AGHeadlessModal.vue +0 -88
  163. package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
  164. package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
  165. package/src/components/headless/modals/index.ts +0 -6
  166. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
  167. package/src/components/headless/snackbars/index.ts +0 -25
  168. package/src/components/modals/AGAlertModal.vue +0 -25
  169. package/src/components/modals/AGConfirmModal.vue +0 -30
  170. package/src/components/modals/AGErrorReportModal.ts +0 -20
  171. package/src/components/modals/AGErrorReportModal.vue +0 -62
  172. package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
  173. package/src/components/modals/AGLoadingModal.vue +0 -19
  174. package/src/components/modals/AGModal.ts +0 -10
  175. package/src/components/modals/AGModal.vue +0 -37
  176. package/src/components/modals/AGModalContext.ts +0 -8
  177. package/src/components/modals/AGModalContext.vue +0 -22
  178. package/src/components/modals/AGModalTitle.vue +0 -9
  179. package/src/components/modals/index.ts +0 -23
  180. package/src/components/snackbars/AGSnackbar.vue +0 -42
  181. package/src/components/snackbars/index.ts +0 -3
  182. package/src/directives/initial-focus.ts +0 -11
  183. package/src/forms/Form.test.ts +0 -58
  184. package/src/forms/Form.ts +0 -176
  185. package/src/forms/composition.ts +0 -6
  186. package/src/types/virtual.d.ts +0 -11
  187. package/tsconfig.json +0 -11
  188. package/vite.config.ts +0 -14
@@ -0,0 +1,110 @@
1
+ import { describe, expect, expectTypeOf, it } from 'vitest';
2
+ import { tt } from '@noeldemartin/testing';
3
+ import type { Equals } from '@noeldemartin/utils';
4
+ import type { Expect } from '@noeldemartin/testing';
5
+
6
+ import {
7
+ numberInput,
8
+ objectInput,
9
+ requiredObjectInput,
10
+ requiredStringInput,
11
+ stringInput,
12
+ } from '@aerogel/core/forms/utils';
13
+ import { useForm } from '@aerogel/core/utils/composition/forms';
14
+
15
+ describe('FormController', () => {
16
+
17
+ it('defines magic fields', () => {
18
+ const form = useForm({
19
+ name: requiredStringInput(),
20
+ age: numberInput(),
21
+ });
22
+
23
+ expectTypeOf(form.name).toEqualTypeOf<string>();
24
+ expectTypeOf(form.age).toEqualTypeOf<number | null>();
25
+ });
26
+
27
+ it('validates required fields', () => {
28
+ // Arrange
29
+ const form = useForm({
30
+ name: {
31
+ type: 'string',
32
+ rules: 'required',
33
+ },
34
+ });
35
+
36
+ // Act
37
+ form.submit();
38
+
39
+ // Assert
40
+ expect(form.valid).toBe(false);
41
+ expect(form.submitted).toBe(true);
42
+ expect(form.errors.name).toEqual(['required']);
43
+ });
44
+
45
+ it('resets form', () => {
46
+ // Arrange
47
+ const form = useForm({
48
+ name: {
49
+ type: 'string',
50
+ rules: 'required',
51
+ },
52
+ });
53
+
54
+ form.name = 'Foo bar';
55
+ form.submit();
56
+
57
+ // Act
58
+ form.reset();
59
+
60
+ // Assert
61
+ expect(form.valid).toBe(true);
62
+ expect(form.submitted).toBe(false);
63
+ expect(form.name).toBeNull();
64
+ });
65
+
66
+ it('trims values', () => {
67
+ // Arrange
68
+ const form = useForm({
69
+ trimmed: {
70
+ type: 'string',
71
+ rules: 'required',
72
+ },
73
+ untrimmed: {
74
+ type: 'string',
75
+ rules: 'required',
76
+ trim: false,
77
+ },
78
+ });
79
+
80
+ // Act
81
+ form.trimmed = ' ';
82
+ form.untrimmed = ' ';
83
+
84
+ form.submit();
85
+
86
+ // Assert
87
+ expect(form.valid).toBe(false);
88
+ expect(form.submitted).toBe(true);
89
+ expect(form.trimmed).toEqual('');
90
+ expect(form.untrimmed).toEqual(' ');
91
+ expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
92
+ });
93
+
94
+ it('infers field types', () => {
95
+ const form = useForm({
96
+ one: stringInput(),
97
+ two: requiredStringInput(),
98
+ three: objectInput(),
99
+ four: requiredObjectInput<{ foo: string; bar?: number }>(),
100
+ });
101
+
102
+ tt<
103
+ | Expect<Equals<typeof form.one, string | null>>
104
+ | Expect<Equals<typeof form.two, string>>
105
+ | Expect<Equals<typeof form.three, object | null>>
106
+ | Expect<Equals<typeof form.four, { foo: string; bar?: number }>>
107
+ >();
108
+ });
109
+
110
+ });
@@ -0,0 +1,246 @@
1
+ import { computed, nextTick, reactive, readonly, ref } from 'vue';
2
+ import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
3
+ import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
4
+
5
+ import { validate } from './validation';
6
+
7
+ export const __objectType: unique symbol = Symbol();
8
+
9
+ export interface FormFieldDefinition<
10
+ TType extends FormFieldType = FormFieldType,
11
+ TRules extends string = string,
12
+ TObjectType = object,
13
+ > {
14
+ type: TType;
15
+ trim?: boolean;
16
+ default?: GetFormFieldValue<TType>;
17
+ rules?: TRules;
18
+ [__objectType]?: TObjectType;
19
+ }
20
+
21
+ export type FormFieldType = 'string' | 'number' | 'boolean' | 'object' | 'date';
22
+ export type FormFieldValue = GetFormFieldValue<FormFieldType>;
23
+ export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
24
+
25
+ export type FormData<T> = {
26
+ -readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules, infer TObjectType>
27
+ ? TRules extends 'required'
28
+ ? GetFormFieldValue<TType, TObjectType>
29
+ : GetFormFieldValue<TType, TObjectType> | null
30
+ : never;
31
+ };
32
+
33
+ export type FormErrors<T> = {
34
+ [k in keyof T]: string[] | null;
35
+ };
36
+
37
+ export type GetFormFieldValue<TType, TObjectType = object> = TType extends 'string'
38
+ ? string
39
+ : TType extends 'number'
40
+ ? number
41
+ : TType extends 'boolean'
42
+ ? boolean
43
+ : TType extends 'object'
44
+ ? TObjectType extends object
45
+ ? TObjectType
46
+ : object
47
+ : TType extends 'date'
48
+ ? Date
49
+ : never;
50
+
51
+ const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
52
+
53
+ export type SubmitFormListener = () => unknown;
54
+ export type FocusFormListener = (input: string) => unknown;
55
+
56
+ export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
57
+
58
+ public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
59
+
60
+ private _fields: Fields;
61
+ private _data: FormData<Fields>;
62
+ private _submitted: Ref<boolean>;
63
+ private _errors: FormErrors<Fields>;
64
+ private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
65
+
66
+ constructor(fields: Fields) {
67
+ super();
68
+
69
+ this._fields = fields;
70
+ this._submitted = ref(false);
71
+ this._data = this.getInitialData(fields);
72
+ this._errors = this.getInitialErrors(fields);
73
+
74
+ validForms.set(
75
+ this,
76
+ computed(() => !Object.values(this._errors).some((error) => error !== null)),
77
+ );
78
+
79
+ this.errors = readonly(this._errors);
80
+ }
81
+
82
+ public get valid(): boolean {
83
+ return !!validForms.get(this)?.value;
84
+ }
85
+
86
+ public get submitted(): boolean {
87
+ return this._submitted.value;
88
+ }
89
+
90
+ public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
91
+ const definition =
92
+ this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
93
+
94
+ this._data[field] =
95
+ definition.type === 'string' && (definition.trim ?? true)
96
+ ? (toString(value).trim() as FormData<Fields>[T])
97
+ : value;
98
+
99
+ if (this._submitted.value) {
100
+ this.validate();
101
+ }
102
+ }
103
+
104
+ public getFieldValue<T extends keyof Fields>(field: T): GetFormFieldValue<Fields[T]['type']> {
105
+ return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
106
+ }
107
+
108
+ public getFieldRules<T extends keyof Fields>(field: T): string[] {
109
+ return this._fields[field]?.rules?.split('|') ?? [];
110
+ }
111
+
112
+ public data(): FormData<Fields> {
113
+ return { ...this._data };
114
+ }
115
+
116
+ public validate(): boolean {
117
+ const errors = Object.entries(this._fields).reduce(
118
+ (formErrors, [name, definition]) => {
119
+ formErrors[name] = this.getFieldErrors(name, definition);
120
+
121
+ return formErrors;
122
+ },
123
+ {} as Record<string, string[] | null>,
124
+ );
125
+
126
+ this.resetErrors(errors);
127
+
128
+ return this.valid;
129
+ }
130
+
131
+ public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
132
+ this._submitted.value = false;
133
+
134
+ options.keepData || this.resetData();
135
+ options.keepErrors || this.resetErrors();
136
+ }
137
+
138
+ public submit(): boolean {
139
+ this._submitted.value = true;
140
+
141
+ const valid = this.validate();
142
+
143
+ valid && this._listeners['submit']?.forEach((listener) => listener());
144
+
145
+ return valid;
146
+ }
147
+
148
+ public on(event: 'focus', listener: FocusFormListener): () => void;
149
+ public on(event: 'submit', listener: SubmitFormListener): () => void;
150
+ public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
151
+ this._listeners[event] ??= [];
152
+
153
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
+ this._listeners[event]?.push(listener as any);
155
+
156
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
157
+ return () => this.off(event as any, listener);
158
+ }
159
+
160
+ public off(event: 'focus', listener: FocusFormListener): void;
161
+ public off(event: 'submit', listener: SubmitFormListener): void;
162
+ public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
163
+ arrayRemove(this._listeners[event] ?? [], listener);
164
+ }
165
+
166
+ public async focus(input: string): Promise<void> {
167
+ await nextTick();
168
+
169
+ this._listeners['focus']?.forEach((listener) => listener(input));
170
+ }
171
+
172
+ protected override __get(property: string): unknown {
173
+ if (!(property in this._fields)) {
174
+ return super.__get(property);
175
+ }
176
+
177
+ return this.getFieldValue(property);
178
+ }
179
+
180
+ protected override __set(property: string, value: unknown): void {
181
+ if (!(property in this._fields)) {
182
+ super.__set(property, value);
183
+
184
+ return;
185
+ }
186
+
187
+ this.setFieldValue(property, value as FormData<Fields>[string]);
188
+ }
189
+
190
+ private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
191
+ const errors = [];
192
+ const value = this._data[name];
193
+ const rules = definition.rules?.split('|') ?? [];
194
+
195
+ for (const rule of rules) {
196
+ if (rule !== 'required' && (value === null || value === undefined)) {
197
+ continue;
198
+ }
199
+
200
+ errors.push(...validate(value, rule));
201
+ }
202
+
203
+ return errors.length > 0 ? errors : null;
204
+ }
205
+
206
+ private getInitialData(fields: Fields): FormData<Fields> {
207
+ if (this.static().isConjuring()) {
208
+ return {} as FormData<Fields>;
209
+ }
210
+
211
+ const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
212
+ initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
213
+
214
+ return initialData;
215
+ }, {} as FormData<Fields>);
216
+
217
+ return reactive(data) as FormData<Fields>;
218
+ }
219
+
220
+ private getInitialErrors(fields: Fields): FormErrors<Fields> {
221
+ if (this.static().isConjuring()) {
222
+ return {} as FormErrors<Fields>;
223
+ }
224
+
225
+ const errors = Object.keys(fields).reduce((formErrors, name) => {
226
+ formErrors[name as keyof Fields] = null;
227
+
228
+ return formErrors;
229
+ }, {} as FormErrors<Fields>);
230
+
231
+ return reactive(errors) as FormErrors<Fields>;
232
+ }
233
+
234
+ private resetData(): void {
235
+ for (const [name, field] of Object.entries(this._fields)) {
236
+ this._data[name as keyof Fields] = (field.default ?? null) as FormData<Fields>[keyof Fields];
237
+ }
238
+ }
239
+
240
+ private resetErrors(errors?: Record<string, string[] | null>): void {
241
+ Object.keys(this._errors).forEach((key) => delete this._errors[key as keyof Fields]);
242
+
243
+ errors && Object.assign(this._errors, errors);
244
+ }
245
+
246
+ }
@@ -1,3 +1,4 @@
1
- export * from './Form';
2
- export * from './composition';
1
+ export * from './FormController';
3
2
  export * from './utils';
3
+ export * from './validation';
4
+ export { default as FormController } from './FormController';
@@ -1,53 +1,84 @@
1
- import { FormFieldTypes } from './Form';
2
- import type { FormFieldDefinition } from './Form';
1
+ import type { FormFieldDefinition } from './FormController';
3
2
 
4
- export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
3
+ export function booleanInput(defaultValue?: boolean, options: { rules?: string } = {}): FormFieldDefinition<'boolean'> {
5
4
  return {
6
5
  default: defaultValue,
7
- type: FormFieldTypes.Boolean,
6
+ type: 'boolean',
7
+ rules: options.rules,
8
8
  };
9
9
  }
10
10
 
11
- export function requiredBooleanInput(
12
- defaultValue?: boolean,
13
- ): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
11
+ export function dateInput(defaultValue?: Date, options: { rules?: string } = {}): FormFieldDefinition<'date'> {
14
12
  return {
15
13
  default: defaultValue,
16
- type: FormFieldTypes.Boolean,
14
+ type: 'date',
15
+ rules: options.rules,
16
+ };
17
+ }
18
+
19
+ export function requiredBooleanInput(defaultValue?: boolean): FormFieldDefinition<'boolean', 'required'> {
20
+ return {
21
+ default: defaultValue,
22
+ type: 'boolean',
23
+ rules: 'required',
24
+ };
25
+ }
26
+
27
+ export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<'date', 'required'> {
28
+ return {
29
+ default: defaultValue,
30
+ type: 'date',
17
31
  rules: 'required',
18
32
  };
19
33
  }
20
34
 
21
- export function requiredNumberInput(
22
- defaultValue?: number,
23
- ): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
35
+ export function requiredNumberInput(defaultValue?: number): FormFieldDefinition<'number', 'required'> {
24
36
  return {
25
37
  default: defaultValue,
26
- type: FormFieldTypes.Number,
38
+ type: 'number',
27
39
  rules: 'required',
28
40
  };
29
41
  }
30
42
 
31
- export function requiredStringInput(
32
- defaultValue?: string,
33
- ): FormFieldDefinition<typeof FormFieldTypes.String, 'required'> {
43
+ export function requiredObjectInput<T extends object>(defaultValue?: T): FormFieldDefinition<'object', 'required', T> {
34
44
  return {
35
45
  default: defaultValue,
36
- type: FormFieldTypes.String,
46
+ type: 'object',
37
47
  rules: 'required',
38
48
  };
39
49
  }
40
50
 
41
- export function numberInput(defaultValue?: number): FormFieldDefinition<typeof FormFieldTypes.Number> {
51
+ export function requiredStringInput(defaultValue?: string): FormFieldDefinition<'string', 'required'> {
52
+ return {
53
+ default: defaultValue,
54
+ type: 'string',
55
+ rules: 'required',
56
+ };
57
+ }
58
+
59
+ export function numberInput(defaultValue?: number, options: { rules?: string } = {}): FormFieldDefinition<'number'> {
60
+ return {
61
+ default: defaultValue,
62
+ type: 'number',
63
+ rules: options.rules,
64
+ };
65
+ }
66
+
67
+ export function objectInput<T extends object>(
68
+ defaultValue?: T,
69
+ options: { rules?: string } = {},
70
+ ): FormFieldDefinition<'object', string, T> {
42
71
  return {
43
72
  default: defaultValue,
44
- type: FormFieldTypes.Number,
73
+ type: 'object',
74
+ rules: options.rules,
45
75
  };
46
76
  }
47
77
 
48
- export function stringInput(defaultValue?: string): FormFieldDefinition<typeof FormFieldTypes.String> {
78
+ export function stringInput(defaultValue?: string, options: { rules?: string } = {}): FormFieldDefinition<'string'> {
49
79
  return {
50
80
  default: defaultValue,
51
- type: FormFieldTypes.String,
81
+ type: 'string',
82
+ rules: options.rules,
52
83
  };
53
84
  }
@@ -0,0 +1,19 @@
1
+ import { arrayFrom } from '@noeldemartin/utils';
2
+
3
+ const builtInRules: Record<string, FormFieldValidator> = {
4
+ required: (value) => (value ? undefined : 'required'),
5
+ };
6
+
7
+ export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
8
+
9
+ export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
10
+
11
+ export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
12
+ validators[rule] = validator as FormFieldValidator;
13
+ }
14
+
15
+ export function validate(value: unknown, rule: string): string[] {
16
+ const errors = validators[rule]?.(value);
17
+
18
+ return errors ? arrayFrom(errors) : [];
19
+ }
package/src/index.css ADDED
@@ -0,0 +1,73 @@
1
+ @import 'tailwindcss';
2
+
3
+ @plugin '@tailwindcss/forms';
4
+ @plugin '@tailwindcss/typography';
5
+
6
+ @source './';
7
+
8
+ @theme {
9
+ --color-primary: oklch(0.205 0 0);
10
+ --color-primary-50: color-mix(in oklab, var(--color-primary-600) 5%, transparent);
11
+ --color-primary-100: color-mix(in oklab, var(--color-primary-600) 15%, transparent);
12
+ --color-primary-200: color-mix(in oklab, var(--color-primary-600) 30%, transparent);
13
+ --color-primary-300: color-mix(in oklab, var(--color-primary-600) 50%, transparent);
14
+ --color-primary-400: color-mix(in oklab, var(--color-primary-600) 65%, transparent);
15
+ --color-primary-500: color-mix(in oklab, var(--color-primary-600) 80%, transparent);
16
+ --color-primary-600: var(--color-primary);
17
+ --color-primary-700: color-mix(in oklab, var(--color-primary-600) 90%, black);
18
+ --color-primary-800: color-mix(in oklab, var(--color-primary-600) 80%, black);
19
+ --color-primary-900: color-mix(in oklab, var(--color-primary-600) 70%, black);
20
+ --color-primary-950: color-mix(in oklab, var(--color-primary-600) 50%, black);
21
+
22
+ --color-background: oklch(1 0 0);
23
+ --color-links: var(--color-primary);
24
+ }
25
+
26
+ .clickable {
27
+ position: relative;
28
+ }
29
+
30
+ .clickable::after {
31
+ --clickable-size: 44px;
32
+ --clickable-inset-by: min(0px, calc((100% - var(--clickable-size)) / 2));
33
+
34
+ content: '';
35
+ position: absolute;
36
+ top: var(--clickable-inset-by);
37
+ left: var(--clickable-inset-by);
38
+ right: var(--clickable-inset-by);
39
+ bottom: var(--clickable-inset-by);
40
+ }
41
+
42
+ input[type='number'].appearance-textfield {
43
+ appearance: textfield;
44
+ }
45
+
46
+ input[type='number'].appearance-textfield::-webkit-outer-spin-button,
47
+ input[type='number'].appearance-textfield::-webkit-inner-spin-button {
48
+ appearance: none;
49
+ }
50
+
51
+ button[data-markdown-action] {
52
+ color: var(--tw-prose-links);
53
+ text-decoration: underline;
54
+ font-weight: 500;
55
+ }
56
+
57
+ @keyframes fade-in {
58
+ 0% {
59
+ opacity: 0;
60
+ }
61
+ 100% {
62
+ opacity: 1;
63
+ }
64
+ }
65
+
66
+ @keyframes grow {
67
+ 0% {
68
+ scale: 0;
69
+ }
70
+ 100% {
71
+ opacity: 1;
72
+ }
73
+ }
@@ -1,9 +1,12 @@
1
1
  export * from './bootstrap';
2
2
  export * from './components';
3
+ export * from './directives';
3
4
  export * from './errors';
4
5
  export * from './forms';
6
+ export * from './jobs';
5
7
  export * from './lang';
6
8
  export * from './plugins';
7
9
  export * from './services';
10
+ export * from './testing';
8
11
  export * from './ui';
9
12
  export * from './utils';