-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ObjectPage): Don't crash when conditional rendering is used for c…
…hildren (#284)
- Loading branch information
1 parent
b269da3
commit 5adfc15
Showing
10 changed files
with
371 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/main/src/components/ObjectPage/ObjectPageHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { AvatarSize } from '@ui5/webcomponents-react/lib/AvatarSize'; | ||
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox'; | ||
import { FlexBoxDirection } from '@ui5/webcomponents-react/lib/FlexBoxDirection'; | ||
import React, { CSSProperties, FC, ReactElement } from 'react'; | ||
import { safeGetChildrenArray } from './ObjectPageUtils'; | ||
|
||
interface Props { | ||
image: string | ReactElement<unknown>; | ||
imageShapeCircle: boolean; | ||
classes: any; | ||
showTitleInHeaderContent: boolean; | ||
renderHeaderContentProp: () => JSX.Element; | ||
renderBreadcrumbs: () => JSX.Element; | ||
renderKeyInfos: () => JSX.Element; | ||
title: string; | ||
subTitle: string; | ||
} | ||
|
||
const positionRelativeStyle: CSSProperties = { position: 'relative' }; | ||
|
||
export const ObjectPageHeader: FC<Props> = (props) => { | ||
const { | ||
image, | ||
classes, | ||
imageShapeCircle, | ||
showTitleInHeaderContent, | ||
renderHeaderContentProp, | ||
renderBreadcrumbs, | ||
title, | ||
subTitle, | ||
renderKeyInfos | ||
} = props; | ||
|
||
let avatar = null; | ||
|
||
if (image) { | ||
if (typeof image === 'string') { | ||
avatar = ( | ||
<span | ||
className={classes.headerImage} | ||
style={{ borderRadius: imageShapeCircle ? '50%' : 0, overflow: 'hidden' }} | ||
> | ||
<img src={image} className={classes.image} alt="Company Logo" /> | ||
</span> | ||
); | ||
} else { | ||
avatar = React.cloneElement(image, { | ||
size: AvatarSize.L, | ||
className: image.props?.className ? `${classes.headerImage} ${image.props?.className}` : classes.headerImage | ||
} as unknown); | ||
} | ||
} | ||
|
||
if (showTitleInHeaderContent) { | ||
const headerContents = renderHeaderContentProp && renderHeaderContentProp(); | ||
let firstElement; | ||
let contents = []; | ||
|
||
if (headerContents?.type === React.Fragment) { | ||
[firstElement, ...contents] = safeGetChildrenArray(headerContents.props.children); | ||
} else { | ||
firstElement = headerContents; | ||
} | ||
return ( | ||
<div className={classes.contentHeader}> | ||
<div className={classes.headerContent}> | ||
<FlexBox> | ||
{avatar} | ||
<FlexBox direction={FlexBoxDirection.Column}> | ||
<div>{renderBreadcrumbs && renderBreadcrumbs()}</div> | ||
<FlexBox> | ||
<FlexBox direction={FlexBoxDirection.Column}> | ||
<h1 className={classes.title}>{title}</h1> | ||
<span className={classes.subTitle}>{subTitle}</span> | ||
<span> {firstElement}</span> | ||
</FlexBox> | ||
<FlexBox> | ||
{contents.map((c, index) => ( | ||
<div key={`customContent-${index}`} className={classes.headerCustomContentItem}> | ||
{c} | ||
</div> | ||
))} | ||
</FlexBox> | ||
<div className={classes.keyInfos}>{renderKeyInfos && renderKeyInfos()}</div> | ||
</FlexBox> | ||
</FlexBox> | ||
</FlexBox> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div style={positionRelativeStyle} className={classes.contentHeader}> | ||
<div className={classes.headerContent}> | ||
{avatar} | ||
{renderHeaderContentProp && <span className={classes.headerCustomContent}>{renderHeaderContentProp()}</span>} | ||
</div> | ||
</div> | ||
); | ||
}; |
59 changes: 59 additions & 0 deletions
59
packages/main/src/components/ObjectPage/ObjectPageScrollBar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ZIndex } from '@ui5/webcomponents-react/enums/ZIndex'; | ||
import { JSSTheme } from '@ui5/webcomponents-react/interfaces/JSSTheme'; | ||
import React, { FC, RefObject, useMemo } from 'react'; | ||
import { createUseStyles } from 'react-jss'; | ||
|
||
interface Props { | ||
scrollBarRef: RefObject<HTMLDivElement>; | ||
innerScrollBarRef: RefObject<HTMLDivElement>; | ||
width: number; | ||
} | ||
|
||
const styles = ({ parameters }: JSSTheme) => ({ | ||
outerScrollbar: { | ||
position: 'absolute', | ||
right: 0, | ||
overflow: 'hidden', | ||
height: '100%', | ||
zIndex: ZIndex.ResponsivePopover, | ||
backgroundColor: parameters.sapUiObjectHeaderBackground, | ||
'& ::-webkit-scrollbar': { | ||
backgroundColor: '#ffffff' | ||
}, | ||
'& ::-webkit-scrollbar-thumb': { | ||
backgroundColor: '#949494', | ||
'&:hover': { | ||
backgroundColor: '#8c8c8c' | ||
} | ||
}, | ||
'& ::-webkit-scrollbar-corner': { | ||
backgroundColor: '#ffffff' | ||
} | ||
}, | ||
innerScrollbar: { | ||
width: '34px', | ||
overflowY: 'scroll', | ||
overflowX: 'hidden', | ||
height: '100%' | ||
} | ||
}); | ||
|
||
const useScrollBarStyles = createUseStyles(styles, { name: 'ObjectPageScrollBar' }); | ||
|
||
export const ObjectPageScrollBar: FC<Props> = (props) => { | ||
const { scrollBarRef, innerScrollBarRef, width } = props; | ||
|
||
const [scrollBarWidthStyle, scrollBarWidthMargin] = useMemo(() => { | ||
return [{ width: `${width}px` }, { marginLeft: `-${width}px`, width: `${2 * width}px` }]; | ||
}, [width]); | ||
|
||
const classes = useScrollBarStyles(); | ||
|
||
return ( | ||
<div style={scrollBarWidthStyle} className={classes.outerScrollbar}> | ||
<div ref={scrollBarRef} style={scrollBarWidthMargin} className={classes.innerScrollbar}> | ||
<div ref={innerScrollBarRef} style={scrollBarWidthStyle} /> | ||
</div> | ||
</div> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/main/src/components/ObjectPage/ObjectPageUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Children, ReactElement } from 'react'; | ||
|
||
export const safeGetChildrenArray = (children) => Children.toArray(children).filter(Boolean); | ||
|
||
export const findSectionIndexById = (sections: ReactElement<any> | Array<ReactElement<any>>, id) => { | ||
const index = safeGetChildrenArray(sections).findIndex((objectPageSection) => objectPageSection.props?.id === id); | ||
if (index === -1) { | ||
return 0; | ||
} | ||
return index; | ||
}; | ||
|
||
export const getProportionateScrollTop = (activeContainer, passiveContainer, base) => { | ||
const activeHeight = activeContainer.current.getBoundingClientRect().height; | ||
const passiveHeight = passiveContainer.current.getBoundingClientRect().height; | ||
|
||
return (base / activeHeight) * passiveHeight; | ||
}; | ||
|
||
export const bindScrollEvent = (scrollContainer, handler) => { | ||
if (scrollContainer.current && handler.current) { | ||
scrollContainer.current.addEventListener('scroll', handler.current, { passive: true }); | ||
} | ||
}; | ||
|
||
export const removeScrollEvent = (scrollContainer, handler) => { | ||
if (scrollContainer.current && handler.current) { | ||
scrollContainer.current.removeEventListener('scroll', handler.current); | ||
} | ||
}; |
Oops, something went wrong.