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
85 changes: 81 additions & 4 deletions packages/main/src/components/ObjectPage/ObjectPage.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,75 @@ describe('ObjectPage', () => {
cy.get('@cb').should('not.been.called');
});

it('programmatic prop selection', () => {
const TestComp = (props: ObjectPagePropTypes) => {
const [selectedSection, setSelectedSection] = useState(props.selectedSectionId);
const [selectedSubSection, setSelectedSubSection] = useState(props.selectedSubSectionId);

return (
<>
<button
onClick={() => {
setSelectedSection('goals');
}}
>
Select Goals
</button>
<button
onClick={() => {
setSelectedSubSection('personal-payment-information');
}}
>
Select Payment Information
</button>
<ObjectPage
{...props}
selectedSubSectionId={selectedSubSection}
selectedSectionId={selectedSection}
style={{ height: '1000px', scrollBehavior: 'auto' }}
>
{[
...OPContent.slice(0, 1),
<ObjectPageSection key="0.5" titleText="Test2" id="test2" aria-label="Test2">
<div data-testid="section 1.5" style={{ height: '1200px', width: '100%', background: 'lightyellow' }}>
<span data-testid="test-content">test-content</span>
</div>
</ObjectPageSection>,
...OPContent.slice(1)
]}
</ObjectPage>
</>
);
};

[
{ headerTitle: DPTitle, headerContent: DPContent },
{ headerTitle: DPTitle },
{ headerContent: DPContent },
{}
].forEach((props: ObjectPagePropTypes) => {
cy.mount(<TestComp {...props} selectedSubSectionId={`employment-job-relationship`} />);
cy.findByText('employment-job-relationship-content').should('be.visible');
cy.findByText('Job Information').should('not.be.visible');
cy.get('[ui5-tabcontainer]').findUi5TabByText('Employment').should('have.attr', 'aria-selected', 'true');

cy.mount(<TestComp {...props} selectedSectionId={`personal`} />);
cy.findByText('personal-connect-content').should('be.visible');
cy.findByText('test-content').should('not.be.visible');
cy.get('[ui5-tabcontainer]').findUi5TabByText('Personal').should('have.attr', 'aria-selected', 'true');

cy.findByText('Select Goals').click();
cy.findByText('goals-content').should('be.visible');
cy.findByText('personal-connect-content').should('not.be.visible');
cy.get('[ui5-tabcontainer]').findUi5TabByText('Goals').should('have.attr', 'aria-selected', 'true');

cy.findByText('Select Payment Information').click();
cy.findByText('personal-payment-information-content').should('be.visible');
cy.findByText('personal-connect-content').should('not.be.visible');
cy.get('[ui5-tabcontainer]').findUi5TabByText('Personal').should('have.attr', 'aria-selected', 'true');
});
});

cypressPassThroughTestsFactory(ObjectPage);
});

Expand Down Expand Up @@ -952,7 +1021,9 @@ const DPContent = (

const OPContent = [
<ObjectPageSection key="0" titleText="Goals" id="goals" aria-label="Goals">
<div data-testid="section 1" style={{ height: '400px', width: '100%', background: 'lightblue' }} />
<div data-testid="section 1" style={{ height: '400px', width: '100%', background: 'lightblue' }}>
<span>goals-content</span>
</div>
</ObjectPageSection>,
<ObjectPageSection key="1" titleText="Test" id="test" aria-label="Test">
<div data-testid="section 2" style={{ height: '1200px', width: '100%', background: 'lightyellow' }}></div>
Expand All @@ -972,14 +1043,18 @@ const OPContent = [
</>
}
>
<div style={{ height: '400px', width: '100%', background: 'black' }} />
<div style={{ height: '400px', width: '100%', background: 'black' }}>
<span>personal-connect-content</span>
</div>
</ObjectPageSubSection>
<ObjectPageSubSection
titleText="Payment Information"
id="personal-payment-information"
aria-label="Payment Information"
>
<div style={{ height: '400px', width: '100%', background: 'blue' }} />
<div style={{ height: '400px', width: '100%', background: 'blue' }}>
<span>personal-payment-information-content</span>
</div>
</ObjectPageSubSection>
</ObjectPageSection>,
<ObjectPageSection key="3" titleText="Employment" id={`~\`!1@#$%^&*()-_+={}[]:;"'z,<.>/?|♥`} aria-label="Employment">
Expand All @@ -990,7 +1065,9 @@ const OPContent = [
<div style={{ height: '100px', width: '100%', background: 'cadetblue' }}></div>
</ObjectPageSubSection>
<ObjectPageSubSection titleText="Job Relationship" id="employment-job-relationship" aria-label="Job Relationship">
<div style={{ height: '100px', width: '100%', background: 'lightgrey' }}></div>
<div style={{ height: '100px', width: '100%', background: 'lightgrey' }}>
<span>employment-job-relationship-content</span>
</div>
</ObjectPageSubSection>
</ObjectPageSection>
];
Expand Down
77 changes: 43 additions & 34 deletions packages/main/src/components/ObjectPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
const [sectionSpacer, setSectionSpacer] = useState(0);
const [currentTabModeSection, setCurrentTabModeSection] = useState(null);
const sections = mode === ObjectPageMode.IconTabBar ? currentTabModeSection : children;
const isScrolling = useRef<ReturnType<typeof setTimeout> | null>(null);

const deprecationNoticeDisplayed = useRef(false);
useEffect(() => {
Expand Down Expand Up @@ -336,7 +337,6 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
} else {
scrollToSectionById(sectionId);
}
isProgrammaticallyScrolled.current = false;
};

const programmaticallySetSection = () => {
Expand Down Expand Up @@ -388,6 +388,7 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
}
return newSelectionSectionId;
});
prevSelectedSectionId.current = newSelectionSectionId;
scrollEvent.current = targetEvent;
fireOnSelectedChangedEvent(targetEvent, index, newSelectionSectionId, section);
};
Expand Down Expand Up @@ -448,6 +449,7 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
});
if (sectionId) {
setInternalSelectedSectionId(sectionId);
prevSelectedSectionId.current = sectionId;
}
}
}
Expand Down Expand Up @@ -504,6 +506,7 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
if (mode === ObjectPageMode.IconTabBar) {
const sectionId = e.detail.sectionId;
setInternalSelectedSectionId(sectionId);
prevSelectedSectionId.current = sectionId;
const sectionNodes = objectPageRef.current?.querySelectorAll(
'section[data-component-name="ObjectPageSection"]'
);
Expand Down Expand Up @@ -552,11 +555,11 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
}
return visibleSectionIds.current.has(section.id);
});

if (sortedVisibleSections.length > 0) {
if (sortedVisibleSections.length > 0 && !isProgrammaticallyScrolled.current) {
const section = sortedVisibleSections[0];
const id = sortedVisibleSections[0].id.slice(18);
setInternalSelectedSectionId(id);
prevSelectedSectionId.current = id;
debouncedOnSectionChange(scrollEvent.current, currentIndex, id, section);
}
});
Expand Down Expand Up @@ -707,40 +710,46 @@ const ObjectPage = forwardRef<HTMLDivElement, ObjectPagePropTypes>((props, ref)
};

const prevScrollTop = useRef();
const onObjectPageScroll = useCallback(
(e) => {
if (!isToggledRef.current) {
isToggledRef.current = true;
}
if (scrollTimeout.current >= performance.now()) {
const onObjectPageScroll = (e) => {
if (!isToggledRef.current) {
isToggledRef.current = true;
}
if (isScrolling.current) {
clearTimeout(isScrolling.current);
}

isScrolling.current = setTimeout(() => {
console.log('end scroll');
isProgrammaticallyScrolled.current = false;
}, 300);

if (scrollTimeout.current >= performance.now()) {
return;
}
scrollEvent.current = e;
if (typeof props.onScroll === 'function') {
props.onScroll(e);
}
if (selectedSubSectionId) {
setSelectedSubSectionId(undefined);
}
if (selectionScrollTimeout.current) {
clearTimeout(selectionScrollTimeout.current);
}
if (!headerPinned || e.target.scrollTop === 0) {
objectPageRef.current?.classList.remove(classNames.headerCollapsed);
}
if (scrolledHeaderExpanded && e.target.scrollTop !== prevScrollTop.current) {
if (e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight) {
return;
}
scrollEvent.current = e;
if (typeof props.onScroll === 'function') {
props.onScroll(e);
}
if (selectedSubSectionId) {
setSelectedSubSectionId(undefined);
prevScrollTop.current = e.target.scrollTop;
if (!headerPinned) {
setHeaderCollapsedInternal(true);
}
if (selectionScrollTimeout.current) {
clearTimeout(selectionScrollTimeout.current);
}
if (!headerPinned || e.target.scrollTop === 0) {
objectPageRef.current?.classList.remove(classNames.headerCollapsed);
}
if (scrolledHeaderExpanded && e.target.scrollTop !== prevScrollTop.current) {
if (e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight) {
return;
}
prevScrollTop.current = e.target.scrollTop;
if (!headerPinned) {
setHeaderCollapsedInternal(true);
}
setScrolledHeaderExpanded(false);
}
},
[topHeaderHeight, headerPinned, props.onScroll, scrolledHeaderExpanded, selectedSubSectionId]
);
setScrolledHeaderExpanded(false);
}
};

const onHoverToggleButton = useCallback(
(e) => {
Expand Down
Loading