@@ -10,8 +10,8 @@ import {
10
10
InputEventDetail ,
11
11
KeyboardHeightEventDetail ,
12
12
} from "types/input"
13
- import { ENV_TYPE , getEnv } from "@tarojs/taro"
14
13
import { uuid } from "../../utils/common"
14
+ import { useModelValue } from "../../composables/model"
15
15
16
16
type PickAtInputProps = Pick < AtInputProps , 'maxLength' | 'disabled' | 'password' >
17
17
type GetInputPropsReturn = PickAtInputProps & Pick < InputProps , 'type' >
@@ -115,15 +115,11 @@ const AtInput = defineComponent({
115
115
} ,
116
116
confirmType : {
117
117
type : String as ( ) => AtInputProps [ "confirmType" ] ,
118
- default : 'done' as AtInputProps [ "confirmType" ] ,
118
+ default : 'done' ,
119
119
validator : ( val : string ) => [ "done" , "send" , "search" , "next" , "go" ] . includes ( val )
120
120
} ,
121
121
// events
122
- onChange : {
123
- type : Function as PropType < AtInputProps [ 'onChange' ] > ,
124
- default : ( ) => ( ) => { } ,
125
- required : true
126
- } ,
122
+ onChange : Function as PropType < AtInputProps [ 'onChange' ] > ,
127
123
onBlur : Function as PropType < AtInputProps [ 'onBlur' ] > ,
128
124
onFocus : Function as PropType < AtInputProps [ 'onFocus' ] > ,
129
125
onConfirm : Function as PropType < AtInputProps [ 'onConfirm' ] > ,
@@ -132,11 +128,9 @@ const AtInput = defineComponent({
132
128
onErrorClick : Function as PropType < AtInputProps [ 'onErrorClick' ] >
133
129
} ,
134
130
135
- setup ( props : AtInputProps , { attrs, slots } ) {
136
- const inputValue = ref ( props . value )
131
+ setup ( props : AtInputProps , { attrs, slots, emit } ) {
132
+ const inputValue = useModelValue ( props , emit , ' value' )
137
133
const inputID = ref ( 'weui-input' + uuid ( ) )
138
- const isWEB = ref ( getEnv ( ) === ENV_TYPE . WEB )
139
-
140
134
const inputProps = computed ( ( ) => getInputProps ( props ) )
141
135
142
136
const rootClasses = computed ( ( ) => ( {
@@ -162,23 +156,27 @@ const AtInput = defineComponent({
162
156
'at-input__title--required' : props . required
163
157
} ) )
164
158
165
- watch ( ( ) => props . value , ( val , preVal ) => {
166
- if ( val !== preVal ) {
167
- inputValue . value = val
168
- }
169
- } )
159
+ // watch(() => props.value, (val, preVal) => {
160
+ // if (val !== preVal) {
161
+ // inputValue.value = val
162
+ // }
163
+ // })
170
164
171
165
function handleInput ( e : BaseEventOrig < InputEventDetail > ) {
172
- props . onChange ( e . detail . value , e )
166
+ if ( attrs [ 'onUpdate:value' ] ) {
167
+ inputValue . value = e . detail . value
168
+ } else {
169
+ props . onChange ?.( e . detail . value , e )
170
+ }
173
171
}
174
172
175
173
function handleFocus ( e : BaseEventOrig < FocusEventDetail > ) {
176
174
if ( typeof props . onFocus === 'function' ) {
177
175
props . onFocus ( e . detail . value , e )
178
176
}
179
- if ( isWEB . value ) {
177
+ if ( process . env . TARO_ENV === 'h5' ) {
180
178
// hack fix: h5 点击清除按钮后,input value 在数据层被清除,但视图层仍未清除
181
- inputID . value = 'weui-input' + String ( e . timeStamp ) . replace ( '.' , '' )
179
+ inputID . value = 'weui-input' + uuid ( 10 , 32 )
182
180
}
183
181
}
184
182
@@ -201,14 +199,17 @@ const AtInput = defineComponent({
201
199
}
202
200
203
201
function handleClearValue ( e : ITouchEvent ) {
204
- props . onChange ( '' , e )
202
+ if ( attrs [ 'onUpdate:value' ] ) {
203
+ inputValue . value = ''
204
+ } else {
205
+ props . onChange ?.( '' , e )
206
+ }
205
207
206
208
// hack fix: h5 点击清除按钮后,input value 在数据层被清除,但视图层仍未清除
207
- if ( isWEB . value ) {
209
+ if ( process . env . TARO_ENV === 'h5' ) {
208
210
const inputNode = document . querySelector < HTMLInputElement > ( `#${ inputID . value } > .weui-input` )
209
211
inputNode ! . value = ''
210
212
}
211
-
212
213
}
213
214
214
215
function handleKeyboardHeightChange (
@@ -247,8 +248,9 @@ const AtInput = defineComponent({
247
248
} , { default : ( ) => props . title } )
248
249
) ,
249
250
250
- h ( Input , mergeProps ( isWEB . value ? { id : inputID . value } : { } , {
251
+ h ( Input , {
251
252
class : 'at-input__input' ,
253
+ id : inputID . value ,
252
254
name : props . name ,
253
255
type : inputProps . value . type ,
254
256
password : inputProps . value . password ,
@@ -270,7 +272,7 @@ const AtInput = defineComponent({
270
272
onFocus : handleFocus ,
271
273
onConfirm : handleConfirm ,
272
274
onKeyboardHeightChange : handleKeyboardHeightChange ,
273
- } ) ) ,
275
+ } ) ,
274
276
275
277
( props . clear && props . value ) && (
276
278
h ( View , {
@@ -300,7 +302,7 @@ const AtInput = defineComponent({
300
302
301
303
h ( View , {
302
304
class : 'at-input__children'
303
- } , { default : ( ) => slots . default && slots . default ( ) } )
305
+ } , { default : ( ) => slots . default ?. ( ) } )
304
306
]
305
307
} )
306
308
]
0 commit comments