Skip to content

Commit 3c61f3c

Browse files
committed
feat(tree): updated defaultTreeItemRenderer for class names
The `defaultTreeItemRenderer` will now correctly pass the `className` and `liClassName` attributes as props to the `TreeItem` by default. This helps with overriding the default styles of when using the `Layout`'s navigation tree. Note: You'll still need to use the `getItemProps` prop if you want to specify custom styles while the tree item is `active` (`selected` state) Closes #920
1 parent b3dbfc5 commit 3c61f3c

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

packages/tree/src/__tests__/Tree.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,23 @@ describe("Tree", () => {
6767
const tree = getByRole("tree");
6868
expect(tree.firstElementChild).not.toBeNull();
6969
});
70+
71+
it("should pass the tree item's className and liClassName attributes by default", () => {
72+
const data = {
73+
"item-1": {
74+
parentId: null,
75+
itemId: "item-1",
76+
name: "Item 1",
77+
className: "item-1-class-name",
78+
liClassName: "item-1-li-class-name",
79+
},
80+
};
81+
82+
const { container, getByRole } = render(<Tree {...PROPS} data={data} />);
83+
expect(container).toMatchSnapshot();
84+
85+
const item = getByRole("treeitem");
86+
expect(item.className).toContain("item-1-li-class-name");
87+
expect(item.children[0].className).toContain("item-1-class-name");
88+
});
7089
});

packages/tree/src/__tests__/__snapshots__/Tree.tsx.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,38 @@ exports[`Tree should be able to use a rootId that is not null 1`] = `
169169
</ul>
170170
</div>
171171
`;
172+
173+
exports[`Tree should pass the tree item's className and liClassName attributes by default 1`] = `
174+
<div>
175+
<ul
176+
aria-activedescendant=""
177+
class="rmd-list rmd-tree"
178+
id="tree"
179+
role="tree"
180+
tabindex="0"
181+
>
182+
<li
183+
aria-level="1"
184+
aria-posinset="1"
185+
aria-setsize="1"
186+
class="rmd-tree-item item-1-li-class-name"
187+
id="tree-item-1"
188+
role="treeitem"
189+
tabindex="-1"
190+
>
191+
<span
192+
class="rmd-tree-item__content rmd-tree-item__content--clickable item-1-class-name"
193+
>
194+
<span
195+
class="rmd-list-item__text"
196+
>
197+
Item 1
198+
</span>
199+
<span
200+
class="rmd-ripple-container"
201+
/>
202+
</span>
203+
</li>
204+
</ul>
205+
</div>
206+
`;

packages/tree/src/defaultTreeItemRenderer.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export default function defaultTreeItemRenderer(
7474
let isLink: boolean | undefined;
7575
let readOnly: boolean | undefined;
7676
let disabled: boolean | undefined;
77+
let className: string | undefined;
78+
let liClassName: string | undefined;
7779
if (typeof treeItem.isLink === "boolean") {
7880
({ isLink } = treeItem);
7981
}
@@ -86,6 +88,14 @@ export default function defaultTreeItemRenderer(
8688
({ disabled } = treeItem);
8789
}
8890

91+
if (typeof treeItem.className === "string") {
92+
({ className } = treeItem);
93+
}
94+
95+
if (typeof treeItem.liClassName === "string") {
96+
({ liClassName } = treeItem);
97+
}
98+
8999
const overrides = getItemProps({
90100
...treeItem,
91101
focused,
@@ -125,6 +135,8 @@ export default function defaultTreeItemRenderer(
125135
rightAddonPosition={rightAddonPosition}
126136
expanderLeft={expanderLeft}
127137
expanderIcon={expanderIcon}
138+
className={className}
139+
liClassName={liClassName}
128140
{...overrides}
129141
>
130142
{children}

0 commit comments

Comments
 (0)