Skip to content

Commit 525b32b

Browse files
authored
feat: Allows onError callback to not use event handler (#98)
1 parent 38a0914 commit 525b32b

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import * as React from 'react';
4+
import { NonCancelableEventHandler } from '../../internal/events';
5+
6+
export interface ErrorBoundaryProps {
7+
/**
8+
* React content.
9+
*/
10+
children: React.ReactNode;
11+
/**
12+
* A special callback that fires when an error is captured.
13+
*/
14+
onError: (error: Error, errorInfo: React.ErrorInfo) => void;
15+
/**
16+
* A callback that fires when the user clicks on the refresh button.
17+
*/
18+
onRefresh?: NonCancelableEventHandler;
19+
}
20+
21+
export default function ErrorBoundary({ children }: ErrorBoundaryProps): JSX.Element {
22+
return <div>{children}</div>;
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "."
5+
},
6+
"include": ["./**/*.tsx"]
7+
}

src/components/component-definition.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ export function buildComponentDefinition(
4444
checker: ts.TypeChecker
4545
): ComponentDefinition {
4646
const regions = props.filter(prop => prop.type === 'React.ReactNode');
47-
const events = props.filter(prop => prop.name.match(/^on[A-Z]/));
47+
const events = props.filter(prop => {
48+
// The onError event handler of the error boundary component does not follow the
49+
// event handlers convention and is categorized to properties instead.
50+
if (name === 'ErrorBoundary' && prop.name === 'onError') {
51+
return false;
52+
} else {
53+
return prop.name.match(/^on[A-Z]/);
54+
}
55+
});
4856
const onlyProps = props.filter(prop => !events.includes(prop) && !regions.includes(prop));
49-
5057
return {
5158
name,
5259
dashCaseName,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Error boundary > should have correct properties definitions 1`] = `
4+
[
5+
{
6+
"dashCaseName": "error-boundary",
7+
"description": undefined,
8+
"events": [
9+
{
10+
"cancelable": false,
11+
"deprecatedTag": undefined,
12+
"description": "A callback that fires when the user clicks on the refresh button.",
13+
"detailInlineType": undefined,
14+
"detailType": undefined,
15+
"name": "onRefresh",
16+
"systemTags": undefined,
17+
},
18+
],
19+
"functions": [],
20+
"name": "ErrorBoundary",
21+
"properties": [
22+
{
23+
"analyticsTag": undefined,
24+
"defaultValue": undefined,
25+
"deprecatedTag": undefined,
26+
"description": "A special callback that fires when an error is captured.",
27+
"i18nTag": undefined,
28+
"inlineType": {
29+
"name": "(error: Error, errorInfo: React.ErrorInfo) => void",
30+
"parameters": [
31+
{
32+
"name": "error",
33+
"type": "Error",
34+
},
35+
{
36+
"name": "errorInfo",
37+
"type": "React.ErrorInfo",
38+
},
39+
],
40+
"returnType": "void",
41+
"type": "function",
42+
},
43+
"name": "onError",
44+
"optional": false,
45+
"systemTags": undefined,
46+
"type": "(error: Error, errorInfo: React.ErrorInfo) => void",
47+
"visualRefreshTag": undefined,
48+
},
49+
],
50+
"regions": [
51+
{
52+
"deprecatedTag": undefined,
53+
"description": "React content.",
54+
"displayName": undefined,
55+
"i18nTag": undefined,
56+
"isDefault": true,
57+
"name": "children",
58+
"systemTags": undefined,
59+
"visualRefreshTag": undefined,
60+
},
61+
],
62+
"releaseStatus": "stable",
63+
"systemTags": undefined,
64+
},
65+
]
66+
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { describe, expect, test } from 'vitest';
5+
import { buildProject } from './test-helpers';
6+
7+
describe('Error boundary', () => {
8+
test('should have correct properties definitions', () => {
9+
const result = buildProject('error-boundary');
10+
expect(result).toMatchSnapshot();
11+
});
12+
});

0 commit comments

Comments
 (0)