Skip to content

Commit 765f233

Browse files
committed
Review radio-rich-button
1 parent ee455d8 commit 765f233

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
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, "type">;
5+
export type RadioButtonsProps = Omit<FieldsetProps.Radio.Regular, "type">;
66

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

src/RadioRichButtons.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 RadioRichButtonsProps = Omit<FieldsetProps.RadioRich, "type" | "rich">;
5+
export type RadioRichButtonsProps = Omit<FieldsetProps.Radio.Rich, "type" | "rich">;
66

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

src/shared/Fieldset.tsx

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import React, {
44
forwardRef,
55
type ReactNode,
66
type CSSProperties,
7-
type InputHTMLAttributes,
8-
type DetailedHTMLProps
7+
type ComponentProps
98
} from "react";
109
import { symToStr } from "tsafe/symToStr";
1110
import { assert } from "tsafe/assert";
@@ -14,27 +13,21 @@ import { cx } from "../tools/cx";
1413
import { fr } from "../fr";
1514
import { useAnalyticsId } from "../tools/useAnalyticsId";
1615

17-
export type FieldsetProps = FieldsetProps.Radio | FieldsetProps.Checkbox | FieldsetProps.RadioRich;
16+
export type FieldsetProps = FieldsetProps.Radio | FieldsetProps.Checkbox;
1817

1918
export namespace FieldsetProps {
20-
export type RegularOption = {
21-
label: ReactNode;
22-
hintText?: ReactNode;
23-
nativeInputProps: DetailedHTMLProps<
24-
InputHTMLAttributes<HTMLInputElement>,
25-
HTMLInputElement
26-
>;
27-
};
28-
export type RadioRichOption = RegularOption & {
29-
illustration: ReactNode;
30-
};
3119
export type Common = {
3220
className?: string;
3321
id?: string;
3422
classes?: Partial<Record<"root" | "legend" | "content", string>>;
3523
style?: CSSProperties;
3624
legend?: ReactNode;
3725
hintText?: ReactNode;
26+
options: {
27+
label: ReactNode;
28+
hintText?: ReactNode;
29+
nativeInputProps: ComponentProps<"input">;
30+
}[];
3831

3932
/** Default: "vertical" */
4033
orientation?: "vertical" | "horizontal";
@@ -51,25 +44,30 @@ export namespace FieldsetProps {
5144
small?: boolean;
5245
};
5346

54-
export type Radio = Common & {
55-
type: "radio";
56-
name?: string;
57-
rich?: false;
58-
options: RegularOption[];
59-
};
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+
}
6066

6167
export type Checkbox = Common & {
6268
type: "checkbox";
6369
name?: never;
64-
rich?: false;
65-
options: RegularOption[];
66-
};
67-
68-
export type RadioRich = Common & {
69-
type: "radio";
70-
name?: string;
71-
rich: true;
72-
options: RadioRichOption[];
70+
rich?: never;
7371
};
7472
}
7573

@@ -165,7 +163,7 @@ export const Fieldset = memo(
165163
<div
166164
className={fr.cx(
167165
`fr-${type}-group`,
168-
rich && "fr-radio-rich",
166+
!!rich && "fr-radio-rich",
169167
small && `fr-${type}-group--sm`
170168
)}
171169
key={i}
@@ -182,7 +180,7 @@ export const Fieldset = memo(
182180
<span className={fr.cx("fr-hint-text")}>{hintText}</span>
183181
)}
184182
</label>
185-
{rich && (
183+
{!!rich && (
186184
<div className={fr.cx("fr-radio-rich__img")}>
187185
{options[i].illustration}
188186
</div>

0 commit comments

Comments
 (0)