Skip to content

Improve SectionView child iteration #4728

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: develop
Choose a base branch
from
Open
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
36 changes: 10 additions & 26 deletions src/components/ui4/SectionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fixSides, mapSides, sidesToMargin } from '../../util/sides'
import { cacheStyles, Theme, useTheme } from '../services/ThemeContext'

interface Props {
children: React.ReactNode | React.ReactNode[]
children: React.ReactNode

// For scene-level usage where we want the line to extend all the way to the
// right
Expand Down Expand Up @@ -40,33 +40,17 @@ export const SectionView = (props: Props): JSX.Element | null => {
? styles.dividerMarginScene
: styles.dividerMarginCard

const nonNullChildren = React.Children.map(children, child => {
if (child != null) {
return child
}
// Add a line divider after each child:
const dividedChildren: React.ReactNode[] = []
React.Children.forEach(children, (child, i): void => {
if (child == null || child === false) return
dividedChildren.push(child)
dividedChildren.push(<View key={`line${i}`} style={[styles.divider, dividerMargin]} />)
})
const numChildren = React.Children.count(nonNullChildren)

if (children == null || numChildren === 0) return null

// Add a line divider between each child if there's more than one:
return (
<View style={[styles.container, margin]}>
{numChildren === 1
? nonNullChildren
: React.Children.map(nonNullChildren, (child, index) => {
if (index < numChildren - 1) {
return (
<>
{child}
<View style={[styles.divider, dividerMargin]} />
</>
)
}
return child
})}
</View>
)
// Render the children, skipping the last line divider:
if (dividedChildren.length === 0) return null
return <View style={[styles.container, margin]}>{dividedChildren.slice(0, -1)}</View>
}

const getStyles = cacheStyles((theme: Theme) => ({
Expand Down