Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
Fixes the v-model behavior
Browse files Browse the repository at this point in the history
Changes:
1. Fixes the error when `v-model` was passed as `null` and no listener was attached
2. Adds `autocomplete` property support
3. Some changes in tests
4. Added new example to `dev/MaterialModel.vue`

Closes #26, closes #25
  • Loading branch information
sobolevn committed Nov 11, 2016
1 parent a4ca774 commit 4096400
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
5 changes: 4 additions & 1 deletion dev/MaterialModel.vue
Expand Up @@ -4,6 +4,8 @@

<p>Hello, {{ username }}</p>
<MaterialInput name="name" v-model="username">Your name</MaterialInput>
<MaterialInput name="email" type="email" v-model="email">Your email</MaterialInput>
<p>We will send you a letter, {{ email }}</p>
</form>
</template>

Expand All @@ -14,7 +16,8 @@
name: 'material-model',
data: function () {
return {
username: 'admin'
username: 'admin',
email: ''
}
},
components: {
Expand Down
34 changes: 20 additions & 14 deletions src/components/MaterialInput.vue
Expand Up @@ -12,6 +12,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:required="required"
>
Expand All @@ -27,6 +28,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:required="required"
>
Expand All @@ -42,6 +44,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:max="max"
:min="min"
Expand All @@ -62,6 +65,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:max="max"
:min="min"
Expand All @@ -80,6 +84,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:required="required"
>
Expand All @@ -95,6 +100,7 @@

:readonly="readonly"
:disabled="disabled"
:autocomplete="autocomplete"

:minlength="minlength"
:maxlength="maxlength"
Expand All @@ -116,7 +122,7 @@
computedClasses () {
const classes = []
if (!!this.valueCopy) {
if (this.valueCopy) {
classes.push('material-input--has-value')
}
Expand All @@ -135,14 +141,12 @@
mounted () {
const input = this.$el.querySelector('input')
if (this.value) { // value might not be provided
// Here we are following the Vue2 convention on custom v-model:
// https://github.com/vuejs/vue/issues/2873#issuecomment-223759341
this.valueCopy = this.value
input.addEventListener(
'input', this.handleModelInput, false
)
}
// Here we are following the Vue2 convention on custom v-model:
// https://github.com/vuejs/vue/issues/2873#issuecomment-223759341
this.valueCopy = this.value
input.addEventListener(
'input', this.handleModelInput, false
)
if (this.pattern) {
input.addEventListener(
Expand All @@ -154,11 +158,9 @@
// Unbinding events:
const input = this.$el.querySelector('input')
if (this.value) {
input.removeEventListener(
'input', this.handleModelInput
)
}
input.removeEventListener(
'input', this.handleModelInput
)
if (this.pattern) {
input.removeEventListener(
Expand Down Expand Up @@ -233,6 +235,10 @@
required: {
type: Boolean,
default: true
},
autocomplete: {
type: String,
default: null
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions test/unit/specs/MaterialInput.spec.js
Expand Up @@ -13,18 +13,12 @@ describe('MaterialInput.vue', () => {
.to.equal('text')
})

it('renders correct input type', () => {
it('renders correct custom input type', () => {
const inst = getComponent(MaterialInput, {type: 'number'})
expect(inst.$el.querySelector('input').type)
.to.equal('number')
})

it('does not render input for wrong type', () => {
const inst = getComponent(MaterialInput, {type: 'doesnotexist'})
expect(inst.$el.querySelector('input'))
.to.equal(null)
})

it('copies the value when provided', () => {
const inst = getComponent(MaterialInput, {value: 'some'})
expect(inst.value).to.equal('some')
Expand Down

0 comments on commit 4096400

Please sign in to comment.