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

Commit 68f98ea

Browse files
committed
Support v-model, add :dispatch property
Fix #8
1 parent 13c8ecb commit 68f98ea

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Wrap input, textarea or select with `<float-label>`:
9999
<textarea placeholder="Comment"></textarea>
100100
</float-label>
101101

102-
<float-label>
102+
<float-label :dispatch="false">
103103
<select>
104104
<option disabled selected>Framework</option>
105105
<option>Vue</option>
@@ -211,6 +211,32 @@ export default {
211211
</script>
212212
```
213213

214+
Set `:dispatch` to `false` to disable triggering `input` event
215+
once the component is mounted:
216+
217+
_By default it's set to true to activate label when form element has value._
218+
219+
```vue
220+
<template>
221+
<float-label :dispatch="false">
222+
<input type="email" placeholder="Email" v-model="email">
223+
</float-label>
224+
</template>
225+
226+
<script>
227+
export default {
228+
data () {
229+
return {
230+
231+
}
232+
},
233+
components: {
234+
FloatLabel
235+
}
236+
}
237+
</script>
238+
```
239+
214240
## Development
215241

216242
1. Clone the repository:

components/FloatLabel.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export default {
1818
label: {
1919
type: String,
2020
default: ''
21+
},
22+
dispatch: {
23+
type: Boolean,
24+
default: true
2125
}
2226
},
2327
data () {
@@ -33,6 +37,7 @@ export default {
3337
this.formEl.addEventListener('input', this.updateIsFocused)
3438
this.formEl.addEventListener('blur', this.updateIsFocused)
3539
this.formEl.addEventListener('focus', this.updateIsFocused)
40+
this.dispatchInput()
3641
},
3742
beforeDestroy () {
3843
this.formEl.removeEventListener('input', this.updateIsActive)
@@ -41,6 +46,13 @@ export default {
4146
this.formEl.removeEventListener('focus', this.updateIsFocused)
4247
},
4348
methods: {
49+
dispatchInput () {
50+
if (this.dispatch) {
51+
const event = document.createEvent('HTMLEvents')
52+
event.initEvent('input', true, false)
53+
this.formEl.dispatchEvent(event)
54+
}
55+
},
4456
focusFormEl () {
4557
this.formEl.focus()
4658
},

demo/Demo.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</float-label>
77

88
<float-label>
9-
<input type="email" placeholder="Email">
9+
<input type="email" placeholder="Email" v-model="email">
1010
</float-label>
1111

1212
<float-label label="Overridden label">
@@ -21,7 +21,7 @@
2121
<textarea placeholder="Comment"></textarea>
2222
</float-label>
2323

24-
<float-label>
24+
<float-label :dispatch="false">
2525
<select>
2626
<option disabled selected>Framework</option>
2727
<option>Vue</option>
@@ -53,6 +53,7 @@ export default {
5353
name: 'demo',
5454
data () {
5555
return {
56+
5657
version: 'beta',
5758
options: [
5859
{ value: 'alpha', text: 'Alpha' },
@@ -89,10 +90,6 @@ select {
8990
width: 100%;
9091
}
9192
92-
.example {
93-
margin-top: 2em;
94-
}
95-
9693
.example .vfl-label {
9794
text-transform: uppercase;
9895
}

test/components/FloatLabel.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ describe('classObject', () => {
2525
expect(classObjectKeys).toContain('vfl-label-on-input')
2626
expect(classObjectKeys).toContain('vfl-label-on-focus')
2727
})
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-
})
3328
})
3429

3530
test('focusFormEl', () => {
@@ -89,3 +84,6 @@ describe('updateIsFocused', () => {
8984
test(':label', () => {
9085
expect(ctorInput({ label: 'Foobar' }).floatLabel).toEqual('Foobar')
9186
})
87+
88+
xtest(':on')
89+
xtest(':dispatch')

0 commit comments

Comments
 (0)