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

Fixed openByDefault to use filter #571

Merged
merged 2 commits into from
Oct 21, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { RepeatingGroupTable } from 'src/features/form/containers/RepeatingGroup
import { FormLayoutActions } from 'src/features/form/layout/formLayoutSlice';
import { makeGetHidden } from 'src/selectors/getLayoutData';
import { Triggers } from 'src/types';
import { createRepeatingGroupComponents } from 'src/utils/formLayout';
import {
createRepeatingGroupComponents,
getRepeatingGroupFilteredIndices,
} from 'src/utils/formLayout';
import { getHiddenFieldsForGroup } from 'src/utils/layout';
import { renderValidationMessagesForComponent } from 'src/utils/render';
import { repeatingGroupHasValidations } from 'src/utils/validation';
Expand Down Expand Up @@ -152,24 +155,12 @@ export function GroupContainer({
);

React.useEffect(() => {
if (container.edit?.filter && container.edit.filter.length > 0) {
container.edit.filter.forEach((rule) => {
const formDataKeys: string[] = Object.keys(formData).filter((key) => {
const keyWithoutIndex = key.replaceAll(/\[\d*\]/g, '');
return keyWithoutIndex === rule.key && formData[key] === rule.value;
});
if (formDataKeys && formDataKeys.length > 0) {
const filtered = formDataKeys.map((key) => {
const match = key.match(/\[(\d*)\]/g);
const currentIndex = match[match.length - 1];
return parseInt(
currentIndex.substring(1, currentIndex.indexOf(']')),
10,
);
});
setFilteredIndexList(filtered);
}
});
const filteredIndexList = getRepeatingGroupFilteredIndices(
formData,
container.edit?.filter,
);
if (filteredIndexList) {
setFilteredIndexList(filteredIndexList);
}
}, [formData, container]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from 'src/utils/databindings';
import {
findChildren,
getRepeatingGroupFilteredIndices,
getRepeatingGroups,
mapFileUploadersWithTag,
removeRepeatingGroupFromUIConfig,
Expand Down Expand Up @@ -675,12 +676,18 @@ export function* initRepeatingGroupsSaga(): SagaIterator {
const container = groupContainers.find(
(element) => element.id === key,
) as ILayoutGroup;

if (container && group.index >= 0) {
const filteredIndexList = getRepeatingGroupFilteredIndices(
formDataState.formData,
container.edit?.filter,
);

if (container.edit?.openByDefault === 'first') {
group.editIndex = 0;
group.editIndex = filteredIndexList ? filteredIndexList[0] : 0;
} else if (container.edit?.openByDefault === 'last') {
group.editIndex = group.index;
group.editIndex = filteredIndexList
? filteredIndexList.at(-1)
: group.index;
}
}
});
Expand Down
34 changes: 34 additions & 0 deletions src/altinn-app-frontend/src/utils/formLayout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { INDEX_KEY_INDICATOR_REGEX } from 'src/utils/databindings';
import type { IFormData } from 'src/features/form/data';
import type {
ComponentTypes,
IGroupEditProperties,
IGroupFilter,
ILayout,
ILayoutComponent,
ILayoutGroup,
Expand Down Expand Up @@ -555,3 +557,35 @@ export function behavesLikeDataTask(
): boolean {
return layoutSets?.sets.some((set) => set.tasks?.includes(task));
}

/**
* (Deprecate this function) Returns the filtered indices of a repeating group.
* This is a buggy implementation, but is used for backward compatibility until a new major version is released.
* @see https://github.com/Altinn/app-frontend-react/issues/339#issuecomment-1286624933
* @param formData IFormData
* @param filter IGroupEditProperties.filter or undefined.
* @returns a list of indices for repeating group elements after applying filters, or null if no filters are provided or if no elements match.
*/
export function getRepeatingGroupFilteredIndices(
formData: IFormData,
filter?: IGroupFilter[],
): number[] | null {
if (filter && filter.length > 0) {
const rule = filter.at(-1);
const formDataKeys: string[] = Object.keys(formData).filter((key) => {
const keyWithoutIndex = key.replaceAll(/\[\d*\]/g, '');
return keyWithoutIndex === rule.key && formData[key] === rule.value;
});
if (formDataKeys && formDataKeys.length > 0) {
return formDataKeys.map((key) => {
const match = key.match(/\[(\d*)\]/g);
const currentIndex = match[match.length - 1];
return parseInt(
currentIndex.substring(1, currentIndex.indexOf(']')),
10,
);
});
}
}
return null;
}