Skip to content

Commit

Permalink
Merge pull request #201 from sbourdon13/feat/badge-span
Browse files Browse the repository at this point in the history
feat(Badge): element can be a span (ref #200)
  • Loading branch information
garronej authored Oct 15, 2023
2 parents e4a36b9 + 12b4b12 commit b9e8ae2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type BadgeProps = {
severity?: AlertProps.Severity | "new";
small?: boolean;
noIcon?: boolean;
/** Default: "p" */
as?: "p" | "span";
children: NonNullable<ReactNode>;
};

Expand All @@ -23,6 +25,7 @@ export const Badge = memo(
const {
id: props_id,
className,
as = "p",
style,
severity,
small: isSmall = false,
Expand All @@ -38,24 +41,24 @@ export const Badge = memo(
"explicitlyProvidedId": props_id
});

return (
<p
id={id}
className={cx(
return React.createElement(
as,
{
"className": cx(
fr.cx(
"fr-badge",
severity !== undefined && `fr-badge--${severity}`,
{ "fr-badge--sm": isSmall },
{ "fr-badge--no-icon": noIcon || severity === undefined }
),
className
)}
style={style}
ref={ref}
{...rest}
>
{children}
</p>
),
id,
style,
ref,
...rest
},
<>{children}</>
);
})
);
Expand Down
12 changes: 12 additions & 0 deletions stories/Badge.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ const { meta, getStory } = getStoryFactory({
"type": { "name": "boolean" },
"description": "Set small badge size (`sm`) when true"
},
"as": {
"options": (() => {
const options = ["p", "span", undefined] as const;

assert<Equals<typeof options[number], BadgeProps["as"]>>();

return options;
})(),
"control": { type: "select", labels: { null: "default p element" } },
"description":
"You can specify a 'span' element instead of default 'p' if the badge is inside a `<p>`."
},
"children": {
"type": { "name": "string", "required": true },
"description": "Label to display on the badge"
Expand Down
24 changes: 24 additions & 0 deletions stories/Header.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from "react";
import { Header } from "../dist/Header";
import { Badge } from "../dist/Badge";
import { MainNavigation } from "../dist/MainNavigation";
import { sectionName } from "./sectionName";
import { getStoryFactory } from "./getStory";
Expand Down Expand Up @@ -115,6 +116,29 @@ export const SimpleHeaderWithServiceTitleAndTagline = getStory({
"serviceTagline": "baseline - précisions sur l'organisation"
});

export const SimpleHeaderWithServiceTitleAndBetaBadge = getStory({
"id": "fr-header-simple-header-with-service-title-and-tagline",
"brandTop": (
<>
INTITULE
<br />
OFFICIEL
</>
),
"homeLinkProps": {
"href": "/",
"title": "Accueil - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)"
},
"serviceTitle": (
<>
Nom du site / service{" "}
<Badge noIcon severity="success" as="span">
Beta
</Badge>
</>
)
});

export const HeaderWithQuickAccessItems = getStory(
{
"id": "fr-header-header-with-quick-access-items",
Expand Down

0 comments on commit b9e8ae2

Please sign in to comment.