Skip to content

Add selection types to TextInput onChange event#55043

Closed
alanleedev wants to merge 1 commit into
react:mainfrom
alanleedev:export-D90123294
Closed

Add selection types to TextInput onChange event#55043
alanleedev wants to merge 1 commit into
react:mainfrom
alanleedev:export-D90123294

Conversation

@alanleedev

@alanleedev alanleedev commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

Summary:
This change adds TypeScript/Flow types and RNTester examples for the selection data in TextInput.onChange event.

This is the JS companion to the native changes that add selection data to the onChange event.

Why

On the web, text input elements provide selectionStart and selectionEnd properties that are always accessible during input events. This change exposes the selection data that native now provides, allowing developers to access cursor position during onChange.

What Changed

  1. Flow Types: Added optional selection?: Selection to TextInputChangeEventData
  2. TypeScript Types: Updated ReactNativeApi.d.ts with selection type
  3. RNTester: Updated examples to display selection in event logs

Changelog

[General][Added] - TextInput onChange event types now include optional selection data

Differential Revision: D90123294

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 5, 2026
@meta-codesync

meta-codesync Bot commented Jan 5, 2026

Copy link
Copy Markdown

@alanleedev has exported this pull request. If you are a Meta employee, you can view the originating Diff in D90123294.

Summary:

This change adds TypeScript/Flow types and RNTester examples for the `selection` data in `TextInput.onChange` event.

This is the JS companion to the native changes that add selection data to the onChange event. 

NOTE:selection only represents the cursor location when returned via onChange as this will not be invoked on a pure selection change without text change. We should also add this note to the documentation.

## Why

On the web, text input elements provide `selectionStart` and `selectionEnd` properties that are always accessible during input events. This change exposes the selection data that native now provides, allowing developers to access cursor position during onChange.

## What Changed

1. **Flow Types**: Added optional `selection?: Selection` to `TextInputChangeEventData`
2. **TypeScript Types**: Updated `ReactNativeApi.d.ts` with selection type
3. **RNTester**: Updated examples to display selection in event logs

Changelog:
[General][Added] - TextInput onChange event types now include optional selection data

Reviewed By: cipolleschi, necolas

Differential Revision: D90123294
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @alanleedev in c1f5445

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Jan 20, 2026
@meta-codesync

meta-codesync Bot commented Jan 20, 2026

Copy link
Copy Markdown

This pull request has been merged in c1f5445.

meta-codesync Bot pushed a commit that referenced this pull request Jun 24, 2026
…7249)

Summary:
The native `TextInput.onChange` event now emits `selection` data (cursor location) on both iOS and Android, added in [162627a](162627a) (#55044).

- #55044

The JS companion PR [c1f5445](c1f5445) (#55043) added the corresponding types, but only to two of the three TypeScript/Flow type sources:

- #55043

| Type source | Has `selection`? |
|---|---|
| `TextInput.flow.js` (Flow) | ✅ |
| `ReactNativeApi.d.ts` (generated API surface) | ✅ |
| `TextInput.d.ts` (legacy public `.d.ts`) | ❌ |

The hand-written legacy `TextInput.d.ts` was missed, so consumers relying on it can't access `selection` from the `onChange` event even though native genuinely sends it. This PR adds the optional `selection` field there so all three type sources agree.

## Changelog:

[GENERAL] [ADDED] - Add `selection` to `TextInputChangeEventData` in TypeScript types

Pull Request resolved: #57249

Test Plan:
Type-only change. `selection` is now available and correctly typed on the `onChange` event:

```tsx
<TextInput
  onChange={e => {
    // e.nativeEvent.selection is now typed as { start: number; end: number } | undefined
    console.log(e.nativeEvent.selection?.start, e.nativeEvent.selection?.end);
  }}
/>

Reviewed By: christophpurrer

Differential Revision: D109435915

Pulled By: fabriziocucci

fbshipit-source-id: b334e52e0e39a7f9053bf41e93077be90b3d9adf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants