1- import React , { FunctionComponent , useRef } from 'react'
1+ import React , { forwardRef , useRef , useImperativeHandle } from 'react'
22import classNames from 'classnames'
33import Taro from '@tarojs/taro'
44import { BaseEventOrig , Textarea , View } from '@tarojs/components'
@@ -19,9 +19,7 @@ const defaultProps = {
1919 plain : false ,
2020 status : 'default' ,
2121} as TaroTextAreaProps
22- export const TextArea : FunctionComponent < Partial < TaroTextAreaProps > > = (
23- props
24- ) => {
22+ export const TextArea = forwardRef ( ( props : Partial < TaroTextAreaProps > , ref ) => {
2523 const { locale } = useConfig ( )
2624 const {
2725 className,
@@ -54,20 +52,21 @@ export const TextArea: FunctionComponent<Partial<TaroTextAreaProps>> = (
5452 return value
5553 }
5654
57- const [ inputValue , setInputValue ] = usePropsValue < string > ( {
55+ const [ innerValue , setInnerValue ] = usePropsValue < string > ( {
5856 value,
5957 defaultValue,
6058 finalValue : format ( defaultValue ) ,
6159 onChange,
6260 } )
61+ const textareaRef = useRef < HTMLTextAreaElement > ( null )
6362
6463 const handleChange = ( event : BaseEventOrig ) => {
6564 const text = event ?. detail ?. value
6665 if ( text ) {
6766 const value = compositionRef . current ? text : format ( text )
68- setInputValue ( value )
67+ setInnerValue ( value )
6968 } else {
70- setInputValue ( '' )
69+ setInnerValue ( '' )
7170 }
7271 }
7372
@@ -83,6 +82,23 @@ export const TextArea: FunctionComponent<Partial<TaroTextAreaProps>> = (
8382 onBlur ?.( event )
8483 }
8584
85+ useImperativeHandle ( ref , ( ) => {
86+ return {
87+ clear : ( ) => {
88+ setInnerValue ( '' )
89+ } ,
90+ focus : ( ) => {
91+ textareaRef . current ?. focus ( )
92+ } ,
93+ blur : ( ) => {
94+ textareaRef . current ?. blur ( )
95+ } ,
96+ get nativeElement ( ) {
97+ return textareaRef . current
98+ } ,
99+ }
100+ } )
101+
86102 return (
87103 < >
88104 < View
@@ -101,6 +117,7 @@ export const TextArea: FunctionComponent<Partial<TaroTextAreaProps>> = (
101117 >
102118 < Textarea
103119 { ...rest }
120+ ref = { textareaRef }
104121 nativeProps = { {
105122 style,
106123 readOnly,
@@ -118,7 +135,7 @@ export const TextArea: FunctionComponent<Partial<TaroTextAreaProps>> = (
118135 style = { Taro . getEnv ( ) === 'WEB' ? undefined : style }
119136 disabled = { Taro . getEnv ( ) === 'WEB' ? disabled : disabled || readOnly }
120137 // @ts -ignore
121- value = { inputValue }
138+ value = { innerValue }
122139 onInput = { handleChange }
123140 onBlur = { handleBlur }
124141 onFocus = { handleFocus }
@@ -134,12 +151,12 @@ export const TextArea: FunctionComponent<Partial<TaroTextAreaProps>> = (
134151 [ `${ classPrefix } -limit-disabled` ] : disabled ,
135152 } ) }
136153 >
137- { inputValue . length } /{ maxLength < 0 ? 0 : maxLength }
154+ { innerValue . length } /{ maxLength < 0 ? 0 : maxLength }
138155 </ View >
139156 ) }
140157 </ View >
141158 </ >
142159 )
143- }
160+ } )
144161
145162TextArea . displayName = 'NutTextArea'
0 commit comments