@uktrade/react-component-library 0.13.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,9 @@ interface CheckboxGroupProps {
15
15
  name: string;
16
16
  legendAs?: LegendAs;
17
17
  legendSize?: LegendSize;
18
+ checkboxSize?: "small" | "default";
19
+ onChange?: (values: string[]) => void;
18
20
  }
19
- export declare const CheckboxGroup: ({ legend, hint, error, options, name, legendAs, legendSize, }: CheckboxGroupProps) => import("react/jsx-runtime").JSX.Element;
21
+ export declare const CheckboxGroup: ({ legend, hint, error, options, name, legendAs, legendSize, checkboxSize, onChange, }: CheckboxGroupProps) => import("react/jsx-runtime").JSX.Element;
20
22
  export {};
21
23
  //# sourceMappingURL=CheckBoxGroupInput.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"CheckBoxGroupInput.d.ts","sourceRoot":"","sources":["../../../../src/components/Inputs/CheckBoxesInput/CheckBoxGroupInput.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,KAAK,QAAQ,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAC1C,KAAK,UAAU,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AAEzC,UAAU,cAAc;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,SAAS,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,UAAU,kBAAkB;IACxB,MAAM,EAAE,SAAS,CAAC;IAClB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,UAAU,CAAC;CAC3B;AAED,eAAO,MAAM,aAAa,GAAI,+DAQ3B,kBAAkB,4CA0DpB,CAAC"}
1
+ {"version":3,"file":"CheckBoxGroupInput.d.ts","sourceRoot":"","sources":["../../../../src/components/Inputs/CheckBoxesInput/CheckBoxGroupInput.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,KAAK,QAAQ,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAC1C,KAAK,UAAU,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AAEzC,UAAU,cAAc;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,SAAS,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,UAAU,kBAAkB;IACxB,MAAM,EAAE,SAAS,CAAC;IAClB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CACzC;AAED,eAAO,MAAM,aAAa,GAAI,uFAU3B,kBAAkB,4CAwEpB,CAAC"}
@@ -2,13 +2,22 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import clsx from "clsx";
4
4
  import { createElement } from "react";
5
- export const CheckboxGroup = ({ legend, hint, error, options, name, legendAs = "h4", legendSize = "m", }) => {
5
+ export const CheckboxGroup = ({ legend, hint, error, options, name, legendAs = "h4", legendSize = "m", checkboxSize = "default", onChange, }) => {
6
6
  const hasError = Boolean(error);
7
7
  const errorId = hasError ? `${name}-error` : undefined;
8
8
  const legendHeading = createElement(legendAs, { className: "govuk-fieldset__heading" }, legend);
9
+ const handleChange = () => {
10
+ if (!onChange)
11
+ return;
12
+ const inputs = document.querySelectorAll(`input[name="${name}"]:checked`);
13
+ const values = Array.from(inputs).map((el) => el.value);
14
+ onChange(values);
15
+ };
9
16
  return (_jsx("div", { className: clsx("govuk-form-group", {
10
17
  "govuk-form-group--error": hasError,
11
18
  }), children: _jsxs("fieldset", { className: "govuk-fieldset", "aria-describedby": errorId, children: [_jsx("legend", { className: clsx("govuk-fieldset__legend", {
12
19
  [`govuk-fieldset__legend--${legendSize}`]: legendSize !== "m",
13
- }), children: legendHeading }), hint && _jsx("div", { className: "govuk-hint", children: hint }), hasError && (_jsxs("p", { id: errorId, className: "govuk-error-message", children: [_jsx("span", { className: "govuk-visually-hidden", children: "Error:" }), " ", error] })), _jsx("div", { className: "govuk-checkboxes", "data-module": "govuk-checkboxes", children: options.map((opt) => (_jsxs("div", { className: "govuk-checkboxes__item", children: [_jsx("input", { className: "govuk-checkboxes__input", id: opt.id, name: name, type: "checkbox", value: opt.value, defaultChecked: opt.checked }), _jsx("label", { className: "govuk-label govuk-checkboxes__label", htmlFor: opt.id, children: opt.label })] }, opt.id))) })] }) }));
20
+ }), children: legendHeading }), hint && _jsx("div", { className: "govuk-hint", children: hint }), hasError && (_jsxs("p", { id: errorId, className: "govuk-error-message", children: [_jsx("span", { className: "govuk-visually-hidden", children: "Error:" }), " ", error] })), _jsx("div", { className: clsx("govuk-checkboxes", {
21
+ "govuk-checkboxes--small": checkboxSize === "small",
22
+ }), "data-module": "govuk-checkboxes", children: options.map((opt) => (_jsxs("div", { className: "govuk-checkboxes__item", children: [_jsx("input", { className: "govuk-checkboxes__input", id: opt.id, name: name, type: "checkbox", value: opt.value, defaultChecked: opt.checked, onChange: handleChange }), _jsx("label", { className: "govuk-label govuk-checkboxes__label", htmlFor: opt.id, children: opt.label })] }, opt.id))) })] }) }));
14
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uktrade/react-component-library",
3
- "version": "0.13.0",
3
+ "version": "0.14.1",
4
4
  "description": "A collection of reusable React components following GOV.UK design patterns.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -51,6 +51,7 @@
51
51
  "@eslint/js": "^9.39.2",
52
52
  "@testing-library/jest-dom": "^6.9.1",
53
53
  "@testing-library/react": "^16.3.2",
54
+ "@testing-library/user-event": "^14.6.1",
54
55
  "@types/react": "^19.2.9",
55
56
  "@types/react-dom": "^19.2.3",
56
57
  "@typescript-eslint/eslint-plugin": "^8.53.1",
@@ -22,6 +22,8 @@ interface CheckboxGroupProps {
22
22
  name: string;
23
23
  legendAs?: LegendAs;
24
24
  legendSize?: LegendSize;
25
+ checkboxSize?: "small" | "default";
26
+ onChange?: (values: string[]) => void;
25
27
  }
26
28
 
27
29
  export const CheckboxGroup = ({
@@ -32,6 +34,8 @@ export const CheckboxGroup = ({
32
34
  name,
33
35
  legendAs = "h4",
34
36
  legendSize = "m",
37
+ checkboxSize = "default",
38
+ onChange,
35
39
  }: CheckboxGroupProps) => {
36
40
  const hasError = Boolean(error);
37
41
  const errorId = hasError ? `${name}-error` : undefined;
@@ -39,9 +43,19 @@ export const CheckboxGroup = ({
39
43
  const legendHeading = createElement(
40
44
  legendAs,
41
45
  { className: "govuk-fieldset__heading" },
42
- legend
46
+ legend,
43
47
  );
44
48
 
49
+ const handleChange = () => {
50
+ if (!onChange) return;
51
+
52
+ const inputs = document.querySelectorAll<HTMLInputElement>(
53
+ `input[name="${name}"]:checked`,
54
+ );
55
+ const values = Array.from(inputs).map((el) => el.value);
56
+ onChange(values);
57
+ };
58
+
45
59
  return (
46
60
  <div
47
61
  className={clsx("govuk-form-group", {
@@ -51,8 +65,7 @@ export const CheckboxGroup = ({
51
65
  <fieldset className="govuk-fieldset" aria-describedby={errorId}>
52
66
  <legend
53
67
  className={clsx("govuk-fieldset__legend", {
54
- [`govuk-fieldset__legend--${legendSize}`]:
55
- legendSize !== "m",
68
+ [`govuk-fieldset__legend--${legendSize}`]: legendSize !== "m",
56
69
  })}
57
70
  >
58
71
  {legendHeading}
@@ -62,12 +75,16 @@ export const CheckboxGroup = ({
62
75
 
63
76
  {hasError && (
64
77
  <p id={errorId} className="govuk-error-message">
65
- <span className="govuk-visually-hidden">Error:</span>{" "}
66
- {error}
78
+ <span className="govuk-visually-hidden">Error:</span> {error}
67
79
  </p>
68
80
  )}
69
81
 
70
- <div className="govuk-checkboxes" data-module="govuk-checkboxes">
82
+ <div
83
+ className={clsx("govuk-checkboxes", {
84
+ "govuk-checkboxes--small": checkboxSize === "small",
85
+ })}
86
+ data-module="govuk-checkboxes"
87
+ >
71
88
  {options.map((opt) => (
72
89
  <div key={opt.id} className="govuk-checkboxes__item">
73
90
  <input
@@ -77,6 +94,7 @@ export const CheckboxGroup = ({
77
94
  type="checkbox"
78
95
  value={opt.value}
79
96
  defaultChecked={opt.checked}
97
+ onChange={handleChange}
80
98
  />
81
99
  <label
82
100
  className="govuk-label govuk-checkboxes__label"