Skip to content

Commit fca5d5f

Browse files
feat: Add setSelectionRange to PromptInput (#3294)
1 parent 187f672 commit fca5d5f

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12604,6 +12604,29 @@ about modifiers (that is, CTRL, ALT, SHIFT, META, etc.).",
1260412604
"parameters": [],
1260512605
"returnType": "void",
1260612606
},
12607+
{
12608+
"description": "Selects a range of text in the textarea control.
12609+
See https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setSelectionRange
12610+
for more details on this method. Be aware that using this method in React has some
12611+
common pitfalls: https://stackoverflow.com/questions/60129605/is-javascripts-setselectionrange-incompatible-with-react-hooks
12612+
",
12613+
"name": "setSelectionRange",
12614+
"parameters": [
12615+
{
12616+
"name": "start",
12617+
"type": "null | number",
12618+
},
12619+
{
12620+
"name": "end",
12621+
"type": "null | number",
12622+
},
12623+
{
12624+
"name": "direction",
12625+
"type": ""backward" | "forward" | "none"",
12626+
},
12627+
],
12628+
"returnType": "void",
12629+
},
1260712630
],
1260812631
"name": "PromptInput",
1260912632
"properties": [

src/prompt-input/__tests__/prompt-input.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ describe('ref', () => {
5555
expect(textarea.selectionStart).toBe(0);
5656
expect(textarea.selectionEnd).toBe(4);
5757
});
58+
59+
test('can be used to select a range', () => {
60+
const ref = React.createRef<HTMLTextAreaElement>();
61+
const { wrapper } = renderPromptInput({ value: 'Test', ref });
62+
const textarea = wrapper.findNativeTextarea().getElement();
63+
64+
// Make sure no text is selected
65+
textarea.selectionStart = textarea.selectionEnd = 0;
66+
textarea.blur();
67+
expect(textarea.selectionStart).toBe(0);
68+
expect(textarea.selectionEnd).toBe(0);
69+
70+
// Select all text
71+
ref.current!.setSelectionRange(1, 3);
72+
expect(textarea.selectionStart).toBe(1);
73+
expect(textarea.selectionEnd).toBe(3);
74+
});
5875
});
5976

6077
describe('disableBrowserAutocorrect', () => {

src/prompt-input/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,14 @@ export namespace PromptInputProps {
119119
* Selects all text in the textarea control.
120120
*/
121121
select(): void;
122+
123+
/**
124+
* Selects a range of text in the textarea control.
125+
*
126+
* See https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setSelectionRange
127+
* for more details on this method. Be aware that using this method in React has some
128+
* common pitfalls: https://stackoverflow.com/questions/60129605/is-javascripts-setselectionrange-incompatible-with-react-hooks
129+
*/
130+
setSelectionRange(start: number | null, end: number | null, direction?: 'forward' | 'backward' | 'none'): void;
122131
}
123132
}

src/prompt-input/internal.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ const InternalPromptInput = React.forwardRef(
7777
select() {
7878
textareaRef.current?.select();
7979
},
80+
setSelectionRange(...args: Parameters<HTMLTextAreaElement['setSelectionRange']>) {
81+
textareaRef.current?.setSelectionRange(...args);
82+
},
8083
}),
8184
[textareaRef]
8285
);

0 commit comments

Comments
 (0)