Skip to content

Commit

Permalink
Get public side working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdelion committed Jan 8, 2025
1 parent a8717ad commit f63cbe0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { object } from "yup";

import { PublicChecklistProps } from "../../types";
import {
partitionGroupedOptions,
partitionGroupedOptionsAgain,
toggleNonExclusiveCheckbox,
} from "../helpers";
import { useExclusiveOption } from "../hooks/useExclusiveOption";
import { useExclusiveOptionInGroupedChecklist } from "../hooks/useExclusiveOption";
import { useSortedOptions } from "../hooks/useSortedOptions";
import { ExclusiveChecklistItem } from "./ExclusiveChecklistItem";
import { GroupedChecklistOptions } from "./GroupedChecklistOptions";
Expand Down Expand Up @@ -58,21 +58,17 @@ export const GroupedChecklist: React.FC<PublicChecklistProps> = (props) => {
formik,
);

const [exclusiveOptions, nonExclusiveOptionGroups] = partitionGroupedOptions(
groupedOptions!,
);
const [exclusiveOptionGroups, nonExclusiveOptionGroups] =
partitionGroupedOptionsAgain(groupedOptions!);

const {
exclusiveOrOption,
exclusiveOptionIsChecked,
toggleExclusiveCheckbox,
} = useExclusiveOption(exclusiveOptions, formik);
const { exclusiveOrOptionGroup, toggleExclusiveCheckbox } =
useExclusiveOptionInGroupedChecklist(exclusiveOptionGroups, formik);

const changeCheckbox = (id: string) => () => {
const currentCheckedIds = formik.values.checked;

const currentCheckboxIsExclusiveOption =
exclusiveOrOption && id === exclusiveOrOption.id;
exclusiveOrOptionGroup && id === exclusiveOrOptionGroup.children[0].id;

if (currentCheckboxIsExclusiveOption) {
const newCheckedIds = toggleExclusiveCheckbox(id);
Expand All @@ -82,7 +78,7 @@ export const GroupedChecklist: React.FC<PublicChecklistProps> = (props) => {
const newCheckedIds = toggleNonExclusiveCheckbox(
id,
currentCheckedIds,
exclusiveOrOption,
exclusiveOrOptionGroup?.children[0],
);
setCheckedFieldValue(newCheckedIds);
};
Expand Down Expand Up @@ -120,17 +116,17 @@ export const GroupedChecklist: React.FC<PublicChecklistProps> = (props) => {
component="fieldset"
>
<legend style={visuallyHidden}>{text}</legend>
{groupedOptions && (
{nonExclusiveOptionGroups && (
<GroupedChecklistOptions
groupedOptions={groupedOptions}
groupedOptions={nonExclusiveOptionGroups}
previouslySubmittedData={previouslySubmittedData}
changeCheckbox={changeCheckbox}
formik={formik}
/>
)}
{exclusiveOrOption && (
{exclusiveOrOptionGroup && (
<ExclusiveChecklistItem
exclusiveOrOption={exclusiveOrOption}
exclusiveOrOption={exclusiveOrOptionGroup?.children[0]}
changeCheckbox={changeCheckbox}
formik={formik}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { FormikProps } from "formik";

import { Option } from "../../../shared";
import { Group } from "../../model";

export const useExclusiveOption = (
exclusiveOptions: Option[],
formik: FormikProps<{ checked: Array<string> }>
formik: FormikProps<{ checked: Array<string> }>,
) => {
const [exclusiveOrOption] = exclusiveOptions;

Expand All @@ -21,3 +22,24 @@ export const useExclusiveOption = (
toggleExclusiveCheckbox,
};
};

export const useExclusiveOptionInGroupedChecklist = (
exclusiveOptions: Group<Option>[],
formik: FormikProps<{ checked: Array<string> }>,
) => {
const [exclusiveOrOptionGroup] = exclusiveOptions;

const exclusiveOptionIsChecked =
exclusiveOrOptionGroup &&
formik.values.checked.includes(exclusiveOrOptionGroup.children[0].id);

const toggleExclusiveCheckbox = (checkboxId: string) => {
return exclusiveOptionIsChecked ? [] : [checkboxId];
};

return {
exclusiveOrOptionGroup,
exclusiveOptionIsChecked,
toggleExclusiveCheckbox,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe("Checklist Component - Grouped Layout", () => {
expect(nonExclusiveOption).not.toHaveAttribute("checked");
expect(exclusiveOption).toHaveAttribute("checked");

await user.click(screen.getByText("Section 2"));
const exclusiveOptionInSection2 = screen.getByLabelText("S2 Option1");
await user.click(exclusiveOptionInSection2);

Expand All @@ -101,7 +102,7 @@ describe("Checklist Component - Grouped Layout", () => {
await user.click(screen.getByTestId("continue-button"));

expect(handleSubmit).toHaveBeenCalledWith({
answers: ["S1_Option2", "S2_Option1", "S2_Option2"],
answers: ["S1_Option1", "S2_Option1", "S2_Option2"],
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,45 @@ export const groupedOptions: Array<Group<Option>> = [
},
];

export const groupedOptionsWithExclusiveOption: Array<Group<Option> | Option> =
[
{
title: "Section 1",
children: [
{
id: "S1_Option1",
data: {
text: "S1 Option1",
},
export const groupedOptionsWithExclusiveOption: Array<Group<Option>> = [
{
title: "Section 1",
children: [
{
id: "S1_Option1",
data: {
text: "S1 Option1",
},
],
},
{
title: "Section 2",
children: [
{
id: "S2_Option1",
data: {
text: "S2 Option1",
},
},
],
},
{
title: "Section 2",
children: [
{
id: "S2_Option1",
data: {
text: "S2 Option1",
},
{
id: "S2_Option2",
data: {
text: "S2 Option2",
},
},
{
id: "S2_Option2",
data: {
text: "S2 Option2",
},
],
},
{
id: "exclusive_option",
data: {
text: "Exclusive",
exclusive: true,
},
},
];
],
},
{
title: "Exclusive 'Or' Option",
children: [
{
id: "exclusive_option",
data: {
text: "Exclusive",
exclusive: true,
},
},
],
},
];

0 comments on commit f63cbe0

Please sign in to comment.