Add option to override description in result renderer

Signed-off-by: ivgo <ivgo@spreadgroup.com>
This commit is contained in:
ivgo
2023-04-13 14:50:52 +02:00
parent 7dc55a8b5b
commit 22963209d2
5 changed files with 58 additions and 15 deletions
+23
View File
@@ -127,3 +127,26 @@ export const myCustomBooleanRenderer: CheckResultRenderer = {
),
};
```
It's also possible to customize the description. Both strings and React components are accepted. As an example, you would like
to display another information if the check has failed. In such cases, you could do something like the following:
```tsx
// packages/app/src/components/myCustomBooleanRenderer.tsx
export const myCustomBooleanRenderer: CheckResultRenderer = {
type: 'boolean',
component: (checkResult: CheckResult) => (
<BooleanCheck checkResult={checkResult} />
),
description: (checkResult: CheckResult) => (
<>
{
checkResult.result
? checkResult.check.description // In case of success, return the same description
: `The check has failed! ${checkResult.check.description}` // Add a prefix text if the check failed
}
</>
),
};
```
+1
View File
@@ -35,6 +35,7 @@ export type Check = {
export type CheckResultRenderer = {
type: string;
component: (check: CheckResult) => React_2.ReactElement;
description?: (check: CheckResult) => string | React_2.ReactElement;
};
// @public (undocumented)
@@ -26,6 +26,7 @@ import { BooleanCheck } from './BooleanCheck';
export type CheckResultRenderer = {
type: string;
component: (check: CheckResult) => React.ReactElement;
description?: (check: CheckResult) => string | React.ReactElement;
};
/**
@@ -38,21 +38,29 @@ export const ScorecardsList = (props: { checkResults: CheckResult[] }) => {
return (
<List>
{checkResults.map((result, index) => (
<ListItem key={result.check.id}>
<ListItemText
key={index}
primary={result.check.name}
secondary={result.check.description}
className={classes.listItemText}
/>
{checkResultRenderers
.find(({ type }) => type === result.check.type)
?.component(result) ?? (
<Alert severity="error">Unknown type.</Alert>
)}
</ListItem>
))}
{checkResults.map((result, index) => {
const description = checkResultRenderers.find(
renderer => renderer.type === result.check.type,
)?.description;
return (
<ListItem key={result.check.id}>
<ListItemText
key={index}
primary={result.check.name}
secondary={
description ? description(result) : result.check.description
}
className={classes.listItemText}
/>
{checkResultRenderers
.find(({ type }) => type === result.check.type)
?.component(result) ?? (
<Alert severity="error">Unknown type.</Alert>
)}
</ListItem>
);
})}
</List>
);
};