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

Upgrade checkboxes (v3) #1581

Merged
merged 2 commits into from
Oct 17, 2023
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
18 changes: 18 additions & 0 deletions src/layout/Checkboxes/CheckboxesContainerComponent.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Override design system component to enable horizontal layout
No longer supported with horizontal layout in design system.
Possible breaking change if we remove this option of styling.
*/
.horizontal div {
display: flex;
flex-wrap: wrap;
flex-direction: row;
gap: var(--fds-spacing-2);
}
.horizontal div div {
display: block;
}
.checkBoxLabelContainer {
display: flex;
gap: var(--fds-spacing-1);
align-items: center;
}
52 changes: 31 additions & 21 deletions src/layout/Checkboxes/CheckboxesContainerComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { act, fireEvent, screen } from '@testing-library/react';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { CheckboxContainerComponent } from 'src/layout/Checkboxes/CheckboxesContainerComponent';
Expand Down Expand Up @@ -276,19 +276,21 @@ describe('CheckboxContainerComponent', () => {
expect(screen.getByTestId('altinn-spinner')).toBeInTheDocument();
});

it('should show items in a row when layout is "row" and options count is 3', () => {
it('should show items in a row when layout is "row" and options count is 3', async () => {
const { container } = render({
component: {
optionsId: 'countries',
layout: LayoutStyle.Row,
},
});

// eslint-disable-next-line
expect(container.querySelector('fieldset > div')).toHaveStyle('flex-direction: row;');
await waitFor(() => {
// eslint-disable-next-line
expect(container.querySelector('fieldset')).toHaveClass('horizontal');
});
});

it('should show items in a row when layout is not defined, and options count is 2', () => {
it('should show items in a row when layout is not defined, and options count is 2', async () => {
const { container } = render({
component: {
optionsId: 'countries',
Expand All @@ -303,11 +305,13 @@ describe('CheckboxContainerComponent', () => {
} as unknown as IOptionsState,
});

// eslint-disable-next-line
expect(container.querySelector('fieldset > div')).toHaveStyle('flex-direction: row;');
await waitFor(() => {
// eslint-disable-next-line
expect(container.querySelector('fieldset')).toHaveClass('horizontal');
});
});

it('should show items in a column when layout is "column" and options count is 2 ', () => {
it('should show items in a column when layout is "column" and options count is 2 ', async () => {
const { container } = render({
component: {
optionsId: 'countries',
Expand All @@ -323,19 +327,21 @@ describe('CheckboxContainerComponent', () => {
} as unknown as IOptionsState,
});

// eslint-disable-next-line
expect(container.querySelector('fieldset > div')).toHaveStyle('flex-direction: column;');
await waitFor(() => {
// eslint-disable-next-line
expect(container.querySelector('fieldset')).not.toHaveClass('horizontal');
});
});

it('should show items in a columns when layout is not defined, and options count is 3', () => {
it('should show items in a columns when layout is not defined, and options count is 3', async () => {
const { container } = render({
component: {
optionsId: 'countries',
},
});

// eslint-disable-next-line
expect(container.querySelector('fieldset > div')).toHaveStyle('flex-direction: column;');
await waitFor(() => expect(container.querySelector('fieldset')).not.toHaveClass('horizontal'));
});

it('should present replaced label if setup with values from repeating group in redux and trigger handleDataChanged with replaced values', async () => {
Expand All @@ -354,22 +360,26 @@ describe('CheckboxContainerComponent', () => {
},
});

expect(getCheckbox({ name: 'The value from the group is: Label for first' })).toBeInTheDocument();
expect(getCheckbox({ name: 'The value from the group is: Label for second' })).toBeInTheDocument();
expect(screen.getByText('Description: The value from the group is: Label for first')).toBeInTheDocument();
expect(screen.getByText('Description: The value from the group is: Label for second')).toBeInTheDocument();
await waitFor(() => {
expect(getCheckbox({ name: /The value from the group is: Label for first/ })).toBeInTheDocument();
});
expect(getCheckbox({ name: /The value from the group is: Label for second/ })).toBeInTheDocument();
expect(screen.getByText(/Description: The value from the group is: Label for first/)).toBeInTheDocument();
expect(screen.getByText(/Description: The value from the group is: Label for second/)).toBeInTheDocument();

expect(screen.getByText(/Help Text: The value from the group is: Label for first/)).toBeInTheDocument();
await act(() =>
user.click(screen.getByRole('button', { name: 'Help text for The value from the group is: Label for first' })),
user.click(screen.getByRole('button', { name: /Help Text: The value from the group is: Label for first/ })),
);
expect(screen.getByText('Help Text: The value from the group is: Label for first')).toBeInTheDocument();
expect(screen.getAllByText(/Help Text: The value from the group is: Label for first/)).toHaveLength(2);

expect(screen.getByText(/Help Text: The value from the group is: Label for second/)).toBeInTheDocument();
await act(() =>
user.click(screen.getByRole('button', { name: 'Help text for The value from the group is: Label for second' })),
user.click(screen.getByRole('button', { name: /Help Text: The value from the group is: Label for second/ })),
);
expect(screen.getByText('Help Text: The value from the group is: Label for second')).toBeInTheDocument();
expect(screen.getAllByText(/Help Text: The value from the group is: Label for second/)).toHaveLength(2);

await act(() => user.click(getCheckbox({ name: 'The value from the group is: Label for second' })));
await act(() => user.click(getCheckbox({ name: /The value from the group is: Label for second/ })));

expect(handleDataChange).not.toHaveBeenCalled();

Expand Down
73 changes: 43 additions & 30 deletions src/layout/Checkboxes/CheckboxesContainerComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import { LegacyCheckboxGroup } from '@digdir/design-system-react';
import { Checkbox, HelpText } from '@digdir/design-system-react';
import cn from 'classnames';

import { AltinnSpinner } from 'src/components/AltinnSpinner';
import { OptionalIndicator } from 'src/components/form/OptionalIndicator';
Expand All @@ -10,8 +11,10 @@ import { useDelayedSavedState } from 'src/hooks/useDelayedSavedState';
import { useGetOptions } from 'src/hooks/useGetOptions';
import { useHasChangedIgnoreUndefined } from 'src/hooks/useHasChangedIgnoreUndefined';
import { useLanguage } from 'src/hooks/useLanguage';
import classes from 'src/layout/Checkboxes/CheckboxesContainerComponent.module.css';
import { shouldUseRowLayout } from 'src/utils/layout';
import { getOptionLookupKey } from 'src/utils/options';
import { getPlainTextFromNode } from 'src/utils/stringHelper';
import type { PropsFromGenericComponent } from 'src/layout';
import type { IOption } from 'src/layout/common.generated';

Expand Down Expand Up @@ -89,18 +92,25 @@ export const CheckboxContainerComponent = ({
}
};

const labelText = (
<span style={{ fontSize: '1rem' }}>
const labelTextGroup = (
<span className={classes.checkBoxLabelContainer}>
{lang(node.item.textResourceBindings?.title)}
<RequiredIndicator required={required} />
<OptionalIndicator
labelSettings={labelSettings}
required={required}
/>
{textResourceBindings?.help && (
<HelpText title={langAsString(textResourceBindings?.help)}>{lang(textResourceBindings?.help)}</HelpText>
)}
</span>
);

const horizontal = shouldUseRowLayout({
layout,
optionsCount: calculatedOptions.length,
});
const hideLabel = overrideDisplay?.renderedInTable === true && calculatedOptions.length === 1;
const ariaLabel = overrideDisplay?.renderedInTable ? langAsString(textResourceBindings?.title) : undefined;

return fetchingOptions ? (
<AltinnSpinner />
Expand All @@ -110,35 +120,38 @@ export const CheckboxContainerComponent = ({
key={`checkboxes_group_${id}`}
onBlur={handleBlur}
>
<LegacyCheckboxGroup
compact={false}
<Checkbox.Group
className={cn({ [classes.horizontal]: horizontal })}
legend={labelTextGroup}
description={lang(textResourceBindings?.description)}
disabled={readOnly}
onChange={(values) => handleChange(values)}
legend={overrideDisplay?.renderLegend === false ? null : labelText}
description={textResourceBindings?.description && lang(textResourceBindings.description)}
hideLegend={overrideDisplay?.renderLegend === false}
error={!isValid}
fieldSetProps={{
'aria-label': overrideDisplay?.renderedInTable ? langAsString(textResourceBindings?.title) : undefined,
}}
helpText={textResourceBindings?.help && lang(textResourceBindings.help)}
variant={
shouldUseRowLayout({
layout,
optionsCount: calculatedOptions.length,
})
? 'horizontal'
: 'vertical'
}
items={calculatedOptions.map((option) => ({
name: option.value,
checkboxId: `${id}-${option.label.replace(/\s/g, '-')}`,
checked: selected.includes(option.value),
hideLabel,
label: langAsString(option.label),
description: langAsString(option.description),
helpText: option.helpText && langAsString(option.helpText),
}))}
/>
aria-label={ariaLabel}
value={selected}
>
{calculatedOptions.map((option) => (
<Checkbox
id={`${id}-${option.label.replace(/\s/g, '-')}`}
name={option.value}
key={option.value}
description={lang(option.description)}
value={option.value}
checked={selected.includes(option.value)}
size='small'
>
{
<span className={cn({ 'sr-only': hideLabel }, classes.checkBoxLabelContainer)}>
{langAsString(option.label)}
{option.helpText && (
<HelpText title={getPlainTextFromNode(option.helpText)}>{lang(option.helpText)}</HelpText>
)}
</span>
}
</Checkbox>
))}
</Checkbox.Group>
</div>
);
};
19 changes: 9 additions & 10 deletions src/layout/RadioButtons/ControlledRadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ export const ControlledRadioGroup = (props: IControlledRadioGroupProps) => {
>
<Radio.Group
legend={
overrideDisplay?.renderLegend === false ? null : (
<span className={classes.label}>
{labelText}
{textResourceBindings?.help ? (
<HelpText title={langAsString(textResourceBindings?.help)}>
{lang(textResourceBindings?.help)}
</HelpText>
) : null}
</span>
)
<span className={classes.label}>
{labelText}
{textResourceBindings?.help ? (
<HelpText title={langAsString(textResourceBindings?.help)}>
{lang(textResourceBindings?.help)}
</HelpText>
) : null}
</span>
}
hideLegend={overrideDisplay?.renderLegend === false}
description={lang(textResourceBindings?.description)}
error={!isValid}
disabled={readOnly}
Expand Down