Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for masking when input value is established on component mount #4

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default {
inserted: (el) => {
el = core.getInputElement(el)
const config = el[CONFIG_KEY]

// Attempt to update if the element did not have a value when `bind` was called, but now does
if (typeof config.config.oldValue === 'undefined') core.updateValue(el, null, { force: config.config.prefill })

// prefer adding event listener to parent element to avoid Firefox bug which does not
// execute `useCapture: true` event handlers before non-capturing event handlers
const handlerOwner = el.parentElement || el
Expand Down
68 changes: 67 additions & 1 deletion tests/directive.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import facade from '../src/directive'
import { CONFIG_KEY } from '../src/core'
import { shallowMount } from '@vue/test-utils'
import { mount, shallowMount } from '@vue/test-utils'

describe('Directive', () => {
let wrapper
Expand Down Expand Up @@ -91,6 +91,72 @@ describe('Directive', () => {
expect(wrapper.element.unmaskedValue).toBe('1122')
})

test('Should update element value when mounted', async () => {
const innerComponent = {
name: 'm-input',
template: '<input ref="innerComponent" @input="handleInput" />',
methods: {
handleInput(e) {
this.internalValue = e.target.value
this.$emit('input', e.target.value)
},
updateInputValue(value) {
this.$refs.innerComponent.value = value
}
},
watch: {
value(newVal) {
this.internalValue = newVal
if (this.$refs.innerComponent) this.updateInputValue(this.internalValue)
}
},
mounted: function() {
this.updateInputValue(this.internalValue)
},
props: {
value: String
},
data() {
return {
internalValue: this.value
}
}
}

const outerComponent = {
template: "<m-input ref='outerComponent' v-facade='mask' :value='internalValue' @input='handleInput' />",
directives: { facade },
components: { 'm-input': innerComponent },
watch: {
value() {
this.internalValue = this.value
}
},
methods: {
handleInput(newVal) {
this.internalValue = newVal
}
},
data() {
return {
value: '1234',
internalValue: '1234',
mask: '##.##'
}
}
}

const wrapper = mount(outerComponent)
await wrapper.vm.$nextTick()

expect(wrapper.vm.$refs.outerComponent.$refs.innerComponent.value).toBe('12.34')

wrapper.vm.value = ''
await wrapper.vm.$nextTick()

expect(wrapper.vm.$refs.outerComponent.$refs.innerComponent.value).toBe('')
})

test('Should honor short modifier', async () => {
buildWrapper({
template: `<input v-facade.short="mask" value="12" @input="inputListener" />`
Expand Down