Skip to content

Commit

Permalink
feat: Add setSelectionRange to PromptInput (#3294)
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster authored Feb 14, 2025
1 parent 187f672 commit fca5d5f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12604,6 +12604,29 @@ about modifiers (that is, CTRL, ALT, SHIFT, META, etc.).",
"parameters": [],
"returnType": "void",
},
{
"description": "Selects a range of text in the textarea control.
See https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setSelectionRange
for more details on this method. Be aware that using this method in React has some
common pitfalls: https://stackoverflow.com/questions/60129605/is-javascripts-setselectionrange-incompatible-with-react-hooks
",
"name": "setSelectionRange",
"parameters": [
{
"name": "start",
"type": "null | number",
},
{
"name": "end",
"type": "null | number",
},
{
"name": "direction",
"type": ""backward" | "forward" | "none"",
},
],
"returnType": "void",
},
],
"name": "PromptInput",
"properties": [
Expand Down
17 changes: 17 additions & 0 deletions src/prompt-input/__tests__/prompt-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('ref', () => {
expect(textarea.selectionStart).toBe(0);
expect(textarea.selectionEnd).toBe(4);
});

test('can be used to select a range', () => {
const ref = React.createRef<HTMLTextAreaElement>();
const { wrapper } = renderPromptInput({ value: 'Test', ref });
const textarea = wrapper.findNativeTextarea().getElement();

// Make sure no text is selected
textarea.selectionStart = textarea.selectionEnd = 0;
textarea.blur();
expect(textarea.selectionStart).toBe(0);
expect(textarea.selectionEnd).toBe(0);

// Select all text
ref.current!.setSelectionRange(1, 3);
expect(textarea.selectionStart).toBe(1);
expect(textarea.selectionEnd).toBe(3);
});
});

describe('disableBrowserAutocorrect', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/prompt-input/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,14 @@ export namespace PromptInputProps {
* Selects all text in the textarea control.
*/
select(): void;

/**
* Selects a range of text in the textarea control.
*
* See https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setSelectionRange
* for more details on this method. Be aware that using this method in React has some
* common pitfalls: https://stackoverflow.com/questions/60129605/is-javascripts-setselectionrange-incompatible-with-react-hooks
*/
setSelectionRange(start: number | null, end: number | null, direction?: 'forward' | 'backward' | 'none'): void;
}
}
3 changes: 3 additions & 0 deletions src/prompt-input/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const InternalPromptInput = React.forwardRef(
select() {
textareaRef.current?.select();
},
setSelectionRange(...args: Parameters<HTMLTextAreaElement['setSelectionRange']>) {
textareaRef.current?.setSelectionRange(...args);
},
}),
[textareaRef]
);
Expand Down

0 comments on commit fca5d5f

Please sign in to comment.