Skip to content
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
51 changes: 42 additions & 9 deletions src/layout/RepeatingGroup/Table/RepeatingGroupTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ describe('RepeatingGroupTable', () => {
expect(screen.getByTestId('editIndex')).toHaveTextContent('0');
});

it('should keep table header visible when editing a single row with editInTable', async () => {
const groupWithEditInTableAndStickyHeader = getFormLayoutRepeatingGroupMock({
id: 'mock-container-id',
stickyHeader: true,
tableColumns: { field1: { editInTable: true } },
});

await render(getLayout(groupWithEditInTableAndStickyHeader, components), {
'some-group': [{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 0' }],
});
expect(document.getElementById('group-mock-container-id-table-header')).toBeInTheDocument();
await userEvent.click(screen.getAllByRole('button', { name: /rediger/i })[0]);
expect(screen.getByTestId('editIndex')).toHaveTextContent('0');
expect(document.getElementById('group-mock-container-id-table-header')).toBeInTheDocument();
});

it('should hide table header when editing a single row without editInTable', async () => {
const groupWithoutEditInTable = getFormLayoutRepeatingGroupMock({
id: 'mock-container-id',
stickyHeader: true,
});

await render(getLayout(groupWithoutEditInTable, components), {
'some-group': [{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 0' }],
});

expect(document.getElementById('group-mock-container-id-table-header')).toBeInTheDocument();
await userEvent.click(screen.getAllByRole('button', { name: /rediger/i })[0]);
expect(screen.getByTestId('editIndex')).toHaveTextContent('0');
expect(document.getElementById('group-mock-container-id-table-header')).not.toBeInTheDocument();
});

it('should render EditableCell when editInTable is enabled for a column', async () => {
const groupWithEditInTable = getFormLayoutRepeatingGroupMock({
id: 'mock-container-id',
Expand Down Expand Up @@ -219,7 +251,7 @@ describe('RepeatingGroupTable', () => {
tableHeaders: ['field1', 'field2', 'field3', 'field4'],
...extra,
});
await render(getLayout(groupWithExtraRows, components), extraRowTextResources);
await render(getLayout(groupWithExtraRows, components), undefined, extraRowTextResources);

expect(screen.getByText('Extra0')).toBeInTheDocument();
expect(screen.queryByText('Extra1Hidden')).not.toBeInTheDocument();
Expand Down Expand Up @@ -303,6 +335,14 @@ describe('RepeatingGroupTable', () => {

const render = async (
layout = getLayout(group, components),
formData: Record<string, unknown> = {
'some-group': [
{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 0' },
{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 1' },
{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 2' },
{ [ALTINN_ROW_ID]: uuidv4(), checkboxBinding: 'option.value', prop1: 'test row 3' },
],
},
extraTextResources: { id: string; value: string }[] = [],
) =>
await renderWithInstanceAndLayout({
Expand Down Expand Up @@ -336,14 +376,7 @@ describe('RepeatingGroupTable', () => {
...extraTextResources,
],
}),
fetchFormData: async () => ({
'some-group': [
{ [ALTINN_ROW_ID]: uuidv4(), checkBoxBinding: 'option.value', prop1: 'test row 0' },
{ [ALTINN_ROW_ID]: uuidv4(), checkBoxBinding: 'option.value', prop1: 'test row 1' },
{ [ALTINN_ROW_ID]: uuidv4(), checkBoxBinding: 'option.value', prop1: 'test row 2' },
{ [ALTINN_ROW_ID]: uuidv4(), checkBoxBinding: 'option.value', prop1: 'test row 3' },
],
}),
fetchFormData: async () => formData,
},
});
});
Expand Down
5 changes: 4 additions & 1 deletion src/layout/RepeatingGroup/Table/RepeatingGroupTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export function RepeatingGroupTable(): React.JSX.Element | null {

const isEmpty = numRows === 0;
const isEditingFirstRow = RepGroupContext.useIsEditingRow(firstRowId);
const showTableHeader = numRows > 0 && !(numRows == 1 && firstRowId !== undefined && isEditingFirstRow);
const hasColumnsWithEditInTable =
tableColumns && Object.keys(tableColumns).some((colId) => tableColumns[colId].editInTable);
const showTableHeader =
numRows > 0 && (hasColumnsWithEditInTable || !(numRows == 1 && firstRowId !== undefined && isEditingFirstRow));

const showDeleteButtonColumns = new Set<boolean>();
const showEditButtonColumns = new Set<boolean>();
Expand Down