Skip to content

Commit cb1a71c

Browse files
authored
fix: Toaster should merge user onKeyDown handler (#34982)
1 parent 5c8f925 commit cb1a71c

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: Toaster should merge user onKeyDown handler",
4+
"packageName": "@fluentui/react-toast",
5+
"email": "lingfangao@hotmail.com",
6+
"dependentChangeType": "patch"
7+
}

packages/react-components/react-toast/library/src/components/Toast/Toast.cy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ describe('Toast', () => {
434434
<button id="make" onClick={makeToast}>
435435
Make toast
436436
</button>
437-
<Toaster shortcuts={{ focus: e => e.ctrlKey && e.key === 'm' }} />
437+
<Toaster onKeyDown={() => console.log('test')} shortcuts={{ focus: e => e.ctrlKey && e.key === 'm' }} />
438438
</>
439439
);
440440
};
@@ -661,7 +661,7 @@ describe('Toast', () => {
661661
<button id="make" onClick={makeToast}>
662662
Make toast
663663
</button>
664-
<Toaster shortcuts={{ focus: e => e.ctrlKey && e.key === 'm' }} />
664+
<Toaster onKeyDown={() => console.log('foo')} shortcuts={{ focus: e => e.ctrlKey && e.key === 'm' }} />
665665
</>
666666
);
667667
};

packages/react-components/react-toast/library/src/components/Toaster/useToaster.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ export const useToaster_unstable = (props: ToasterProps): ToasterState => {
3232
const announce = React.useCallback<Announce>((message, options) => announceRef.current(message, options), []);
3333
const { dir } = useFluent();
3434

35-
const rootProps = slot.always(getIntrinsicElementProps<ExtractSlotProps<Slot<'div'>>>('div', rest), {
36-
elementType: 'div',
37-
});
35+
const { onKeyDown: onKeyDownProp, ...rootProps } = slot.always(
36+
getIntrinsicElementProps<ExtractSlotProps<Slot<'div'>>>('div', rest),
37+
{
38+
elementType: 'div',
39+
},
40+
);
3841
const focusableGroupAttr = useFocusableGroup({
3942
tabBehavior: 'limited-trap-focus',
4043
ignoreDefaultKeydown: { Escape: true },
@@ -44,7 +47,7 @@ export const useToaster_unstable = (props: ToasterProps): ToasterState => {
4447
e.preventDefault();
4548
closeAllToasts();
4649
}
47-
props.onKeyDown?.(e);
50+
onKeyDownProp?.(e);
4851
});
4952
const usePositionSlot = (toastPosition: ToastPosition) => {
5053
const focusManagementRef = useToasterFocusManagement_unstable(pauseAllToasts, playAllToasts);

0 commit comments

Comments
 (0)