@@ -52,34 +52,13 @@ export const vModelText: ModelDirective<
5252 'trim' | 'number' | 'lazy'
5353> = {
5454 created ( el , { modifiers : { lazy, trim, number } } , vnode ) {
55- el [ assignKey ] = getModelAssigner ( vnode )
56- const castToNumber =
57- number || ( vnode . props && vnode . props . type === 'number' )
58- addEventListener ( el , lazy ? 'change' : 'input' , e => {
59- if ( ( e . target as any ) . composing ) return
60- let domValue : string | number = el . value
61- if ( trim ) {
62- domValue = domValue . trim ( )
63- }
64- if ( castToNumber ) {
65- domValue = looseToNumber ( domValue )
66- }
67- el [ assignKey ] ( domValue )
68- } )
69- if ( trim ) {
70- addEventListener ( el , 'change' , ( ) => {
71- el . value = el . value . trim ( )
72- } )
73- }
74- if ( ! lazy ) {
75- addEventListener ( el , 'compositionstart' , onCompositionStart )
76- addEventListener ( el , 'compositionend' , onCompositionEnd )
77- // Safari < 10.2 & UIWebView doesn't fire compositionend when
78- // switching focus before confirming composition choice
79- // this also fixes the issue where some browsers e.g. iOS Chrome
80- // fires "change" instead of "input" on autocomplete.
81- addEventListener ( el , 'change' , onCompositionEnd )
82- }
55+ vModelTextInit (
56+ el ,
57+ ( el [ assignKey ] = getModelAssigner ( vnode ) ) ,
58+ trim ,
59+ number || ! ! ( vnode . props && vnode . props . type === 'number' ) ,
60+ lazy ,
61+ )
8362 } ,
8463 // set value on mounted so it's after min/max for type="range"
8564 mounted ( el , { value } ) {
@@ -91,30 +70,81 @@ export const vModelText: ModelDirective<
9170 vnode ,
9271 ) {
9372 el [ assignKey ] = getModelAssigner ( vnode )
94- // avoid clearing unresolved text. #2302
95- if ( ( el as any ) . composing ) return
96- const elValue =
97- ( number || el . type === 'number' ) && ! / ^ 0 \d / . test ( el . value )
98- ? looseToNumber ( el . value )
99- : el . value
100- const newValue = value == null ? '' : value
73+ vModelTextUpdate ( el , value , oldValue , trim , number , lazy )
74+ } ,
75+ }
10176
102- if ( elValue === newValue ) {
103- return
77+ /**
78+ * @internal
79+ */
80+ export const vModelTextInit = (
81+ el : HTMLInputElement | HTMLTextAreaElement ,
82+ set : ( v : any ) => void ,
83+ trim : boolean | undefined ,
84+ number : boolean | undefined ,
85+ lazy : boolean | undefined ,
86+ ) : void => {
87+ addEventListener ( el , lazy ? 'change' : 'input' , e => {
88+ if ( ( e . target as any ) . composing ) return
89+ let domValue : string | number = el . value
90+ if ( trim ) {
91+ domValue = domValue . trim ( )
92+ }
93+ if ( number ) {
94+ domValue = looseToNumber ( domValue )
10495 }
96+ set ( domValue )
97+ } )
98+ if ( trim ) {
99+ addEventListener ( el , 'change' , ( ) => {
100+ el . value = el . value . trim ( )
101+ } )
102+ }
103+ if ( ! lazy ) {
104+ addEventListener ( el , 'compositionstart' , onCompositionStart )
105+ addEventListener ( el , 'compositionend' , onCompositionEnd )
106+ // Safari < 10.2 & UIWebView doesn't fire compositionend when
107+ // switching focus before confirming composition choice
108+ // this also fixes the issue where some browsers e.g. iOS Chrome
109+ // fires "change" instead of "input" on autocomplete.
110+ addEventListener ( el , 'change' , onCompositionEnd )
111+ }
112+ }
105113
106- if ( document . activeElement === el && el . type !== 'range' ) {
107- // #8546
108- if ( lazy && value === oldValue ) {
109- return
110- }
111- if ( trim && el . value . trim ( ) === newValue ) {
112- return
113- }
114+ /**
115+ * @internal
116+ */
117+ export const vModelTextUpdate = (
118+ el : HTMLInputElement | HTMLTextAreaElement ,
119+ value : any ,
120+ oldValue : any ,
121+ trim : boolean | undefined ,
122+ number : boolean | undefined ,
123+ lazy : boolean | undefined ,
124+ ) : void => {
125+ // avoid clearing unresolved text. #2302
126+ if ( ( el as any ) . composing ) return
127+ const elValue =
128+ ( number || el . type === 'number' ) && ! / ^ 0 \d / . test ( el . value )
129+ ? looseToNumber ( el . value )
130+ : el . value
131+ const newValue = value == null ? '' : value
132+
133+ if ( elValue === newValue ) {
134+ return
135+ }
136+
137+ if ( document . activeElement === el && el . type !== 'range' ) {
138+ // #8546
139+ if ( lazy && value === oldValue ) {
140+ return
114141 }
142+ if ( trim && el . value . trim ( ) === newValue ) {
143+ return
144+ }
145+ }
115146
116- el . value = newValue
117- } ,
147+ el . value = newValue
118148}
119149
120150export const vModelCheckbox : ModelDirective < HTMLInputElement > = {
0 commit comments