Skip to content

Commit

Permalink
feat(react19): Remove findDomNode in textCopyInput
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Dec 27, 2024
1 parent 1710c08 commit bdec4d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
39 changes: 36 additions & 3 deletions static/app/components/textCopyInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import {render, screen} from 'sentry-test/reactTestingLibrary';
import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';

import TextCopyInput from 'sentry/components/textCopyInput';

describe('TextCopyInput', function () {
it('renders', function () {
beforeEach(() => {
Object.assign(navigator, {
clipboard: {
writeText: jest.fn().mockResolvedValue(''),
},
});
});

it('copies text to clipboard on click', async function () {
render(<TextCopyInput>Text to Copy</TextCopyInput>);
const button = screen.getByRole('button', {name: 'Copy'});
expect(button).toBeInTheDocument();

await userEvent.click(button);

expect(navigator.clipboard.writeText).toHaveBeenCalledWith('Text to Copy');
});

it('selects text in input on click', async function () {
render(<TextCopyInput>Text to Copy</TextCopyInput>);
expect(screen.getByDisplayValue('Text to Copy')).toBeInTheDocument();
const input = screen.getByRole<HTMLInputElement>('textbox');
expect(input).toHaveValue('Text to Copy');
const selectSpy = jest.spyOn(input, 'select');

await userEvent.click(input);

expect(selectSpy).toHaveBeenCalled();
});

it('handles RTL text selection', async function () {
render(<TextCopyInput rtl>Text to Copy</TextCopyInput>);
const input = screen.getByRole<HTMLInputElement>('textbox');
const setSelectionRangeSpy = jest.spyOn(input, 'setSelectionRange');

await userEvent.click(input);
expect(setSelectionRangeSpy).toHaveBeenCalledWith(1, input.value.length - 1);
});
});
20 changes: 7 additions & 13 deletions static/app/components/textCopyInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {useCallback, useRef} from 'react';
import {findDOMNode} from 'react-dom';
import {useCallback, useId} from 'react';
import styled from '@emotion/styled';

import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
Expand Down Expand Up @@ -33,17 +32,11 @@ function TextCopyInput({
children,
...inputProps
}: Props) {
const textRef = useRef<HTMLInputElement>(null);
const textNodeId = useId();

const handleSelectText = useCallback(() => {
if (!textRef.current) {
return;
}

// We use findDOMNode here because `this.textRef` is not a dom node,
// it's a ref to AutoSelectText
const node = findDOMNode(textRef.current); // eslint-disable-line react/no-find-dom-node
if (!node || !(node instanceof HTMLElement)) {
const node = document.getElementById(textNodeId);
if (!node) {
return;
}

Expand All @@ -53,7 +46,7 @@ function TextCopyInput({
} else {
selectText(node);
}
}, [rtl]);
}, [rtl, textNodeId]);

/**
* We are using direction: rtl; to always show the ending of a long overflowing text in input.
Expand All @@ -67,10 +60,11 @@ function TextCopyInput({

return (
<InputGroup className={className}>
hello
<StyledInput
id={textNodeId}
readOnly
disabled={disabled}
ref={textRef}
style={style}
value={inputValue}
onClick={handleSelectText}
Expand Down

0 comments on commit bdec4d2

Please sign in to comment.