Skip to content

Commit a055bab

Browse files
fix(modal): allow data-modal-primary-focus in danger (#18951)
1 parent a8c231d commit a055bab

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

packages/react/src/components/Modal/Modal-test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,86 @@ describe('Modal', () => {
395395
expect(container.firstChild).toHaveClass(`${prefix}--modal--slug`);
396396
spy.mockRestore();
397397
});
398+
399+
it('should set correct focus if data-modal-primary-focus is used', () => {
400+
render(
401+
<Modal
402+
open
403+
id="custom-modal-id"
404+
data-testid="modal-4"
405+
loadingStatus="active"
406+
loadingDescription="loading..."
407+
primaryButtonText="Save"
408+
secondaryButtonText="Cancel">
409+
<p>
410+
Custom domains direct requests for your apps in this Cloud Foundry
411+
organization to a URL that you own. A custom domain can be a shared
412+
domain, a shared subdomain, or a shared domain and host.
413+
</p>
414+
<TextInput
415+
data-modal-primary-focus
416+
id="text-input-1"
417+
data-testid="text-input-1"
418+
labelText="Domain name"
419+
/>
420+
</Modal>
421+
);
422+
423+
expect(screen.getByTestId('text-input-1')).toHaveFocus();
424+
});
425+
426+
it('should set correct focus on a danger modal if data-modal-primary-focus is used', () => {
427+
render(
428+
<Modal
429+
open
430+
danger
431+
id="custom-modal-id"
432+
data-testid="modal-4"
433+
loadingStatus="active"
434+
loadingDescription="loading..."
435+
primaryButtonText="Save"
436+
secondaryButtonText="Cancel">
437+
<p>
438+
Custom domains direct requests for your apps in this Cloud Foundry
439+
organization to a URL that you own. A custom domain can be a shared
440+
domain, a shared subdomain, or a shared domain and host.
441+
</p>
442+
<TextInput
443+
data-modal-primary-focus
444+
id="text-input-1"
445+
data-testid="text-input-1"
446+
labelText="Domain name"
447+
/>
448+
</Modal>
449+
);
450+
451+
expect(screen.getByTestId('text-input-1')).toHaveFocus();
452+
});
453+
454+
it('should set focus on secondary button if danger modal is used', () => {
455+
render(
456+
<Modal
457+
open
458+
danger
459+
id="custom-modal-id"
460+
data-testid="modal-4"
461+
primaryButtonText="Save"
462+
secondaryButtonText="Cancel">
463+
<p>
464+
Custom domains direct requests for your apps in this Cloud Foundry
465+
organization to a URL that you own. A custom domain can be a shared
466+
domain, a shared subdomain, or a shared domain and host.
467+
</p>
468+
<TextInput
469+
id="text-input-1"
470+
data-testid="text-input-1"
471+
labelText="Domain name"
472+
/>
473+
</Modal>
474+
);
475+
476+
expect(screen.getByText('Cancel')).toHaveFocus();
477+
});
398478
});
399479

400480
describe('events', () => {

packages/react/src/components/Modal/Modal.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,15 @@ const Modal = React.forwardRef(function Modal(
437437
if (!enableDialogElement) {
438438
const initialFocus = (focusContainerElement: HTMLElement | null) => {
439439
const containerElement = focusContainerElement || innerModal.current;
440-
const primaryFocusElement = containerElement
441-
? containerElement.querySelector<HTMLElement | SVGElement>(
442-
danger ? `.${prefix}--btn--secondary` : selectorPrimaryFocus
443-
)
444-
: null;
440+
const primaryFocusElement =
441+
containerElement &&
442+
(containerElement.querySelector<HTMLElement | SVGElement>(
443+
selectorPrimaryFocus
444+
) ||
445+
(danger &&
446+
containerElement.querySelector<HTMLElement | SVGElement>(
447+
`.${prefix}--btn--secondary`
448+
)));
445449

446450
if (primaryFocusElement) {
447451
return primaryFocusElement;

0 commit comments

Comments
 (0)