Skip to content

Commit 8134d01

Browse files
author
Jakub Jankowski
authored
feat: toc start expanded (#197)
* feat: toc start expanded * fix: flat expand
1 parent 3ccebba commit 8134d01

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/TableOfContents/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type TableOfContentsItem = {
2424
isLoading?: boolean;
2525
isDisabled?: boolean;
2626
showSkeleton?: boolean;
27+
startExpanded?: boolean;
2728
action?: {
2829
icon?: FAIconProp;
2930
name?: string;
@@ -105,7 +106,10 @@ function TableOfContentsInner<T extends TableOfContentsItem = TableOfContentsIte
105106
rowComponent: RowComponent = DefaultRow,
106107
rowComponentExtraProps,
107108
}: Pick<ITableOfContents<T, E>, 'className' | 'contents' | 'rowComponent' | 'rowComponentExtraProps'>) {
108-
const [expanded, setExpanded] = React.useState({});
109+
const [expanded, setExpanded] = React.useState(() => {
110+
const itemsToExpand = contents.filter(item => item.startExpanded);
111+
return Object.fromEntries(itemsToExpand.map(item => [contents.indexOf(item), true]));
112+
});
109113

110114
// an array of functions. Invoking the N-th function toggles the expanded flag on the N-th content item
111115
const toggleExpandedFunctions = React.useMemo(() => {

src/__fixtures__/table-of-contents/tree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const tree: ITableOfContentsLink[] = [
1212
type: 'group',
1313
to: '/path',
1414
icon: 'cloud',
15+
startExpanded: true,
1516
},
1617
{
1718
name: 'Nested Item with text icon',

0 commit comments

Comments
 (0)