1
1
import { VNode } from 'vue'
2
2
import NumberFormat from './number-format'
3
3
4
+ export const MINUS = '-'
5
+
4
6
export type Input = number | string
5
7
6
8
export interface Options {
@@ -12,8 +14,8 @@ export interface Options {
12
14
minimumFractionDigits : number
13
15
prefill : boolean
14
16
reverseFill : boolean
15
- min : number
16
- max : number
17
+ min ? : number
18
+ max ? : number
17
19
nullValue : string
18
20
}
19
21
@@ -42,32 +44,12 @@ export function cloneDeep(data: object) {
42
44
return JSON . parse ( JSON . stringify ( data ) )
43
45
}
44
46
45
- export function getConfig ( el : HTMLInputElement ) {
46
- return JSON . parse ( el . dataset . config as string ) as Config
47
- }
48
-
49
- export function setConfig ( el : HTMLInputElement , config : any ) {
50
- el . dataset . config = JSON . stringify ( config )
51
- }
52
-
53
- /**
54
- * Creates a CustomEvent('input') with detail = { facade: true }
55
- * used as a way to identify our own input event
56
- */
57
- export function FacadeInputEvent ( ) {
58
- return new CustomEvent ( 'input' , {
59
- bubbles : true ,
60
- cancelable : true ,
61
- detail : { facade : true }
62
- } )
63
- }
64
-
65
47
/**
66
- * Creates a CustomEvent('change') with detail = { facade: true }
67
- * used as a way to identify our own change event
48
+ * Creates a CustomEvent with detail = { facade: true }
49
+ * used as a way to identify our own event
68
50
*/
69
- export function FacadeChangeEvent ( ) {
70
- return new CustomEvent ( 'change' , {
51
+ export function FacadeEvent ( event : string ) {
52
+ return new CustomEvent ( event , {
71
53
bubbles : true ,
72
54
cancelable : true ,
73
55
detail : { facade : true }
@@ -139,7 +121,7 @@ export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit
139
121
140
122
// this part needs to be outside the above IF statement for vuetify in firefox
141
123
// drawback is that we endup with two's input events in firefox
142
- return emit && el . dispatchEvent ( FacadeInputEvent ( ) )
124
+ return emit && el . dispatchEvent ( FacadeEvent ( 'input' ) )
143
125
}
144
126
}
145
127
@@ -148,7 +130,7 @@ export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit
148
130
*
149
131
* @param {CustomInputEvent } event The event object
150
132
*/
151
- export function inputHandler ( event : CustomInputEvent , el ?: CustomInputElement ) {
133
+ export function inputHandler ( event : CustomInputEvent ) {
152
134
const { target, detail } = event
153
135
154
136
// We dont need to run this method on the event we emit (prevent event loop)
@@ -180,14 +162,14 @@ export function inputHandler(event: CustomInputEvent, el?: CustomInputElement) {
180
162
181
163
if ( oldValue !== target . value ) {
182
164
target . oldValue = masked
183
- target . dispatchEvent ( FacadeInputEvent ( ) )
165
+ target . dispatchEvent ( FacadeEvent ( 'input' ) )
184
166
}
185
167
}
186
168
187
169
/**
188
170
* Blur event handler
189
171
*/
190
- export function blurHandler ( event : Event , el ?: CustomInputElement ) {
172
+ export function blurHandler ( event : Event ) {
191
173
const { target, detail } = event as CustomInputEvent
192
174
193
175
// We dont need to run this method on the event we emit (prevent event loop)
@@ -201,7 +183,7 @@ export function blurHandler(event: Event, el?: CustomInputElement) {
201
183
202
184
if ( oldValue !== target . value ) {
203
185
target . oldValue = masked
204
- target . dispatchEvent ( FacadeChangeEvent ( ) )
186
+ target . dispatchEvent ( FacadeEvent ( 'change' ) )
205
187
}
206
188
}
207
189
@@ -210,38 +192,34 @@ export function blurHandler(event: Event, el?: CustomInputElement) {
210
192
*/
211
193
export function keydownHandler ( event : KeyboardEvent , el : CustomInputElement ) {
212
194
const { options } = el
213
- const regExp = new RegExp ( `${ options . prefix } |${ options . suffix } ` , 'g' )
195
+ const { prefix, suffix, decimal, min, separator } = options as Options
196
+ const { key } = event
197
+ const regExp = new RegExp ( `${ prefix } |${ suffix } ` , 'g' )
214
198
const newValue = el . value . replace ( regExp , '' )
215
- const canNegativeInput = options . min < 0
216
- if ( ( [ 110 , 190 ] . includes ( event . keyCode ) || event . key === options . decimal ) && newValue . includes ( options . decimal ) ) {
199
+ const canNegativeInput = min === undefined || Number ( min ) < 0 || Number ( min ) !== min
200
+ if ( key === decimal && newValue . includes ( decimal ) ) {
217
201
event . preventDefault ( )
218
- } else if ( [ 109 ] . includes ( event . keyCode ) && ! canNegativeInput ) {
202
+ } else if ( key === MINUS && ! canNegativeInput ) {
219
203
event . preventDefault ( )
220
- } else if ( [ 8 ] . includes ( event . keyCode ) ) {
204
+ } else if ( key === 'Backspace' ) {
221
205
// check current cursor position is after separator when backspace key down
222
206
const selectionEnd = el . selectionEnd || 0
223
207
const character = el . value . slice ( selectionEnd - 1 , selectionEnd )
224
208
const replace = el . value . slice ( selectionEnd - 2 , selectionEnd )
225
- if ( character === options . separator ) {
209
+ let positionFromEnd = el . value . length - selectionEnd
210
+ if ( [ prefix , MINUS , separator ] . includes ( character ) ) {
226
211
event . preventDefault ( )
227
- let positionFromEnd = el . value . length - selectionEnd
228
- // remove separator and before character
229
- el . value = el . value . replace ( replace , '' )
230
- // updated cursor position
231
- if ( options . suffix ) {
232
- positionFromEnd = Math . max ( positionFromEnd , options . suffix . length )
212
+ if ( character === separator ) {
213
+ el . value = el . value . replace ( replace , '' )
214
+ } else {
215
+ el . value = el . value . replace ( RegExp ( `${ prefix } |${ MINUS } ` , 'g' ) , '' )
233
216
}
217
+ positionFromEnd = Math . max ( positionFromEnd , suffix . length )
234
218
positionFromEnd = el . value . length - positionFromEnd
235
- if ( options . prefix ) {
236
- positionFromEnd = Math . max ( positionFromEnd , options . prefix . length )
237
- }
219
+ positionFromEnd = Math . max ( positionFromEnd , prefix . length )
238
220
updateCursor ( el , positionFromEnd )
239
221
// trigger input event
240
222
el . dispatchEvent ( new Event ( 'input' ) )
241
- } else if ( [ options . prefix , '-' ] . includes ( character ) ) {
242
- event . preventDefault ( )
243
- el . value = ''
244
- el . dispatchEvent ( new Event ( 'input' ) )
245
223
}
246
224
}
247
225
}
0 commit comments