Skip to content

Commit 4ba70cf

Browse files
authored
[Fabric] Implement selectTextOnFocus prop in TextInput (#14641)
1 parent 88a33b9 commit 4ba70cf

File tree

7 files changed

+287
-48
lines changed

7 files changed

+287
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "[Fabric] Implement selectTextOnFocus in Text Input",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/@react-native-windows/tester/src/js/examples/TextInput/TextInputExample.windows.js

+24
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ const examples: Array<RNTesterModuleExample> = [
209209
);
210210
},
211211
},
212+
{
213+
title: 'Select text on focus',
214+
render: function (): React.Node {
215+
return (
216+
<View>
217+
<Text>Select text on focus:</Text>
218+
<ExampleTextInput
219+
selectTextOnFocus={true}
220+
style={styles.singleLine}
221+
testID="select-text-on-focus"
222+
/>
223+
<Text>
224+
Do not select text on focus if clear text on focus is enabled:
225+
</Text>
226+
<ExampleTextInput
227+
selectTextOnFocus={true}
228+
clearTextOnFocus={true}
229+
style={styles.singleLine}
230+
testID="select-text-on-focus-while-clear-text-on-focus"
231+
/>
232+
</View>
233+
);
234+
},
235+
},
212236
{
213237
title: 'Colors and text inputs',
214238
render: function (): React.Node {

packages/e2e-test-app-fabric/test/TextInputComponentTest.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,59 @@ describe('TextInput Tests', () => {
504504
// Verify the textInput contents are cleared after regaining focus
505505
expect(await componentFocusTrue.getText()).toBe('');
506506
});
507+
test('TextInputs can select text on focus', async () => {
508+
const component = await app.findElementByTestID('select-text-on-focus');
509+
await component.waitForDisplayed({timeout: 5000});
510+
511+
await app.waitUntil(
512+
async () => {
513+
await component.setValue('Hello World');
514+
return (await component.getText()) === 'Hello World';
515+
},
516+
{
517+
interval: 1500,
518+
timeout: 5000,
519+
timeoutMsg: `Unable to enter correct text.`,
520+
},
521+
);
522+
523+
// Check if the text is selected on focus.
524+
await component.click();
525+
526+
const dump = await dumpVisualTree('select-text-on-focus');
527+
expect(dump).toMatchSnapshot();
528+
});
529+
test('TextInputs can clear text on focus even if selectTextOnFocus == true', async () => {
530+
const targetComponent = await app.findElementByTestID(
531+
'select-text-on-focus-while-clear-text-on-focus',
532+
);
533+
await targetComponent.waitForDisplayed({timeout: 5000});
534+
535+
await app.waitUntil(
536+
async () => {
537+
await targetComponent.setValue('Hello World');
538+
return (await targetComponent.getText()) === 'Hello World';
539+
},
540+
{
541+
interval: 1500,
542+
timeout: 5000,
543+
timeoutMsg: `Unable to enter correct text.`,
544+
},
545+
);
546+
547+
// Click on the previous textInput to move focus away from this TextInput
548+
const anotherTextInput = await app.findElementByTestID(
549+
'select-text-on-focus',
550+
);
551+
await anotherTextInput.waitForDisplayed({timeout: 5000});
552+
await anotherTextInput.click();
553+
554+
// Now click on the tested component, make sure the text is cleared.
555+
await targetComponent.click();
556+
557+
// Verify the textInput contents are cleared after regaining focus
558+
expect(await targetComponent.getText()).toBe('');
559+
});
507560
test('TextInputs can have inline images', async () => {
508561
const component = await app.findElementByTestID('textinput-inline-images');
509562
await component.waitForDisplayed({timeout: 5000});

0 commit comments

Comments
 (0)