1
1
import React , { FunctionComponent , useRef } from 'react'
2
2
import classNames from 'classnames'
3
3
import Taro from '@tarojs/taro'
4
- import { Textarea , TextareaProps , View , Text } from '@tarojs/components'
4
+ import {
5
+ BaseEventOrig ,
6
+ Text ,
7
+ Textarea ,
8
+ TextareaProps ,
9
+ View ,
10
+ } from '@tarojs/components'
5
11
import { useConfig , useRtl } from '@/packages/configprovider/index.taro'
6
12
import { BasicComponent , ComponentDefaults } from '@/utils/typings'
7
13
import { usePropsValue } from '@/utils/use-props-value'
@@ -21,8 +27,8 @@ export interface TextAreaProps
21
27
plain : boolean
22
28
status : 'error' | 'default'
23
29
onChange : ( value : string ) => void
24
- onBlur : ( event : Event ) => void
25
- onFocus : ( event : Event ) => void
30
+ onBlur : ( event : BaseEventOrig ) => void
31
+ onFocus : ( event : BaseEventOrig ) => void
26
32
}
27
33
28
34
const defaultProps = {
@@ -77,7 +83,7 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
77
83
onChange,
78
84
} )
79
85
80
- const handleChange = ( event : any ) => {
86
+ const handleChange = ( event : BaseEventOrig ) => {
81
87
const text = event ?. detail ?. value
82
88
if ( text ) {
83
89
const value = compositionRef . current ? text : format ( text )
@@ -87,32 +93,36 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
87
93
}
88
94
}
89
95
90
- const handleFocus = ( event : Event ) => {
91
- if ( disabled ) return
92
- if ( readOnly ) return
93
- onFocus && onFocus ( event )
96
+ const isDisabled = ( ) => disabled || readOnly
97
+
98
+ const handleFocus = ( event : BaseEventOrig ) => {
99
+ if ( isDisabled ( ) ) return
100
+ onFocus ?.( event )
94
101
}
95
102
96
- const handleBlur = ( event : Event ) => {
97
- if ( disabled ) return
98
- if ( readOnly ) return
99
- onBlur && onBlur ( event )
103
+ const handleBlur = ( event : BaseEventOrig ) => {
104
+ if ( isDisabled ( ) ) return
105
+ onBlur ?.( event )
100
106
}
101
107
102
108
return (
103
109
< >
104
110
< View
105
111
className = { classNames (
106
112
classPrefix ,
107
- disabled ? `${ classPrefix } -disabled` : '' ,
108
- readOnly ? `${ classPrefix } -readonly` : '' ,
109
- rtl ? `${ classPrefix } -rtl` : '' ,
110
- plain ? `${ classPrefix } -plain` : `${ classPrefix } -container` ,
111
- status ? `${ classPrefix } -${ status } ` : '' ,
113
+ {
114
+ [ `${ classPrefix } -disabled` ] : disabled ,
115
+ [ `${ classPrefix } -readonly` ] : readOnly ,
116
+ [ `${ classPrefix } -rtl` ] : rtl ,
117
+ [ `${ classPrefix } -plain` ] : plain ,
118
+ [ `${ classPrefix } -container` ] : ! plain ,
119
+ [ `${ classPrefix } -${ status } ` ] : status ,
120
+ } ,
112
121
className
113
122
) }
114
123
>
115
124
< Textarea
125
+ { ...rest }
116
126
nativeProps = { {
117
127
style,
118
128
readOnly,
@@ -124,22 +134,27 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
124
134
compositionRef . current = false
125
135
} ,
126
136
} }
127
- className = { `${ classPrefix } -textarea ${ disabled ? `${ classPrefix } -textarea-disabled` : '' } ` }
137
+ className = { classNames ( `${ classPrefix } -textarea` , {
138
+ [ `${ classPrefix } -textarea-disabled` ] : disabled ,
139
+ } ) }
128
140
style = { Taro . getEnv ( ) === 'WEB' ? undefined : style }
129
141
disabled = { Taro . getEnv ( ) === 'WEB' ? disabled : disabled || readOnly }
130
142
// @ts -ignore
131
143
value = { inputValue }
132
- onInput = { ( e : any ) => handleChange ( e ) }
133
- onBlur = { ( e : any ) => handleBlur ( e ) }
134
- onFocus = { ( e : any ) => handleFocus ( e ) }
144
+ onInput = { handleChange }
145
+ onBlur = { handleBlur }
146
+ onFocus = { handleFocus }
135
147
autoHeight = { autoSize }
136
148
maxlength = { maxLength }
137
- placeholder = { placeholder || locale . placeholder }
138
- { ...rest }
149
+ placeholder = {
150
+ placeholder !== undefined ? placeholder : locale . placeholder
151
+ }
139
152
/>
140
153
{ showCount && (
141
154
< Text
142
- className = { `${ classPrefix } -limit ${ disabled ? `${ classPrefix } -limit-disabled` : '' } ` }
155
+ className = { classNames ( `${ classPrefix } -limit` , {
156
+ [ `${ classPrefix } -limit-disabled` ] : disabled ,
157
+ } ) }
143
158
>
144
159
{ inputValue . length } /{ maxLength < 0 ? 0 : maxLength }
145
160
</ Text >
0 commit comments