-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
toHierarchy
even more generic (reusable with RecursiveList
)
- Loading branch information
Showing
15 changed files
with
111 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
docs/src/pages/framed/RecursiveList/RecursiveListFlatArray.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script> | ||
import { RecursiveList, toHierarchy } from "carbon-components-svelte"; | ||
const nodesFlat = [ | ||
{ id: 1, text: "Item 1" }, | ||
{ id: 2, text: "Item 1a", pid: 1 }, | ||
{ id: 3, html: "<h5>HTML content</h5>", pid: 2 }, | ||
{ id: 4, text: "Item 2" }, | ||
{ id: 5, href: "https://svelte.dev/", pid: 4 }, | ||
{ | ||
id: 6, | ||
href: "https://svelte.dev/", | ||
text: "Link with custom text", | ||
pid: 4, | ||
}, | ||
{ id: 7, text: "Item 3" }, | ||
]; | ||
</script> | ||
|
||
<RecursiveList nodes={toHierarchy(nodesFlat, (node) => node.pid)} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as TreeView } from "./TreeView.svelte"; | ||
export { toHierarchy } from "./treeview"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as TreeView } from "./TreeView.svelte"; | ||
export { toHierarchy } from "./treeview"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,5 @@ export function toHierarchy(flatArray, getParentId) { | |
|
||
return tree; | ||
} | ||
|
||
export default toHierarchy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script lang="ts"> | ||
import { RecursiveList } from "carbon-components-svelte"; | ||
import toHierarchy from "../../src/utils/toHierarchy"; | ||
let nodes = toHierarchy( | ||
[ | ||
{ id: 1, text: "Item 1" }, | ||
{ id: 2, text: "Item 1a", pid: 1 }, | ||
{ id: 3, html: "<h5>HTML content</h5>", pid: 2 }, | ||
{ id: 4, text: "Item 2" }, | ||
{ id: 5, href: "https://svelte.dev/", pid: 4 }, | ||
{ | ||
id: 6, | ||
href: "https://svelte.dev/", | ||
text: "Link with custom text", | ||
pid: 4, | ||
}, | ||
{ id: 7, text: "Item 3" }, | ||
], | ||
(node) => node.pid, | ||
); | ||
</script> | ||
|
||
<RecursiveList type="ordered" {nodes} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { render, screen } from "@testing-library/svelte"; | ||
import RecursiveListHierarchyTest from "./RecursiveList.hierarchy.test.svelte"; | ||
import RecursiveListTest from "./RecursiveList.test.svelte"; | ||
|
||
const testCases = [ | ||
{ name: "RecursiveList", component: RecursiveListTest }, | ||
{ name: "RecursiveList hierarchy", component: RecursiveListHierarchyTest }, | ||
]; | ||
|
||
describe.each(testCases)("$name", ({ component }) => { | ||
it("renders all top-level items", () => { | ||
render(component); | ||
|
||
expect(screen.getByText("Item 1")).toBeInTheDocument(); | ||
expect(screen.getByText("Item 2")).toBeInTheDocument(); | ||
expect(screen.getByText("Item 3")).toBeInTheDocument(); | ||
|
||
expect(screen.getAllByRole("list")).toHaveLength(4); | ||
|
||
// Nested items | ||
expect(screen.getByText("Item 1a")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders HTML content", () => { | ||
render(component); | ||
|
||
const htmlContent = screen.getByText("HTML content"); | ||
expect(htmlContent.tagName).toBe("H5"); | ||
}); | ||
|
||
it("renders links correctly", () => { | ||
render(component); | ||
|
||
const links = screen.getAllByRole("link"); | ||
expect(links).toHaveLength(2); | ||
|
||
// Link with custom text | ||
const customLink = screen.getByText("Link with custom text"); | ||
expect(customLink).toHaveAttribute("href", "https://svelte.dev/"); | ||
|
||
// Plain link | ||
const plainLink = links.find( | ||
(link) => link.textContent === "https://svelte.dev/", | ||
); | ||
expect(plainLink).toHaveAttribute("href", "https://svelte.dev/"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as TreeView } from "./TreeView.svelte"; | ||
export { toHierarchy } from "./treeview"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters