Skip to content

Commit d623c83

Browse files
committed
Rework the Badge component
1 parent 791bf3b commit d623c83

File tree

2 files changed

+54
-56
lines changed

2 files changed

+54
-56
lines changed

src/Badge.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@ import { assert } from "tsafe/assert";
44
import type { Equals } from "tsafe";
55
import { fr } from "./lib";
66
import { cx } from "./lib/tools/cx";
7-
8-
// We make users import dsfr.css, so we don't need to import the scoped CSS
9-
// but in the future if we have a complete component coverage it
10-
// we could stop requiring users to import the hole CSS and only import on a
11-
// per component basis.
12-
import "./dsfr/component/badge/badge.css";
7+
import type { AlertProps } from "./Alert";
138

149
export type BadgeProps = {
1510
className?: string;
16-
severity?: BadgeProps.Severity;
17-
isSmall?: boolean;
11+
severity?: AlertProps.Severity | "new";
12+
small?: boolean;
1813
noIcon?: boolean;
1914
label: NonNullable<ReactNode>;
2015
};
2116

22-
export namespace BadgeProps {
23-
export type Severity = "info" | "success" | "error" | "warning" | "new";
24-
}
25-
2617
/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-badge> */
2718
export const Badge = memo(
2819
forwardRef<HTMLDivElement, BadgeProps>((props, ref) => {
29-
const { className, severity, label, isSmall, noIcon, ...rest } = props;
20+
const {
21+
className,
22+
severity,
23+
label,
24+
small: isSmall = false,
25+
noIcon = false,
26+
...rest
27+
} = props;
3028

3129
assert<Equals<keyof typeof rest, never>>();
3230

@@ -35,9 +33,9 @@ export const Badge = memo(
3533
className={cx(
3634
fr.cx(
3735
"fr-badge",
38-
{ [`fr-badge--${severity}`]: severity != null },
36+
severity !== undefined && `fr-badge--${severity}`,
3937
{ "fr-badge--sm": isSmall },
40-
{ "fr-badge--no-icon": noIcon || severity == null }
38+
{ "fr-badge--no-icon": noIcon || severity === undefined }
4139
),
4240
className
4341
)}

stories/Badge.stories.tsx

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,103 @@ import type { Equals } from "tsafe";
77

88
const { meta, getStory } = getStoryFactory<BadgeProps>({
99
sectionName,
10-
wrappedComponent: { Badge },
10+
"wrappedComponent": { Badge },
1111
description: `
1212
- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/badge)
1313
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Badge.tsx)`,
14-
argTypes: {
15-
severity: {
16-
options: (() => {
17-
const severities = ["success", "warning", "info", "error", "new"] as const;
14+
"argTypes": {
15+
"severity": {
16+
"options": (() => {
17+
const options = ["success", "warning", "info", "error", "new", undefined] as const;
1818

19-
assert<Equals<typeof severities[number], BadgeProps.Severity>>();
19+
assert<Equals<typeof options[number], BadgeProps["severity"]>>();
2020

21-
return [null, ...severities];
21+
return options;
2222
})(),
23-
control: { type: "select", labels: { null: "no severity" } }
23+
"control": { type: "select", labels: { null: "no severity" } }
2424
},
25-
noIcon: {
26-
type: { name: "boolean" },
27-
description: "Remove badge icon when true"
25+
"noIcon": {
26+
"type": { "name": "boolean" },
27+
"description": "Remove badge icon when true"
2828
},
29-
isSmall: {
30-
type: { name: "boolean" },
31-
description: "Set small badge size (`sm`) when true"
29+
"small": {
30+
"type": { "name": "boolean" },
31+
"description": "Set small badge size (`sm`) when true"
3232
},
33-
label: {
34-
type: { name: "string", required: true },
35-
description: "Label to display on the badge"
33+
"label": {
34+
"type": { "name": "string", "required": true },
35+
"description": "Label to display on the badge"
3636
}
3737
},
38-
disabledProps: ["lang"]
38+
"disabledProps": ["lang"]
3939
});
4040

4141
export default meta;
4242

4343
export const Default = getStory({
44-
severity: "success",
45-
label: "Label badge"
44+
"severity": "success",
45+
"label": "Label badge"
4646
});
4747

4848
export const BadgeWithoutSeverity = getStory(
4949
{
50-
label: "Label"
50+
"label": "Label"
5151
},
5252
{
53-
description: "Medium info `Badge` with icon"
53+
"description": "Medium info `Badge` with icon"
5454
}
5555
);
5656

5757
export const InfoBadge = getStory(
5858
{
59-
severity: "info",
60-
label: "Label info"
59+
"severity": "info",
60+
"label": "Label info"
6161
},
6262
{
63-
description: "Medium info `Badge` with icon"
63+
"description": "Medium info `Badge` with icon"
6464
}
6565
);
6666

6767
export const WarningBadge = getStory(
6868
{
69-
severity: "warning",
70-
noIcon: false,
71-
label: 'Label "warning"'
69+
"severity": "warning",
70+
"noIcon": false,
71+
"label": 'Label "warning"'
7272
},
7373
{
74-
description: "Medium warning `Badge` with icon"
74+
"description": "Medium warning `Badge` with icon"
7575
}
7676
);
7777

7878
export const SuccessBadge = getStory(
7979
{
80-
severity: "success",
81-
noIcon: true,
82-
label: "Label success"
80+
"severity": "success",
81+
"noIcon": true,
82+
"label": "Label success"
8383
},
8484
{
85-
description: "Medium success `Badge` without icon"
85+
"description": "Medium success `Badge` without icon"
8686
}
8787
);
8888

8989
export const ErrorBadge = getStory(
9090
{
91-
severity: "error",
92-
noIcon: true,
93-
label: "Label error"
91+
"severity": "error",
92+
"noIcon": true,
93+
"label": "Label error"
9494
},
9595
{
96-
description: "Medium error `Badge` without icon"
96+
"description": "Medium error `Badge` without icon"
9797
}
9898
);
9999

100100
export const NewBadge = getStory(
101101
{
102-
severity: "new",
103-
isSmall: true,
104-
label: "Label new"
102+
"severity": "new",
103+
"small": true,
104+
"label": "Label new"
105105
},
106106
{
107-
description: "Small new `Badge` with icon"
107+
"description": "Small new `Badge` with icon"
108108
}
109109
);

0 commit comments

Comments
 (0)