Skip to content

Commit d0f17f8

Browse files
HariniMalothu17Harini Malothu
andauthored
[Fabric] Implement autoFocus property for TextInput for fabric (#14816)
* Added fix for AutoFocus * Change files --------- Co-authored-by: Harini Malothu <hmalothu@microsoft.com>
1 parent 51b8d55 commit d0f17f8

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": "hmalothu@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/Samples/textinput.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ export default class Bootstrap extends React.Component<{}, any> {
281281
placeholder="Focus and then click outside"
282282
onEndEditing={this.handleEndEditing}
283283
/>
284+
<TextInput
285+
style={[styles.input, {borderColor: 'green', borderWidth: 2}]}
286+
placeholder={
287+
'autoFocus: true - This input will be focused automatically'
288+
}
289+
autoFocus={true}
290+
/>
291+
<TextInput
292+
style={[styles.input, {borderColor: 'red', borderWidth: 2}]}
293+
placeholder={
294+
'autoFocus: false - This input will NOT be focused automatically'
295+
}
296+
autoFocus={false}
297+
/>
284298

285299
<KeyboardAvoidingView
286300
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
@@ -1371,6 +1371,13 @@ void WindowsTextInputComponentView::onMounted() noexcept {
13711371
m_propBits |= TXTBIT_CHARFORMATCHANGE;
13721372
}
13731373
InternalFinalize();
1374+
1375+
// Handle autoFocus property - focus the component when mounted if autoFocus is true
1376+
if (windowsTextInputProps().autoFocus) {
1377+
if (auto root = rootComponentView()) {
1378+
root->TrySetFocusedComponent(*get_strong(), winrt::Microsoft::ReactNative::FocusNavigationDirection::None);
1379+
}
1380+
}
13741381
}
13751382

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

0 commit comments

Comments
 (0)