Skip to content

Commit 780206a

Browse files
authored
fix(Modals - TypeScript): allow typing the container element (#3856)
1 parent beb032e commit 780206a

File tree

4 files changed

+272
-203
lines changed

4 files changed

+272
-203
lines changed

packages/main/src/components/Modals/Modals.test.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { act, fireEvent, render, screen } from '@shared/tests';
22
import React from 'react';
3+
import { DialogPropTypes, MessageBoxPropTypes, PopoverPropTypes, ResponsivePopoverPropTypes } from '../..';
34
import { Modals } from './index';
45

56
describe('Modals - static helpers', function () {
67
test('showDialog', () => {
78
render(null);
9+
const props: DialogPropTypes & { 'data-testid': string } = {
10+
children: 'Dialog Content',
11+
'data-testid': 'dialog'
12+
};
813
act(() => {
9-
Modals.showDialog({
10-
children: 'Dialog Content',
11-
'data-testid': 'dialog'
12-
});
14+
Modals.showDialog(props);
1315
});
1416

1517
expect(screen.getByTestId('dialog')).toHaveAttribute('open', 'true');
@@ -18,12 +20,13 @@ describe('Modals - static helpers', function () {
1820

1921
test('showPopover', () => {
2022
render(<span id="opener" />);
23+
const props: PopoverPropTypes & { 'data-testid': string } = {
24+
opener: 'opener',
25+
children: 'Popover Content',
26+
'data-testid': 'popover'
27+
};
2128
act(() => {
22-
Modals.showPopover({
23-
opener: 'opener',
24-
children: 'Popover Content',
25-
'data-testid': 'popover'
26-
});
29+
Modals.showPopover(props);
2730
});
2831

2932
expect(screen.getByTestId('popover')).toHaveAttribute('open', 'true');
@@ -32,12 +35,13 @@ describe('Modals - static helpers', function () {
3235

3336
test('showResponsivePopover', () => {
3437
render(<span id="opener" />);
38+
const props: ResponsivePopoverPropTypes & { 'data-testid': string } = {
39+
opener: 'opener',
40+
children: 'ResponsivePopover Content',
41+
'data-testid': 'responsivepopover'
42+
};
3543
act(() => {
36-
Modals.showResponsivePopover({
37-
opener: 'opener',
38-
children: 'ResponsivePopover Content',
39-
'data-testid': 'responsivepopover'
40-
});
44+
Modals.showResponsivePopover(props);
4145
});
4246

4347
expect(screen.getByTestId('responsivepopover')).toHaveAttribute('open', 'true');
@@ -46,11 +50,12 @@ describe('Modals - static helpers', function () {
4650

4751
test('showMessageBox', async () => {
4852
render(null);
53+
const props: MessageBoxPropTypes & { 'data-testid': string } = {
54+
children: 'MessageBox Content',
55+
'data-testid': 'messagebox'
56+
};
4957
act(() => {
50-
Modals.showMessageBox({
51-
children: 'MessageBox Content',
52-
'data-testid': 'messagebox'
53-
});
58+
Modals.showMessageBox(props);
5459
});
5560

5661
await screen.findByText('Confirmation');

packages/main/src/components/Modals/ModalsProvider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ export interface ModalsProviderPropTypes {
66
children: ReactNode;
77
}
88

9+
//@ts-expect-error: can't assume state generics at this point
910
const modalStateReducer = (state: ModalState[], action: UpdateModalStateAction) => {
1011
switch (action.type) {
1112
case 'set':
12-
return [...state, action.payload as ModalState];
13+
return [...state, action.payload];
1314
case 'reset':
1415
return state.filter((modal) => modal.id !== action.payload.id);
1516
default:
@@ -34,7 +35,7 @@ export function ModalsProvider({ children }: ModalsProviderPropTypes) {
3435
{modals.map((modal) => {
3536
if (modal?.Component) {
3637
return createPortal(
37-
<modal.Component {...modal.props} key={modal.id} data-id={modal.id} />,
38+
<modal.Component {...modal.props} ref={modal.ref} key={modal.id} data-id={modal.id} />,
3839
modal.container ?? document.body
3940
);
4041
}

0 commit comments

Comments
 (0)