-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVSFSwitch.vue
More file actions
100 lines (85 loc) · 3.18 KB
/
VSFSwitch.vue
File metadata and controls
100 lines (85 loc) · 3.18 KB
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
<template>
<Field
v-slot="props"
v-model="modelValue"
:name="field.name"
:syncVModel="true"
type="checkbox"
:unchecked-value="field.falseValue"
:validate-on-blur="fieldValidateOn === 'blur'"
:validate-on-change="fieldValidateOn === 'change'"
:validate-on-input="fieldValidateOn === 'input'"
:validate-on-model-update="false"
:value="field.trueValue"
>
<v-switch
v-bind="{ ...(boundSettings as Omit<Settings, 'validateOn'>), ...props.field }"
:data-cy="`vsf-field-${field.name}`"
:density="fieldDensity"
:disabled="isValidating"
:error="props.errorMessage ? props.errorMessage?.length > 0 : false"
:error-messages="props.errorMessage"
@input="fieldValidateOn === 'input' ? onActions((props.validate as ValidateFieldResult), 'input') : undefined"
@update:model-value="fieldValidateOn === 'blur' || fieldValidateOn === 'change' ? onActions(props.validate, 'click') : undefined"
>
<template #label>
<FieldLabel
:label="field.label"
:required="fieldRequired"
/>
</template>
</v-switch>
</Field>
</template>
<script lang="ts" setup>
import { Field } from 'vee-validate';
import type { VSFSwitchProps } from './index';
import type { FieldLabelProps } from '../../shared/FieldLabel.vue';
import type { VSwitch } from 'vuetify/components';
import { useBindingSettings } from '../../../composables/bindings';
import { useOnActions } from '../../../composables/validation';
import FieldLabel from '../../shared/FieldLabel.vue';
const emit = defineEmits(['validate']);
const modelValue = defineModel<any>();
const props = defineProps<VSFSwitchProps>();
const { field } = toRefs(props);
const settings = inject<Ref<Settings>>('settings')!;
const fieldDensity = computed<VSwitch['density']>(() => (field.value?.density ?? settings.value?.density) as VSwitch['density']);
const fieldRequired = computed<FieldLabelProps['required']>(() => {
return field.value.required || false;
});
const fieldValidateOn = computed(() => field.value?.validateOn ?? settings.value.validateOn);
const originalValue = modelValue.value;
onUnmounted(() => {
if (!settings.value.keepValuesOnUnmount) {
modelValue.value = originalValue;
}
});
// ------------------------- Validate On Actions //
const isValidating = ref<boolean>(field.value?.disabled as boolean);
async function onActions(validate: ValidateFieldResult, action: ValidateAction): Promise<void> {
if (!isValidating.value) {
isValidating.value = true;
await useOnActions({
action: field.value?.autoPage ? 'click' : action,
emit,
field: field.value,
settingsValidateOn: settings.value.validateOn,
validate,
}).then(() => {
isValidating.value = false;
});
}
}
// -------------------------------------------------- Bound Settings //
const bindSettings = computed(() => ({
...field.value,
color: field.value.color || settings.value.color,
density: field.value.density || settings.value.density,
falseValue: field.value.falseValue || false,
hideDetails: field.value.hideDetails || settings.value.hideDetails,
trueValue: field.value.trueValue || true,
}));
const boundSettings = computed(() => useBindingSettings(bindSettings.value as Partial<Settings>));
</script>
<style lang="scss" scoped></style>