Skip to content
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

'selection' prop is not working in TextInput on Android after React Native upgrade to v0.73 #44171

Open
hariomsinha7 opened this issue Apr 20, 2024 · 4 comments
Assignees
Labels
Component: TextInput Related to the TextInput component. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Newer Patch Available Platform: Android Android applications. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@hariomsinha7
Copy link

Description

Before React Native upgrade, 'selection' prop was used to set the position of the cursor to the specified position. This used to work for readOnly inputs, pre-filled inputs and even for the inputs where the data is being filled.

After the React Native upgrade to v0.73, it is only working for the scenario where the data is being filled and does not work when the TextInput is readOnly or have some pre-filled data.

Our implementation:

We have used state variable to toggle the selection between {start:0} and 'null'. When input is on focus, set the selection to null and when the input is blurred, change the selection to 0. We have implemented this to always show the input data from starting position. ( In Android, by default, this behaviour does not works, and the input shows the data from end ).

  const [value, onChangeText] = React.useState(
    'This is a prefilled text inside this TextInput. Add more contents and check the selection prop',
  );
  const [selection, setSelection] = useState(
    Platform.OS === 'android' ? {start: 0} : null,
  );
  const handleOnFocus = () => {
    if (Platform.OS === 'android') {
      setSelection(null);
    }
  };
  const handleOnBlur = () => {
    if (Platform.OS === 'android') {
      setSelection({start: 0});
    }
  };

 <SafeAreaView>
      <ScrollView>
        <View style={styles?.container}>
          <Text>{'Text Input with prefilled data'}</Text>
          <TextInput
            onChangeText={text => onChangeText(text)}
            value={value}
            style={{
              padding: 10,
              width: 300,
              borderStartWidth: 1,
              borderEndWidth: 1,
              borderTopWidth: 1,
              boderLeftWidth: 1,
              borderRightWidth: 1,
              borderBottomWidth: 1,
              marginBottom: 10,
              borderRadius: 10,
            }}
            placeholder="Enter long text here..."
            selection={selection}
            onFocus={handleOnFocus}
            onBlur={handleOnBlur}
          />
          <Text>{'Text Input with readOnly data'}</Text>
          <TextInput
            onChangeText={text => onChangeText(text)}
            value={value}
            readOnly
            style={{
              padding: 10,
              width: 300,
              borderStartWidth: 1,
              borderEndWidth: 1,
              borderTopWidth: 1,
              boderLeftWidth: 1,
              borderRightWidth: 1,
              borderBottomWidth: 1,
              marginBottom: 10,
              borderRadius: 10,
              backgroundColor: '#f2f2f2',
            }}
            placeholder="Enter long text here..."
            selection={selection}
            onFocus={handleOnFocus}
            onBlur={handleOnBlur}
          />

          <Text>{'Change input focus here'}</Text>
          <TextInput
            placeholder="Use this to change focus"
            style={{
              padding: 10,
              width: 300,
              borderStartWidth: 1,
              borderEndWidth: 1,
              borderTopWidth: 1,
              boderLeftWidth: 1,
              borderRightWidth: 1,
              borderBottomWidth: 1,
              borderRadius: 10,
            }}
          />
        </View>
      </ScrollView>
    </SafeAreaView>```

**Note: This issue is only coming on Android.**

### Steps to reproduce

1. Clone the attached repo and install dependencies `( npm install )`
2. Run the app on Android simulator `( Pixel 6 Pro API 34 )`
3. In the launch page only, you will see 3 inputs. First input has prefilled data, where you can input some characters and remove the focus from this input field, by using the third input. The second input is readOnly, where the expected behavior is to show the input from start. As of now, in Android, it shows from end, when the input is blurred.

`Repo URL: https://github.com/hariomsinha7/bug-react-native-textinput`

### React Native Version

0.73.0

### Affected Platforms

Runtime - Android

### Areas

Fabric - The New Renderer, TurboModule - The New Native Module System, JSI - Javascript Interface, Codegen

### Output of `npx react-native info`

```text
System:
  OS: macOS 14.4.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 120.02 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.17.0
    path: ~/.nvm/versions/node/v18.17.0/bin/node
  Yarn:
    version: 1.22.19
    path: /opt/homebrew/bin/yarn
  npm:
    version: 9.6.7
    path: ~/.nvm/versions/node/v18.17.0/bin/npm
  Watchman:
    version: 2024.04.15.00
    path: /opt/homebrew/bin/watchman

Stacktrace or Logs

NA

Reproducer

https://github.com/hariomsinha7/bug-react-native-textinput

Screenshots and Videos

NA

@hariomsinha7 hariomsinha7 added Needs: Triage 🔍 Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) labels Apr 20, 2024
Copy link

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available - 0.73.7. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@github-actions github-actions bot added Newer Patch Available Component: TextInput Related to the TextInput component. Platform: Android Android applications. labels Apr 20, 2024
@cortinico cortinico added Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 labels Apr 22, 2024
@mannoeu
Copy link

mannoeu commented Apr 23, 2024

The same here. I've already tried several ways to make the cursor return to the beginning after losing focus, allowing the beginning of the input value to be visible and the end cut off and not the other way around, which is the default.

@alanleedev
Copy link
Contributor

alanleedev commented Apr 29, 2024

@hariomsinha7 could you tell us the React Native version that you were using before the upgrade to 73.0 when it was working? I think I see same issue with 0.72.7?

@alanleedev
Copy link
Contributor

Found the issue issue in current code and will work on a fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: TextInput Related to the TextInput component. Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Newer Patch Available Platform: Android Android applications. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
Development

No branches or pull requests

4 participants