Skip to content

Commit cb8609a

Browse files
committed
Feat: Pass data attributes through on Toast
1 parent 1457ded commit cb8609a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/@react-aria/toast/src/useToast.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {AriaLabelingProps, DOMAttributes, FocusableElement, RefObject} from '@re
1616
import intlMessages from '../intl/*.json';
1717
import {QueuedToast, ToastState} from '@react-stately/toast';
1818
import {useEffect, useState} from 'react';
19-
import {useId, useSlotId} from '@react-aria/utils';
19+
import {useId, useSlotId, filterDOMProps, mergeProps} from '@react-aria/utils';
2020
import {useLocalizedStringFormatter} from '@react-aria/i18n';
2121

2222
export interface AriaToastProps<T> extends AriaLabelingProps {
@@ -72,16 +72,16 @@ export function useToast<T>(props: AriaToastProps<T>, state: ToastState<T>, ref:
7272
setIsVisible(true);
7373
}, []);
7474

75+
let toastProps = filterDOMProps(props, {labelable: true});
76+
7577
return {
76-
toastProps: {
78+
toastProps: mergeProps(toastProps, {
7779
role: 'alertdialog',
7880
'aria-modal': 'false',
79-
'aria-label': props['aria-label'],
8081
'aria-labelledby': props['aria-labelledby'] || titleId,
8182
'aria-describedby': props['aria-describedby'] || descriptionId,
82-
'aria-details': props['aria-details'],
8383
tabIndex: 0
84-
},
84+
} as const),
8585
contentProps: {
8686
role: 'alert',
8787
'aria-atomic': 'true',

packages/@react-aria/toast/test/useToast.test.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ describe('useToast', () => {
2626
close.mockClear();
2727
});
2828

29-
let renderToastHook = (toast, state, wrapper) => {
30-
let {result} = renderHook(() => useToast({toast}, state, useRef(document.createElement('div'))), {wrapper});
29+
let renderToastHook = (props, state, wrapper) => {
30+
let {result} = renderHook(() => useToast(props, state, useRef(document.createElement('div'))), {wrapper});
3131
return result.current;
3232
};
3333

3434
it('handles defaults', function () {
35-
let {closeButtonProps, toastProps, contentProps, titleProps} = renderToastHook({}, {close});
35+
let {closeButtonProps, toastProps, contentProps, titleProps} = renderToastHook({toast:{}}, {close});
3636

3737
expect(toastProps.role).toBe('alertdialog');
3838
expect(contentProps.role).toBe('alert');
@@ -42,12 +42,18 @@ describe('useToast', () => {
4242
});
4343

4444
it('handles close button', function () {
45-
let {closeButtonProps} = renderToastHook({key: 1}, {close});
45+
let {closeButtonProps} = renderToastHook({toast:{key: 1}}, {close});
4646
closeButtonProps.onPress();
4747

4848
expect(close).toHaveBeenCalledTimes(1);
4949
expect(close).toHaveBeenCalledWith(1);
5050
});
51+
52+
it('passes through data attributes', function () {
53+
let {toastProps} = renderToastHook({toast:{}, 'data-test-id': 'toast'}, {close});
54+
55+
expect(toastProps['data-test-id']).toBe('toast');
56+
});
5157
});
5258

5359
describe('single toast at a time', () => {
@@ -71,7 +77,7 @@ describe('single toast at a time', () => {
7177
await user.tab();
7278
await user.keyboard('{Enter}');
7379
await user.keyboard('{Enter}');
74-
80+
7581
let toast = tree.getByRole('alertdialog');
7682
expect(toast.textContent).toContain('Mmmmm toast 2x');
7783
let closeButton = within(toast).getByRole('button');

0 commit comments

Comments
 (0)