Skip to content

Commit 602915a

Browse files
author
Justin Moreland
committed
Float label refinements & clarity
1 parent 6e422b6 commit 602915a

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/components/FloatLabel.vue

+18-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
<div class="float-label" :class="{'float-label--fade-anim' : shouldFade, 'float-label--on-focus' : props.onFocus, 'float-label--fixed' : isFloated}" ref="root">
33
<slot></slot>
44
<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>
96
</div>
107
</template>
118

@@ -20,13 +17,14 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
2017
let formElemId = ref('')
2118
let formElemType = ref('')
2219
let formElemHasContent = ref(false)
23-
let isValidFormElem = ref(false)
20+
let isUsableFormElem = ref(false)
21+
let shouldWatchElem = ref(false)
2422
2523
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'
2624
2725
const props = defineProps({
2826
label: {type: String, default: ''},
29-
float: {type: Boolean},
27+
float: {type: Boolean, default: null},
3028
onFocus: {type: Boolean, default: false}
3129
})
3230
@@ -42,13 +40,13 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
4240
}
4341
})
4442
const isFloated = computed(()=>{
45-
let float = false
43+
let shouldFloat = false
4644
if( props.float ) {
47-
float = true
45+
shouldFloat = true
4846
} else {
49-
float = formElemHasContent.value && formElemHasContent.value !== '0'
47+
shouldFloat = formElemHasContent.value && formElemHasContent.value !== '0'
5048
}
51-
return float
49+
return shouldFloat
5250
})
5351
const getLabelText = () => {
5452
labelText.value = labelComputed.value ? props.label : placeholderText.value
@@ -80,7 +78,7 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
8078
}
8179
}
8280
// Watch for form element input
83-
const watchForFormChanges = () => {
81+
const setUpWatchForFormChanges = () => {
8482
if(formElemType.value === 'select') {
8583
formElem.value.addEventListener('change', updateIsFloatedOnChange)
8684
} else {
@@ -105,23 +103,26 @@ import { ref, computed, onMounted, watch, onBeforeUnmount } from 'vue'
105103
106104
onMounted(() => {
107105
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 ) {
110109
formElemId.value = getFormElemId()
111110
formElemType.value = formElem.value ? formElem.value.tagName.toLowerCase() : ''
112111
formElemHasContent.value = formElem.value.value ? true : false
113112
placeholderText.value = getPlaceholderValue()
114113
setMatchingIds()
115-
setTimeout(() => {
116-
watchForFormChanges()
117-
}, 200);
114+
if(shouldWatchElem.value === true) {
115+
setTimeout(() => {
116+
setUpWatchForFormChanges()
117+
}, 200);
118+
}
118119
} else {
119120
placeholderText.value = ''
120121
}
121122
getLabelText()
122123
})
123124
onBeforeUnmount(() => {
124-
if(isValidFormElem.value) {
125+
if(isUsableFormElem.value) {
125126
destroyWatchers()
126127
}
127128
})

0 commit comments

Comments
 (0)