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

Add divider to Likert #2765

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 31 additions & 1 deletion src/codegen/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,37 @@ const common = {
maxHeight: 2,
},
}),

ILikertColumnProperties: () =>
new CG.obj(
new CG.prop(
'columns',
new CG.arr(
new CG.obj(
new CG.prop(
'value',
new CG.union(new CG.str().setPattern(/^\d+$/), new CG.num())
.setTitle('Value')
.setDescription('The value of the answer column'),
),
new CG.prop(
'addLeftDivider',
tina-ahm marked this conversation as resolved.
Show resolved Hide resolved
new CG.bool()
.optional()
.setDescription('Set this to true if you would like to have a divider on the left-hand side.'),
),
new CG.prop(
'addRightDivider',
new CG.bool()
.optional()
.setDescription('Set this to true if you would like to have a divider on the right-hand side.'),
),
),
)
.optional()
.setTitle('Columns')
.setDescription('Add customization to the columns of the likert component'),
),
),
// Types that component definitions extend:
ComponentBase: () =>
new CG.obj(
Expand Down
1 change: 1 addition & 0 deletions src/layout/Likert/Generator/LikertGeneratorChildren.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const GenerateRow = React.memo(function GenerateRow({ rowIndex, questionsBinding
hidden: parentItem.hidden,
pageBreak: parentItem.pageBreak,
renderAsSummary: parentItem.renderAsSummary,
columns: parentItem.columns,
}),
[parentItem, childId],
);
Expand Down
28 changes: 19 additions & 9 deletions src/layout/Likert/LikertComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const LikertComponent = ({ node }: LikertComponentProps) => {
const firstLikertNodeId = rowNodeIds[0];
const firstLikertNode = useNode(firstLikertNodeId) as LayoutNode<'LikertItem'> | undefined;
const { options: calculatedOptions, isFetching } = useNodeOptions(firstLikertNode);
const columns = useNodeItem(node, (item) => item.columns);

const id = node.id;

Expand Down Expand Up @@ -122,15 +123,24 @@ export const LikertComponent = ({ node }: LikertComponentProps) => {
<Lang id={textResourceBindings?.leftColumnHeader ?? 'likert.left_column_default_header_text'} />
</span>
</Table.HeaderCell>
{calculatedOptions.map((option, index) => (
<Table.HeaderCell
key={option.value}
scope='col'
id={`${id}-likert-columnheader-${index}`}
>
<Lang id={option.label} />
</Table.HeaderCell>
))}
{calculatedOptions.map((option, index) => {
const addLeftDivider = columns?.find((column) => column.value == option.value)?.addLeftDivider;
const addRightDivider = columns?.find((column) => column.value == option.value)?.addRightDivider;

return (
<Table.HeaderCell
key={option.value}
scope='col'
id={`${id}-likert-columnheader-${index}`}
className={cn({
[classes.likertCellLeftDivider]: addLeftDivider,
[classes.likertCellRightDivider]: addRightDivider,
})}
>
<Lang id={option.label} />
</Table.HeaderCell>
);
})}
</Table.Row>
</Table.Head>
<Table.Body>
Expand Down
1 change: 1 addition & 0 deletions src/layout/Likert/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ export const Config = new CG.component({
.exportAs('ILikertFilter'),
),
)
.extends(CG.common('ILikertColumnProperties'))
.addPlugin(new OptionsPlugin({ supportsPreselection: false, type: 'single' }));
8 changes: 8 additions & 0 deletions src/layout/LikertItem/LikertItemComponent.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ Altinn, and making sure they are consistent. */
margin: 0;
gap: 0;
}

.likertCellLeftDivider {
border-left: 1px solid #dde3e5;
}

.likertCellRightDivider {
border-right: 1px solid #dde3e5;
}
15 changes: 14 additions & 1 deletion src/layout/LikertItem/LikertItemComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { forwardRef } from 'react';

import { Label, Radio, Table } from '@digdir/designsystemet-react';
import cn from 'classnames';

import { RequiredIndicator } from 'src/components/form/RequiredIndicator';
import { getLabelId } from 'src/components/label/Label';
Expand All @@ -15,6 +16,7 @@ import { BaseLayoutNode } from 'src/utils/layout/LayoutNode';
import { useNodeItem } from 'src/utils/layout/useNodeItem';
import type { PropsFromGenericComponent } from 'src/layout';
import type { IControlledRadioGroupProps } from 'src/layout/RadioButtons/ControlledRadioGroup';
import type { NodeItemFromNode } from 'src/utils/layout/types';

export const LikertItemComponent = forwardRef<HTMLTableRowElement, PropsFromGenericComponent<'LikertItem'>>(
(props, ref) => {
Expand Down Expand Up @@ -45,6 +47,9 @@ const RadioGroupTableRow = forwardRef<HTMLTableRowElement, IControlledRadioGroup
const groupContainer =
node.parent instanceof BaseLayoutNode && node.parent.isType('Likert') ? node.parent : undefined;

const item = useNodeItem(props.node) as NodeItemFromNode<BaseLayoutNode<'LikertItem'>>;
const columns = item.columns;
tina-ahm marked this conversation as resolved.
Show resolved Hide resolved

return (
<Table.Row
data-componentid={node.id}
Expand Down Expand Up @@ -72,9 +77,17 @@ const RadioGroupTableRow = forwardRef<HTMLTableRowElement, IControlledRadioGroup
const isChecked = selectedValues[0] === option.value;
const rowLabelId = getLabelId(id);
const labelledby = `${rowLabelId} ${groupContainer?.baseId}-likert-columnheader-${index}`;
const addLeftDivider = columns?.find((column) => column.value == option.value)?.addLeftDivider;
const addRightDivider = columns?.find((column) => column.value == option.value)?.addRightDivider;

return (
<Table.Cell key={option.value}>
<Table.Cell
key={option.value}
className={cn({
[classes.likertCellLeftDivider]: addLeftDivider,
[classes.likertCellRightDivider]: addRightDivider,
})}
>
<Radio
checked={isChecked}
readOnly={readOnly}
Expand Down
1 change: 1 addition & 0 deletions src/layout/LikertItem/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ export const Config = new CG.component({
.setDescription('Boolean value indicating if the label should be visible when only one option exists in table'),
),
)
.extends(CG.common('ILikertColumnProperties'))
.addPlugin(new OptionsPlugin({ supportsPreselection: true, type: 'single' }))
.addProperty(new CG.prop('layout', CG.common('LayoutStyle').optional()));
Loading