Skip to content

Commit 73bc443

Browse files
committed
Some clean up for NavList
1 parent 8f1bc3b commit 73bc443

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/react/src/NavList/NavList.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,18 @@ export const GroupExpand = React.forwardRef<HTMLButtonElement, NavListGroupExpan
436436
({label = 'Show more', pages = 0, items, renderItem, ...props}, forwardedRef) => {
437437
const [currentPage, setCurrentPage] = React.useState(0)
438438
const groupId = useId()
439+
439440
const teamEnabled = useFeatureFlag('primer_react_css_modules_team')
440441
const staffEnabled = useFeatureFlag('primer_react_css_modules_staff')
441442

443+
const calculateVisibleItems = React.useCallback(() => {
444+
const itemsPerPage = items.length / pages
445+
const amountToShow = pages === 0 ? items.length : Math.ceil(itemsPerPage * currentPage)
446+
const focusTargetIndex = currentPage === 1 ? 0 : amountToShow - Math.floor(itemsPerPage)
447+
448+
return {amountToShow, focusTargetIndex}
449+
}, [pages, items, currentPage])
450+
442451
return (
443452
<>
444453
{currentPage > 0 ? (
@@ -451,9 +460,7 @@ export const GroupExpand = React.forwardRef<HTMLButtonElement, NavListGroupExpan
451460
trailingAction,
452461
...rest
453462
} = itemArr
454-
const itemsPerPage = items.length / pages
455-
const amountToShow = pages === 0 ? items.length : Math.ceil(itemsPerPage * currentPage)
456-
const focusTargetIndex = currentPage === 1 ? 0 : amountToShow - Math.floor(itemsPerPage)
463+
const {amountToShow, focusTargetIndex} = calculateVisibleItems()
457464
const focusTarget = index === focusTargetIndex ? groupId : 'false'
458465

459466
const {icon, label: actionLabel, ...props} = trailingAction || {}
@@ -473,14 +480,12 @@ export const GroupExpand = React.forwardRef<HTMLButtonElement, NavListGroupExpan
473480
</LeadingVisual>
474481
) : null}
475482
{text}
476-
{!trailingAction && TrailingVisualIcon ? (
483+
{TrailingVisualIcon ? (
477484
<TrailingVisual>
478485
<TrailingVisualIcon />
479486
</TrailingVisual>
480487
) : null}
481-
{!TrailingVisualIcon && trailingAction ? (
482-
<TrailingAction icon={icon} label={actionLabel || ''} {...props} />
483-
) : null}
488+
{trailingAction ? <TrailingAction icon={icon} label={actionLabel || ''} {...props} /> : null}
484489
</Item>
485490
)
486491
}

0 commit comments

Comments
 (0)