-
Notifications
You must be signed in to change notification settings - Fork 1
feat(library): added <RenderChild> and <RenderChildren> #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfd8c5e
feat(library): added <RenderChild> and <RenderChildren>
GauBen 0781c32
deprecated area as sub node
GauBen 8299251
index
GauBen 1434eeb
lint
GauBen fa66b9e
added tests
GauBen 484ccba
moar tests
GauBen e23526d
Merge branch 'main' into feat/render-children
GauBen 68785e4
docs: updated lib readme
GauBen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
29 changes: 29 additions & 0 deletions
29
javascript-modules-library/src/core/server/components/render/RenderChild.tsx
This file contains hidden or 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,29 @@ | ||
| import type { JSX } from "react"; | ||
| import { Render } from "./Render.js"; | ||
| import { useServerContext } from "../../hooks/useServerContext.js"; | ||
| import { AddContentButtons } from "../AddContentButtons.js"; | ||
|
|
||
| /** | ||
| * Renders a child of the current node, designated by its name. | ||
| * | ||
| * If the child node does not exist, it will display the "Add content" buttons. | ||
| */ | ||
| export function RenderChild({ | ||
| name, | ||
| view, | ||
| }: { | ||
| /** | ||
| * The name of the child node to render. | ||
| * | ||
| * In the CND file, it's what's after the `+` in the child node definition: `+ name (type)`. | ||
| */ | ||
| name: string; | ||
| /** View to use when rendering the child. */ | ||
| view?: string | undefined; | ||
| }): JSX.Element { | ||
| const { currentNode } = useServerContext(); | ||
| if (currentNode.hasNode(name)) { | ||
|
GauBen marked this conversation as resolved.
|
||
| return <Render node={currentNode.getNode(name)} view={view} />; | ||
| } | ||
| return <AddContentButtons childName={name} />; | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
javascript-modules-library/src/core/server/components/render/RenderChildren.tsx
This file contains hidden or 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,58 @@ | ||
| import type { JSX } from "react"; | ||
| import { Render } from "./Render.js"; | ||
| import { useServerContext } from "../../hooks/useServerContext.js"; | ||
| import { AddContentButtons } from "../AddContentButtons.js"; | ||
| import { getChildNodes } from "../../utils/jcr/getChildNodes.js"; | ||
| import type { JCRNodeWrapper } from "org.jahia.services.content"; | ||
|
|
||
| /** Renders the children of the current node, and "Add content" buttons afterwards. */ | ||
| export function RenderChildren({ | ||
| view, | ||
| pagination, | ||
| filter = "jnt:content", | ||
| }: { | ||
| /** View to use when rendering the children. */ | ||
| view?: string | undefined; | ||
| /** | ||
| * Pagination parameters: | ||
| * | ||
| * - `{ count: number; start?: number }` to specify the number of children to display and the | ||
| * starting index (defaults to 0). | ||
| * - `{ count: number; page: number }` to specify the number of children to display and the page | ||
| * number. | ||
| * | ||
| * If not provided, all children will be displayed. | ||
| */ | ||
| pagination?: { count: number; start?: number } | { count: number; page: number }; | ||
| /** | ||
| * Filter to apply to the children: | ||
| * | ||
| * - A string to filter by node type. | ||
| * - A function to filter by custom logic. | ||
| * | ||
| * @default "jnt:content" | ||
| */ | ||
| filter?: string | ((node: JCRNodeWrapper) => boolean); | ||
|
GauBen marked this conversation as resolved.
|
||
| }): JSX.Element { | ||
| const { currentNode } = useServerContext(); | ||
| const offset = pagination | ||
| ? "page" in pagination | ||
| ? pagination.page * pagination.count | ||
| : (pagination.start ?? 0) | ||
| : 0; | ||
| const limit = pagination ? pagination.count : -1; | ||
|
|
||
| return ( | ||
| <> | ||
| {getChildNodes( | ||
| currentNode, | ||
| limit, | ||
| offset, | ||
| typeof filter === "string" ? (node) => node.isNodeType(filter) : filter, | ||
| ).map((node) => ( | ||
| <Render key={node.getIdentifier()} node={node} view={view} /> | ||
| ))} | ||
| <AddContentButtons /> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.