Skip to content

Commit 9b677ee

Browse files
authored
fix: [WOAUILIB-3903] TextField multiline auto-expanding on web (#2874)
* fix: [WOAUILIB-3903] TextField multiline auto-expanding on web * small fixes
1 parent e94e3fb commit 9b677ee

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/components/textField/Input.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {useContext, useMemo} from 'react';
2-
import {TextInput as RNTextInput, StyleSheet, Platform} from 'react-native';
2+
import {StyleSheet, Platform} from 'react-native';
3+
import {TextInput as RNTextInput} from './textInput';
34
import {Constants, ForwardRefInjectedProps} from '../../commons/new';
45
import {InputProps} from './types';
56
import {getColorByState} from './Presenter';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {TextInput} from 'react-native';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, {type BaseSyntheticEvent, useCallback} from 'react';
2+
import {TextInput as RNTextInput, type TextInputChangeEventData, type TextInputProps, type LayoutChangeEvent} from 'react-native';
3+
4+
const adjustInputHeight = (element: BaseSyntheticEvent<TextInputProps, TextInputProps>['target']) => {
5+
element.style.height = 0;
6+
const newHeight = element.offsetHeight - element.clientHeight + element.scrollHeight;
7+
element.style.height = `${newHeight}px`;
8+
};
9+
10+
// we need this wrapper of TextInput on web because of multiline bug in react-native-web
11+
// https://github.com/necolas/react-native-web/issues/795
12+
export const TextInput = (props: TextInputProps) => {
13+
const {multiline, onChange, onLayout, ...other} = props;
14+
15+
const _onLayout = useCallback((event: LayoutChangeEvent) => {
16+
const element = event?.target;
17+
18+
if (element && multiline) {
19+
adjustInputHeight(element);
20+
}
21+
22+
onLayout?.(event);
23+
}, [multiline, onLayout]);
24+
25+
const _onChange = useCallback((event: BaseSyntheticEvent<TextInputChangeEventData>) => {
26+
const element = event?.target || event?.nativeEvent?.target;
27+
28+
if (element && multiline) {
29+
adjustInputHeight(element);
30+
}
31+
32+
onChange?.(event);
33+
}, [multiline, onChange]);
34+
35+
return (
36+
<RNTextInput
37+
{...other}
38+
multiline={multiline}
39+
onChange={_onChange}
40+
onLayout={_onLayout}
41+
/>
42+
);
43+
};

webDemo/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ const itemsToRender: ItemToRender[] = [
216216
<Incubator.TextField
217217
text70
218218
migrate
219+
multiline
219220
defaultValue={defaultValue}
220221
containerStyle={{marginBottom: 10}}
221222
placeholder="Enter your email..."

0 commit comments

Comments
 (0)