Search code examples
javascriptandroidtypescriptreact-nativetextinput

React Native - Length of placeholder takes precedence in multiline-input's height


Problem:

My text input placeholder could take a max of 2000 characters. As long as my user starts typing in the text input, the placeholder goes away but the text input height does not automatically shrink back down.

AFAIK, the height of my multiline text input is being set based on the original length of my placeholder text. Is there anyway to get around this?

My Code:

import { Input } from 'react-native-elements';

interface Props {
    placeHolder: string;
    onChangeText: (text: string) => void;
}

const MyTextInput = (inputs: Props) => (
    <View>
        <Input
            inputStyle={{ textAlignVertical: 'top' }}
            placeholder={inputs.placeHolder}
            onChangeText={(text) => inputs.onChangeText(text)}
            multiline={true}
            maxLength={2000}
        />
    </View>
);

export default MyTextInput;

Screenshots:

Long placeholder: enter image description here

User inputs text: enter image description here

Text Input height doesn't shrink: enter image description here


Solution

  • In the end, I added an additional value prop such that whenever user starts typing, inputs.onChangeText(text) will update the value, and the height of the input will scale automatically to fit the content. When value is empty or cleared, simply show placeHolder.

    interface Props {
        placeHolder: string;
        onChangeText: (text: string) => void;
        value: string;
    }
    
    const MyTextInput = (inputs: Props) => (
        <View>
            <Input
                inputStyle={{ textAlignVertical: 'top' }}
                value={inputs.value}
                placeholder={inputs.placeHolder}
                onChangeText={(text) => inputs.onChangeText(text)}
                multiline={true}
                maxLength={2000}
            />
        </View>
    );