Skip to content

Hide pages with no visible components on multiPage repeating group #3485

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -7,6 +7,7 @@ import { useRepeatingGroup } from 'src/layout/RepeatingGroup/Providers/Repeating
import { RepGroupHooks } from 'src/layout/RepeatingGroup/utils';
import { LayoutNode } from 'src/utils/layout/LayoutNode';
import { LayoutPage } from 'src/utils/layout/LayoutPage';
import { isHidden, NodesInternal } from 'src/utils/layout/NodesContext';
import { useNodeItem } from 'src/utils/layout/useNodeItem';

interface RepeatingGroupEditRowContext {
Expand All @@ -26,26 +27,58 @@ const { Provider, useCtx } = createContext<RepeatingGroupEditRowContext>({
function useRepeatingGroupEditRowState(
node: LayoutNode<'RepeatingGroup'>,
): RepeatingGroupEditRowContext & { setMultiPageIndex: (index: number) => void } {
const edit = useNodeItem(node, (i) => i.edit);
const lastPage = RepGroupHooks.useLastMultiPageIndex(node) ?? 0;
const edit = useNodeItem(node, (item) => item.edit);
const multiPageEnabled = edit?.multiPage ?? false;
const [multiPageIndex, setMultiPageIndex] = useState(0);

const children = RepGroupHooks.useChildIdsWithMultiPage(node);

const hiddenState = NodesInternal.useMemoSelector((state) =>
children.map(({ id, multiPageIndex }) => ({
nodeId: id,
page: multiPageIndex,
hidden: isHidden(state, 'node', id),
})),
);

const visiblePages = [...new Set(hiddenState.filter(({ hidden }) => !hidden).map(({ page }) => page ?? 0))];
const firstVisiblePage = Math.min(...visiblePages);
const lastVisiblePage = Math.max(...visiblePages);

const [multiPageIndex, setMultiPageIndex] = useState(firstVisiblePage);

const findNextVisiblePage = useCallback(
(start: number, step: number): number | undefined => {
for (let page = start; step > 0 ? page <= lastVisiblePage : page >= firstVisiblePage; page += step) {
if (hiddenState.some((state) => state.page === page && !state.hidden)) {
return page;
}
}
return undefined;
},
[firstVisiblePage, hiddenState, lastVisiblePage],
);

const nextMultiPage = useCallback(() => {
setMultiPageIndex((prev) => Math.min(prev + 1, lastPage));
}, [lastPage]);
const nextPage = findNextVisiblePage(multiPageIndex + 1, 1);
if (nextPage !== undefined) {
setMultiPageIndex(nextPage);
}
}, [findNextVisiblePage, multiPageIndex]);

const prevMultiPage = useCallback(() => {
setMultiPageIndex((prev) => Math.max(prev - 1, 0));
}, []);
const prevPage = findNextVisiblePage(multiPageIndex - 1, -1);
if (prevPage !== undefined) {
setMultiPageIndex(prevPage);
}
}, [findNextVisiblePage, multiPageIndex]);

return {
multiPageEnabled,
multiPageIndex,
nextMultiPage,
prevMultiPage,
hasNextMultiPage: multiPageEnabled && multiPageIndex < lastPage,
hasPrevMultiPage: multiPageEnabled && multiPageIndex > 0,
hasNextMultiPage: multiPageEnabled && multiPageIndex < lastVisiblePage,
hasPrevMultiPage: multiPageEnabled && multiPageIndex > firstVisiblePage,
setMultiPageIndex,
};
}
Expand Down
18 changes: 18 additions & 0 deletions src/layout/RepeatingGroup/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,22 @@ export const RepGroupHooks = {

return childIds;
},

useChildIdsWithMultiPage(
node: LayoutNode<'RepeatingGroup'> | undefined,
): { id: string; multiPageIndex: number | undefined }[] {
const component = useLayoutLookups().getComponent(node?.baseId, 'RepeatingGroup');
const idMutator = useComponentIdMutator();
if (!component?.edit?.multiPage) {
return component?.children.map(idMutator).map((id) => ({ id, multiPageIndex: undefined })) ?? [];
}

const children: { id: string; multiPageIndex: number | undefined }[] = [];
for (const id of component.children) {
const [multiPageIndex, baseId] = id.split(':', 2);
children.push({ id: idMutator(baseId), multiPageIndex: parseInt(multiPageIndex) });
}

return children;
},
};