Skip to content

Commit a0f9b36

Browse files
nakamboSaadnajmi
andauthored
[0.76] [TextInput] Export TI State's focusInput/blurInput (#2464)
[These two functions](https://github.com/microsoft/react-native-macos/blob/9ea059c33143e6766582a1f682e4f144e00cc740/packages/react-native/Libraries/Components/TextInput/TextInputState.js#L60C1-L70C2) are used to [properly handle onFocus\onBlur for TextInput implementations](https://github.com/microsoft/react-native-macos/blob/9ea059c33143e6766582a1f682e4f144e00cc740/packages/react-native/Libraries/Components/TextInput/TextInput.js#L1623C1-L1635C5), specifically they are used to track the currently focused TextInput control and as such cause [focusTextInput](https://github.com/microsoft/react-native-macos/blob/9ea059c33143e6766582a1f682e4f144e00cc740/packages/react-native/Libraries/Components/TextInput/TextInputState.js#L106C1-L106C48) and [blurTextInput](https://github.com/microsoft/react-native-macos/blob/9ea059c33143e6766582a1f682e4f144e00cc740/packages/react-native/Libraries/Components/TextInput/TextInputState.js#L143C1-L143C69) to skip native focus\blur calls if the JS side thinks the control in question is already focused\blurred. In other words, if someone wants to build a custom `<TextInput>` from the ground up, they can't expect `focusTextInput`, `blurTextInput`, and `currentlyFocusedInput` (the last one which is especially already used in a bunch of places in the framework) to work properly. This change exports the necessary `focusInput` and `blurInput` out of [TextInput](https://github.com/microsoft/react-native-macos/blob/9ea059c33143e6766582a1f682e4f144e00cc740/packages/react-native/Libraries/Components/TextInput/TextInput.js#L2041C1-L2048C3) statics. Co-authored-by: Saad Najmi <[email protected]>
1 parent eb5f8d1 commit a0f9b36

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

packages/react-native/Libraries/Components/TextInput/TextInput.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,22 @@ interface TextInputState {
10591059
* noop if it wasn't focused
10601060
*/
10611061
blurTextInput(textField?: React.ElementRef<HostComponent<unknown>>): void;
1062+
1063+
// [macOS
1064+
/**
1065+
* @param textField ref of the text field that was focused
1066+
* Call on custom implementations of TextInput to notify the control was focused
1067+
* noop if it was already focused
1068+
*/
1069+
onTextInputFocus(textField?: React.ElementRef<HostComponent<unknown>>): void;
1070+
1071+
/**
1072+
* @param textField ref of the text field that was blurred
1073+
* Call on custom implementations of TextInput to notify the control was blurred
1074+
* noop if it wasn't focused
1075+
*/
1076+
onTextInputBlur(textField?: React.ElementRef<HostComponent<unknown>>): void;
1077+
// macOS]
10621078
}
10631079

10641080
/**

packages/react-native/Libraries/Components/TextInput/TextInput.flow.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,8 @@ export type TextInputComponentStatics = $ReadOnly<{|
12231223
currentlyFocusedField: () => ?number,
12241224
focusTextInput: (textField: ?ComponentRef) => void,
12251225
blurTextInput: (textField: ?ComponentRef) => void,
1226+
onTextInputFocus: (textField: ?ComponentRef) => void, // [macOS]
1227+
onTextInputBlur: (textField: ?ComponentRef) => void, // [macOS]
12261228
|}>,
12271229
|}>;
12281230

packages/react-native/Libraries/Components/TextInput/TextInput.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,8 @@ ExportedForwardRef.State = {
20132013
currentlyFocusedField: TextInputState.currentlyFocusedField,
20142014
focusTextInput: TextInputState.focusTextInput,
20152015
blurTextInput: TextInputState.blurTextInput,
2016+
onTextInputFocus: TextInputState.focusInput, // [macOS]
2017+
onTextInputBlur: TextInputState.blurInput, // [macOS]
20162018
};
20172019

20182020
export type TextInputComponentStatics = $ReadOnly<{|
@@ -2021,6 +2023,8 @@ export type TextInputComponentStatics = $ReadOnly<{|
20212023
currentlyFocusedField: typeof TextInputState.currentlyFocusedField,
20222024
focusTextInput: typeof TextInputState.focusTextInput,
20232025
blurTextInput: typeof TextInputState.blurTextInput,
2026+
onTextInputFocus: typeof TextInputState.focusInput, // [macOS]
2027+
onTextInputBlur: typeof TextInputState.blurInput, // [macOS]
20242028
|}>,
20252029
|}>;
20262030

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,8 @@ export type TextInputComponentStatics = $ReadOnly<{|
31643164
currentlyFocusedField: () => ?number,
31653165
focusTextInput: (textField: ?ComponentRef) => void,
31663166
blurTextInput: (textField: ?ComponentRef) => void,
3167+
onTextInputFocus: (textField: ?ComponentRef) => void,
3168+
onTextInputBlur: (textField: ?ComponentRef) => void,
31673169
|}>,
31683170
|}>;
31693171
export type TextInputType = React.AbstractComponent<
@@ -3553,6 +3555,8 @@ export type TextInputComponentStatics = $ReadOnly<{|
35533555
currentlyFocusedField: typeof TextInputState.currentlyFocusedField,
35543556
focusTextInput: typeof TextInputState.focusTextInput,
35553557
blurTextInput: typeof TextInputState.blurTextInput,
3558+
onTextInputFocus: typeof TextInputState.focusInput,
3559+
onTextInputBlur: typeof TextInputState.blurInput,
35563560
|}>,
35573561
|}>;
35583562
declare module.exports: TextInputType;

0 commit comments

Comments
 (0)