-
-
Notifications
You must be signed in to change notification settings - Fork 69
allow functions as props in renderNode in Tree component #608
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
Conversation
Test Environment for snehilvj/dash-mantine-components-608 |
Hey @Godisemo - I thought you might be interested in this one. I'd love to get your feedback! Here is the sample app deployed on PyCafe |
This looks like a great addition @AnnMarieW. My only question is how would it work if one makes a custom node renderer with some custom checkboxes, would it still be possible to connect the state to the tree checked property? |
Great question! I works well. As mentioned in the Mantine docs, it's necessary to use the Here is an example (which I'll also add to the docs) Uses the same data as above import json
import dash_mantine_components as dmc
from dash import Dash, callback, Input, Output
import dash_iconify
app = Dash()
component = dmc.Stack([
dmc.Tree(
data=data,
levelOffset=23,
expandOnClick=False,
renderNode={"function": "myLeafCheckbox"},
id="tree-checkboxes" ),
dmc.CodeHighlight(id="checked-nodes", code="", language="json"),
])
app.layout = dmc.MantineProvider(
component
)
@callback(
Output("checked-nodes", "code"),
Input("tree-checkboxes", "checked")
)
def update(checked):
return json.dumps( checked, indent=4)
if __name__ == "__main__":
app.run(debug=True) var dmcfuncs = window.dashMantineFunctions = window.dashMantineFunctions || {};
dmcfuncs.myLeafCheckbox = function (payload) {
const React = window.React;
const dmc = window.dash_mantine_components;
const iconify = window.dash_iconify;
const { node, expanded, hasChildren, elementProps, tree } = payload;
const checked = tree.isNodeChecked(node.value);
const indeterminate = tree.isNodeIndeterminate(node.value);
return React.createElement(
dmc.Group,
{ key: node.value, gap: "xs", ...elementProps },
[
React.createElement(dmc.CheckboxIndicator, {
key: "checkbox",
checked,
indeterminate,
onClick: (e) => {
e.stopPropagation();
if (checked) {
tree.uncheckNode(node.value);
} else {
tree.checkNode(node.value);
}
},
}),
React.createElement(
dmc.Group,
{
key: "label-group",
gap: 5,
onClick: () => tree.toggleExpanded(node.value),
},
[
React.createElement("span", { key: "label" }, node.label),
hasChildren &&
React.createElement(iconify.DashIconify, {
key: "icon",
icon: "tabler:chevron-down",
width: 14,
style: {
transform: expanded ? "rotate(180deg)" : "rotate(0deg)",
transition: "transform 0.2s ease",
},
}),
]
),
]
);
};
|
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.
💃 Nice use of emoji in the test 🍃
This PR enables custom rendering of the labels in the
Tree
componentrenderNode
prop and allows for functions to be passed like{"function": "myLeaf"}
wheremyLeaf
is a JavaScript function defined in theassets
folder.renderNode
is not provided, then the default rendering is used.To do:
Sample app based on the example in the Mantine docs: