Skip to content

Commit 0850a86

Browse files
committed
updated directive, component and core functions
1 parent d75ca26 commit 0850a86

File tree

9 files changed

+81
-63
lines changed

9 files changed

+81
-63
lines changed

.prettierrc.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

.prettierrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"trailingComma": "es5",
3+
"proseWrap": "always",
4+
"tabWidth": 2,
5+
"printWidth": 130,
6+
"semi": false,
7+
"singleQuote": true
8+
}

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
},
77
"[javascript]": {
88
"editor.defaultFormatter": "esbenp.prettier-vscode"
9-
}
9+
},
10+
"prettier.requireConfig": true
1011
}

docs/.vuepress/components/BaseInput.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<script>
1111
export default {
12-
name: "BaseInput",
12+
name: 'BaseInput',
1313
props: {
1414
modelValue: {
1515
default: undefined,
@@ -19,8 +19,8 @@ export default {
1919
required: false,
2020
},
2121
},
22-
emits: ["update:modelValue"],
23-
};
22+
emits: ['update:modelValue'],
23+
}
2424
</script>
2525

2626
<style scoped></style>

docs/.vuepress/components/PlayGround.vue

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
v-model="price"
88
v-bind="config"
99
class="shadow-sm rounded-md text-base transition-all disabled:cursor-not-allowed disabled:border-gray-300 disabled:text-gray-300 focus:border-primary focus:ring focus:ring-offset-0 focus:ring-primary focus:ring-opacity-50"
10+
@update:model-value="onChange"
11+
@input="onInput"
12+
@focus="onFocus"
13+
@blur="onBlur"
1014
/>
1115
<div class="mt-2">
1216
Number value: <code class="ml-2">{{ price }}</code>
@@ -16,9 +20,12 @@
1620
<div class="font-medium mb-2">Directive</div>
1721
<BaseInput
1822
v-if="updated"
19-
:modelValue="priceDirective"
20-
@update:model-value="(val) => (priceDirective = val)"
23+
v-model="priceDirective"
2124
v-number="config"
25+
@update:model-value="onChange"
26+
@input="onInput"
27+
@focus="onFocus"
28+
@blur="onBlur"
2229
/>
2330
<div class="mt-2">
2431
Value: <code class="ml-2">{{ priceDirective }}</code>
@@ -35,7 +42,7 @@
3542
Export
3643
</button>
3744
<Dialog v-model="exportDialogVisible">
38-
<pre class="white--text m-0" style="margin: 0">{{ config }}</pre>
45+
<pre class="m-0" style="margin: 0">{{ config }}</pre>
3946
</Dialog>
4047
</div>
4148
</div>
@@ -117,5 +124,19 @@ export default {
117124
},
118125
},
119126
},
127+
methods: {
128+
onChange() {
129+
console.log('onChange', arguments)
130+
},
131+
onInput() {
132+
console.log('onInput', arguments)
133+
},
134+
onFocus() {
135+
console.log('onFocus', arguments)
136+
},
137+
onBlur() {
138+
console.log('onBlur', arguments)
139+
},
140+
},
120141
}
121142
</script>

src/component.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import directive from './directive'
1515
import options from './options'
1616
1717
export default {
18+
name: 'Number',
1819
props: {
1920
modelValue: {
2021
required: true,

src/core.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ export function FacadeChangeEvent() {
2626
})
2727
}
2828

29-
/**
30-
* Creates a CustomEvent('blur') with detail = { facade: true }
31-
* used as a way to identify our own blur event
32-
*/
33-
export function FacadeBlurEvent() {
34-
return new CustomEvent('blur', {
35-
bubbles: true,
36-
cancelable: true,
37-
detail: { facade: true },
38-
})
39-
}
40-
4129
/**
4230
* ensure that the element we're attaching to is an input element
4331
* if not try to find an input element in this elements childrens
@@ -90,36 +78,33 @@ export function updateValue(
9078
oldValue = oldValue || ''
9179
currentValue = currentValue || ''
9280

93-
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
94-
let masked = number.format(currentValue)
95-
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
96-
97-
// check value with in range max and min value
98-
if (clean) {
99-
if (Number(config.max) && unmasked > Number(config.max)) {
100-
masked = number.format(config.max)
101-
unmasked = number.unformat(config.max)
102-
} else if (Number(config.min) && unmasked < Number(config.min)) {
103-
masked = number.format(config.min)
104-
unmasked = number.unformat(config.min)
81+
if (force || oldValue !== currentValue) {
82+
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
83+
let masked = number.format(currentValue)
84+
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
85+
86+
// check value with in range max and min value
87+
if (clean) {
88+
if (Number(config.max) && unmasked > Number(config.max)) {
89+
masked = number.format(config.max)
90+
unmasked = number.unformat(config.max)
91+
} else if (Number(config.min) && unmasked < Number(config.min)) {
92+
masked = number.format(config.min)
93+
unmasked = number.unformat(config.min)
94+
}
10595
}
106-
}
10796

108-
if (force || oldValue !== currentValue) {
10997
el[CONFIG_KEY].oldValue = masked
11098
el.unmaskedValue = unmasked
99+
111100
// safari makes the cursor jump to the end if el.value gets assign even if to the same value
112101
if (el.value !== masked) {
113102
el.value = masked
114103
}
115104

116105
// this part needs to be outside the above IF statement for vuetify in firefox
117106
// drawback is that we endup with two's input events in firefox
118-
return (
119-
emit &&
120-
el.dispatchEvent(FacadeInputEvent()) &&
121-
el.dispatchEvent(FacadeChangeEvent())
122-
)
107+
return emit && el.dispatchEvent(FacadeInputEvent())
123108
}
124109
}
125110

@@ -130,8 +115,9 @@ export function updateValue(
130115
*/
131116
export function inputHandler(event) {
132117
const { target, detail } = event
118+
133119
// We dont need to run this method on the event we emit (prevent event loop)
134-
if (detail && detail.facade) {
120+
if (detail?.facade) {
135121
return false
136122
}
137123

@@ -143,6 +129,7 @@ export function inputHandler(event) {
143129
const { oldValue, config } = target[CONFIG_KEY]
144130

145131
updateValue(target, null, { emit: false }, event)
132+
146133
// updated cursor position
147134
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
148135
positionFromEnd = target.value.length - positionFromEnd
@@ -161,16 +148,17 @@ export function inputHandler(event) {
161148
*/
162149
export function blurHandler(event) {
163150
const { target, detail } = event
151+
164152
// We dont need to run this method on the event we emit (prevent event loop)
165-
if (detail && detail.facade) {
153+
if (detail?.facade) {
166154
return false
167155
}
168156

169157
const { oldValue } = target[CONFIG_KEY]
170158

171-
updateValue(target, null, { force: true, clean: true }, event)
159+
updateValue(target, null, { force: true, emit: false, clean: true }, event)
172160

173161
if (oldValue !== target.value) {
174-
target.dispatchEvent(FacadeBlurEvent())
162+
target.dispatchEvent(FacadeChangeEvent())
175163
}
176164
}

src/directive.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ export default {
2222
const handlerOwner = el.parentElement || el
2323

2424
// use anonymous event handler to avoid inadvertently removing masking for all inputs within a container
25-
const oninput = (e) => core.inputHandler(e)
25+
const oninput = (e) => {
26+
if (e.target !== el) {
27+
return
28+
}
29+
core.inputHandler(e, el)
30+
}
2631

2732
handlerOwner.addEventListener('input', oninput, true)
2833

@@ -31,17 +36,24 @@ export default {
3136
// check decimal key and insert to current element
3237
// updated cursor position after format the value
3338
el.onkeydown = (e) => {
34-
if (([110, 190].includes(e.keyCode) || e.key === config.decimal) && !el.value.includes(config.decimal)) {
39+
if (
40+
([110, 190].includes(e.keyCode) || e.key === config.decimal) &&
41+
!el.value.includes(config.decimal)
42+
) {
3543
e.preventDefault()
3644
el.setRangeText(config.decimal)
3745
el.dispatchEvent(new Event('input'))
3846
core.updateCursor(el, el.value.indexOf(config.decimal) + 1)
39-
} else if (([110, 190].includes(e.keyCode) || e.key === config.decimal) && el.value.includes(config.decimal)) {
47+
} else if (
48+
([110, 190].includes(e.keyCode) || e.key === config.decimal) &&
49+
el.value.includes(config.decimal)
50+
) {
4051
e.preventDefault()
4152
}
4253
}
4354

44-
option.cleanup = () => handlerOwner.removeEventListener('input', oninput, true)
55+
option.cleanup = () =>
56+
handlerOwner.removeEventListener('input', oninput, true)
4557
},
4658

4759
updated: (el, { value, oldValue, modifiers }, vnode) => {
@@ -57,5 +69,5 @@ export default {
5769

5870
unmounted: (el) => {
5971
core.getInputElement(el)[CONFIG_KEY].cleanup()
60-
}
72+
},
6173
}

src/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ import vNumber from './directive'
33
import options from './options'
44
import NumberFormat from './number-format'
55

6-
export {
7-
number,
8-
vNumber,
9-
options,
10-
NumberFormat
11-
}
6+
export { number, vNumber, options, NumberFormat }
127

138
export default {
14-
install(app, config) {
9+
install(app, config = {}) {
1510
if (config) {
1611
Object.assign(options, config)
1712
}
1813
app.directive('number', vNumber)
1914
app.component('number', number)
20-
}
15+
},
2116
}

0 commit comments

Comments
 (0)