Skip to content

Commit

Permalink
feat: toc start expanded (#197)
Browse files Browse the repository at this point in the history
* feat: toc start expanded

* fix: flat expand
  • Loading branch information
Jakub Jankowski authored Nov 17, 2020
1 parent 3ccebba commit 8134d01
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type TableOfContentsItem = {
isLoading?: boolean;
isDisabled?: boolean;
showSkeleton?: boolean;
startExpanded?: boolean;
action?: {
icon?: FAIconProp;
name?: string;
Expand Down Expand Up @@ -105,7 +106,10 @@ function TableOfContentsInner<T extends TableOfContentsItem = TableOfContentsIte
rowComponent: RowComponent = DefaultRow,
rowComponentExtraProps,
}: Pick<ITableOfContents<T, E>, 'className' | 'contents' | 'rowComponent' | 'rowComponentExtraProps'>) {
const [expanded, setExpanded] = React.useState({});
const [expanded, setExpanded] = React.useState(() => {
const itemsToExpand = contents.filter(item => item.startExpanded);
return Object.fromEntries(itemsToExpand.map(item => [contents.indexOf(item), true]));
});

// an array of functions. Invoking the N-th function toggles the expanded flag on the N-th content item
const toggleExpandedFunctions = React.useMemo(() => {
Expand Down
1 change: 1 addition & 0 deletions src/__fixtures__/table-of-contents/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const tree: ITableOfContentsLink[] = [
type: 'group',
to: '/path',
icon: 'cloud',
startExpanded: true,
},
{
name: 'Nested Item with text icon',
Expand Down

0 comments on commit 8134d01

Please sign in to comment.