Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { memo, forwardRef } from "react";
import { symToStr } from "tsafe/symToStr";
import { Fieldset, type FieldsetProps } from "./shared/Fieldset";

export type CheckboxProps = FieldsetProps.Common;
export type CheckboxProps = Omit<FieldsetProps.Checkbox, "type">;

/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-checkbox> */
export const Checkbox = memo(
forwardRef<HTMLFieldSetElement, CheckboxProps>((props, ref) => (
<Fieldset ref={ref} type="checkbox" {...props} />
<Fieldset ref={ref} {...props} type="checkbox" />
))
);

Expand Down
4 changes: 2 additions & 2 deletions src/RadioButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { memo, forwardRef } from "react";
import { symToStr } from "tsafe/symToStr";
import { Fieldset, type FieldsetProps } from "./shared/Fieldset";

export type RadioButtonsProps = FieldsetProps.Common & { name?: string };
export type RadioButtonsProps = Omit<FieldsetProps.Radio, "type">;

/** @see <https://components.react-dsfr.codegouv.studio/?path=/docs/components-radiobutton> */
export const RadioButtons = memo(
forwardRef<HTMLFieldSetElement, RadioButtonsProps>((props, ref) => (
<Fieldset ref={ref} type="radio" {...props} />
<Fieldset ref={ref} {...props} type="radio" />
))
);

Expand Down
31 changes: 22 additions & 9 deletions src/shared/Fieldset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import React, {
forwardRef,
type ReactNode,
type CSSProperties,
type InputHTMLAttributes,
type DetailedHTMLProps
type ComponentProps
} from "react";
import { symToStr } from "tsafe/symToStr";
import { assert } from "tsafe/assert";
Expand All @@ -27,11 +26,9 @@ export namespace FieldsetProps {
options: {
label: ReactNode;
hintText?: ReactNode;
nativeInputProps: DetailedHTMLProps<
InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
>;
nativeInputProps: ComponentProps<"input">;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's best to use the ComponetProps type helper from React.

}[];

/** Default: "vertical" */
orientation?: "vertical" | "horizontal";
/** Default: "default" */
Expand All @@ -47,9 +44,12 @@ export namespace FieldsetProps {
small?: boolean;
};

export type Radio = Common & {
export type Radio = Omit<Common, "options"> & {
type: "radio";
name?: string;
options: (Common["options"][number] & {
illustration?: ReactNode;
})[];
};

export type Checkbox = Common & {
Expand Down Expand Up @@ -79,6 +79,10 @@ export const Fieldset = memo(
...rest
} = props;

const isRichRadio =
type === "radio" &&
options.find(options => options.illustration !== undefined) !== undefined;

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

const id = useAnalyticsId({
Expand Down Expand Up @@ -145,9 +149,13 @@ export const Fieldset = memo(
</legend>
)}
<div className={cx(fr.cx("fr-fieldset__content"), classes.content)}>
{options.map(({ label, hintText, nativeInputProps }, i) => (
{options.map(({ label, hintText, nativeInputProps, ...rest }, i) => (
<div
className={fr.cx(`fr-${type}-group`, small && `fr-${type}-group--sm`)}
className={fr.cx(
`fr-${type}-group`,
isRichRadio && "fr-radio-rich",
small && `fr-${type}-group--sm`
)}
key={i}
>
<input
Expand All @@ -162,6 +170,11 @@ export const Fieldset = memo(
<span className={fr.cx("fr-hint-text")}>{hintText}</span>
)}
</label>
{"illustration" in rest && (
<div className={fr.cx("fr-radio-rich__img")}>
{rest.illustration}
</div>
)}
</div>
))}
</div>
Expand Down
31 changes: 31 additions & 0 deletions stories/RadioButtons.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import { RadioButtons, type RadioButtonsProps } from "../dist/RadioButtons";
import { sectionName } from "./sectionName";
import { getStoryFactory } from "./getStory";
Expand Down Expand Up @@ -384,3 +385,33 @@ export const Small = getStory({
}
]
});

export const Rich = getStory({
"legend": "Légende pour l’ensemble de champs",
"name": "radio",
"options": [
{
"label": "Label radio",
"nativeInputProps": {
"value": "value1"
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
},
{
"label": "Label radio 2",
"nativeInputProps": {
"value": "value2"
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
},
{
"label": "Label radio 3",
"nativeInputProps": {
"value": "value3"
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
}
],
"state": "default",
"stateRelatedMessage": "State description"
});
48 changes: 48 additions & 0 deletions test/types/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from "react";
import { Checkbox } from "../../src/Checkbox";

{
<Checkbox
legend="Label"
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
}
},
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
}
}
]}
/>;
}
{
<Checkbox
orientation="horizontal"
className={"fr-mt-1w"}
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
}
},
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
},
// @ts-expect-error
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
}
]}
/>;
}
101 changes: 101 additions & 0 deletions test/types/RadioButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from "react";
import { RadioButtons } from "../../src/RadioButtons";
import { RadioRichButtons } from "../../src/RadioRichButtons";

{
<RadioButtons
legend="Label"
name="name"
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
}
},
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
}
}
]}
/>;
}

{
<RadioRichButtons
legend="Label"
name="name"
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
},
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
}
]}
/>;
}

{
<RadioButtons
legend="Label"
name="name"
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
},
// @ts-expect-error
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
},
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
}
}
]}
/>;
}

{
<RadioRichButtons
legend="Label"
name="name"
options={[
{
"label": "Label 1",
"hintText": "Hint text",
"nativeInputProps": {
"value": 1
},
"illustration": <img src="https://placehold.it/100x100" alt="illustration" />
},
// @ts-expect-error
{
"label": "Label 2",
"hintText": "Hint text",
"nativeInputProps": {
"value": 2
}
}
]}
/>;
}