@@ -11,6 +11,9 @@ import {
1111 Text ,
1212 TextInput ,
1313 TextStyle ,
14+ NativeSyntheticEvent ,
15+ TextInputChangeEventData ,
16+ TextInputContentSizeChangeEventData ,
1417} from 'react-native' ;
1518import Icon , { IconsName } from '../Icon' ;
1619
@@ -30,13 +33,15 @@ export interface TextAreaProps extends ViewProps {
3033 /** 是否禁用 */
3134 editable ?: boolean ;
3235 /** 文本域内容变化时触发 */
33- onChange ?: ( val : string ) => void ;
36+ onChangeText ?: ( val : string ) => void ;
3437 /** 文本框中的文字内容 */
3538 value ?: string ;
3639 /** 是否展示字数 */
3740 showWords ?: boolean ;
3841 /** 输入框文字样式 */
3942 fontStyle ?: StyleProp < TextStyle > ;
43+ /** 自适应内容高度 */
44+ autoSize ?: boolean ;
4045}
4146
4247function TextArea ( props : TextAreaProps ) {
@@ -45,28 +50,46 @@ function TextArea(props: TextAreaProps) {
4550 placeholder = '' ,
4651 placeholderTextColor = '#989FB2' ,
4752 numberOfLines = 30 ,
48- onChange = ( ) => { } ,
53+ onChangeText = ( ) => { } ,
4954 maxLength = 50 ,
5055 value = '' ,
5156 editable = true ,
5257 showWords = false ,
58+ autoSize = false ,
5359 style,
5460 fontStyle,
5561 ...other
5662 } = props ;
5763
64+ const [ defaultText , setDefaultText ] = useState < string > ( '' ) ;
65+ const [ height = 0 , setHeight ] = useState < number > ( 0 ) ;
66+
67+ const onChange = ( event : NativeSyntheticEvent < TextInputChangeEventData > ) => {
68+ if ( autoSize ) {
69+ setDefaultText ( event . nativeEvent . text ) ;
70+ }
71+ } ;
72+
73+ const onContentSizeChange = ( event : NativeSyntheticEvent < TextInputContentSizeChangeEventData > ) => {
74+ if ( autoSize ) {
75+ setHeight ( event . nativeEvent . contentSize . height ) ;
76+ }
77+ } ;
78+
5879 return (
5980 < View style = { StyleSheet . flatten ( [ styles . viewBody , style ] ) } >
6081 < View style = { styles . bodyLayout } >
6182 < TextInput
62- style = { [ styles . TextInput , fontStyle ] }
83+ style = { [ styles . TextInput , fontStyle , { height : Math . max ( 35 , height ) } ] }
6384 multiline = { true }
6485 textAlignVertical = { textAlignVertical }
6586 placeholder = { placeholder }
6687 placeholderTextColor = { placeholderTextColor }
6788 numberOfLines = { numberOfLines }
6889 maxLength = { maxLength }
69- onChangeText = { onChange }
90+ onChangeText = { onChangeText }
91+ onChange = { onChange }
92+ onContentSizeChange = { onContentSizeChange }
7093 editable = { editable }
7194 value = { value }
7295 { ...other }
@@ -79,7 +102,7 @@ function TextArea(props: TextAreaProps) {
79102
80103const styles = StyleSheet . create ( {
81104 viewBody : {
82- height : 100 ,
105+ // height: 100,
83106 marginHorizontal : 10 ,
84107 borderColor : 'gray' ,
85108 borderWidth : 0.2 ,
@@ -88,13 +111,13 @@ const styles = StyleSheet.create({
88111 backgroundColor : '#fff' ,
89112 } ,
90113 bodyLayout : {
91- height : '100%' ,
92- flexDirection : 'column' ,
114+ // height: '100%',
115+ // flexDirection: 'column',
93116 justifyContent : 'space-between' ,
94117 padding : 10 ,
95118 } ,
96119 TextInput : {
97- height : '80 %' ,
120+ height : '100 %' ,
98121 } ,
99122 textWords : {
100123 padding : 0 ,
0 commit comments