Skip to content

Commit 6f61808

Browse files
authored
Merge pull request #495 from huqiaoli/dev
fix(TextArea):修改TextArea根据内容自动调整高度
2 parents ea014ef + 6f14507 commit 6f61808

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

example/examples/src/routes/TextArea/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class TextAreaView extends Component<TextAreaProps> {
1414
value1: '只读状态不可输入',
1515
value3: '自定义输入框样式',
1616
value4: '',
17+
value5: '',
1718
};
1819

1920
render() {
@@ -35,6 +36,16 @@ export default class TextAreaView extends Component<TextAreaProps> {
3536
placeholder="默认提示语"
3637
/>
3738
</Card>
39+
<Card title="根据内容自动调整高度" style={styles.card}>
40+
<TextArea
41+
onChange={(value5: string) => {
42+
this.setState({value5});
43+
}}
44+
value={this.state.value5}
45+
placeholder="默认提示语"
46+
autoSize
47+
/>
48+
</Card>
3849
<Card title="展示字数" style={styles.card}>
3950
<TextArea
4051
onChange={(value4: string) => {

packages/core/src/TextArea/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@ function Demo() {
112112
export default Demo
113113
```
114114

115+
### 根据内容自动调整高度
116+
117+
```jsx mdx:preview
118+
import React, { useState } from 'react';
119+
import TextArea from '@uiw/react-native/lib/TextArea';
120+
121+
function Demo() {
122+
const [value, setValue] = useState('')
123+
124+
return (
125+
<TextArea
126+
value={value}
127+
onChange={(value) => {
128+
setValue(value);
129+
}}
130+
placeholder='请输入'
131+
autoSize
132+
/>
133+
)
134+
}
135+
export default Demo
136+
```
137+
115138
### 自定义输入框样式
116139
```jsx mdx:preview
117140
import React, { useState } from 'react';

packages/core/src/TextArea/index.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
Text,
1212
TextInput,
1313
TextStyle,
14+
NativeSyntheticEvent,
15+
TextInputChangeEventData,
16+
TextInputContentSizeChangeEventData,
1417
} from 'react-native';
1518
import 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

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

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

Comments
 (0)