@@ -2,9 +2,16 @@ import classNames from 'classnames'
2
2
import PropTypes , { InferProps } from 'prop-types'
3
3
import React from 'react'
4
4
import { Input , Label , Text , View } from '@tarojs/components'
5
- import { CommonEvent , ITouchEvent } from '@tarojs/components/types/common'
5
+ import { BaseEventOrig , ITouchEvent } from '@tarojs/components/types/common'
6
6
import { InputProps } from '@tarojs/components/types/Input'
7
- import { AtInputProps } from '../../../types/input'
7
+ import {
8
+ AtInputProps ,
9
+ BlurEventDetail ,
10
+ ConfirmEventDetail ,
11
+ FocusEventDetail ,
12
+ InputEventDetail ,
13
+ KeyboardHeightEventDetail
14
+ } from '../../../types/input'
8
15
9
16
type PickAtInputProps = Pick <
10
17
AtInputProps ,
@@ -38,49 +45,66 @@ function getInputProps(props: AtInputProps): GetInputPropsReturn {
38
45
return actualProps as GetInputPropsReturn
39
46
}
40
47
41
- type ExtendEvent = {
42
- target : {
43
- value : string | number
44
- }
45
- }
46
-
47
48
export default class AtInput extends React . Component < AtInputProps > {
48
49
public static defaultProps : AtInputProps
49
50
public static propTypes : InferProps < AtInputProps >
51
+ // TODO: 有待考证是否为合理方式处理 #840
52
+ private inputClearing = false
50
53
51
- private onInput = ( event : CommonEvent & ExtendEvent ) : void => {
52
- this . props . onChange ( event . target . value , event )
53
- }
54
+ private handleInput = ( event : BaseEventOrig < InputEventDetail > ) : void =>
55
+ this . props . onChange ( event . detail . value , event )
54
56
55
- private onFocus = ( event : CommonEvent & ExtendEvent ) : void => {
56
- this . props . onFocus && this . props . onFocus ( event . target . value , event )
57
+ private handleFocus = ( event : BaseEventOrig < FocusEventDetail > ) : void => {
58
+ if ( typeof this . props . onFocus === 'function' ) {
59
+ this . props . onFocus ( event . detail . value , event )
60
+ }
57
61
}
58
62
59
- private onBlur = ( event : CommonEvent & ExtendEvent ) : void => {
60
- this . props . onBlur && this . props . onBlur ( event . target . value , event )
61
- // fix # 583 AtInput 不触发 onChange 的问题
62
- this . props . onChange ( event . target . value , event )
63
+ private handleBlur = ( event : BaseEventOrig < BlurEventDetail > ) : void => {
64
+ if ( typeof this . props . onBlur === 'function' ) {
65
+ this . props . onBlur ( event . detail . value , event )
66
+ }
67
+ if ( event . type === 'blur' && ! this . inputClearing ) {
68
+ // fix # 583 AtInput 不触发 onChange 的问题
69
+ this . props . onChange (
70
+ event . detail . value ,
71
+ event as BaseEventOrig < InputEventDetail >
72
+ )
73
+ }
74
+ // 还原状态
75
+ this . inputClearing = false
63
76
}
64
77
65
- private onConfirm = ( event : CommonEvent & ExtendEvent ) : void => {
66
- this . props . onConfirm && this . props . onConfirm ( event . target . value , event )
78
+ private handleConfirm = ( event : BaseEventOrig < ConfirmEventDetail > ) : void => {
79
+ if ( typeof this . props . onConfirm === 'function' ) {
80
+ this . props . onConfirm ( event . detail . value , event )
81
+ }
67
82
}
68
83
69
- private onClick = ( ) : void => {
70
- if ( ! this . props . editable ) {
71
- this . props . onClick && this . props . onClick ( )
84
+ private handleClick = ( event : ITouchEvent ) : void => {
85
+ if ( ! this . props . editable && typeof this . props . onClick === 'function' ) {
86
+ this . props . onClick ( event )
72
87
}
73
88
}
74
89
75
- private clearValue = ( event : ITouchEvent ) : void => {
76
- // fix #840
77
- setTimeout ( ( ) => {
78
- this . props . onChange ( '' , event )
79
- } , 50 )
90
+ private handleClearValue = ( event : ITouchEvent ) : void => {
91
+ this . inputClearing = true
92
+ this . props . onChange ( '' , event )
80
93
}
81
94
82
- private onErrorClick = ( ) : void =>
83
- this . props . onErrorClick && this . props . onErrorClick ( )
95
+ private handleKeyboardHeightChange = (
96
+ event : BaseEventOrig < KeyboardHeightEventDetail >
97
+ ) : void => {
98
+ if ( typeof this . props . onKeyboardHeightChange === 'function' ) {
99
+ this . props . onKeyboardHeightChange ( event )
100
+ }
101
+ }
102
+
103
+ private handleErrorClick = ( event : ITouchEvent ) : void => {
104
+ if ( typeof this . props . onErrorClick === 'function' ) {
105
+ this . props . onErrorClick ( event )
106
+ }
107
+ }
84
108
85
109
public render ( ) : JSX . Element {
86
110
const {
@@ -103,10 +127,7 @@ export default class AtInput extends React.Component<AtInputProps> {
103
127
autoFocus,
104
128
focus,
105
129
value,
106
- required,
107
- // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
108
- // @ts -ignore
109
- onKeyboardHeightChange
130
+ required
110
131
} = this . props
111
132
const { type, maxLength, disabled, password } = getInputProps ( this . props )
112
133
@@ -129,7 +150,7 @@ export default class AtInput extends React.Component<AtInputProps> {
129
150
return (
130
151
< View className = { rootCls } style = { customStyle } >
131
152
< View className = { containerCls } >
132
- < View className = { overlayCls } onClick = { this . onClick } > </ View >
153
+ < View className = { overlayCls } onClick = { this . handleClick } > </ View >
133
154
{ title && (
134
155
< Label
135
156
className = { `at-input__title ${
@@ -159,23 +180,24 @@ export default class AtInput extends React.Component<AtInputProps> {
159
180
selectionStart = { selectionStart }
160
181
selectionEnd = { selectionEnd }
161
182
adjustPosition = { adjustPosition }
162
- onInput = { this . onInput }
163
- // fix # 840 input 清除问题
164
- // onChange={this.onInput}
165
- onFocus = { this . onFocus }
166
- onBlur = { this . onBlur }
167
- onConfirm = { this . onConfirm }
183
+ onInput = { this . handleInput }
184
+ onFocus = { this . handleFocus }
185
+ onBlur = { this . handleBlur }
186
+ onConfirm = { this . handleConfirm }
168
187
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
169
188
// @ts -ignore
170
- onKeyboardHeightChange = { onKeyboardHeightChange }
189
+ onKeyboardHeightChange = { this . handleKeyboardHeightChange }
171
190
/>
172
191
{ clear && value && (
173
- < View className = 'at-input__icon' onTouchEnd = { this . clearValue } >
192
+ < View className = 'at-input__icon' onTouchEnd = { this . handleClearValue } >
174
193
< Text className = 'at-icon at-icon-close-circle at-input__icon-close' > </ Text >
175
194
</ View >
176
195
) }
177
196
{ error && (
178
- < View className = 'at-input__icon' onTouchStart = { this . onErrorClick } >
197
+ < View
198
+ className = 'at-input__icon'
199
+ onTouchStart = { this . handleErrorClick }
200
+ >
179
201
< Text className = 'at-icon at-icon-alert-circle at-input__icon-alert' > </ Text >
180
202
</ View >
181
203
) }
0 commit comments