Skip to content

Commit aaa4366

Browse files
committed
fix: radio button issue with default checked and checked values
1 parent da1d268 commit aaa4366

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

src/components/Radios/Radios.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
$govuk-images-path: "~govuk-frontend/dist/govuk/assets/images/";
22
$govuk-fonts-path: "~govuk-frontend/dist/govuk/assets/fonts/";
3-
$govuk-assets-path: "~govuk-frontend/govuk/assets/";
3+
$govuk-assets-path: "~govuk-frontend/dist/govuk/assets/";
44
$govuk-font-family: "Arial", sans-serif;
55

6-
// $govuk-brand-colour: #87c426;
7-
6+
@import "~govuk-frontend/dist/govuk/base";
7+
@import "~govuk-frontend/dist/govuk/core/all";
88
@import "~govuk-frontend/dist/govuk/components/radios/radios";

src/components/Radios/Radios.stories.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
import React, { useEffect } from "react";
22
import "./Radios.scss";
33
import Radios from "./Radios";
4-
import { Meta, StoryFn, StoryObj } from "@storybook/react";
4+
import { Meta, StoryFn, StoryObj } from "@storybook/react-vite";
55
import fixtures from "govuk-frontend/dist/govuk/components/radios/fixtures.json";
66
import { extractShownFixtures } from "../../utils/ProcessExampleData";
77
import { ComponentFixture } from "../../dynamics";
88
import { WithItemRefs } from "../../utils/WithReference";
99
import { ConfigureOverallRadios } from "./Radios.config";
10-
import { action } from "@storybook/addon-actions";
10+
import { action } from "storybook/actions";
1111

12-
let configured = false;
1312
const meta: Meta<typeof Radios> = {
1413
title: "GOVUK Design System/Radios",
1514
component: Radios,
1615
decorators: [
17-
(Story, { parameters }) => {
16+
(Story) => {
1817
useEffect(() => {
19-
const configureRadios = () => {
20-
const isDocsMode = window.location.search.includes("viewMode=docs");
21-
if (
22-
isDocsMode &&
23-
!configured &&
24-
parameters.initializeConfigurations
25-
) {
26-
ConfigureOverallRadios();
27-
configured = true;
28-
} else if (!isDocsMode) {
29-
ConfigureOverallRadios();
30-
}
31-
};
32-
configureRadios();
18+
// Remove any existing initialization before configuring
19+
const radios = document.querySelectorAll(".govuk-radios");
20+
radios.forEach((radio) => {
21+
radio.removeAttribute("data-module");
22+
});
23+
24+
// Initialize once
25+
ConfigureOverallRadios();
3326
}, []);
3427
return <Story />;
3528
},
3629
],
37-
tags: ["autodocs"],
38-
args: { onChange: action("on-change"), onBlur: action("on-blur") },
30+
parameters: {
31+
initializeConfigurations: true,
32+
},
33+
args: {
34+
onChange: action("on-change"),
35+
onBlur: action("on-blur"),
36+
},
3937
};
4038

4139
export default meta;

src/components/Radios/Radios.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ const Radios: React.FC<RadiosProps> = (props) => {
99
const processedItems: BooleanItem[] | undefined = items
1010
? items.map((item) => {
1111
if (item) {
12-
return {
13-
...item,
14-
...(value != null && { checked: item.value === value }),
15-
...(defaultValue != null && {
12+
// Only set checked/defaultChecked - not both
13+
if (value != null) {
14+
return {
15+
...item,
16+
checked: item.value === value,
17+
defaultChecked: undefined, // Remove defaultChecked if value is provided
18+
};
19+
} else if (defaultValue != null) {
20+
return {
21+
...item,
1622
defaultChecked: item.value === defaultValue,
17-
}),
18-
};
23+
checked: undefined, // Remove checked if defaultValue is provided
24+
};
25+
}
26+
return item;
1927
}
2028
return item;
2129
})

0 commit comments

Comments
 (0)