Skip to content

Commit 37ac221

Browse files
[DEV] refactoring
1 parent 2e6f241 commit 37ac221

File tree

6 files changed

+318
-200
lines changed

6 files changed

+318
-200
lines changed

src/plugin/components/fields/VSFColorField/VSFColorField.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
<script lang="ts" setup>
2929
import type { VSFColorFieldProps } from './index';
3030
import type { FieldLabelProps } from '../../shared/FieldLabel.vue';
31+
import VColorField from '@wdns/vuetify-color-field';
32+
import FieldLabel from '../../shared/FieldLabel.vue';
3133
import { useBindingSettings } from '../../../composables/bindings';
3234
import { useOnActions } from '../../../composables/validation';
33-
import FieldLabel from '../../shared/FieldLabel.vue';
3435
import { Field } from 'vee-validate';;
3536
3637
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<template>
2+
<Field
3+
v-slot="{ errorMessage, validate }"
4+
v-model="modelValue"
5+
:name="field.name"
6+
:validate-on-model-update="false"
7+
>
8+
<component
9+
:is="component"
10+
v-model="modelValue"
11+
v-bind="boundSettings"
12+
:error="errorMessage ? errorMessage?.length > 0 : false"
13+
:error-messages="errorMessage"
14+
:items="fieldItems"
15+
@blur="onActions(validate, 'blur')"
16+
@change="onActions(validate, 'change')"
17+
@input="onActions(validate, 'input')"
18+
>
19+
<template #label>
20+
<FieldLabel
21+
:label="field.label"
22+
:required="fieldRequired"
23+
/>
24+
</template>
25+
</component>
26+
</Field>
27+
28+
<!-- Vuetify Field {{ field }} -->
29+
</template>
30+
31+
32+
<script lang="ts" setup>
33+
import type { VuetifyFieldProps } from './index';
34+
import type { FieldLabelProps } from '../../shared/FieldLabel.vue';
35+
import { useBindingSettings } from '../../../composables/bindings';
36+
import { useOnActions } from '../../../composables/validation';
37+
import FieldLabel from '../../shared/FieldLabel.vue';
38+
import { Field } from 'vee-validate';
39+
40+
41+
const emit = defineEmits(['validate']);
42+
const modelValue = defineModel<any>();
43+
const props = defineProps<VuetifyFieldProps>();
44+
45+
const { field, settings } = props;
46+
47+
const fieldRequired = computed(() => {
48+
const hasRequiredRule = field.rules?.find((rule) => rule.type === 'required');
49+
return field.required || hasRequiredRule as FieldLabelProps['required'];
50+
});
51+
52+
53+
// ------------------------- Validate On Actions //
54+
async function onActions(validate: FieldValidateResult, action: ValidateAction): Promise<void> {
55+
useOnActions({
56+
action,
57+
emit,
58+
field,
59+
settingsValidateOn: settings.validateOn,
60+
validate,
61+
});
62+
}
63+
64+
const fieldItems = computed(() => field?.items ? field.items as any : undefined);
65+
66+
// -------------------------------------------------- Bound Settings //
67+
const bindSettings = computed(() => ({
68+
...field,
69+
color: field.color || settings?.color,
70+
density: field.density || settings?.density,
71+
hideDetails: field.hideDetails || settings?.hideDetails,
72+
variant: field.variant || settings?.variant,
73+
}));
74+
75+
const boundSettings = computed(() => useBindingSettings(bindSettings.value));
76+
</script>
77+
78+
<style lang="scss" scoped></style>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type {
2+
Field,
3+
GlobalChips,
4+
GlobalClosableChips,
5+
GlobalCloseText,
6+
GlobalMultiple,
7+
GlobalVariant,
8+
SharedProps,
9+
} from '@/plugin/types';
10+
import type VuetifyField from './VuetifyField.vue';
11+
12+
13+
interface InternalField extends Omit<Field,
14+
'inline' | 'inlineSpacing' | 'labelPositionLeft'
15+
> {
16+
chips?: GlobalChips;
17+
closableChips?: GlobalClosableChips;
18+
closeText?: GlobalCloseText;
19+
multiple?: GlobalMultiple;
20+
variant?: GlobalVariant;
21+
}
22+
23+
export interface VuetifyFieldProps extends SharedProps {
24+
field: InternalField;
25+
component: string;
26+
}
27+
28+
export type VuetifyField = InstanceType<typeof VuetifyField>;
29+
30+
export default VuetifyField;

src/plugin/components/fields/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import VSFSelect from './VSFSelect/VSFSelect.vue';
1010
import VSFSwitch from './VSFSwitch/VSFSwitch.vue';
1111
import VSFTextarea from './VSFTextarea/VSFTextarea.vue';
1212
import VSFTextField from './VSFTextField/VSFTextField.vue';
13+
import VuetifyField from './VuetifyField/VuetifyField.vue';
1314

1415
export {
1516
VSFAutocomplete,
@@ -24,4 +25,5 @@ export {
2425
VSFSwitch,
2526
VSFTextarea,
2627
VSFTextField,
28+
VuetifyField,
2729
};

src/plugin/components/shared/PageContainer.vue

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@
6868
@validate="onValidate"
6969
/>
7070

71-
<Fields.VSFAutocomplete
71+
<!-- <Fields.VSFAutocomplete
7272
v-if="field.type === 'autocomplete'"
7373
v-model="modelValue[field.name]"
7474
:field="field"
7575
:settings="settings"
7676
@validate="onValidate"
77-
/>
77+
/> -->
7878

7979
<Fields.VSFCombobox
8080
v-if="field.type === 'combobox'"
@@ -93,41 +93,55 @@
9393
@validate="onValidate"
9494
/>
9595

96-
<!-- ================================================== Text Field -->
97-
<Fields.VSFTextField
98-
v-if="field.type === 'text' || field.type === 'textField' || field.type === 'number' || field.type === 'email' || field.type === 'password' || field.type === 'tel' || field.type === 'url'"
96+
<!-- ========================= Color Field -->
97+
<Fields.VSFColorField
98+
v-if="field.type === 'color'"
9999
v-model="modelValue[field.name]"
100100
:field="field"
101101
:settings="settings"
102102
@validate="onValidate"
103103
/>
104104

105-
<!-- ========================= Color Field -->
106-
<Fields.VSFColorField
107-
v-if="field.type === 'color'"
105+
106+
<!-- ! ================================================== Vuetify Field -->
107+
<Fields.VuetifyField
108+
v-if="getComponent(field.type) != null"
108109
v-model="modelValue[field.name]"
110+
:component="getComponent(field.type)"
109111
:field="field"
110112
:settings="settings"
111113
@validate="onValidate"
112114
/>
115+
<!-- ! ================================================== Vuetify Field -->
116+
117+
<!-- ================================================== Text Field -->
118+
<!-- <Fields.VSFTextField
119+
v-if="field.type === 'text' || field.type === 'textField' || field.type === 'number' || field.type === 'email' || field.type === 'password' || field.type === 'tel' || field.type === 'url'"
120+
v-model="modelValue[field.name]"
121+
:field="field"
122+
:settings="settings"
123+
@validate="onValidate"
124+
/> -->
125+
126+
113127

114128
<!-- ========================= File Input -->
115-
<Fields.VSFFileInput
129+
<!-- <Fields.VSFFileInput
116130
v-if="field.type === 'file'"
117131
v-model="modelValue[field.name]"
118132
:field="field"
119133
:settings="settings"
120134
@validate="onValidate"
121-
/>
135+
/> -->
122136

123137
<!-- ================================================== Textarea -->
124-
<Fields.VSFTextarea
138+
<!-- <Fields.VSFTextarea
125139
v-if="field.type === 'textarea'"
126140
v-model="modelValue[field.name]"
127141
:field="field"
128142
:settings="settings"
129143
@validate="onValidate"
130-
/>
144+
/> -->
131145

132146
<!-- ================================================== Custom Field -->
133147
<template v-if="field.type === 'custom'">
@@ -174,6 +188,13 @@ import type {
174188
Page,
175189
Settings,
176190
} from '../../types/index';
191+
import {
192+
VAutocomplete,
193+
VFileInput,
194+
VSelect,
195+
VTextarea,
196+
VTextField,
197+
} from 'vuetify/components';
177198
178199
179200
export interface FieldLabelProps {
@@ -187,6 +208,42 @@ const slots = defineSlots();
187208
const { page } = defineProps<FieldLabelProps>();
188209
189210
211+
const textFields = [
212+
'text',
213+
'textField',
214+
'number',
215+
'email',
216+
'password',
217+
'tel',
218+
'url',
219+
];
220+
221+
function getComponent(fieldType: string): any {
222+
if (fieldType === 'autocomplete') {
223+
return markRaw(VAutocomplete);
224+
}
225+
226+
if (fieldType === 'file') {
227+
return markRaw(VFileInput);
228+
}
229+
230+
if (fieldType === 'select') {
231+
return markRaw(VSelect);
232+
}
233+
234+
if (fieldType === 'textarea') {
235+
return markRaw(VTextarea);
236+
}
237+
238+
if (textFields.includes(fieldType)) {
239+
return markRaw(VTextField);
240+
}
241+
242+
243+
return null;
244+
}
245+
246+
190247
const modelValue = defineModel<any>();
191248
192249
function onValidate(field: Field): void {

0 commit comments

Comments
 (0)