2
2
<div class =" float-label" :class =" {'float-label--fade-anim' : shouldFade, 'float-label--on-focus' : props.onFocus, 'float-label--fixed' : isFloated}" ref =" root" >
3
3
<slot ></slot >
4
4
<div v-if =" formElemType === ''" class =" float-label__label float-label--no-click" ><span class =" float-label__label__text" >{{ labelText }}</span ></div >
5
- <label v-else class =" float-label__label" :class =" {'float-label--no-click': formElemType === 'select'}" :for =" formElemId" >
6
- <span class =" float-label__label__bg" v-if =" formElemType === 'textarea'" ></span >
7
- <span class =" float-label__label__text" >{{ labelText }}</span >
8
- </label >
5
+ <label v-else class =" float-label__label" :class =" {'float-label--no-click': formElemType === 'select'}" :for =" formElemId" ><span class =" float-label__label__bg" v-if =" formElemType === 'textarea'" ></span > <span class =" float-label__label__text" >{{ labelText }}</span ></label >
9
6
</div >
10
7
</template >
11
8
@@ -20,13 +17,14 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
20
17
let formElemId = ref (' ' )
21
18
let formElemType = ref (' ' )
22
19
let formElemHasContent = ref (false )
23
- let isValidFormElem = ref (false )
20
+ let isUsableFormElem = ref (false )
21
+ let shouldWatchElem = ref (false )
24
22
25
23
const compatibleFloatElemsQuery = ' [type=date], [type=datetime-local], [type=datetime], [type=email], [type=month], [type=number], [type=password], [type=search], [type=tel], [type=text], [type=time], [type=url], [type=week], textarea, select'
26
24
27
25
const props = defineProps ({
28
26
label: {type: String , default: ' ' },
29
- float: {type: Boolean },
27
+ float: {type: Boolean , default : null },
30
28
onFocus: {type: Boolean , default: false }
31
29
})
32
30
@@ -42,13 +40,13 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
42
40
}
43
41
})
44
42
const isFloated = computed (()=> {
45
- let float = false
43
+ let shouldFloat = false
46
44
if ( props .float ) {
47
- float = true
45
+ shouldFloat = true
48
46
} else {
49
- float = formElemHasContent .value && formElemHasContent .value !== ' 0'
47
+ shouldFloat = formElemHasContent .value && formElemHasContent .value !== ' 0'
50
48
}
51
- return float
49
+ return shouldFloat
52
50
})
53
51
const getLabelText = () => {
54
52
labelText .value = labelComputed .value ? props .label : placeholderText .value
@@ -80,7 +78,7 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
80
78
}
81
79
}
82
80
// Watch for form element input
83
- const watchForFormChanges = () => {
81
+ const setUpWatchForFormChanges = () => {
84
82
if (formElemType .value === ' select' ) {
85
83
formElem .value .addEventListener (' change' , updateIsFloatedOnChange)
86
84
} else {
@@ -105,23 +103,26 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
105
103
106
104
onMounted (() => {
107
105
formElem .value = root .value .querySelector (compatibleFloatElemsQuery)
108
- isValidFormElem .value = formElem .value ? true : false ;
109
- if ( isValidFormElem .value ) {
106
+ isUsableFormElem .value = formElem .value ? true : false ;
107
+ shouldWatchElem .value = props .float === null ? true : false ;
108
+ if ( isUsableFormElem .value ) {
110
109
formElemId .value = getFormElemId ()
111
110
formElemType .value = formElem .value ? formElem .value .tagName .toLowerCase () : ' '
112
111
formElemHasContent .value = formElem .value .value ? true : false
113
112
placeholderText .value = getPlaceholderValue ()
114
113
setMatchingIds ()
115
- setTimeout (() => {
116
- watchForFormChanges ()
117
- }, 200 );
114
+ if (shouldWatchElem .value === true ) {
115
+ setTimeout (() => {
116
+ setUpWatchForFormChanges ()
117
+ }, 200 );
118
+ }
118
119
} else {
119
120
placeholderText .value = ' '
120
121
}
121
122
getLabelText ()
122
123
})
123
124
onBeforeUnmount (() => {
124
- if (isValidFormElem .value ) {
125
+ if (isUsableFormElem .value ) {
125
126
destroyWatchers ()
126
127
}
127
128
})
0 commit comments