Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react19): Remove findDomNode in textCopyInput #82637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
19 changes: 6 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getElementById might meant that the else case below isn't needed anymore... it should always be an HTMLInputElement .... unless the <StyledInput> implementation changes...

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 @@ -68,9 +61,9 @@ function TextCopyInput({
return (
<InputGroup className={className}>
<StyledInput
id={textNodeId}
readOnly
disabled={disabled}
ref={textRef}
style={style}
value={inputValue}
onClick={handleSelectText}
Expand Down
Loading