-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
TextInput: autoexpandable #795
Comments
@gut4 There is a problem with such an implementation. It will break the window scroll occasionally. It's probably much harder to implement with fake height placeholder etc. I had to switch to contentEditable. |
Here is how you do it:
|
@woodpav This with Using the solution proposed by @gut4 worked in both direction for me.
|
I'm curious if this approach translates across platforms, i.e. can we rely on
|
@necolas Are there any plans getting this natively supported by react native web at some point? Could we use https://www.npmjs.com/package/react-textarea-autosize or a similar approach to get closer to the behaviour of the iOS/Android counterparts of TextInput with multiline enabled? |
Ya I have changed the
|
@divonelnc's |
This comment was marked as off-topic.
This comment was marked as off-topic.
@woodpav your answer fails to account for border width. import React, { useState } from 'react';
import { TextInput } from 'react-native';
export default function TextField(props) {
const [scrollHeight, setScrollHeight] = useState(null);
return (
<TextInput
style={{ height: scrollHeight }}
onChange={(e) => setScrollHeight(e.target.offsetHeight - e.target.clientHeight + e.target.scrollHeight)}
{...props}
/>
);
} |
I've taken what's here and adapted it for my own use-case. It seems to work well, resizing upon initialization and any subsequent changes: import * as React from 'react';
import { TextInput } from 'react-native';
export default function App() {
const adjustTextInputSize = (evt) => {
const el = evt?.target || evt?.nativeEvent?.target;
if (el) {
el.style.height = 0;
const newHeight = el.offsetHeight - el.clientHeight + el.scrollHeight;
el.style.height = `${newHeight}px`;
}
};
return (
<TextInput
multiline
onChange={adjustTextInputSize}
onLayout={adjustTextInputSize}
/>
);
} |
@willstepp, your solution works perfectly, but I am struggling with one case on the web expo app. Screen.Recording.2023-06-06.at.21.41.33.mov |
@arhipy97 you can extract this part into a fn:
and pass in a ref to the TextInput so you can either hook into the onSubmit or use an effect to reset it:
|
Albeit some fairly good solutions here, any plans to support this out the box? In both ios and android text input auto resizes given user input - it would seem same should occur on web? @willstepp's solution is closest to the mark. |
@willstepp's solution is the only one that works on the web for me. Here's a Snack demo with the willstepp's solution. The logic is OS-dependent as It still looks a bit buggy on Android (default TextInput behavior with |
In react-native TextInput is always autoexpandable. See facebook/react-native@dabb78b
I think simplest way to implement this is to use https://github.com/rpearce/react-expanding-textarea
It's just update textarea height based on el.scrollHeight:
The text was updated successfully, but these errors were encountered: