Hello there!
Right now I’m trying to combine both, to have the editor pop out and behave like the TagsInput from dmc (having proposals etc) and have tags being shown in the column.
But just as you wrote it, it makes more sense to break it down easier. Today I’ll try to make it work as cell editor and just render it as a concatenated string. So I’ll be trying to use a value formatter instead of a cell renderer today.
Here is the JS i’m having so far. It doesnt work yet, but I hope I get one step closer today.
var dagcomponentfuncs = (window.dashAgGridComponentFunctions =
window.dashAgGridComponentFunctions || {});
var dagfuncs = (window.dashAgGridFunctions = window.dashAgGridFunctions || {});
dagfuncs.TagsInputEditor = class {
// gets called once before the renderer is used
init(params) {
// create the cell
this.params = params;
this.eInput = document.createElement('input');
this.eInput.value = params.value;
this.eInput.style.height = 'var(--ag-row-height)';
this.eInput.style.width = '100%';
this.eInput.classList.add('mantine-TagsInput-input');
this.eInput.placeholder = params.placeholder || "";
this.eInput.name = params.name;
if (window.dash_mantine_components.TagsInput === undefined) {
console.log('Dash Mantine Components TagsInput not found');
return;
} else {
console.log('Dash Mantine Components TagsInput found');
}
let tagsInputElement = React.createElement(window.dash_mantine_components.TagsInput, {
placeholder: "Enter tags...",
clearable: true,
searchable: true,
creatable: true,
});
if (tagsInputElement === undefined) {
console.log('React.createElement failed');
} else {
console.log('React.createElement success');
}
console.log(tagsInputElement);
}
// gets called once when grid ready to insert the element
getGui() {
return this.eInput;
}
// focus and select can be done after the gui is attached
afterGuiAttached() {
this.eInput.focus();
this.eInput.select();
}
// returns the new value after editing
getValue() {
return this.value;
}
// any cleanup we need to be done here
destroy() {
// sets focus back to the grid's previously active cell
}
isPopup() {
return false;
}
};
dagcomponentfuncs.Tags = function (props) {
if (props.value == 'Placeholder') {
backgroundColor = '#d8f0d3';
} else if (props.value == 'Tags') {
backgroundColor = '#f5cccc';
} else {
backgroundColor = '#fffec8';
}
return React.createElement(
'div',
{
style: {
width: '100%',
height: '100%',
padding: '5px',
display: 'flex',
justifyContent: 'left',
alignItems: 'center',
},
},
React.createElement(
'div',
{
style: {
backgroundColor: backgroundColor,
borderRadius: '15px',
padding: '5px',
color: 'black',
},
},
props.value
)
);
};
So the component exists, I just need to return it somehow for my .py dash app to recognize it.
This is the dummy .py file:
import dash._dash_renderer
import dash_ag_grid as dag
from dash import Dash, html, Input, Output
import pandas as pd
import dash_mantine_components as dmc
import dash
dash._dash_renderer._set_react_version('18.2.0')
app = Dash(__name__)
df = pd.DataFrame(
{
"customTags": [str(i) for i in range(4)],
"tryOut": ["Placeholder" for i in range(4)],
}
)
columnDefs = [
{"field": "customTags",},
{
"field": "tryOut",
"cellEditor": {"function": "TagsInputEditor"},
"cellEditorParams" : {"placeholder": "Enter some tags"},
"cellEditorPopup": True,
"cellRenderer": "Tags",
}
]
defaultColDef = {
"resizable": True,
"sortable": True,
"minWidth": 100,
"editable": True,
}
app.layout = dmc.MantineProvider(children=[html.Div(
[
dag.AgGrid(
id="cell-editor-dmc-grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="responsiveSizeToFit",
defaultColDef=defaultColDef,
),
html.Div(id="cell-editor-dmc-output"),
html.Div(
)
],
style={"margin": 20},
),
dmc.TagsInput(
label="Select frameworks",
placeholder="Select all you like!",
id="framework-tags-input",
value=["ng", "vue"],
w=400,
mb=10,
),
dmc.Text(id="tags-input-value"),
])
@app.callback(
Output("cell-editor-dmc-output", "children"),
Input("cell-editor-dmc-grid", "cellValueChanged"),
)
def selected(changed):
return f"You have selected {changed}"
if __name__ == "__main__":
app.run_server(debug=True)