Skip to content

fix: [WOAUILIB-3903] TextField multiline auto-expanding on web #2874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/textField/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useContext, useMemo} from 'react';
import {TextInput as RNTextInput, StyleSheet, Platform} from 'react-native';
import {StyleSheet, Platform} from 'react-native';
import {TextInput as RNTextInput} from './textInput';
import {Constants, ForwardRefInjectedProps} from '../../commons/new';
import {InputProps} from './types';
import {getColorByState} from './Presenter';
Expand Down
1 change: 1 addition & 0 deletions src/components/textField/textInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {TextInput} from 'react-native';
43 changes: 43 additions & 0 deletions src/components/textField/textInput/index.web.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, {type BaseSyntheticEvent, useCallback} from 'react';
import {TextInput as RNTextInput, type TextInputChangeEventData, type TextInputProps, type LayoutChangeEvent} from 'react-native';

const adjustInputHeight = (element: BaseSyntheticEvent<TextInputProps, TextInputProps>['target']) => {
element.style.height = 0;
const newHeight = element.offsetHeight - element.clientHeight + element.scrollHeight;
element.style.height = `${newHeight}px`;
};

// we need this wrapper of TextInput on web because of multiline bug in react-native-web
// https://github.com/necolas/react-native-web/issues/795
export const TextInput = (props: TextInputProps) => {
const {multiline, onChange, onLayout, ...other} = props;

const _onLayout = useCallback((event: LayoutChangeEvent) => {
const element = event?.target;

if (element && multiline) {
adjustInputHeight(element);
}

onLayout?.(event);
}, [multiline, onLayout]);

const _onChange = useCallback((event: BaseSyntheticEvent<TextInputChangeEventData>) => {
const element = event?.target || event?.nativeEvent?.target;

if (element && multiline) {
adjustInputHeight(element);
}

onChange?.(event);
}, [multiline, onChange]);

return (
<RNTextInput
{...other}
multiline={multiline}
onChange={_onChange}
onLayout={_onLayout}
/>
);
};
1 change: 1 addition & 0 deletions webDemo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ const itemsToRender: ItemToRender[] = [
<Incubator.TextField
text70
migrate
multiline
defaultValue={defaultValue}
containerStyle={{marginBottom: 10}}
placeholder="Enter your email..."
Expand Down