Skip to content
This repository was archived by the owner on Apr 25, 2020. It is now read-only.

Commit f1b9dc0

Browse files
committed
Add prop for additional condition to show label
Fix #4
1 parent a526363 commit f1b9dc0

File tree

5 files changed

+92
-40
lines changed

5 files changed

+92
-40
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ to meet your design requirements:
143143
}
144144
```
145145

146+
Set `:on` prop if you need additional condition for label activation:
147+
148+
```vue
149+
<template>
150+
<float-label :on="isActive">
151+
<input type="text" placeholder="Inactive">
152+
</float-label>
153+
</template>
154+
155+
<script>
156+
export default {
157+
computed: {
158+
isActive () {
159+
return false
160+
}
161+
},
162+
components: {
163+
FloatLabel
164+
}
165+
}
166+
</script>
167+
```
168+
146169
## Development
147170

148171
1. Clone the repository:

components/FloatLabel.vue

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,48 @@
1010
<script>
1111
export default {
1212
name: 'float-label',
13+
props: {
14+
on: {
15+
type: Boolean,
16+
default: true
17+
}
18+
},
1319
data () {
1420
return {
1521
formEl: undefined,
16-
hasValue: false,
17-
isActive: false
22+
isActive: false,
23+
isFocused: false
1824
}
1925
},
2026
mounted () {
2127
this.formEl = this.$el.querySelector('input, textarea, select')
22-
this.formEl.addEventListener('input', this.updateHasValue)
2328
this.formEl.addEventListener('input', this.updateIsActive)
24-
this.formEl.addEventListener('blur', this.updateIsActive)
25-
this.formEl.addEventListener('focus', this.updateIsActive)
29+
this.formEl.addEventListener('input', this.updateIsFocused)
30+
this.formEl.addEventListener('blur', this.updateIsFocused)
31+
this.formEl.addEventListener('focus', this.updateIsFocused)
2632
},
27-
beforyDestroy () {
28-
this.formEl.removeEventListener('input', this.updateHasValue)
33+
beforeDestroy () {
2934
this.formEl.removeEventListener('input', this.updateIsActive)
30-
this.formEl.removeEventListener('blur', this.updateIsActive)
31-
this.formEl.removeEventListener('focus', this.updateIsActive)
35+
this.formEl.removeEventListener('input', this.updateIsFocused)
36+
this.formEl.removeEventListener('blur', this.updateIsFocused)
37+
this.formEl.removeEventListener('focus', this.updateIsFocused)
3238
},
3339
methods: {
3440
focusFormEl () {
3541
this.formEl.focus()
3642
},
37-
updateHasValue (e) {
38-
this.hasValue = e.target.value.length > 0
39-
},
4043
updateIsActive (e) {
41-
this.isActive = e.target === document.activeElement && this.hasValue
44+
this.isActive = e.target.value.length > 0
45+
},
46+
updateIsFocused (e) {
47+
this.isFocused = e.target === document.activeElement && this.isActive
4248
}
4349
},
4450
computed: {
4551
classObject () {
4652
return {
47-
'vfl-label-on-input': this.hasValue,
48-
'vfl-label-on-focus': this.isActive
53+
'vfl-label-on-input': this.on && this.isActive,
54+
'vfl-label-on-focus': this.isFocused
4955
}
5056
},
5157
formElType () {

demo/Demo.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<input type="password" placeholder="Password">
1414
</float-label>
1515

16+
<float-label :on="isActive">
17+
<input type="text" placeholder="Inactive">
18+
</float-label>
19+
1620
<float-label>
1721
<textarea placeholder="Comment"></textarea>
1822
</float-label>
@@ -36,6 +40,17 @@
3640
</div>
3741
</template>
3842

43+
<script>
44+
export default {
45+
name: 'demo',
46+
computed: {
47+
isActive () {
48+
return false
49+
}
50+
}
51+
}
52+
</script>
53+
3954
<style>
4055
.container {
4156
width: 150px;

test/components/FloatLabel.test.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ test('formElType', () => {
1818
expect(ctorSelect().formElType).toEqual('select')
1919
})
2020

21-
test('classObject', () => {
22-
const vm = ctorInput()
23-
const classObjectKeys = Object.keys(vm.classObject)
24-
expect(classObjectKeys).toContain('vfl-label-on-input')
25-
expect(classObjectKeys).toContain('vfl-label-on-focus')
21+
describe('classObject', () => {
22+
test('contain onInput and onFocus classes', () => {
23+
const vm = ctorInput()
24+
const classObjectKeys = Object.keys(vm.classObject)
25+
expect(classObjectKeys).toContain('vfl-label-on-input')
26+
expect(classObjectKeys).toContain('vfl-label-on-focus')
27+
})
28+
29+
test('onInput to be false when on prop is false', () => {
30+
const vm = ctorInput({ on: false })
31+
expect(vm.classObject['vfl-label-on-input']).toBe(false)
32+
})
2633
})
2734

2835
test('focusFormEl', () => {
@@ -32,49 +39,49 @@ test('focusFormEl', () => {
3239
expect(vm.formEl.focus).toHaveBeenCalledTimes(1)
3340
})
3441

35-
describe('updateHasValue', () => {
36-
test('hasValue is true when element value isnt empty', () => {
42+
describe('updateIsActive', () => {
43+
test('isActive is true when element value isnt empty', () => {
3744
const vm = ctorInput()
3845
const event = { target: vm.formEl }
3946
vm.formEl.value = 'Foo'
40-
vm.updateHasValue(event)
41-
expect(vm.hasValue).toEqual(true)
47+
vm.updateIsActive(event)
48+
expect(vm.isActive).toEqual(true)
4249
})
4350

44-
test('hasValue is false when element value is empty', () => {
51+
test('isActive is false when element value is empty', () => {
4552
const vm = ctorInput()
4653
const event = { target: vm.formEl }
4754
vm.formEl.value = ''
48-
vm.updateHasValue(event)
49-
expect(vm.hasValue).toEqual(false)
55+
vm.updateIsActive(event)
56+
expect(vm.isActive).toEqual(false)
5057
})
5158
})
5259

53-
describe('updateIsActive', () => {
54-
test('isActive is true when element is focused and has content', () => {
60+
describe('updateIsFocused', () => {
61+
test('isFocused is true when element is focused and is active', () => {
5562
const vm = ctorInput()
5663
const event = { target: vm.formEl }
57-
vm.hasValue = true
64+
vm.isActive = true
5865
vm.formEl.focus()
59-
vm.updateIsActive(event)
66+
vm.updateIsFocused(event)
6067

61-
expect(vm.isActive).toEqual(true)
68+
expect(vm.isFocused).toEqual(true)
6269
})
6370

64-
test('isActive is false when element isnt focused and has content', () => {
71+
test('isFocused is false when element isnt focused and is active', () => {
6572
const vm = ctorInput()
6673
const event = { target: vm.formEl }
67-
vm.hasValue = true
68-
vm.updateIsActive(event)
74+
vm.isActive = true
75+
vm.updateIsFocused(event)
6976

70-
expect(vm.isActive).toEqual(false)
77+
expect(vm.isFocused).toEqual(false)
7178
})
7279

73-
test('isActive is false when element isnt focused and doesnt have content', () => {
80+
test('isFocused is false when element isnt focused and isnt active', () => {
7481
const vm = ctorInput()
7582
const event = { target: vm.formEl }
76-
vm.updateIsActive(event)
83+
vm.updateIsFocused(event)
7784

78-
expect(vm.isActive).toEqual(false)
85+
expect(vm.isFocused).toEqual(false)
7986
})
8087
})

test/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Vue from 'vue'
22
import FloatLabel from 'components/FloatLabel'
33

4-
export const ctorInput = () => {
4+
export const ctorInput = (propsData={}) => {
55
return new Vue({
66
components: { FloatLabel },
7+
propsData,
78
render: h => {
89
return h('float-label', [
910
h('input', {

0 commit comments

Comments
 (0)