Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to uncheck in checkboxOne #1190

Merged
merged 2 commits into from
Dec 11, 2024
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
6 changes: 2 additions & 4 deletions e2e/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ test.describe('Checkboxes', () => {
await expectCollectedData(page, 'Q2', '3');
await expectCollectedData(page, 'Q3', 'Bonjour');
});
test(`Clicking multiple time should not trigger onChange`, async ({
page,
}) => {
test(`Allow to uncheck modality`, async ({ page }) => {
await goToStory(page, 'components-checkboxone--default');
await expect(page.getByRole('radio', { name: 'oui' })).toBeVisible();
await page.getByRole('radio', { name: 'oui' }).click();
await expectChanges(page, 0, () => {
await expectChanges(page, 1, () => {
return page.getByRole('radio', { name: 'oui' }).click();
});
});
Expand Down
55 changes: 55 additions & 0 deletions src/components/shared/Radio/RadioOption.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,61 @@ describe('RadioOption', () => {
expect(onClickMock).toHaveBeenCalled();
});

it('does not allow to uncheck modality if checkboxStyle is not defined', () => {
const onClickMock = vi.fn();
render(
<RadioOption
id="radio-option"
label="Test Option"
onCheck={onClickMock}
onUncheck={onClickMock}
checked
/>
);

const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).not.toHaveBeenCalled();
});

it('does not allow to uncheck modality if checkboxStyle is false', () => {
const onClickMock = vi.fn();

render(
<RadioOption
id="radio-option"
label="Test Option"
onCheck={onClickMock}
onUncheck={onClickMock}
checkboxStyle={false}
checked
/>
);

const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).not.toHaveBeenCalled();
});

it('allows to uncheck modality if checkboxStyle = true and onUncheck', () => {
const onClickMock = vi.fn();

render(
<RadioOption
id="radio-option"
label="Test Option"
onCheck={onClickMock}
onUncheck={onClickMock}
checkboxStyle={true}
checked
/>
);

const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).toHaveBeenCalled();
});

it('sets the tabIndex to 0 when unchecked', () => {
const { getByRole } = render(
<RadioOption id="radio-option" label="Test Option" checked={false} />
Expand Down
5 changes: 5 additions & 0 deletions src/components/shared/Radio/RadioOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function LunaticRadioOption({
detailLabel,
detailValue,
onCheck,
onUncheck,
}: Props) {
const divEl = useRef<HTMLDivElement>(null);
const isEnabled = !disabled && !readOnly;
Expand All @@ -48,6 +49,10 @@ function LunaticRadioOption({

const onClickOption = () => {
if (!isEnabled || !onCheck || checked) {
// for checkboxStyle=true (only used by CheckboxOne) , we allow uncheck
if (checkboxStyle && onUncheck) {
onUncheck();
}
return;
}
onCheck();
Expand Down
5 changes: 5 additions & 0 deletions src/use-lunatic/props/propOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type InterpretedOption = {
detailValue?: string | null;
onDetailChange?: (value: string) => void;
onCheck?: () => void;
onUncheck?: () => void;
};

/**
Expand Down Expand Up @@ -86,6 +87,10 @@ export function getOptionsProp(
{ name: definition.response.name, value: option.value },
]);
},
// for CheckboxOne, we allow uncheck
onUncheck: () => {
handleChanges([{ name: definition.response.name, value: null }]);
},
detailValue:
'detail' in option && option.detail
? variables.get(option.detail.response.name, iteration)
Expand Down
Loading