Skip to content

Commit 1fab139

Browse files
committed
Add ServerComponent declinaison of the Sumary component
1 parent c5c0fa9 commit 1fab139

File tree

9 files changed

+67
-37
lines changed

9 files changed

+67
-37
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133
"./mui": "./dist/mui.js",
134134
"./Tile": "./dist/Tile.js",
135135
"./Tabs": "./dist/Tabs.js",
136-
"./Summary": "./dist/Summary.js",
136+
"./Summary/ServerComponent": "./dist/Summary/ServerComponent.js",
137+
"./Summary": "./dist/Summary/index.js",
137138
"./Stepper": "./dist/Stepper.js",
138139
"./SkipLinks": "./dist/SkipLinks.js",
139140
"./Quote": "./dist/Quote.js",

src/Pagination.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React, { memo, forwardRef } from "react";
22
import { symToStr } from "tsafe/symToStr";
33
import { assert } from "tsafe/assert";
44
import type { Equals } from "tsafe";
5-
65
import { fr } from "./fr";
76
import { cx } from "./tools/cx";
8-
import { createComponentI18nApi } from "./i18n/i18n";
7+
import { createComponentI18nApi } from "./i18n/createComponentI18nApi";
8+
import { useLang } from "./i18n/useLang";
99
import { RegisteredLinkProps, getLink } from "./link";
1010

1111
export type PaginationProps = {
@@ -73,7 +73,7 @@ export const Pagination = memo(
7373

7474
assert<Equals<keyof typeof rest, never>>();
7575

76-
const { t } = useTranslation();
76+
const { t } = getTranslation(useLang());
7777

7878
const { Link } = getLink();
7979

@@ -177,7 +177,7 @@ export const Pagination = memo(
177177

178178
Pagination.displayName = symToStr({ Pagination });
179179

180-
const { useTranslation, addPaginationTranslations } = createComponentI18nApi({
180+
const { getTranslation, addPaginationTranslations } = createComponentI18nApi({
181181
"componentName": symToStr({ Pagination }),
182182
"frMessages": {
183183
"first page": "Première page",

src/Summary.tsx renamed to src/Summary/ServerComponent.tsx

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
1-
"use client";
2-
31
import React, { memo, forwardRef, useId } from "react";
42
import { symToStr } from "tsafe/symToStr";
53
import { assert } from "tsafe/assert";
64
import type { Equals } from "tsafe";
7-
import { createComponentI18nApi } from "./i18n/createComponentI18nApi";
8-
import { useLang } from "./i18n/useLang";
9-
import { cx } from "./tools/cx";
10-
import { fr } from "./fr";
11-
import { getLink } from "./link";
12-
import type { RegisteredLinkProps } from "./link";
13-
14-
type SummaryLink = {
15-
text: string;
16-
linkProps: RegisteredLinkProps;
17-
};
18-
19-
export type SummaryProps = {
20-
className?: string;
21-
links: SummaryLink[];
22-
title?: string;
23-
/** Default: "p" */
24-
as?: "p" | "h2" | "h3" | "h4" | "h5" | "h6";
25-
classes?: Partial<Record<"root" | "title" | "link", string>>;
26-
};
5+
import { createComponentI18nApi } from "../i18n/createComponentI18nApi";
6+
import { cx } from "../tools/cx";
7+
import { fr } from "../fr";
8+
import { getLink } from "../link";
9+
import type { SummaryProps } from "./Summary";
2710

2811
/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-summary> */
2912
export const Summary = memo(
30-
forwardRef<HTMLDivElement, SummaryProps>((props, ref) => {
31-
const { className, links, as = "p", title, classes = {}, ...rest } = props;
13+
forwardRef<HTMLDivElement, SummaryProps & { lang?: string }>((props, ref) => {
14+
const { className, links, as = "p", title, classes = {}, lang = "fr", ...rest } = props;
3215

33-
const { t } = getTranslation(useLang());
16+
const { t } = getTranslation(lang);
3417

3518
const titleId = useId();
3619
const summaryTitle = title ?? t("title");

src/Summary/Summary.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use client";
2+
3+
import React from "react";
4+
import type { RegisteredLinkProps } from "../link";
5+
import { Summary as SummarySc } from "./ServerComponent";
6+
import { useLang } from "../i18n/useLang";
7+
8+
export type SummaryProps = {
9+
className?: string;
10+
links: {
11+
text: string;
12+
linkProps: RegisteredLinkProps;
13+
}[];
14+
title?: string;
15+
/** Default: "p" */
16+
as?: "p" | "h2" | "h3" | "h4" | "h5" | "h6";
17+
classes?: Partial<Record<"root" | "title" | "link", string>>;
18+
};
19+
20+
export function Summary(props: SummaryProps) {
21+
return <SummarySc {...props} lang={useLang()} />;
22+
}
23+
24+
export default Summary;

src/Summary/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { addSummaryTranslations } from "./ServerComponent";
2+
import { Summary } from "./Summary";
3+
export { SummaryProps } from "./Summary";
4+
5+
export { Summary };
6+
7+
export default Summary;

src/i18n/createComponentI18nApi.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use client";
2-
31
import { useMemo } from "react";
42

53
function getLanguageBestApprox<Language extends string>(params: {

src/scripts/list-exports.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from "fs";
22
import { join as pathJoin } from "path";
33
import { getProjectRoot } from "../bin/tools/getProjectRoot";
44
import { exclude } from "tsafe/exclude";
5-
import { execSync } from "child_process";
65
import { same } from "evt/tools/inDepth/same";
76
import { capitalize } from "tsafe/capitalize";
87

@@ -45,7 +44,22 @@ const newExports = {
4544
continue;
4645
}
4746

48-
return [basename, relativePath];
47+
const serverComponentRelativePath = pathJoin(
48+
basename,
49+
"ServerComponent.tsx"
50+
);
51+
52+
return [
53+
[basename, relativePath],
54+
...(fs.existsSync(pathJoin(srcDirPath, serverComponentRelativePath))
55+
? [
56+
[
57+
pathJoin(basename, "ServerComponent"),
58+
serverComponentRelativePath
59+
]
60+
]
61+
: [])
62+
] as const;
4963
}
5064

5165
return undefined;
@@ -60,12 +74,14 @@ const newExports = {
6074

6175
const componentName = match[1];
6276

63-
return [componentName, `${componentName}.tsx`];
77+
return [[componentName, `${componentName}.tsx`]];
6478
}
6579

6680
return undefined;
6781
})
6882
.filter(exclude(undefined))
83+
.flat()
84+
/*
6985
.filter(([, relativePath]) => {
7086
try {
7187
execSync(`git ls-files --error-unmatch ${pathJoin(srcDirPath, relativePath)}`, {
@@ -77,6 +93,7 @@ const newExports = {
7793
7894
return true;
7995
})
96+
*/
8097
.filter(exclude(undefined))
8198
.sort()
8299
.reverse()

stories/Summary.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { meta, getStory } = getStoryFactory({
77
"wrappedComponent": { Summary },
88
"description": `
99
- [See DSFR documentation](//www.systeme-de-design.gouv.fr/elements-d-interface/composants/sommaire)
10-
- [See source code](//github.com/codegouvfr/react-dsfr/blob/main/src/Summary.tsx)`,
10+
- [See source code](//github.com/codegouvfr/react-dsfr/blob/main/src/Summary/ServerComponent.tsx)`,
1111
"disabledProps": ["lang"]
1212
});
1313

test/integration/next-appdir/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Alert } from "@codegouvfr/react-dsfr/Alert";
33
import { Badge } from "@codegouvfr/react-dsfr/Badge";
44
import { Card } from "@codegouvfr/react-dsfr/Card";
55
import { Tabs } from "@codegouvfr/react-dsfr/Tabs";
6-
import { Summary } from "@codegouvfr/react-dsfr/Summary";
6+
import { Summary } from "@codegouvfr/react-dsfr/Summary/ServerComponent";
77

88
export default function Page() {
99

0 commit comments

Comments
 (0)