1- import { number } from 'prop-types' ;
2- import React , { useState , useRef } from 'react' ;
1+ import React , { useState , useRef , useEffect } from 'react' ;
32import {
43 StyleSheet ,
54 TouchableOpacity ,
@@ -14,7 +13,11 @@ import {
1413 NativeSyntheticEvent ,
1514 TextInputChangeEventData ,
1615 TextInputContentSizeChangeEventData ,
16+ PanResponder ,
17+ PanResponderInstance ,
18+ LayoutChangeEvent ,
1719} from 'react-native' ;
20+
1821import Icon , { IconsName } from '../Icon' ;
1922
2023export interface TextAreaProps extends ViewProps {
@@ -42,11 +45,15 @@ export interface TextAreaProps extends ViewProps {
4245 fontStyle ?: StyleProp < TextStyle > ;
4346 /** 自适应内容高度 */
4447 autoSize ?: boolean ;
48+ /** 初始高度 */
4549 height ?: number ;
50+ /** 是否允许拖拽 */
51+ draggable ?: boolean ;
4652}
4753
4854function TextArea ( props : TextAreaProps ) {
4955 const {
56+ draggable = false ,
5057 textAlignVertical = 'top' ,
5158 placeholder = '' ,
5259 placeholderTextColor = '#989FB2' ,
@@ -64,7 +71,24 @@ function TextArea(props: TextAreaProps) {
6471 } = props ;
6572
6673 const [ defaultText , setDefaultText ] = useState < string > ( '' ) ;
67- const [ height = 0 , setHeight ] = useState < number > ( defaultHeight ) ;
74+ const [ height , setHeight ] = useState < number > ( defaultHeight ) ;
75+ const [ panResponder , setPanResponder ] = useState < PanResponderInstance | undefined > ( ) ;
76+
77+ // Create PanResponder for resizing text box
78+ useEffect ( ( ) => {
79+ if ( draggable ) {
80+ const panResponder = PanResponder . create ( {
81+ onMoveShouldSetPanResponder : ( ) => true ,
82+ onPanResponderMove : ( _ , gestureState ) => {
83+ const { dy } = gestureState ;
84+ setHeight ( Math . max ( height + dy , 35 ) ) ;
85+ } ,
86+ } ) ;
87+ setPanResponder ( panResponder ) ;
88+ } else {
89+ setPanResponder ( undefined ) ;
90+ }
91+ } , [ draggable ] ) ;
6892
6993 const onChangeValue = ( event : NativeSyntheticEvent < TextInputChangeEventData > ) => {
7094 if ( autoSize ) {
@@ -74,12 +98,12 @@ function TextArea(props: TextAreaProps) {
7498
7599 const onContentSizeChange = ( event : NativeSyntheticEvent < TextInputContentSizeChangeEventData > ) => {
76100 if ( autoSize ) {
77- setHeight ( event . nativeEvent . contentSize . height ) ;
101+ setHeight ( event . nativeEvent . contentSize . height + 20 ) ;
78102 }
79103 } ;
80104
81105 return (
82- < View style = { StyleSheet . flatten ( [ styles . viewBody , style ] ) } >
106+ < View style = { [ styles . viewBody , style ] } { ... panResponder ?. panHandlers } >
83107 < View style = { styles . bodyLayout } >
84108 < TextInput
85109 style = { [ styles . TextInput , fontStyle , { height : Math . max ( 35 , height ) } ] }
@@ -98,15 +122,14 @@ function TextArea(props: TextAreaProps) {
98122 value = { value }
99123 { ...other }
100124 />
101- { showWords === true && < Text style = { [ styles . textWords , fontStyle ] } > { value . length + '/' + maxLength } </ Text > }
125+ { showWords && < Text style = { [ styles . textWords , fontStyle ] } > { value . length + '/' + maxLength } </ Text > }
102126 </ View >
103127 </ View >
104128 ) ;
105129}
106130
107131const styles = StyleSheet . create ( {
108132 viewBody : {
109- // height: 100,
110133 marginHorizontal : 10 ,
111134 borderColor : 'gray' ,
112135 borderWidth : 0.2 ,
@@ -115,13 +138,12 @@ const styles = StyleSheet.create({
115138 backgroundColor : '#fff' ,
116139 } ,
117140 bodyLayout : {
118- // height: '100%',
119- // flexDirection: 'column',
120- justifyContent : 'space-between' ,
121141 padding : 10 ,
122142 } ,
123143 TextInput : {
124- height : '100%' ,
144+ backgroundColor : 'transparent' ,
145+ borderWidth : 0 ,
146+ fontSize : 16 ,
125147 } ,
126148 textWords : {
127149 padding : 0 ,
0 commit comments