Skip to content

Commit e5d7eaf

Browse files
HariniMalothu17Harini Malothu
authored andcommitted
[Fabric] Implement autoFocus property for TextInput for fabric (microsoft#14816)
* Added fix for AutoFocus * Change files --------- Co-authored-by: Harini Malothu <[email protected]>
1 parent 2a0b02b commit e5d7eaf

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Added fix for AutoFocus",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/Samples/textinput.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ export default class Bootstrap extends React.Component<{}, any> {
257257
placeholder="Focus and then click outside"
258258
onEndEditing={this.handleEndEditing}
259259
/>
260+
<TextInput
261+
style={[styles.input, {borderColor: 'green', borderWidth: 2}]}
262+
placeholder={
263+
'autoFocus: true - This input will be focused automatically'
264+
}
265+
autoFocus={true}
266+
/>
267+
<TextInput
268+
style={[styles.input, {borderColor: 'red', borderWidth: 2}]}
269+
placeholder={
270+
'autoFocus: false - This input will NOT be focused automatically'
271+
}
272+
autoFocus={false}
273+
/>
260274

261275
<KeyboardAvoidingView
262276
style={styles.container}

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,13 @@ void WindowsTextInputComponentView::onMounted() noexcept {
13121312
m_propBits |= TXTBIT_CHARFORMATCHANGE;
13131313
}
13141314
InternalFinalize();
1315+
1316+
// Handle autoFocus property - focus the component when mounted if autoFocus is true
1317+
if (windowsTextInputProps().autoFocus) {
1318+
if (auto root = rootComponentView()) {
1319+
root->TrySetFocusedComponent(*get_strong(), winrt::Microsoft::ReactNative::FocusNavigationDirection::None);
1320+
}
1321+
}
13151322
}
13161323

13171324
std::optional<std::string> WindowsTextInputComponentView::getAccessiblityValue() noexcept {

0 commit comments

Comments
 (0)