11import { VNode } from 'vue'
22import NumberFormat from './number-format'
33
4+ export const MINUS = '-'
5+
46export type Input = number | string
57
68export interface Options {
@@ -12,8 +14,8 @@ export interface Options {
1214 minimumFractionDigits : number
1315 prefill : boolean
1416 reverseFill : boolean
15- min : number
16- max : number
17+ min ? : number
18+ max ? : number
1719 nullValue : string
1820}
1921
@@ -42,32 +44,12 @@ export function cloneDeep(data: object) {
4244 return JSON . parse ( JSON . stringify ( data ) )
4345}
4446
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-
6547/**
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
6850 */
69- export function FacadeChangeEvent ( ) {
70- return new CustomEvent ( 'change' , {
51+ export function FacadeEvent ( event : string ) {
52+ return new CustomEvent ( event , {
7153 bubbles : true ,
7254 cancelable : true ,
7355 detail : { facade : true }
@@ -139,7 +121,7 @@ export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit
139121
140122 // this part needs to be outside the above IF statement for vuetify in firefox
141123 // 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' ) )
143125 }
144126}
145127
@@ -148,7 +130,7 @@ export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit
148130 *
149131 * @param {CustomInputEvent } event The event object
150132 */
151- export function inputHandler ( event : CustomInputEvent , el ?: CustomInputElement ) {
133+ export function inputHandler ( event : CustomInputEvent ) {
152134 const { target, detail } = event
153135
154136 // 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) {
180162
181163 if ( oldValue !== target . value ) {
182164 target . oldValue = masked
183- target . dispatchEvent ( FacadeInputEvent ( ) )
165+ target . dispatchEvent ( FacadeEvent ( 'input' ) )
184166 }
185167}
186168
187169/**
188170 * Blur event handler
189171 */
190- export function blurHandler ( event : Event , el ?: CustomInputElement ) {
172+ export function blurHandler ( event : Event ) {
191173 const { target, detail } = event as CustomInputEvent
192174
193175 // 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) {
201183
202184 if ( oldValue !== target . value ) {
203185 target . oldValue = masked
204- target . dispatchEvent ( FacadeChangeEvent ( ) )
186+ target . dispatchEvent ( FacadeEvent ( 'change' ) )
205187 }
206188}
207189
@@ -210,38 +192,34 @@ export function blurHandler(event: Event, el?: CustomInputElement) {
210192 */
211193export function keydownHandler ( event : KeyboardEvent , el : CustomInputElement ) {
212194 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' )
214198 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 ) ) {
217201 event . preventDefault ( )
218- } else if ( [ 109 ] . includes ( event . keyCode ) && ! canNegativeInput ) {
202+ } else if ( key === MINUS && ! canNegativeInput ) {
219203 event . preventDefault ( )
220- } else if ( [ 8 ] . includes ( event . keyCode ) ) {
204+ } else if ( key === 'Backspace' ) {
221205 // check current cursor position is after separator when backspace key down
222206 const selectionEnd = el . selectionEnd || 0
223207 const character = el . value . slice ( selectionEnd - 1 , selectionEnd )
224208 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 ) ) {
226211 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' ) , '' )
233216 }
217+ positionFromEnd = Math . max ( positionFromEnd , suffix . length )
234218 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 )
238220 updateCursor ( el , positionFromEnd )
239221 // trigger input event
240222 el . dispatchEvent ( new Event ( 'input' ) )
241- } else if ( [ options . prefix , '-' ] . includes ( character ) ) {
242- event . preventDefault ( )
243- el . value = ''
244- el . dispatchEvent ( new Event ( 'input' ) )
245223 }
246224 }
247225}
0 commit comments