-
Notifications
You must be signed in to change notification settings - Fork 16
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: Menu and MenuTrigger Docs #1051
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1285148
menu trigger docs
dgodinez-dh 053221a
merge latest
dgodinez-dh 6d6acd0
finish menu trigger doc
dgodinez-dh bb1d7d1
initial menu doc
dgodinez-dh af1e12c
menu doc
dgodinez-dh 45ccee9
item table source
dgodinez-dh 3b24bb2
merge latest
dgodinez-dh 183f80d
Apply suggestions from code review
dgodinez-dh 1fddb44
merge latest
dgodinez-dh ffad948
merge latest
dgodinez-dh 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 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,246 @@ | ||
# Menu | ||
|
||
Menus display a list of actions or options that a user can choose. | ||
|
||
## Example | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
my_menu_example = ui.menu_trigger( | ||
ui.action_button("Edit"), | ||
ui.menu( | ||
ui.item("Cut", key="cut"), | ||
ui.item("Copy", key="copy"), | ||
ui.item("Paste", key="paste"), | ||
ui.item("Replace", key="replace"), | ||
on_action=lambda key: print(key), | ||
), | ||
) | ||
``` | ||
|
||
## Content | ||
|
||
Menu accepts `item` elements as children, each with a `key` prop. Basic usage of `menu`, seen in the example above, shows multiple items populated with a string. | ||
|
||
## Events | ||
|
||
Use the `on_selection_change` prop as a callback to handle press events on items when `selection_mode` is either `single` or `multiple`. See Selection for more information. | ||
|
||
Menu also supports the `on_action` callback when `selection_mode` is `none` (default). | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
@ui.component | ||
def open_action_example(): | ||
action, set_action = ui.use_state() | ||
return ui.flex( | ||
ui.menu_trigger( | ||
ui.action_button("Edit"), | ||
ui.menu( | ||
ui.item("Cut", key="cut"), | ||
ui.item("Copy", key="copy"), | ||
ui.item("Paste", key="paste"), | ||
on_action=set_action, | ||
), | ||
), | ||
ui.text(f"Action {action}"), | ||
gap="size-100", | ||
align_items="center", | ||
) | ||
|
||
|
||
my_open_action_example = open_action_example() | ||
``` | ||
|
||
## Selection | ||
|
||
Menu supports multiple selection modes. By default, selection is disabled, however this can be changed using the `selection_mode` prop. Use `default_selected_keys` to provide a default set of selected items (uncontrolled) or `selected_keys` to set the selected items (controlled). The value of the selected keys must match the key prop of the items. | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
@ui.component | ||
def single_selection_example(): | ||
selected, set_selected = ui.use_state(["middle"]) | ||
return ui.flex( | ||
ui.menu_trigger( | ||
ui.action_button("Align"), | ||
ui.menu( | ||
ui.item("Left", key="left"), | ||
ui.item("Middle", key="middle"), | ||
ui.item("Right", key="right"), | ||
selection_mode="single", | ||
selected_keys=selected, | ||
on_selection_change=set_selected, | ||
), | ||
), | ||
ui.text(f"Current selection (controlled) {selected}"), | ||
gap="size-100", | ||
align_items="center", | ||
) | ||
|
||
|
||
my_single_selection_example = single_selection_example() | ||
``` | ||
|
||
Set `selection_mode` prop to `multiple` to allow more than one selection. | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
@ui.component | ||
def multiple_selection_example(): | ||
selected, set_selected = ui.use_state(["sidebar", "console"]) | ||
return ui.flex( | ||
ui.menu_trigger( | ||
ui.action_button("Show"), | ||
ui.menu( | ||
ui.item("Sidebar", key="sidebar"), | ||
ui.item("Searchbar", key="searchbar"), | ||
ui.item("Tools", key="tools"), | ||
ui.item("Console", key="console"), | ||
selection_mode="multiple", | ||
selected_keys=selected, | ||
on_selection_change=set_selected, | ||
), | ||
close_on_select=False, | ||
), | ||
ui.text(f"Current selection (controlled) {selected}"), | ||
gap="size-100", | ||
align_items="center", | ||
) | ||
|
||
|
||
my_multiple_selection_example = multiple_selection_example() | ||
``` | ||
|
||
## Links | ||
|
||
By default, interacting with an item in a Menu triggers `on_action` and optionally `on_selection_change` depending on the `selection_mode`. Alternatively, items may be links to another page or website. This can be achieved by passing the `href` prop to the `item` component. Link items in a menu are not selectable. | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
my_link_example = ui.menu_trigger( | ||
ui.action_button("Links"), | ||
ui.menu( | ||
ui.item("Adobe", href="https://adobe.com/", target="_blank"), | ||
ui.item("Apple", href="https://apple.com/", target="_blank"), | ||
ui.item("Google", href="https://google.com/", target="_blank"), | ||
ui.item("Microsoft", href="https://microsoft.com/", target="_blank"), | ||
), | ||
) | ||
``` | ||
|
||
## Sections | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
@ui.component | ||
def sections_example(): | ||
selected, set_selected = ui.use_state(["bold", "left"]) | ||
return ( | ||
ui.menu_trigger( | ||
ui.action_button("Show"), | ||
ui.menu( | ||
ui.section( | ||
ui.item("Bold", key="bold"), | ||
ui.item("Underline", key="underline"), | ||
title="Styles", | ||
), | ||
ui.section( | ||
ui.item("Left", key="left"), | ||
ui.item("Middle", key="middle"), | ||
ui.item("Right", key="right"), | ||
title="Align", | ||
), | ||
selection_mode="multiple", | ||
selected_keys=selected, | ||
on_selection_change=set_selected, | ||
), | ||
close_on_select=False, | ||
), | ||
) | ||
|
||
|
||
my_sections_example = sections_example() | ||
``` | ||
|
||
## Submenus | ||
|
||
Submenus can be created by wrapping an item and a menu in a `submenu_trigger`. The `submenu_trigger` accepts exactly two children: the `item` which triggers opening of the submenu, and the `menu` itself. Each submenu's `menu` accepts its own set of menu props, allowing you to customize its user action and selection behavior. | ||
|
||
```python | ||
from deephaven import ui | ||
|
||
|
||
my_submenu_example = ui.menu_trigger( | ||
ui.action_button("Actions"), | ||
ui.menu( | ||
ui.item("Cut", key="cut"), | ||
ui.item("Copy", key="copy"), | ||
ui.item("Paste", key="paste"), | ||
ui.submenu_trigger( | ||
ui.item("Share", key="share"), | ||
ui.menu( | ||
ui.item("Copy link", key="copy link"), | ||
ui.submenu_trigger( | ||
ui.item("Email", key="email"), | ||
ui.menu( | ||
ui.item("Email as attachment", "attachment"), | ||
ui.item("Email as link", "link"), | ||
on_action=lambda key: print(f"Email menu {key} action"), | ||
), | ||
), | ||
ui.item("SMS", key="sms"), | ||
on_action=lambda key: print(f"Share menu {key} action"), | ||
), | ||
), | ||
ui.item("Delete", key="delete"), | ||
on_action=lambda key: print(f"Root menu {key} action"), | ||
), | ||
) | ||
``` | ||
|
||
## Using item_table_source | ||
|
||
`item_table_source` is used to create complex items from a table (ie., defining which columns are the keys/labels of the data). | ||
|
||
```python | ||
from deephaven import ui, new_table | ||
from deephaven.column import string_col | ||
|
||
_table = new_table( | ||
[ | ||
string_col("Keys", ["key-0", "key-1", "key-2"]), | ||
string_col("Labels", ["Option 0", "Option 1", "Option 2"]), | ||
] | ||
) | ||
|
||
|
||
@ui.component | ||
def menu_table_source(): | ||
source = ui.item_table_source(_table, key_column="Keys", label_column="Labels") | ||
return ui.menu_trigger( | ||
ui.action_button("Table source"), | ||
ui.menu(source), | ||
) | ||
|
||
|
||
my_menu_table_source = menu_table_source() | ||
``` | ||
|
||
## API Reference | ||
|
||
```{eval-rst} | ||
.. dhautofunction:: deephaven.ui.menu | ||
``` |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool to show use with a ticking table as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we really want to use a ticking table as item source for a menu? I suppose it would work, but I dislike the idea of menu items appearing / disappearing as the table ticks. Also, what would happen if the menu was open while the table ticked? Does it update, or is the menu now out of date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess we should clarify the behaviour with @dsmmcken , but I see a few possible options when the table ticks while the menu is open (ordered by my preference):
We definitely need to decide on what to do with a ticking table... and if you're hooking up a ticking table to a menu, I imagine having it update in place is acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spun this off into another ticket:
https://deephaven.atlassian.net/browse/DH-18305
Going to remove this section from the doc for now.