Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 6x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 2x 25x 25x 11x 11x 11x 16x 16x 16x 16x 7x 7x 7x 153x 7x 7x 7x 8x 8x 8x 7x 9x 9x 4x 4x 3x 3x 6x 6x 1x 5x 5x 5x 3x 2x 2x 1x 5x 248x 248x 2x 13x 5x 69x 6x 9x 1x 1x 8x 3x | import { computed, ref } from 'vue';
import { useStore } from 'vuex';
import createLogger from '../utils/logger';
const log = createLogger('formSlideout');
export default function useFormSlideout(
slideoutId,
initialData = {},
options = {},
) {
const store = useStore();
const pristineInitialData = JSON.parse(JSON.stringify(initialData));
const formData = ref(JSON.parse(JSON.stringify(initialData)));
const isOpen = ref(false);
const mode = ref(options.defaultMode || 'create');
const isSubmitting = ref(false);
const validationErrors = ref({});
const config = {
resetOnClose: true,
validateOnSubmit: true,
...options,
};
const isCreateMode = computed(() => mode.value === 'create');
const isEditMode = computed(() => mode.value === 'edit');
const hasErrors = computed(
() => Object.keys(validationErrors.value).length > 0,
);
function toggleSlideout() {
store.commit('slideoutStore/TOGGLE', slideoutId);
isOpen.value = !isOpen.value;
}
function resetForm() {
formData.value = JSON.parse(JSON.stringify(pristineInitialData));
validationErrors.value = {};
isSubmitting.value = false;
}
function openCreate(defaultData = {}) {
mode.value = 'create';
formData.value = JSON.parse(
JSON.stringify({ ...pristineInitialData, ...defaultData }),
);
validationErrors.value = {};
toggleSlideout();
}
function openEdit(data) {
mode.value = 'edit';
const cloned = JSON.parse(JSON.stringify(data));
Object.keys(data).forEach((key) => {
Iif (data[key] instanceof Date) {
cloned[key] = new Date(data[key]);
}
});
formData.value = cloned;
validationErrors.value = {};
toggleSlideout();
}
function closeSlideout() {
store.commit('slideoutStore/TOGGLE', slideoutId);
isOpen.value = false;
if (config.resetOnClose) {
resetForm();
}
}
function validateForm(customValidator = null) {
validationErrors.value = {};
if (customValidator) {
const errors = customValidator(formData.value);
if (errors && Object.keys(errors).length > 0) {
validationErrors.value = errors;
return false;
}
}
return true;
}
async function handleSubmit(submitHandler, customValidator = null) {
if (config.validateOnSubmit && !validateForm(customValidator)) {
return;
}
isSubmitting.value = true;
try {
await submitHandler(formData.value, mode.value);
closeSlideout();
} catch (error) {
log.error('Submit error:', error);
if (error.response?.data?.errors) {
validationErrors.value = error.response.data.errors;
}
} finally {
isSubmitting.value = false;
}
}
function updateField(fieldName, value) {
formData.value[fieldName] = value;
if (validationErrors.value[fieldName]) {
delete validationErrors.value[fieldName];
}
}
function setFieldError(fieldName, errorMessage) {
validationErrors.value[fieldName] = errorMessage;
}
function clearErrors() {
validationErrors.value = {};
}
return {
formData,
isOpen,
mode,
isSubmitting,
validationErrors,
isCreateMode,
isEditMode,
hasErrors,
openCreate,
openEdit,
toggleSlideout,
closeSlideout,
resetForm,
validateForm,
handleSubmit,
updateField,
setFieldError,
clearErrors,
};
}
export const fieldTypes = {
TEXT: 'text',
TEXTAREA: 'textarea',
DROPDOWN: 'dropdown',
RADIO: 'radio',
CHECKBOX: 'checkbox',
CHECKBOX_GROUP: 'checkbox-group',
DATE: 'date',
DATERANGE: 'daterange',
SEARCH: 'search',
TOGGLE: 'toggle',
BUTTON_GROUP: 'button-group',
CUSTOM: 'custom',
};
export function createField(config) {
if (!config.name) {
log.error('Field name is required:', config);
throw new Error('Field name is required');
}
return {
name: config.name,
type: config.type || fieldTypes.TEXT,
label: config.label || '',
placeholder: config.placeholder || '',
required: config.required || false,
disabled: config.disabled || false,
defaultValue: config.defaultValue !== undefined ? config.defaultValue : '',
options: config.options || [],
optionLabel: config.optionLabel || 'label',
optionValue: config.optionValue || 'value',
description: config.description || '',
helperText: config.helperText || '',
errorMessage: config.errorMessage || '',
rows: config.rows || 3,
toggleLabel: config.toggleLabel || '',
...config,
};
}
export function createSection(config) {
return {
name: config.name,
label: config.label,
fields: config.fields || [],
expanded: config.expanded !== undefined ? config.expanded : true,
};
}
|