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

Merged
merged 4 commits into from
Jan 2, 2025
Merged
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);
});
});
21 changes: 7 additions & 14 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,27 +32,21 @@ function TextCopyInput({
children,
...inputProps
}: Props) {
const textRef = useRef<HTMLInputElement>(null);
const textNodeId = useId();

const handleSelectText = useCallback(() => {
if (!textRef.current) {
const node = document.getElementById(textNodeId) as HTMLInputElement | null;
if (!node) {
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)) {
return;
}

if (rtl && node instanceof HTMLInputElement) {
if (rtl) {
// we don't want to select the first character - \u202A, nor the last - \u202C
node.setSelectionRange(1, node.value.length - 1);
} 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