Skip to content

Commit 9e72fef

Browse files
committed
Fix passing an element to notify requires to wrap it in forwardRef
1 parent 9c8a01e commit 9e72fef

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

packages/ra-ui-materialui/src/layout/Notification.stories.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import {
66
useNotify,
77
useCloseNotification,
88
} from 'ra-core';
9-
import {
10-
Alert,
11-
Button,
12-
SnackbarContent,
13-
SnackbarContentProps,
14-
Stack,
15-
} from '@mui/material';
9+
import { Alert, Button, SnackbarContent, Stack } from '@mui/material';
1610

1711
import { Notification } from './Notification';
1812

@@ -214,11 +208,7 @@ export const ConsecutiveUndoable = ({
214208
</CoreAdminContext>
215209
);
216210

217-
// forwardRef is required for Snackbar to work (transitions) in React 18
218-
const CustomNotificationWithActionContent = React.forwardRef<
219-
HTMLDivElement,
220-
SnackbarContentProps
221-
>((props, ref) => {
211+
const CustomNotificationWithActionContent = props => {
222212
const closeNotification = useCloseNotification();
223213
const handleClick = () => {
224214
console.log('Custom action');
@@ -229,15 +219,16 @@ const CustomNotificationWithActionContent = React.forwardRef<
229219
message="Applied automatic changes"
230220
action={<Button onClick={handleClick}>Cancel</Button>}
231221
{...props}
232-
ref={ref}
233222
/>
234223
);
235-
});
224+
};
236225

237226
const CustomNotificationElementWithAction = () => {
238227
const notify = useNotify();
239228
React.useEffect(() => {
240-
notify(<CustomNotificationWithActionContent />);
229+
notify(<CustomNotificationWithActionContent />, {
230+
autoHideDuration: null,
231+
});
241232
}, [notify]);
242233
return null;
243234
};

packages/ra-ui-materialui/src/layout/Notification.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ export const Notification = (inProps: NotificationProps) => {
133133
...options
134134
} = notificationOptions || {};
135135

136+
console.log({ message, type: typeof message === 'string' });
137+
136138
return (
137139
<CloseNotificationContext.Provider value={handleRequestClose}>
138140
<StyledSnackbar
@@ -180,9 +182,11 @@ export const Notification = (inProps: NotificationProps) => {
180182
>
181183
{message &&
182184
typeof message !== 'string' &&
183-
React.isValidElement(message)
184-
? message
185-
: undefined}
185+
React.isValidElement(message) ? (
186+
// Wrap the element in a div to avoid forcing our users to wrap their component with forwardRef
187+
// which is required for animating the Snackbar
188+
<div>{message}</div>
189+
) : undefined}
186190
</StyledSnackbar>
187191
</CloseNotificationContext.Provider>
188192
);

0 commit comments

Comments
 (0)