Skip to content

Commit ab1371c

Browse files
committed
Do not use a separate component for RichRadioButton
1 parent 765f233 commit ab1371c

File tree

5 files changed

+47
-466
lines changed

5 files changed

+47
-466
lines changed

src/RadioButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { memo, forwardRef } from "react";
22
import { symToStr } from "tsafe/symToStr";
33
import { Fieldset, type FieldsetProps } from "./shared/Fieldset";
44

5-
export type RadioButtonsProps = Omit<FieldsetProps.Radio.Regular, "type">;
5+
export type RadioButtonsProps = Omit<FieldsetProps.Radio, "type">;
66

77
/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-radiobutton> */
88
export const RadioButtons = memo(

src/RadioRichButtons.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/shared/Fieldset.tsx

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,17 @@ export namespace FieldsetProps {
4444
small?: boolean;
4545
};
4646

47-
export type Radio = Radio.Regular | Radio.Rich;
48-
49-
export namespace Radio {
50-
type CommonRadio = Common & {
51-
type: "radio";
52-
name?: string;
53-
};
54-
55-
export type Regular = CommonRadio & {
56-
rich?: false;
57-
};
58-
59-
export type Rich = CommonRadio & {
60-
rich: true;
61-
options: (Common["options"][number] & {
62-
illustration: ReactNode;
63-
})[];
64-
};
65-
}
47+
export type Radio = Omit<Common, "options"> & {
48+
type: "radio";
49+
name?: string;
50+
options: (Common["options"][number] & {
51+
illustration?: ReactNode;
52+
})[];
53+
};
6654

6755
export type Checkbox = Common & {
6856
type: "checkbox";
6957
name?: never;
70-
rich?: never;
7158
};
7259
}
7360

@@ -89,10 +76,13 @@ export const Fieldset = memo(
8976
type,
9077
name: name_props,
9178
small = false,
92-
rich,
9379
...rest
9480
} = props;
9581

82+
const isRichRadio =
83+
type === "radio" &&
84+
options.find(options => options.illustration !== undefined) !== undefined;
85+
9686
assert<Equals<keyof typeof rest, never>>();
9787

9888
const id = useAnalyticsId({
@@ -159,11 +149,11 @@ export const Fieldset = memo(
159149
</legend>
160150
)}
161151
<div className={cx(fr.cx("fr-fieldset__content"), classes.content)}>
162-
{options.map(({ label, hintText, nativeInputProps }, i) => (
152+
{options.map(({ label, hintText, nativeInputProps, ...rest }, i) => (
163153
<div
164154
className={fr.cx(
165155
`fr-${type}-group`,
166-
!!rich && "fr-radio-rich",
156+
isRichRadio && "fr-radio-rich",
167157
small && `fr-${type}-group--sm`
168158
)}
169159
key={i}
@@ -180,9 +170,9 @@ export const Fieldset = memo(
180170
<span className={fr.cx("fr-hint-text")}>{hintText}</span>
181171
)}
182172
</label>
183-
{!!rich && (
173+
{"illustration" in rest && (
184174
<div className={fr.cx("fr-radio-rich__img")}>
185-
{options[i].illustration}
175+
{rest.illustration}
186176
</div>
187177
)}
188178
</div>

stories/RadioButtons.stories.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from "react";
12
import { RadioButtons, type RadioButtonsProps } from "../dist/RadioButtons";
23
import { sectionName } from "./sectionName";
34
import { getStoryFactory } from "./getStory";
@@ -384,3 +385,33 @@ export const Small = getStory({
384385
}
385386
]
386387
});
388+
389+
export const Rich = getStory({
390+
"legend": "Légende pour l’ensemble de champs",
391+
"name": "radio",
392+
"options": [
393+
{
394+
"label": "Label radio",
395+
"nativeInputProps": {
396+
"value": "value1"
397+
},
398+
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
399+
},
400+
{
401+
"label": "Label radio 2",
402+
"nativeInputProps": {
403+
"value": "value2"
404+
},
405+
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
406+
},
407+
{
408+
"label": "Label radio 3",
409+
"nativeInputProps": {
410+
"value": "value3"
411+
},
412+
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
413+
}
414+
],
415+
"state": "default",
416+
"stateRelatedMessage": "State description"
417+
});

0 commit comments

Comments
 (0)