Skip to content

Commit e3f759f

Browse files
authored
feat: Adds class-name and data- props to error boundaries (#4221)
1 parent ea2b35b commit e3f759f

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11532,6 +11532,13 @@ exports[`Components definition for error-boundary matches the snapshot: error-bo
1153211532
"functions": [],
1153311533
"name": "ErrorBoundary",
1153411534
"properties": [
11535+
{
11536+
"deprecatedTag": "Custom CSS is not supported. For testing and other use cases, use [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes).",
11537+
"description": "Adds the specified classes to the root element of the component.",
11538+
"name": "className",
11539+
"optional": true,
11540+
"type": "string",
11541+
},
1153511542
{
1153611543
"description": "Optional identifier for the error boundary instance.
1153711544

@@ -11601,6 +11608,15 @@ attribute \`data-awsui-boundary-id={errorBoundaryId}\` to support debugging.",
1160111608
"optional": true,
1160211609
"type": "ErrorBoundaryProps.I18nStrings",
1160311610
},
11611+
{
11612+
"deprecatedTag": "The usage of the \`id\` attribute is reserved for internal use cases. For testing and other use cases,
11613+
use [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes). If you must
11614+
use the \`id\` attribute, consider setting it on a parent element instead.",
11615+
"description": "Adds the specified ID to the root element of the component.",
11616+
"name": "id",
11617+
"optional": true,
11618+
"type": "string",
11619+
},
1160411620
{
1160511621
"description": "Callback invoked when an error is intercepted by the boundary.
1160611622
Use this function to record, log, or report errors (for example, to telemetry or monitoring systems).

src/error-boundary/__tests__/error-boundary.test.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ jest.mock('../../../lib/components/error-boundary/utils', () => ({
1717
}));
1818

1919
type RenderProps = Omit<
20-
Partial<ErrorBoundaryProps> & { i18nProvider?: Record<string, Record<string, string>> },
20+
Partial<ErrorBoundaryProps> & { i18nProvider?: Record<string, Record<string, string>> } & {
21+
[key: `data-${string}`]: string;
22+
},
2123
'children'
2224
>;
2325

@@ -623,6 +625,23 @@ describe('default behaviors', () => {
623625
});
624626
});
625627

628+
describe('base props passing to fallback', () => {
629+
test('class name is added to the fallback', () => {
630+
renderWithErrorBoundary(<b>{{}}</b>, { className: 'test' });
631+
expect(findBoundary()!.getElement()).toHaveClass('test');
632+
});
633+
634+
test('data-attributes are added to the fallback', () => {
635+
renderWithErrorBoundary(<b>{{}}</b>, { 'data-resource-guidance': 'off' });
636+
expect(findBoundary()!.getElement().dataset.resourceGuidance).toBe('off');
637+
});
638+
639+
test('other attributes are not added to the fallback', () => {
640+
renderWithErrorBoundary(<b>{{}}</b>, { 'aria-label': 'label' } as any);
641+
expect(findBoundary()!.getElement()).not.toHaveAttribute('aria-label');
642+
});
643+
});
644+
626645
describe('__awsui__ API', () => {
627646
test('error state can be forced with forceError() and cleared with clearForcedError()', () => {
628647
const onError = jest.fn();

src/error-boundary/fallback.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import IntlMessageFormat from 'intl-messageformat';
88
import InternalAlert from '../alert/internal';
99
import InternalButton from '../button/internal';
1010
import { useInternalI18n } from '../i18n/context';
11+
import { getBaseProps } from '../internal/base-component';
1112
import { ErrorBoundaryProps } from './interfaces';
1213
import { refreshPage } from './utils';
1314

@@ -17,7 +18,9 @@ import testUtilStyles from './test-classes/styles.css.js';
1718
export function ErrorBoundaryFallback({
1819
i18nStrings = {},
1920
renderFallback,
21+
...props
2022
}: Pick<ErrorBoundaryProps, 'renderFallback' | 'i18nStrings'>) {
23+
const baseProps = getBaseProps(props);
2124
const defaultSlots = {
2225
header: (
2326
<div className={clsx(styles.header, testUtilStyles.header)}>
@@ -36,7 +39,7 @@ export function ErrorBoundaryFallback({
3639
),
3740
};
3841
return (
39-
<div className={testUtilStyles.fallback}>
42+
<div {...baseProps} className={clsx(baseProps.className, testUtilStyles.fallback)}>
4043
{renderFallback?.(defaultSlots) ?? (
4144
<InternalAlert type="error" header={defaultSlots.header} action={defaultSlots.action}>
4245
{defaultSlots.description}

src/error-boundary/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import { ErrorInfo } from 'react';
55

6-
export interface ErrorBoundaryProps {
6+
import { BaseComponentProps } from '../internal/base-component';
7+
8+
export interface ErrorBoundaryProps extends BaseComponentProps {
79
/**
810
* Optional identifier for the error boundary instance.
911
*

0 commit comments

Comments
 (0)