Skip to content

Commit f400bdc

Browse files
committed
Merge branch 'dev'
2 parents 88d6abf + d7a2115 commit f400bdc

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

packages/core/src/TextArea/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ function Demo() {
193193
export default Demo
194194
```
195195

196+
### 允许拖拽
197+
198+
```jsx mdx:preview&background=#bebebe29
199+
import React from 'react';
200+
import TextArea from '@uiw/react-native/lib/TextArea';
201+
202+
function Demo() {
203+
return (
204+
<TextArea
205+
draggable={true}
206+
fontStyle={{ color:'#aaa' }}
207+
value={"Hello TextArea \nplease input word"}
208+
placeholder='请输入'
209+
/>
210+
)
211+
}
212+
export default Demo
213+
```
214+
196215
### props
197216

198217
组件继承 [`TextInput`](https://www.react-native.cn/docs/textinput)
@@ -210,3 +229,5 @@ export default Demo
210229
| `showWords` | 是否展示字数 | `boolean` | `false` |
211230
| `fontStyle` | 输入框文字样式 | ` StyleProp<TextStyle>` | - |
212231
| `height` | 输入框高度 | `number` | 0 |
232+
| `draggable` | 是否允许拖拽 | `boolean` | `false` |
233+

packages/core/src/TextArea/index.tsx

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { number } from 'prop-types';
2-
import React, { useState, useRef } from 'react';
1+
import React, { useState, useRef, useEffect } from 'react';
32
import {
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+
1821
import Icon, { IconsName } from '../Icon';
1922

2023
export 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

4854
function 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

107131
const 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

Comments
 (0)