Skip to content

Commit

Permalink
feat: count all descendant visible section items
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Oct 27, 2021
1 parent 554a404 commit 8220c8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function SectionRenderer<ItemType, ScopeType>(props: SectionRendererProps<ItemTy
isSectionInitialized={Boolean(sectionInstance?.isInitialized)}
isSectionVisible={Boolean(sectionInstance?.isVisible)}
level={level}
itemsLength={sectionInstance?.visibleItemIds.length || 0}
itemsLength={sectionInstance?.visibleDescendantItemsCount || 0}
expandSection={expandSection}
collapseSection={collapseSection}
/>
Expand Down
1 change: 1 addition & 0 deletions src/models/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface SectionInstance {
visibleGroupIds: string[];
visibleChildSectionIds?: string[];
visibleDescendantSectionIds?: string[];
visibleDescendantItemsCount?: number;
isLoading: boolean;
isVisible: boolean;
isInitialized: boolean;
Expand Down
34 changes: 28 additions & 6 deletions src/navsections/sectionBlueprintMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,33 @@ const hasNavigatorInstanceStateChanged = (
function computeSectionVisibility(
sectionInstance: SectionInstance,
sectionInstanceMap: Record<string, SectionInstance>
): [boolean, string[] | undefined] {
): [boolean, string[] | undefined, number | undefined] {
const sectionBlueprint = sectionBlueprintMap.getById(sectionInstance.id);
let visibleDescendantItemsCount = 0;

if (sectionBlueprint.childSectionIds && sectionBlueprint.childSectionIds.length > 0) {
const childSectionVisibilityMap: Record<string, boolean> = {};

sectionBlueprint.childSectionIds.forEach(childSectionId => {
const childSectionInstance = sectionInstanceMap[childSectionId];
const [isChildSectionVisible, visibleDescendantSectionIdsOfChildSection] = computeSectionVisibility(
childSectionInstance,
sectionInstanceMap
);
const [
isChildSectionVisible,
visibleDescendantSectionIdsOfChildSection,
visibleDescendantItemsCountOfChildSection,
] = computeSectionVisibility(childSectionInstance, sectionInstanceMap);

if (visibleDescendantSectionIdsOfChildSection) {
if (sectionInstance.visibleDescendantSectionIds) {
sectionInstance.visibleDescendantSectionIds.push(...visibleDescendantSectionIdsOfChildSection);
} else {
sectionInstance.visibleDescendantSectionIds = [...visibleDescendantSectionIdsOfChildSection];
}
}

if (visibleDescendantItemsCountOfChildSection) {
visibleDescendantItemsCount += visibleDescendantItemsCountOfChildSection;
}

childSectionVisibilityMap[childSectionId] = isChildSectionVisible;
if (isChildSectionVisible) {
if (sectionInstance.visibleChildSectionIds) {
Expand All @@ -64,6 +74,7 @@ function computeSectionVisibility(
}
}
});

if (sectionInstance.visibleChildSectionIds) {
if (sectionInstance.visibleDescendantSectionIds) {
sectionInstance.visibleDescendantSectionIds.push(...sectionInstance.visibleChildSectionIds);
Expand All @@ -72,10 +83,21 @@ function computeSectionVisibility(
sectionInstance.visibleDescendantSectionIds = [...sectionInstance.visibleChildSectionIds];
}
}

sectionInstance.isVisible =
sectionInstance.isVisible || Object.values(childSectionVisibilityMap).some(isVisible => isVisible === true);
}
return [sectionInstance.isVisible, sectionInstance.visibleDescendantSectionIds];

if (sectionInstance.visibleItemIds) {
visibleDescendantItemsCount += sectionInstance.visibleItemIds.length;
}

sectionInstance.visibleDescendantItemsCount = visibleDescendantItemsCount;
return [
sectionInstance.isVisible,
sectionInstance.visibleDescendantSectionIds,
sectionInstance.visibleDescendantItemsCount,
];
}
/**
* Build the section and item instances based on the registered section blueprints
Expand Down

0 comments on commit 8220c8a

Please sign in to comment.