srcdev-nuxt-forms 2.4.23 → 3.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.
- package/nuxt.config.ts +3 -0
- package/package.json +2 -2
- package/assets/styles/ally/_utils.css +0 -20
- package/assets/styles/ally/_variables.css +0 -8
- package/assets/styles/ally/index.css +0 -2
- package/assets/styles/forms/index.css +0 -2
- package/assets/styles/forms/themes/_error.css +0 -85
- package/assets/styles/forms/themes/_ghost.css +0 -85
- package/assets/styles/forms/themes/_input-action-underlined.css +0 -20
- package/assets/styles/forms/themes/_input-action.css +0 -20
- package/assets/styles/forms/themes/_primary.css +0 -90
- package/assets/styles/forms/themes/_secondary.css +0 -85
- package/assets/styles/forms/themes/_success.css +0 -85
- package/assets/styles/forms/themes/_tertiary.css +0 -85
- package/assets/styles/forms/themes/_warning.css +0 -85
- package/assets/styles/forms/themes/index.css +0 -9
- package/assets/styles/forms/variables/_sizes.css +0 -71
- package/assets/styles/forms/variables/_theme.css +0 -11
- package/assets/styles/forms/variables/index.css +0 -2
- package/assets/styles/main.css +0 -5
- package/assets/styles/typography/index.css +0 -2
- package/assets/styles/typography/utils/_font-classes.css +0 -190
- package/assets/styles/typography/utils/_weights.css +0 -69
- package/assets/styles/typography/utils/index.css +0 -2
- package/assets/styles/typography/variables/_colors.css +0 -14
- package/assets/styles/typography/variables/_reponsive-font-size.css +0 -10
- package/assets/styles/typography/variables/index.css +0 -2
- package/assets/styles/utils/_margin-helpers.css +0 -334
- package/assets/styles/utils/_padding-helpers.css +0 -308
- package/assets/styles/utils/_page.css +0 -36
- package/assets/styles/utils/index.css +0 -3
- package/assets/styles/variables/colors/_blue.css +0 -15
- package/assets/styles/variables/colors/_gray.css +0 -16
- package/assets/styles/variables/colors/_green.css +0 -15
- package/assets/styles/variables/colors/_orange.css +0 -15
- package/assets/styles/variables/colors/_red.css +0 -15
- package/assets/styles/variables/colors/_yellow.css +0 -15
- package/assets/styles/variables/colors/colors.css +0 -6
- package/assets/styles/variables/index.css +0 -1
- package/components/forms/c12/prop-validators/index.ts +0 -38
- package/components/forms/c12/utils.ts +0 -14
- package/components/forms/form-errors/InputError.vue +0 -172
- package/components/forms/form-errors/tests/InputError.spec.ts +0 -67
- package/components/forms/input-button/InputButtonCore.vue +0 -191
- package/components/forms/input-button/variants/InputButtonConfirm.vue +0 -66
- package/components/forms/input-button/variants/InputButtonSubmit.vue +0 -62
- package/components/forms/input-checkbox/MultipleCheckboxes.vue +0 -203
- package/components/forms/input-checkbox/SingleCheckbox.vue +0 -169
- package/components/forms/input-checkbox/tests/MultipleCheckboxes.spec.ts +0 -98
- package/components/forms/input-checkbox/tests/data/tags.json +0 -67
- package/components/forms/input-checkbox-radio/InputCheckboxRadioButton.vue +0 -214
- package/components/forms/input-checkbox-radio/InputCheckboxRadioCore.vue +0 -191
- package/components/forms/input-checkbox-radio/InputCheckboxRadioWithLabel.vue +0 -111
- package/components/forms/input-number/InputNumberCore.vue +0 -203
- package/components/forms/input-number/variants/InputNumberDefault.vue +0 -154
- package/components/forms/input-radio/MultipleRadiobuttons.vue +0 -201
- package/components/forms/input-radio/tests/MultipleRadioButtons.spec.ts +0 -89
- package/components/forms/input-radio/tests/data/tags.json +0 -67
- package/components/forms/input-range/InputRangeCore.vue +0 -274
- package/components/forms/input-range/variants/InputRangeDefault.vue +0 -156
- package/components/forms/input-range-fancy/InputRangeFancyCore.vue +0 -450
- package/components/forms/input-range-fancy/InputRangeFancyWithLabel.vue +0 -124
- package/components/forms/input-text/InputTextCore.vue +0 -331
- package/components/forms/input-text/variants/InputPasswordWithLabel.vue +0 -130
- package/components/forms/input-text/variants/InputTextAsNumberWithLabel.vue +0 -187
- package/components/forms/input-text/variants/InputTextWithLabel.vue +0 -298
- package/components/forms/input-textarea/InputTextareaCore.vue +0 -234
- package/components/forms/input-textarea/variants/InputTextareaWithLabel.vue +0 -267
- package/components/forms/toggle-switch/ToggleSwitchCore.vue +0 -216
- package/components/forms/toggle-switch/variants/ToggleSwitchWithLabel.vue +0 -105
- package/components/forms/toggle-switch/variants/ToggleSwitchWithLabelInline.vue +0 -102
- package/components/forms/ui/FormField.vue +0 -78
- package/components/forms/ui/FormWrapper.vue +0 -35
- package/components/utils/colour-scheme-select/ColourSchemeSelect.vue +0 -270
- package/components/utils/colour-scheme-select/ColourSchemeSelectOld.vue +0 -225
- package/components/utils/dark-mode-switcher/DarkModeSwitcher.vue +0 -47
- package/composables/useApiRequest.ts +0 -25
- package/composables/useColourScheme.ts +0 -25
- package/composables/useErrorMessages.ts +0 -59
- package/composables/useFormControl.ts +0 -248
- package/composables/useSleep.ts +0 -5
- package/composables/useStyleClassPassthrough.ts +0 -30
- package/composables/useZodValidation.ts +0 -148
- package/types/types.forms.ts +0 -216
- package/types/types.zodFormControl.ts +0 -21
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export const useColourScheme = () => {
|
|
2
|
-
const currentColourScheme = ref<'auto' | 'dark' | 'light' | null>(null);
|
|
3
|
-
|
|
4
|
-
const returnSavedColourPreferenceFromLocalStorage = () => {
|
|
5
|
-
if (import.meta.client) {
|
|
6
|
-
return localStorage.getItem('colourScheme') as 'auto' | 'dark' | 'light' | null;
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
onMounted(() => {
|
|
11
|
-
currentColourScheme.value = returnSavedColourPreferenceFromLocalStorage() || 'auto';
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
watch(currentColourScheme, (newVal) => {
|
|
15
|
-
if (import.meta.client && newVal !== null) {
|
|
16
|
-
localStorage.setItem('colourScheme', newVal);
|
|
17
|
-
document.documentElement.dataset.colorScheme = newVal;
|
|
18
|
-
currentColourScheme.value = newVal;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
currentColourScheme,
|
|
24
|
-
};
|
|
25
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { IFormData } from '@/types/types.forms';
|
|
2
|
-
|
|
3
|
-
export function useErrorMessage(name: string, formData: Ref<IFormData>) {
|
|
4
|
-
const defaultError = ref('');
|
|
5
|
-
const errorMessages = ref(formData.value.errorMessages);
|
|
6
|
-
|
|
7
|
-
const hasCustomError = () => {
|
|
8
|
-
return errorMessages.value[name] !== undefined && errorMessages.value[name].useCustomError;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const errorMessage = computed(() => {
|
|
12
|
-
console.log(`errorMessage()`);
|
|
13
|
-
if (hasCustomError()) {
|
|
14
|
-
console.log(`errorMessage() | IF`);
|
|
15
|
-
return errorMessages.value[name].message;
|
|
16
|
-
} else {
|
|
17
|
-
return defaultError.value;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const setDefaultError = (newDefaultError: string) => {
|
|
22
|
-
defaultError.value = newDefaultError;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const fieldHasError = computed(() => {
|
|
26
|
-
// console.log(`fieldHasError() | name(${name})`);
|
|
27
|
-
nextTick();
|
|
28
|
-
if (formData.value.submitDisabled) {
|
|
29
|
-
console.log(`fieldHasError() | name(${name}) | IF`);
|
|
30
|
-
if (hasCustomError()) {
|
|
31
|
-
console.log(`fieldHasError() | name(${name}) | IF | IF`);
|
|
32
|
-
|
|
33
|
-
return true;
|
|
34
|
-
} else if (Object.keys(formData.value.validityState).length > 0 && formData.value.validityState[name] !== undefined) {
|
|
35
|
-
console.log(`fieldHasError() | name(${name}) | IF | ELSE IF`);
|
|
36
|
-
|
|
37
|
-
return !formData.value.validityState[name];
|
|
38
|
-
}
|
|
39
|
-
console.log(`fieldHasError() | name(${name}) | IF | ELSE`);
|
|
40
|
-
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const removeCustomError = (valid: boolean = false) => {
|
|
46
|
-
console.log(`useErrorMessage | removeCustomError | name(${name}) | valid(${valid})`);
|
|
47
|
-
// formData.value.validityState[name] = valid;
|
|
48
|
-
// await nextTick();
|
|
49
|
-
// delete formData.value.errorMessages[name];
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
hasCustomError,
|
|
54
|
-
errorMessage,
|
|
55
|
-
setDefaultError,
|
|
56
|
-
fieldHasError,
|
|
57
|
-
removeCustomError,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import type { IFormData, IFieldsInitialState, IFormFieldC12, IApiErrorMessages, ICustomErrorMessage, IErrorMessagesArr } from '@/types/types.forms';
|
|
2
|
-
import { formFieldC12 } from '@/components/forms/c12/utils';
|
|
3
|
-
|
|
4
|
-
// export function useFormControl(name: string = '') {
|
|
5
|
-
export function useFormControl(name: string = '') {
|
|
6
|
-
let savedInitialState = {};
|
|
7
|
-
|
|
8
|
-
const formData = ref<IFormData>({
|
|
9
|
-
data: {} as IFieldsInitialState,
|
|
10
|
-
validityState: {},
|
|
11
|
-
dirtyFields: {},
|
|
12
|
-
focusedField: '',
|
|
13
|
-
isPending: false,
|
|
14
|
-
errorCount: 0,
|
|
15
|
-
errorMessages: {},
|
|
16
|
-
formFieldsC12: {},
|
|
17
|
-
hasCustomErrorMessages: false,
|
|
18
|
-
formIsValid: false,
|
|
19
|
-
submitAttempted: false,
|
|
20
|
-
submitDisabled: false,
|
|
21
|
-
submitSuccess: false,
|
|
22
|
-
displayErrorMessages: false,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const initValidationState = async (fieldsInitialState: IFieldsInitialState | Ref<IFieldsInitialState | null>) => {
|
|
26
|
-
const fields = Object.keys(fieldsInitialState.value || {});
|
|
27
|
-
const state = fields.reduce((accumulatedFields, field) => {
|
|
28
|
-
accumulatedFields[field] = false;
|
|
29
|
-
return accumulatedFields;
|
|
30
|
-
}, {} as Record<string, boolean>);
|
|
31
|
-
formData.value.validityState = state;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const initFormData = async (fieldsInitialState: IFieldsInitialState | Ref<IFieldsInitialState | null>) => {
|
|
35
|
-
initValidationState(fieldsInitialState);
|
|
36
|
-
|
|
37
|
-
if (fieldsInitialState !== null) {
|
|
38
|
-
savedInitialState = toRaw(fieldsInitialState.value) as IFieldsInitialState;
|
|
39
|
-
formData.value.data = fieldsInitialState as IFieldsInitialState;
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const initFormFieldsC12 = (name: string, formFieldC12: IFormFieldC12) => {
|
|
45
|
-
formData.value.formFieldsC12[name] = formFieldC12;
|
|
46
|
-
return;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const updatePreviousValues = () => {
|
|
50
|
-
console.log(`useFormControl | updatePreviousValues`);
|
|
51
|
-
|
|
52
|
-
Object.keys(formData.value.data).forEach((key) => {
|
|
53
|
-
formData.value.formFieldsC12[key].previousValue = formData.value.data[key];
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const getErrorCount = async (updateState: boolean = false) => {
|
|
58
|
-
await nextTick();
|
|
59
|
-
|
|
60
|
-
const errorCount = Object.values(formData.value.validityState).filter((value) => !value).length;
|
|
61
|
-
formData.value.errorCount = errorCount;
|
|
62
|
-
formData.value.formIsValid = errorCount === 0;
|
|
63
|
-
|
|
64
|
-
if (updateState) {
|
|
65
|
-
formData.value.submitDisabled = true;
|
|
66
|
-
formData.value.displayErrorMessages = formData.value.errorCount > 0;
|
|
67
|
-
formData.value.submitAttempted = true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (formData.value.submitDisabled) {
|
|
71
|
-
formData.value.submitDisabled = !formData.value.formIsValid;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// update fieldHasError ref
|
|
75
|
-
// if (typeof formData.value!.formFieldsC12[name] !== 'undefined') {
|
|
76
|
-
// fieldHasError.value = formData.value!.submitAttempted && !formData.value!.formFieldsC12[name].isValid;
|
|
77
|
-
// } else {
|
|
78
|
-
// fieldHasError.value = false;
|
|
79
|
-
// }
|
|
80
|
-
|
|
81
|
-
return formData.value.errorCount;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// Function to count items with "useCustomError" set to true
|
|
85
|
-
const countItemsWithCustomError = (obj: IErrorMessagesArr) => {
|
|
86
|
-
let count = 0;
|
|
87
|
-
|
|
88
|
-
for (const key in obj) {
|
|
89
|
-
if (obj.hasOwnProperty(key) && obj[key].useCustomError === true) {
|
|
90
|
-
count++;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return count;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/*
|
|
98
|
-
* Useage:
|
|
99
|
-
*
|
|
100
|
-
* const { updateErrorMessages } = useFormControl();
|
|
101
|
-
*
|
|
102
|
-
* Add/Update entry
|
|
103
|
-
* const sampleCustomErrorEmail = {
|
|
104
|
-
* useCustomError: true,
|
|
105
|
-
* message: "This is a sample custom error for error EMAIL",
|
|
106
|
-
* };
|
|
107
|
-
* updateErrorMessages("email", sampleCustomErrorEmail);
|
|
108
|
-
*/
|
|
109
|
-
const updateErrorMessages = async (name: string, message: string = '', valid: boolean = false) => {
|
|
110
|
-
if (!valid) {
|
|
111
|
-
// formData.value.validityState[name] = valid;
|
|
112
|
-
// formData.value.errorMessages[name] = {
|
|
113
|
-
// useCustomError: true,
|
|
114
|
-
// message,
|
|
115
|
-
// };
|
|
116
|
-
|
|
117
|
-
formData.value.formFieldsC12[name].useCustomError = true;
|
|
118
|
-
|
|
119
|
-
// if (typeof message === 'string') {
|
|
120
|
-
// formData.value.formFieldsC12[name].customErrors = message;
|
|
121
|
-
// } else if (typeof message === 'object') {
|
|
122
|
-
// formData.value.formFieldsC12[name].customErrors = message;
|
|
123
|
-
// }
|
|
124
|
-
|
|
125
|
-
formData.value.formFieldsC12[name].customErrors = message;
|
|
126
|
-
formData.value.formFieldsC12[name].isValid = valid;
|
|
127
|
-
|
|
128
|
-
// formData.value.errorMessages[name].useCustomError = true;
|
|
129
|
-
// formData.value.errorMessages[name].message = message;
|
|
130
|
-
}
|
|
131
|
-
formData.value.hasCustomErrorMessages = countItemsWithCustomError(formData.value.errorMessages) > 0;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const useApiErrors = async (errors: IApiErrorMessages) => {
|
|
135
|
-
// Object.keys(errors).forEach((key) => {
|
|
136
|
-
// updateErrorMessages(key, errors[key]);
|
|
137
|
-
// });
|
|
138
|
-
|
|
139
|
-
for (const [key, message] of Object.entries(errors)) {
|
|
140
|
-
// console.log(`${key}: ${message}`);
|
|
141
|
-
updateErrorMessages(key, message);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
// const resetForm = () => {
|
|
146
|
-
// console.log('resetForm()');
|
|
147
|
-
// formData.value.data = toRaw(fieldsInitialState.value) as IFieldsInitialState;
|
|
148
|
-
// formData.value.validityState = {};
|
|
149
|
-
// formData.value.errorCount = 0;
|
|
150
|
-
// formData.value.isPending = false;
|
|
151
|
-
// formData.value.errorMessages = {};
|
|
152
|
-
// formData.value.formIsValid = false;
|
|
153
|
-
// };
|
|
154
|
-
|
|
155
|
-
const fieldIsDirty = (name: string) => {
|
|
156
|
-
if (typeof formData.value.formFieldsC12[name] !== 'undefined') {
|
|
157
|
-
return formData.value.formFieldsC12[name].isDirty;
|
|
158
|
-
} else {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
// const fieldHasError = (name: string) => {
|
|
164
|
-
// const currentValidityState = formData.value.validityState[name];
|
|
165
|
-
|
|
166
|
-
// if (formData.value.submitAttempted) {
|
|
167
|
-
// return currentValidityState;
|
|
168
|
-
// }
|
|
169
|
-
// return false;
|
|
170
|
-
// };
|
|
171
|
-
|
|
172
|
-
// const fieldHasError = computed({
|
|
173
|
-
// // getter
|
|
174
|
-
// get() {
|
|
175
|
-
// console.log(`fieldHasError getter: ${name}`);
|
|
176
|
-
// if (typeof formData.value!.formFieldsC12[name] !== 'undefined') {
|
|
177
|
-
// return !formData.value!.formFieldsC12[name].isValid;
|
|
178
|
-
// }
|
|
179
|
-
// return formData.value.validityState[name];
|
|
180
|
-
// },
|
|
181
|
-
// // setter
|
|
182
|
-
// set(newValue) {
|
|
183
|
-
// if (formData.value.submitAttempted) {
|
|
184
|
-
// return newValue;
|
|
185
|
-
// }
|
|
186
|
-
// return false;
|
|
187
|
-
// },
|
|
188
|
-
// });
|
|
189
|
-
|
|
190
|
-
// const fieldHasError = ref(false);
|
|
191
|
-
|
|
192
|
-
const formIsValid = computed(() => {
|
|
193
|
-
return formData.value.formIsValid;
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
const submitDisabled = computed(() => {
|
|
197
|
-
return formData.value.submitDisabled;
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// Keep an eye on this for performance issue
|
|
201
|
-
|
|
202
|
-
// const updateFieldValidity = (name: string, valid: boolean) => {
|
|
203
|
-
// console.log(`updateFieldValidity: name:${name} - valid:${valid}`);
|
|
204
|
-
// console.log(formData.value);
|
|
205
|
-
// // formData.value.formFieldsC12[name].isValid = valid;
|
|
206
|
-
// formData.value.validityState[name] = valid;
|
|
207
|
-
// };
|
|
208
|
-
|
|
209
|
-
watch(
|
|
210
|
-
() => formData.value.validityState,
|
|
211
|
-
() => {
|
|
212
|
-
getErrorCount();
|
|
213
|
-
},
|
|
214
|
-
{ deep: true }
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
watch(
|
|
218
|
-
() => formData.value.formFieldsC12,
|
|
219
|
-
() => {
|
|
220
|
-
formData.value.formFieldsC12;
|
|
221
|
-
},
|
|
222
|
-
{ deep: true }
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
watch(
|
|
226
|
-
() => formData.value.isPending,
|
|
227
|
-
(newValue, oldValue) => {
|
|
228
|
-
if (newValue) {
|
|
229
|
-
updatePreviousValues();
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
return {
|
|
235
|
-
formData,
|
|
236
|
-
initFormData,
|
|
237
|
-
initFormFieldsC12,
|
|
238
|
-
getErrorCount,
|
|
239
|
-
updateErrorMessages,
|
|
240
|
-
// resetForm,
|
|
241
|
-
formIsValid,
|
|
242
|
-
submitDisabled,
|
|
243
|
-
useApiErrors,
|
|
244
|
-
// fieldHasError,
|
|
245
|
-
fieldIsDirty,
|
|
246
|
-
// updateFieldValidity,
|
|
247
|
-
};
|
|
248
|
-
}
|
package/composables/useSleep.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export const useStyleClassPassthrough = (styleClassPassthrough: string[]) => {
|
|
2
|
-
const styleClassPassthroughRef = ref(styleClassPassthrough);
|
|
3
|
-
|
|
4
|
-
const elementClasses = computed(() => {
|
|
5
|
-
return styleClassPassthroughRef.value.join(' ');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
const updateElementClasses = (cssClass: string | string[]) => {
|
|
9
|
-
let cssClasses = [] as string[];
|
|
10
|
-
if (typeof cssClass === 'string') {
|
|
11
|
-
cssClasses = [cssClass];
|
|
12
|
-
} else if (Array.isArray(cssClass)) {
|
|
13
|
-
cssClasses = cssClass;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
cssClasses.forEach((cssClass) => {
|
|
17
|
-
if (styleClassPassthroughRef.value.includes(cssClass)) {
|
|
18
|
-
styleClassPassthroughRef.value = styleClassPassthroughRef.value.filter((className) => className !== cssClass);
|
|
19
|
-
} else {
|
|
20
|
-
styleClassPassthroughRef.value.push(cssClass);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
elementClasses,
|
|
27
|
-
updateElementClasses,
|
|
28
|
-
styleClassPassthroughRef,
|
|
29
|
-
};
|
|
30
|
-
};
|
|
@@ -1,148 +0,0 @@
|
|
|
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;
|