1
- <script setup>
2
- import { computed , useTemplateRef } from ' vue'
3
- import { getFieldComponentName } from ' @/helpers'
4
-
5
- const fieldComponent = useTemplateRef (' fieldComponent' )
6
-
7
- const props = defineProps ({
8
- formOptions: {
9
- type: Object ,
10
- default : () => ({})
11
- },
12
- model: {
13
- type: Object ,
14
- required: true
15
- },
16
- field: {
17
- type: Object ,
18
- required: true
19
- },
20
- errors: {
21
- type: Array ,
22
- default : () => []
23
- }
24
- })
25
-
26
- const emit = defineEmits ([ ' value-updated' , ' validated' ])
27
-
28
- function onInput (value ) {
29
- emit (' value-updated' , { model: props .field .model , value })
30
- }
31
-
32
- function onValidated (isValid , fieldErrors , field ) {
33
- emit (' validated' , { isValid, fieldErrors, field })
34
- }
35
-
36
- /** Computed */
37
- const fieldId = computed (() => {
38
- return ` ${ props .formOptions .idPrefix ? props .formOptions .idPrefix + ' _' : ' ' }${ props .field .name } `
39
- })
40
-
41
- const shouldHaveLabel = computed (() => {
42
- if (fieldComponent .value ? .noLabel || props .field .noLabel === true ) {
43
- return false
44
- }
45
- return props .field .label
46
- })
47
- < / script>
48
-
49
1
<template >
50
- < div class = " form-group" >
2
+ <div class =" form-group" :style = " fieldStyle " >
51
3
<label v-if =" shouldHaveLabel" :for =" fieldId" >
52
4
<span > {{ props.field.label }}</span >
53
5
</label >
@@ -65,14 +17,61 @@ const shouldHaveLabel = computed(() => {
65
17
/>
66
18
</div >
67
19
68
- < div v- if = " fieldComponent && fieldComponent.hint " class = " hints" >
20
+ <div v-if =" fieldComponent && fieldHasHint " class =" hints" >
69
21
<span class =" hint" >{{ fieldComponent.hint }}</span >
70
22
</div >
71
23
72
- < div v- if = " fieldComponent && fieldComponent.errors && fieldComponent.errors.length " class = " errors help-block" >
24
+ <div v-if =" fieldComponent && fieldHasErrors " class =" errors help-block" >
73
25
<template v-for =" error in fieldComponent .errors " :key =" error " >
74
26
<span class =" error" >{{ error }}</span > <br >
75
27
</template >
76
28
</div >
77
29
</div >
78
- < / template>
30
+ </template >
31
+
32
+ <script setup lang="ts">
33
+ import type { ComputedRef , ShallowRef } from ' vue'
34
+ import type { FieldComponent , FormGroupProps } from ' @/resources/types/generic'
35
+ import type { Field } from ' @/resources/types/field/fields'
36
+ import { computed , useTemplateRef } from ' vue'
37
+ import { getFieldComponentName } from ' @/helpers'
38
+
39
+ const fieldComponent = useTemplateRef (' fieldComponent' ) as Readonly <ShallowRef <FieldComponent | undefined >>
40
+
41
+ const props = withDefaults (defineProps <FormGroupProps >(), {
42
+ formOptions : () => ({}),
43
+ errors : () => []
44
+ })
45
+ const emit = defineEmits ([ ' value-updated' , ' validated' ])
46
+
47
+ function onInput (value : any ) {
48
+ emit (' value-updated' , { model: props .field .model , value })
49
+ }
50
+
51
+ function onValidated (isValid : boolean , fieldErrors : string [], field : Field ) {
52
+ emit (' validated' , { isValid , fieldErrors , field })
53
+ }
54
+
55
+ /** Computed */
56
+ const fieldId: ComputedRef <string > = computed (() => {
57
+ return ` ${props .formOptions .idPrefix ? props .formOptions .idPrefix + ' _' : ' ' }${props .field .name } `
58
+ })
59
+
60
+ const fieldStyle: ComputedRef <Record <string , string | undefined >> = computed (() => ({
61
+ display: fieldComponent .value && fieldComponent .value .isVisible ? undefined : ' none'
62
+ }))
63
+
64
+ const fieldHasErrors: ComputedRef <boolean > = computed (() => {
65
+ return Boolean (fieldComponent .value && fieldComponent .value .errors && fieldComponent .value .errors .length )
66
+ })
67
+ const fieldHasHint: ComputedRef <boolean > = computed (() => {
68
+ return Boolean (fieldComponent .value && fieldComponent .value .hint )
69
+ })
70
+
71
+ const shouldHaveLabel: ComputedRef <boolean > = computed (() => {
72
+ if (fieldComponent .value ?.noLabel || props .field .noLabel === true ) {
73
+ return false
74
+ }
75
+ return Boolean (props .field .label )
76
+ })
77
+ </script >
0 commit comments