Skip to content

Commit 145846f

Browse files
author
Dipak Sarkar
committed
updated component and directive
1 parent cc023d8 commit 145846f

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

docs/.vuepress/enhanceApp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* https://v1.vuepress.vuejs.org/guide/basic-config.html#app-level-enhancements
55
*/
66

7-
import number from '../../src'
7+
import number from '../../'
88
import Quasar from 'quasar'
99
import 'quasar/dist/quasar.min.css'
1010

src/component.vue

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default {
1919
value: {
2020
required: true,
2121
type: [Number, String],
22-
default: 0,
2322
},
2423
null_value: {
2524
type: [Number, String],
@@ -62,23 +61,27 @@ export default {
6261
},
6362
6463
watch: {
65-
value: {
64+
masked: {
6665
immediate: true,
67-
handler(newValue) {
68-
const formatted = new NumberFormat(this.$props).format(newValue)
69-
if (formatted !== this.formattedValue) {
70-
this.formattedValue = formatted
71-
}
66+
deep: true,
67+
handler() {
68+
// console.log('src/component.vue:watch()', val)
69+
const number = new NumberFormat(this.$props).clean()
70+
this.$emit('input', this.masked ? this.formattedValue : number.unformat(this.value))
7271
},
7372
},
7473
},
7574
7675
methods: {
7776
change(evt) {
77+
// console.log('src/component.vue:change()', evt.target.value)
7878
const number = new NumberFormat(this.$props).clean()
79-
console.log('number.unformat(evt.target.value)', number.unformat(evt.target.value));
8079
this.$emit('input', this.masked ? number.format(evt.target.value) : number.unformat(evt.target.value))
8180
},
8281
},
82+
mounted() {
83+
// console.log('src/component.vue:created()', this.value)
84+
this.formattedValue = new NumberFormat(this.$props).format(this.value)
85+
},
8386
}
8487
</script>

src/directive.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function (el, binding) {
55
const { value } = binding
66
if (!value) return false
77
const config = Object.assign(options, value)
8-
// console.log('src/components/directive:config', config)
8+
// console.log('src/components/directive:init()', config)
99

1010
// v-number used on a component that's not a input element
1111
if (el.localName !== 'input') {
@@ -21,28 +21,29 @@ export default function (el, binding) {
2121
el.setAttribute('type', 'text')
2222

2323
el.oninput = () => {
24-
// console.log('oninput()')
24+
// console.log('src/directive.js:oninput()', evt)
2525
var positionFromEnd = el.value.length - el.selectionEnd
2626
el.value = new NumberFormat(config).format(el.value)
2727
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
2828
positionFromEnd = el.value.length - positionFromEnd
2929
positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1)
3030
setCursor(el, positionFromEnd)
31-
// el.dispatchEvent(new Event('change'))
3231
}
3332

3433
el.onblur = () => {
3534
// clean up after end the input
35+
// console.log('src/directive.js:onblur()')
3636
el.value = new NumberFormat(config).clean().format(el.value)
3737
el.dispatchEvent(new Event('change'))
3838
}
3939

4040
el.onfocus = () => {
41-
// console.log('onfocus()')
41+
// console.log('src/directive.js:onfocus()')
4242
setCursor(el, el.value.length - config.suffix.length)
4343
}
4444

4545
el.onkeydown = (evt) => {
46+
// console.log('src/directive.js:onkeydown()')
4647
// Check deciaml
4748
if (evt.key === config.decimal && evt.target.value.includes(config.decimal)) {
4849
evt.preventDefault()
@@ -75,5 +76,4 @@ export default function (el, binding) {
7576
// force format after initialization
7677
el.oninput()
7778
el.dispatchEvent(new Event('input'))
78-
el.dispatchEvent(new Event('change'))
7979
}

0 commit comments

Comments
 (0)