react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Placeholder's TextInput breaking lines after change on Android

Open RigottiG opened this issue 4 years ago • 21 comments
trafficstars

Description

When component start, works fine. After change value from <TextInput /> and delete it, the placeholder appears, passes 1 second and it break line.

React Native version:

    OS: macOS 11.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 102.36 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.5.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.3.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.3, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 30.0.3
      System Images: android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.6953283
    Xcode: 12.3/12C33 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_242-release - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.2 => 0.63.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

  1. Access snack and set Android
  2. Type on TextInput to disappear placeholder
  3. Break placeholder

Expected Results

Placeholder not break line after disappear

Snack, code example, screenshot, or link to a repository:

https://snack.expo.io/z7tbAWsYc

RigottiG avatar Dec 31 '20 13:12 RigottiG

Was able to repro in the snack.

  1. Initial state image

  2. Type something image

  3. After deleting the text you just typed, the placeholder text no longer respects numberOfLines image

chrisglein avatar Jan 12 '21 19:01 chrisglein

I justed tested this exact code in the RNTester example and I could not reproduce on Android API 30. Please include details of the Android API on which you experience this issue and the rn version.

I have been testing on the latest react-native master. Thanks

CLICK TO OPEN TESTS RESULTS

AFTER
class RewriteExample extends React.Component<$FlowFixMeProps, any> {
  constructor(props) {
    super(props);
    this.state = {text: '', value: ''};
  }
  render() {
    const limit = 20;
    const remainder = limit - this.state.text.length;
    const remainderColor = remainder > 5 ? 'blue' : 'red';
    return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        autoCorrect={false}
        placeholder="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has"
        returnKeyType='search'
        style={styles.input}
        value={this.state.value}
        numberOfLines={1}
        onChangeText={(value) => this.setState({ value })}
      />
    </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    height: 50,
    borderWidth: 1,
    borderColor: "#222",
    borderRadius: 25,
    marginTop: 20,
    paddingLeft: 20,
  },
 input: {
    flex: 1,
    fontSize: 14,
    marginHorizontal: 10,
  },
  default: {
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: '#0f0f0f',
    flex: 1,
    fontSize: 13,
    padding: 4,
  },
  multiline: {
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: '#0f0f0f',
    flex: 1,
    fontSize: 13,
    height: 50,
    padding: 4,
    marginBottom: 4,
  },
  singleLine: {
    fontSize: 16,
  },
  labelContainer: {
    flexDirection: 'row',
    marginVertical: 2,
  },
  label: {
    width: 115,
    alignItems: 'flex-end',
    marginRight: 10,
    paddingTop: 2,
  },
  inputContainer: {
    flex: 1,
  },
  rewriteContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  remainder: {
    textAlign: 'right',
    width: 24,
  },
  hashtag: {
    color: 'blue',
    fontWeight: 'bold',
  },
  eventLabel: {
    margin: 3,
    fontSize: 12,
  },
});

fabOnReact avatar Feb 26 '21 14:02 fabOnReact

Since this does repro in Expo, is the problem that Expo is lagging behind on Android SDK maybe?

chrisglein avatar Mar 16 '21 18:03 chrisglein

Any news here? Facing the same error.

luanccp avatar Jul 19 '21 19:07 luanccp

It is also appear on Android version 10 (real device Samsung A7). React native version 0.64.2

Previously, the TextInput shows: "This is a tex...". But now, the placeholder has multiple lines, which break the App's UI

LuongTruong avatar Aug 14 '21 02:08 LuongTruong

I am also having this issue. Any updates here?

ryekerjh avatar Oct 18 '21 20:10 ryekerjh

anything update?

ghost avatar Oct 22 '21 02:10 ghost

Hi, I'm also running into this issue. Any updates on this by chance?

I don't know if it helps others in the meantime, but I found by forcing the minWidth of the input to be large than to contentSize of the placeholder. For me, that ended up '95%' of the container width (to account for padding). It was not the most ideal solution, but it was the simplest trying to resolve it until this was actually addressed.

SparkleFaerieCoder avatar Nov 09 '21 00:11 SparkleFaerieCoder

i'm also running into this issue. did anyone find a good solution?

jasminesung avatar Mar 07 '22 09:03 jasminesung

Adding a flex: 1 with my 'width' parameters to the style of the TextInput seems to correct the issues in my case.

MagnasiePro avatar Jun 01 '22 09:06 MagnasiePro

anything update?

MarquisAlexander avatar Jan 16 '23 16:01 MarquisAlexander

?

jdziadek avatar Apr 04 '23 14:04 jdziadek

same here

parthasarathi95 avatar Jun 09 '23 08:06 parthasarathi95

i have it with reactnative "0.71.4"

A : untouched , placeholder take 1 line B : input with 199 C : delete 199 => placeholder take 2 lines

Untitled-1

adding min-width: 50px; to TextInput solve it in this specific case

alainib avatar Jul 17 '23 11:07 alainib

Adding a flex: 1 with my 'width' parameters to the style of the TextInput seems to correct the issues in my case.

Thanks THIS WORKED FOR ME

nayan-dey avatar Jul 28 '23 07:07 nayan-dey

Still encountering this issue with longer placeholder text

CleanShot 2023-08-10 at 17 33 45@2x

If placeholder text is contained within input it looks fine CleanShot 2023-08-10 at 17 34 01@2x

Typing within the text input works as expected CleanShot 2023-08-10 at 17 34 26@2x

lyqht avatar Aug 11 '23 00:08 lyqht

I cannot use "flex:1" because i have input + next to it centered...

Capture d’écran 2023-10-09 à 11 08 24

any fix for this ?

the only half working solution is to put "width"

width: ${(props: {value: string}) => props.value?.length > 0 ? props.value?.length * 20 : 125};px;

alainib avatar Oct 09 '23 09:10 alainib

Hack:

const TextInput = ({ style, ...rest }: TextInputProps) => {
  const [initialWidth, setInitialWidth] = useState<number | undefined>();

  const handleOnLayout = useCallback((event: LayoutChangeEvent) => {
    const { width } = event.nativeEvent.layout;
    setInitialWidth((prev) => prev ?? width);
  }, []);

  const dynamicStyle = useMemo<ViewStyle>(
    () => ({
      ...(initialWidth && { minWidth: Math.ceil(initialWidth) + 2 }),
    }),
    [initialWidth]
  );

  return (
    <TextInput
      style={[style, dynamicStyle]}
      onLayout={handleOnLayout}
      {...rest}
    />
  );
};

vsaulistoptal avatar Feb 25 '25 10:02 vsaulistoptal

Still encountering this issue with longer placeholder text

ddx533534 avatar Apr 09 '25 08:04 ddx533534

Encountered this as well on expo SDK-52. It's working fine on IOS but longer placeholder breaking lines on android

yourjhay avatar Apr 24 '25 20:04 yourjhay

Experiencing the same. Input text displays properly but placeholder text does not respect numberOfLines

oserrabassa-jcm avatar Aug 07 '25 07:08 oserrabassa-jcm

Encountered this on expo SDK-53. Works on iOS but long placeholder text will break lines on Android

chancesmith avatar Aug 08 '25 17:08 chancesmith

Expo 54 has still same problem

PiotrKedra avatar Sep 24 '25 16:09 PiotrKedra

+1

abdulmateen-pb avatar Sep 30 '25 09:09 abdulmateen-pb