Skip to content

Commit

Permalink
fix(v-text-field): fire change event
Browse files Browse the repository at this point in the history
when pressing keydown.enter, the default behavior for an html input is to fire a change event

fixes #2982
  • Loading branch information
johnleider committed May 4, 2018
1 parent ab00c4a commit 47c73e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/VTextField/VTextField.js
Expand Up @@ -14,6 +14,11 @@ import Maskable from '../../mixins/maskable'
// Directives
import Ripple from '../../directives/ripple'

// Utilities
import {
keyCodes
} from '../../util/helpers'

const dirtyTypes = ['color', 'file', 'time', 'date', 'datetime-local', 'week', 'month']

export default {
Expand Down Expand Up @@ -342,8 +347,10 @@ export default {
this.internalValue = e.target.value
this.badInput = e.target.validity && e.target.validity.badInput
},
onKeyDown () {
onKeyDown (e) {
this.internalChange = true

if (e.keyCode === keyCodes.enter) this.$emit('change', this.internalValue)
},
onMouseDown (e) {
// Prevent input from being blurred
Expand Down
16 changes: 16 additions & 0 deletions test/unit/components/VTextField/VTextField.spec.js
Expand Up @@ -729,4 +729,20 @@ test('VTextField.js', ({ mount }) => {
expect(wrapper.element.id).toBe('')
expect(input.element.id).toBe('foo')
})

it('should fire change event when pressing enter', () => {
const wrapper = mount(VTextField)
const input = wrapper.first('input')
const change = jest.fn()

wrapper.vm.$on('change', change)

input.trigger('focus')
input.element.value = 'foo'
input.trigger('input')
input.trigger('keydown.enter')
input.trigger('keydown.enter')

expect(change).toHaveBeenCalledTimes(2)
})
})

0 comments on commit 47c73e6

Please sign in to comment.