Skip to content
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

docs: fragment #962

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions plugins/ui/docs/components/fragment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Fragment

The `fragment` component allows you to group multiple elements without adding extra nodes to the DOM. This is especially useful when you need to return several elements but want to avoid wrapping them in an additional element. By using `fragment`, you can maintain a clean DOM tree and prevent unnecessary nesting.

## Example

```python
from deephaven import ui

my_fragment = ui.fragment(ui.text("Child 1"), ui.text("Child 2"))
```

## Rendering a List

When rendering multiple elements in a loop, ensure each fragment has a unique key. This is crucial if array items might be inserted, deleted, or reordered.

```python
from deephaven import ui


@ui.component
def ui_fragment_list():
children = []

for i in range(1, 4):
children.append(ui.fragment(ui.text(f"Child {i}"), key=i))

return ui.column(*children)


my_fragment = ui_fragment_list()
```

## API Reference

```{eval-rst}
.. dhautofunction:: deephaven.ui.fragment
```
6 changes: 5 additions & 1 deletion plugins/ui/src/deephaven/ui/components/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

from typing import Any
from .basic import component_element
from ..elements import Element


def fragment(*children: Any, key: str | None = None):
def fragment(*children: Any, key: str | None = None) -> Element:
"""
A React.Fragment: https://react.dev/reference/react/Fragment.
Used to group elements together without a wrapper node.

Args:
*children: The children in the fragment.
key: A unique identifier used by React to render elements in a list.

Returns:
The rendered fragment element.
"""
return component_element("Fragment", children=children, key=key)
Loading