srcdev-nuxt-forms 3.0.0 → 4.0.0

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 (86) hide show
  1. package/assets/styles/ally/_utils.css +20 -0
  2. package/assets/styles/ally/_variables.css +8 -0
  3. package/assets/styles/ally/index.css +2 -0
  4. package/assets/styles/forms/index.css +2 -0
  5. package/assets/styles/forms/themes/_error.css +85 -0
  6. package/assets/styles/forms/themes/_ghost.css +85 -0
  7. package/assets/styles/forms/themes/_input-action-underlined.css +20 -0
  8. package/assets/styles/forms/themes/_input-action.css +20 -0
  9. package/assets/styles/forms/themes/_primary.css +92 -0
  10. package/assets/styles/forms/themes/_secondary.css +85 -0
  11. package/assets/styles/forms/themes/_success.css +85 -0
  12. package/assets/styles/forms/themes/_tertiary.css +85 -0
  13. package/assets/styles/forms/themes/_warning.css +85 -0
  14. package/assets/styles/forms/themes/index.css +9 -0
  15. package/assets/styles/forms/variables/_sizes.css +71 -0
  16. package/assets/styles/forms/variables/_theme.css +11 -0
  17. package/assets/styles/forms/variables/index.css +2 -0
  18. package/assets/styles/main.css +5 -0
  19. package/assets/styles/typography/index.css +2 -0
  20. package/assets/styles/typography/utils/_font-classes.css +190 -0
  21. package/assets/styles/typography/utils/_weights.css +69 -0
  22. package/assets/styles/typography/utils/index.css +2 -0
  23. package/assets/styles/typography/variables/_colors.css +14 -0
  24. package/assets/styles/typography/variables/_reponsive-font-size.css +10 -0
  25. package/assets/styles/typography/variables/index.css +2 -0
  26. package/assets/styles/utils/_margin-helpers.css +334 -0
  27. package/assets/styles/utils/_padding-helpers.css +308 -0
  28. package/assets/styles/utils/_page.css +49 -0
  29. package/assets/styles/utils/index.css +3 -0
  30. package/assets/styles/variables/colors/_blue.css +15 -0
  31. package/assets/styles/variables/colors/_gray.css +16 -0
  32. package/assets/styles/variables/colors/_green.css +15 -0
  33. package/assets/styles/variables/colors/_orange.css +15 -0
  34. package/assets/styles/variables/colors/_red.css +15 -0
  35. package/assets/styles/variables/colors/_yellow.css +15 -0
  36. package/assets/styles/variables/colors/colors.css +6 -0
  37. package/assets/styles/variables/index.css +1 -0
  38. package/components/forms/c12/prop-validators/index.ts +38 -0
  39. package/components/forms/c12/utils.ts +14 -0
  40. package/components/forms/form-errors/InputError.vue +172 -0
  41. package/components/forms/form-errors/tests/InputError.spec.ts +67 -0
  42. package/components/forms/input-button/InputButtonCore.vue +191 -0
  43. package/components/forms/input-button/variants/InputButtonConfirm.vue +66 -0
  44. package/components/forms/input-button/variants/InputButtonSubmit.vue +62 -0
  45. package/components/forms/input-checkbox/MultipleCheckboxes.vue +203 -0
  46. package/components/forms/input-checkbox/SingleCheckbox.vue +169 -0
  47. package/components/forms/input-checkbox/tests/MultipleCheckboxes.spec.ts +98 -0
  48. package/components/forms/input-checkbox/tests/data/tags.json +67 -0
  49. package/components/forms/input-checkbox-radio/InputCheckboxRadioButton.vue +214 -0
  50. package/components/forms/input-checkbox-radio/InputCheckboxRadioCore.vue +191 -0
  51. package/components/forms/input-checkbox-radio/InputCheckboxRadioWithLabel.vue +111 -0
  52. package/components/forms/input-number/InputNumberCore.vue +203 -0
  53. package/components/forms/input-number/variants/InputNumberDefault.vue +154 -0
  54. package/components/forms/input-radio/MultipleRadiobuttons.vue +201 -0
  55. package/components/forms/input-radio/tests/MultipleRadioButtons.spec.ts +89 -0
  56. package/components/forms/input-radio/tests/data/tags.json +67 -0
  57. package/components/forms/input-range/InputRangeCore.vue +274 -0
  58. package/components/forms/input-range/variants/InputRangeDefault.vue +156 -0
  59. package/components/forms/input-range-fancy/InputRangeFancyCore.vue +450 -0
  60. package/components/forms/input-range-fancy/InputRangeFancyWithLabel.vue +124 -0
  61. package/components/forms/input-text/InputTextCore.vue +331 -0
  62. package/components/forms/input-text/variants/InputPasswordWithLabel.vue +130 -0
  63. package/components/forms/input-text/variants/InputTextAsNumberWithLabel.vue +187 -0
  64. package/components/forms/input-text/variants/InputTextWithLabel.vue +298 -0
  65. package/components/forms/input-textarea/InputTextareaCore.vue +234 -0
  66. package/components/forms/input-textarea/variants/InputTextareaWithLabel.vue +267 -0
  67. package/components/forms/toggle-switch/ToggleSwitchCore.vue +198 -0
  68. package/components/forms/toggle-switch/ToggleSwitchCoreOld.vue +216 -0
  69. package/components/forms/toggle-switch/variants/ToggleSwitchWithLabel.vue +105 -0
  70. package/components/forms/toggle-switch/variants/ToggleSwitchWithLabelInline.vue +102 -0
  71. package/components/forms/ui/FormField.vue +78 -0
  72. package/components/forms/ui/FormWrapper.vue +35 -0
  73. package/components/utils/colour-scheme-select/ColourSchemeSelect.vue +270 -0
  74. package/components/utils/colour-scheme-select/ColourSchemeSelectOld.vue +225 -0
  75. package/components/utils/dark-mode-switcher/DarkModeSwitcher.vue +47 -0
  76. package/composables/useApiRequest.ts +25 -0
  77. package/composables/useColourScheme.ts +25 -0
  78. package/composables/useErrorMessages.ts +59 -0
  79. package/composables/useFormControl.ts +248 -0
  80. package/composables/useSleep.ts +5 -0
  81. package/composables/useStyleClassPassthrough.ts +30 -0
  82. package/composables/useZodValidation.ts +148 -0
  83. package/nuxt.config.ts +0 -3
  84. package/package.json +1 -1
  85. package/types/types.forms.ts +216 -0
  86. package/types/types.zodFormControl.ts +21 -0
@@ -0,0 +1,148 @@
1
+ import { ref, reactive, toRaw, type Ref } from 'vue';
2
+ import { z, ZodError } from 'zod';
3
+ import type { ApiErrorResponse } from '../types/types.forms';
4
+
5
+ const useZodValidation = (formSchema: any, formRef: Ref<HTMLFormElement | null>) => {
6
+ const zodFormControl = reactive({
7
+ errorCount: 0,
8
+ displayLoader: false,
9
+ submitDisabled: false,
10
+ submitAttempted: false,
11
+ submitSuccessful: false,
12
+ formIsValid: false,
13
+ isPending: false,
14
+ isDisabled: false,
15
+ previousState: {} as Record<string, any>,
16
+ });
17
+
18
+ type formSchema = z.infer<typeof formSchema>;
19
+ const zodErrorObj = ref<z.ZodFormattedError<formSchema> | null>(null);
20
+
21
+ const resetPreviousValues = () => {
22
+ for (const [field] of Object.entries(formSchema.shape)) {
23
+ const previousValue = {
24
+ value: null,
25
+ message: '',
26
+ };
27
+
28
+ zodFormControl.previousState[field] = previousValue;
29
+ }
30
+ };
31
+
32
+ const initZodForm = () => {
33
+ resetPreviousValues();
34
+ };
35
+
36
+ const getErrorCount = (zodErrorObj: Ref<z.ZodFormattedError<formSchema> | null>) => {
37
+ const zodCountErrors = zodErrorObj.value ?? [];
38
+ // @ts-ignore
39
+ delete zodCountErrors._errors;
40
+ const errorCount = Object.keys(zodCountErrors ?? []).length;
41
+ return errorCount;
42
+ };
43
+
44
+ const transformErrorMessages = (errors: any) => {
45
+ const apiErrors = ref({}) as any;
46
+ for (const [key, value] of Object.entries(errors)) {
47
+ const fieldPath = key.split('.').map((key: string) => key.charAt(0).toLowerCase() + key.slice(1));
48
+ apiErrors.value[fieldPath.join('.')] = value;
49
+ }
50
+
51
+ return apiErrors.value;
52
+ };
53
+
54
+ const updatePreviousValue = async (field: string, message: string, state: Record<string, any>) => {
55
+ const previousValue = {
56
+ value: state[field],
57
+ message: message,
58
+ };
59
+ zodFormControl.previousState[field] = previousValue;
60
+ };
61
+
62
+ const pushCustomErrors = async (apiErrorResponse: ApiErrorResponse, state: Record<string, any>) => {
63
+ const apiErrors = transformErrorMessages(apiErrorResponse.data.errors);
64
+
65
+ // 1: Create a ZodError object to hold the issues
66
+ const zodError = new ZodError([]);
67
+
68
+ // 2: Reset previous state values
69
+ resetPreviousValues();
70
+
71
+ // 3: Add issues to the ZodError object
72
+ for (const [path, message] of Object.entries(apiErrors)) {
73
+ zodError.addIssue({
74
+ path: path.split('.'),
75
+ message: message as string,
76
+ code: z.ZodIssueCode.custom,
77
+ });
78
+ await updatePreviousValue(path, message as string, state);
79
+ }
80
+
81
+ zodErrorObj.value = zodError.format();
82
+ zodFormControl.errorCount = getErrorCount(zodErrorObj);
83
+ zodFormControl.formIsValid = zodFormControl.errorCount === 0;
84
+ zodFormControl.displayLoader = false;
85
+ zodFormControl.submitAttempted = true;
86
+ scrollToFirstError();
87
+ return zodErrorObj.value;
88
+ };
89
+
90
+ const doZodValidate = async (state: Record<string, any>) => {
91
+ const valid = formSchema.safeParse(toRaw(state));
92
+ if (!valid.success) {
93
+ zodErrorObj.value = valid.error.format();
94
+ } else {
95
+ zodErrorObj.value = null;
96
+ }
97
+
98
+ zodFormControl.errorCount = getErrorCount(zodErrorObj);
99
+ zodFormControl.formIsValid = valid.success;
100
+
101
+ return valid.success;
102
+ };
103
+
104
+ const fieldMaxLength = (name: string) => {
105
+ const fieldSchema = formSchema.shape[name];
106
+ if (fieldSchema instanceof z.ZodString) {
107
+ return fieldSchema._def.checks.find((check) => check.kind === 'max')?.value;
108
+ }
109
+ };
110
+
111
+ const scrollToFirstError = async () => {
112
+ if (formRef.value) {
113
+ const firstErrorElement = formRef.value.querySelector('[aria-invalid=true]');
114
+
115
+ if (firstErrorElement) {
116
+ window.scrollTo({
117
+ top: firstErrorElement?.getBoundingClientRect().y + window.scrollY,
118
+ left: 0,
119
+ behavior: 'smooth',
120
+ });
121
+ }
122
+ }
123
+ };
124
+
125
+ const scrollToFormHead = () => {
126
+ if (formRef.value) {
127
+ const formHead = formRef.value.getBoundingClientRect().top;
128
+ window.scrollTo({
129
+ top: formHead,
130
+ left: 0,
131
+ behavior: 'smooth',
132
+ });
133
+ }
134
+ };
135
+
136
+ return {
137
+ initZodForm,
138
+ zodFormControl,
139
+ zodErrorObj,
140
+ pushCustomErrors,
141
+ doZodValidate,
142
+ fieldMaxLength,
143
+ scrollToFirstError,
144
+ scrollToFormHead,
145
+ };
146
+ };
147
+
148
+ export default useZodValidation;
package/nuxt.config.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  // https://nuxt.com/docs/api/configuration/nuxt-config
2
2
 
3
3
  export default defineNuxtConfig({
4
- future: {
5
- compatibilityVersion: 4,
6
- },
7
4
  devtools: { enabled: true },
8
5
  css: ['modern-normalize', './assets/styles/main.css'],
9
6
  modules: ['@nuxt/icon', '@nuxt/test-utils/module'],
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "srcdev-nuxt-forms",
3
3
  "type": "module",
4
- "version": "3.0.0",
4
+ "version": "4.0.0",
5
5
  "main": "nuxt.config.ts",
6
6
  "scripts": {
7
7
  "clean": "rm -rf .nuxt && rm -rf .output && rm -rf .playground/.nuxt && rm -rf .playground/.output",
@@ -0,0 +1,216 @@
1
+ export interface IValidationPatterns {
2
+ pattern: string;
3
+ minlength: string;
4
+ maxlength: string;
5
+ hint: string;
6
+ }
7
+
8
+ export interface IOptionsConfig {
9
+ id: string;
10
+ name: string;
11
+ value: string;
12
+ label: string;
13
+ }
14
+
15
+ export interface IFormMultipleOptions {
16
+ data: IOptionsConfig[];
17
+ total: number;
18
+ skip: number;
19
+ limit: number;
20
+ }
21
+
22
+ export interface IOptionsValueArr {
23
+ [key: string]: string | boolean | number | URL | object;
24
+ }
25
+
26
+ export interface IFieldsInitialState {
27
+ [key: string]: null | string | boolean | number | URL | object | IOptionsValueArr[];
28
+ }
29
+
30
+ export type TFieldsInitialState = {
31
+ [key: string]: null | string | boolean | number | URL | object | IOptionsValueArr[];
32
+ };
33
+
34
+ export interface IValidityState {
35
+ badInput: boolean;
36
+ customError: boolean;
37
+ patternMismatch: boolean;
38
+ rangeOverflow: boolean;
39
+ rangeUnderflow: boolean;
40
+ stepMismatch: boolean;
41
+ tooLong: boolean;
42
+ tooShort: boolean;
43
+ typeMismatch: boolean;
44
+ valid: boolean;
45
+ valueMissing: boolean;
46
+ }
47
+
48
+ export interface IValidityStateArr {
49
+ [key: string]: {
50
+ badInput: boolean;
51
+ customError: boolean;
52
+ patternMismatch: boolean;
53
+ rangeOverflow: boolean;
54
+ rangeUnderflow: boolean;
55
+ stepMismatch: boolean;
56
+ tooLong: boolean;
57
+ tooShort: boolean;
58
+ typeMismatch: boolean;
59
+ valid: boolean;
60
+ valueMissing: boolean;
61
+ };
62
+ }
63
+
64
+ export interface IFormFieldsState {
65
+ [key: string]: boolean;
66
+ }
67
+
68
+ export interface ICustomErrorMessage {
69
+ useCustomError: boolean;
70
+ message: string;
71
+ }
72
+
73
+ export interface InpuTextC12 {
74
+ label: string;
75
+ placeholder: string;
76
+ errorMessage: string;
77
+ }
78
+
79
+ export interface IErrorMessagesArr {
80
+ [x: string]: ICustomErrorMessage;
81
+ }
82
+
83
+ export interface IFormFieldC12 {
84
+ label: string;
85
+ placeholder: string;
86
+ errorMessage: string;
87
+ useCustomError: boolean;
88
+ customErrors: null | string | string[];
89
+ isValid: boolean;
90
+ isDirty: boolean;
91
+ type: string;
92
+ previousValue: null | string | boolean | number | URL | object;
93
+ }
94
+
95
+ export interface IFormFieldsC12 {
96
+ [x: string]: IFormFieldC12;
97
+ }
98
+
99
+ export interface IFormFieldState {
100
+ isValid: boolean;
101
+ isDirty: boolean;
102
+ previousValue: null | string | boolean | number | URL | object;
103
+ }
104
+
105
+ export interface IFormFieldStateObj {
106
+ [x: string]: IFormFieldState;
107
+ }
108
+
109
+ export interface IFormData {
110
+ [x: string]: string | boolean | number | URL | object;
111
+ data: IFieldsInitialState;
112
+ validityState: IFormFieldsState;
113
+ dirtyFields: IFormFieldsState;
114
+ focusedField: string;
115
+ isPending: boolean;
116
+ errorCount: number;
117
+ errorMessages: IErrorMessagesArr;
118
+ formFieldsC12: IFormFieldsC12;
119
+ formIsValid: boolean;
120
+ submitAttempted: boolean;
121
+ submitDisabled: boolean;
122
+ submitSuccess: boolean;
123
+ displayErrorMessages: boolean;
124
+ }
125
+
126
+ export interface IApiErrorMessages {
127
+ [x: string]: string;
128
+ }
129
+
130
+ // New types
131
+
132
+ export interface C12nInputText {
133
+ type: string;
134
+ id: string;
135
+ name: string;
136
+ label: string;
137
+ placeholder: string;
138
+ errorMessage: string;
139
+ fieldHasError: boolean;
140
+ required: boolean;
141
+ styleClassPassthrough: string[];
142
+ }
143
+
144
+ export interface C12nMultipleCheckboxes {
145
+ id: string;
146
+ name: string;
147
+ label: string;
148
+ legend: string;
149
+ placeholder: string;
150
+ errorMessage: string;
151
+ fieldHasError: boolean;
152
+ required: boolean;
153
+ styleClassPassthrough: string[];
154
+ }
155
+
156
+ export interface C12nInputCheckboxWithLabel {
157
+ id: string;
158
+ name: string;
159
+ required: string;
160
+ label: string;
161
+ placeholder: string;
162
+ errorMessage: string | string[];
163
+ fieldHasError: boolean;
164
+ styleClassPassthrough: string[];
165
+ }
166
+
167
+ export interface C12nInputRange {
168
+ id: string;
169
+ name: string;
170
+ label: string;
171
+ min: number;
172
+ max: number;
173
+ step: number;
174
+ placeholder: string;
175
+ errorMessage: string;
176
+ fieldHasError: boolean;
177
+ required: boolean;
178
+ styleClassPassthrough: string[];
179
+ }
180
+
181
+ export interface C12nInputTextCore {
182
+ type: string;
183
+ id: string;
184
+ name: string;
185
+ label: string;
186
+ placeholder: string;
187
+ errorMessage: string;
188
+ fieldHasError: boolean;
189
+ required: boolean;
190
+ }
191
+
192
+ export interface InputTextWithLabel {
193
+ type: string;
194
+ id: string;
195
+ name: string;
196
+ label: string;
197
+ placeholder: string;
198
+ errorMessage: string;
199
+ fieldHasError: boolean;
200
+ required: boolean;
201
+ styleClassPassthrough: string[];
202
+ deepCssClassPassthrough: string[];
203
+ }
204
+
205
+ export interface ApiErrorResponse {
206
+ url: string;
207
+ statusCode: number;
208
+ statusMessage: string;
209
+ message: string;
210
+ stack: string;
211
+ data: {
212
+ errors: {
213
+ [key: string]: string | string[]; // Index signature for dynamic keys
214
+ };
215
+ };
216
+ }
@@ -0,0 +1,21 @@
1
+ import type { IFieldsInitialState, IFormFieldsState, IFormFieldsC12, IFormFieldC12, IApiErrorMessages, ICustomErrorMessage, IErrorMessagesArr } from '@/types/types.forms';
2
+
3
+ export interface IZodeFormControl {
4
+ [x: string]: string | boolean | number | URL | object;
5
+ data: IFieldsInitialState;
6
+ validityState: IFormFieldsState;
7
+ dirtyFields: IFormFieldsState;
8
+ focusedField: string;
9
+ isDisabled: boolean;
10
+ isPending: boolean;
11
+ errorCount: number;
12
+ errorMessages: IErrorMessagesArr;
13
+ formFieldsC12: IFormFieldsC12;
14
+ formIsValid: boolean;
15
+ submitAttempted: boolean;
16
+ submitDisabled: boolean;
17
+ submitSuccess: boolean;
18
+ displayErrorMessages: boolean;
19
+ displayLoader: boolean;
20
+ submitSuccessful: boolean;
21
+ }