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 ContextPickerModal #82635

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions static/app/components/contextPickerModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('ContextPickerModal', function () {
render(getComponent());

expect(screen.getByText('Select an Organization')).toBeInTheDocument();
expect(screen.getByRole('textbox')).toHaveFocus();
expect(screen.queryByText('Select a Project to continue')).not.toBeInTheDocument();
});

Expand Down Expand Up @@ -144,6 +145,7 @@ describe('ContextPickerModal', function () {
// Should see 1 selected, and 1 as an option
expect(screen.getAllByText('org-slug')).toHaveLength(2);

expect(screen.getByRole('textbox')).toHaveFocus();
expect(await screen.findByText('My Projects')).toBeInTheDocument();
expect(screen.getByText(project.slug)).toBeInTheDocument();
expect(screen.getByText(project2.slug)).toBeInTheDocument();
Expand Down Expand Up @@ -180,6 +182,7 @@ describe('ContextPickerModal', function () {

// Should not have anything selected
expect(screen.getByText('Select an Organization')).toBeInTheDocument();
expect(screen.getByRole('textbox')).toHaveFocus();

// Select org2
await selectEvent.select(screen.getByText('Select an Organization'), org2.slug);
Expand Down
52 changes: 14 additions & 38 deletions static/app/components/contextPickerModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Component, Fragment} from 'react';
import {findDOMNode} from 'react-dom';
import {components} from 'react-select';
import styled from '@emotion/styled';
import type {Query} from 'history';
Expand Down Expand Up @@ -69,6 +68,14 @@ type Props = ModalRenderProps & {
allowAllProjectsSelection?: boolean;
};

function autoFocusReactSelect(reactSelectRef: any) {
if (!reactSelectRef) {
return;
}

reactSelectRef.select?.focus?.();
scttcper marked this conversation as resolved.
Show resolved Hide resolved
}

const selectStyles: StylesConfig = {
menu: provided => ({
...provided,
Expand Down Expand Up @@ -116,12 +123,6 @@ class ContextPickerModal extends Component<Props> {

onFinishTimeout: number | undefined = undefined;

// TODO(ts) The various generics in react-select types make getting this
// right hard.
orgSelect: any | null = null;
projectSelect: any | null = null;
configSelect: any | null = null;

// Performs checks to see if we need to prompt user
// i.e. When there is only 1 org and no project is needed or
// there is only 1 org and only 1 project (which should be rare)
Expand Down Expand Up @@ -178,21 +179,6 @@ class ContextPickerModal extends Component<Props> {
) ?? undefined;
};

doFocus = (ref: any | null) => {
if (!ref || this.props.loading) {
return;
}

// eslint-disable-next-line react/no-find-dom-node
const el = findDOMNode(ref) as HTMLElement;

if (el !== null) {
const input = el.querySelector('input');

input?.focus();
}
};

handleSelectOrganization = ({value}: {value: string}) => {
// If we do not need to select a project, we can early return after selecting an org
// No need to fetch org details
Expand Down Expand Up @@ -316,10 +302,7 @@ class ContextPickerModal extends Component<Props> {

return (
<StyledSelectControl
ref={(ref: any) => {
this.projectSelect = ref;
this.doFocus(this.projectSelect);
}}
ref={autoFocusReactSelect}
placeholder={t('Select a Project to continue')}
name="project"
options={projectOptions}
Expand Down Expand Up @@ -354,10 +337,7 @@ class ContextPickerModal extends Component<Props> {
];
return (
<StyledSelectControl
ref={(ref: any) => {
this.configSelect = ref;
this.doFocus(this.configSelect);
}}
ref={autoFocusReactSelect}
placeholder={t('Select a configuration to continue')}
name="configurations"
options={options}
Expand Down Expand Up @@ -398,18 +378,14 @@ class ContextPickerModal extends Component<Props> {

return (
<Fragment>
<Header closeButton>{this.headerText}</Header>
<Header closeButton>
<h5>{this.headerText}</h5>
</Header>
<Body>
{loading && <StyledLoadingIndicator overlay />}
{needOrg && (
<StyledSelectControl
ref={(ref: any) => {
this.orgSelect = ref;
if (shouldShowProjectSelector) {
return;
}
this.doFocus(this.orgSelect);
}}
ref={shouldShowProjectSelector ? undefined : autoFocusReactSelect}
placeholder={t('Select an Organization')}
name="organization"
options={orgChoices}
Expand Down
Loading