Skip to content

Commit edee45d

Browse files
committed
Ensure compatibility between legacy themes and modern themes
1 parent 8c67d20 commit edee45d

10 files changed

Lines changed: 171 additions & 0 deletions

File tree

src/lib/fragments/AgGrid.react.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ import RowMenuRenderer from '../renderers/rowMenuRenderer';
4646
import {customFunctions} from '../renderers/customFunctions';
4747

4848
import {AgGridReact, useGridFilter} from 'ag-grid-react';
49+
import {
50+
themeAlpine,
51+
themeBalham,
52+
themeMaterial,
53+
themeQuartz,
54+
} from 'ag-grid-community';
55+
const themes = {themeAlpine, themeBalham, themeMaterial, themeQuartz};
4956

5057
// d3 imports
5158
import * as d3Format from 'd3-format';
@@ -153,6 +160,7 @@ export function DashAgGrid(props) {
153160
const parsedCondition =
154161
esprima.parse(funcString).body[0].expression;
155162
const context = {
163+
...themes,
156164
d3,
157165
dash_clientside,
158166
...customFunctions,
@@ -170,6 +178,7 @@ export function DashAgGrid(props) {
170178
const parsedCondition =
171179
esprima.parse(funcString).body[0].expression;
172180
const context = {
181+
...themes,
173182
d3,
174183
dash_clientside,
175184
...customFunctions,
@@ -189,6 +198,7 @@ export function DashAgGrid(props) {
189198
const parsedCondition =
190199
esprima.parse(funcString).body[0].expression;
191200
const context = {
201+
...themes,
192202
d3,
193203
...customFunctions,
194204
...window.dashAgGridFunctions,
@@ -1519,6 +1529,14 @@ export function DashAgGrid(props) {
15191529
omit(NO_CONVERT_PROPS, {...dashGridOptions, ...restProps})
15201530
);
15211531

1532+
if ('theme' in convertedProps) {
1533+
if (typeof convertedProps.theme === 'function') {
1534+
convertedProps.theme = convertedProps.theme();
1535+
} else if (convertedProps.theme in themes) {
1536+
convertedProps.theme = themes[convertedProps.theme];
1537+
}
1538+
}
1539+
15221540
let alignedGrids;
15231541
if (dashGridOptions) {
15241542
if ('alignedGrids' in dashGridOptions) {

src/lib/utils/propCategories.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export const GRID_MAYBE_FUNCTIONS_NO_PARAMS = {
141141
setPopupParent: 1,
142142
popupParent: 1,
143143
quickFilterMatcher: 1,
144+
145+
// Themeing
146+
theme: 1,
144147
};
145148

146149
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a custom assets folder for `themes_legacy.py`. It is used to demonstrate
2+
how you can supply a legacy theme via a custom CSS file, along with the "legacy: True" option
3+
for dashGridOptions.

tests/examples/assets_legacy_themes/ag-grid.css

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/examples/assets_legacy_themes/ag-theme-alpine.min.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/examples/assets_legacy_themes/ag-theme-balham.min.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/examples/assets_legacy_themes/ag-theme-material.min.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/examples/assets_legacy_themes/ag-theme-quartz.min.css

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/examples/themes.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import dash_ag_grid as dag
2+
from dash import Dash, Input, Output, State, dcc, html
3+
import json
4+
5+
app = Dash(__name__)
6+
7+
rowData = [
8+
{"weight": 0.074657, "date": "01/01/2024"},
9+
{"weight": 0.06948567, "date": "02/01/2024"},
10+
{"weight": 0.02730574, "date": "03/01/2024"},
11+
{"weight": 0.0182345, "date": "04/01/2024"},
12+
]
13+
14+
columnDefs = [
15+
{"field": "weight", "cellDataType": "percentage"},
16+
{"field": "date", "cellDataType": "dateString"},
17+
]
18+
19+
dataTypeDefinitions = {
20+
"percentage": {
21+
"baseDataType": "number",
22+
"extendsDataType": "number",
23+
"valueFormatter": {
24+
"function": "params.value == null ? '' : d3.format(',.1%')(params.value)"
25+
},
26+
},
27+
"dateString": {
28+
"baseDataType": 'dateString',
29+
"extendsDataType": 'dateString',
30+
"valueParser": {
31+
"function": r"params.newValue != null && !!params.newValue.match(/\d{2}\/\d{2}\/\d{4}/) ? params.newValue : null"
32+
},
33+
"valueFormatter": {"function": "params.value == null ? '' : params.value"},
34+
"dataTypeMatcher": {"function": r"params != null && !!params.match(/\d{2}\/\d{2}\/\d{4}/)"},
35+
"dateParser": {"function": "dateParser(params)"},
36+
"dateFormatter": {"function": "dateFormatter(params)"},
37+
},
38+
}
39+
40+
app.layout = html.Div(
41+
[
42+
dcc.Dropdown(options=[
43+
{"label": "Quartz", "value": "themeQuartz"},
44+
{"label": "Balham", "value": "themeBalham"},
45+
{"label": "Material", "value": "themeMaterial"},
46+
{"label": "Alpine", "value": "themeAlpine"},
47+
{"label": "Custom Balham with red accent", "value": '{ "function": "themeBalham.withParams({ accentColor: \'red\' })"}'},
48+
{"label": "Invalid theme name", "value": "invalidTheme"},
49+
], value="themeQuartz", id="themeSelector"),
50+
dag.AgGrid(
51+
id="grid",
52+
columnDefs=columnDefs,
53+
rowData=rowData,
54+
defaultColDef={"filter": True, "editable": True},
55+
dashGridOptions={"dataTypeDefinitions": dataTypeDefinitions},
56+
),
57+
],
58+
)
59+
60+
@app.callback(
61+
Output("grid", "dashGridOptions"),
62+
Input("themeSelector", "value"),
63+
State("grid", "dashGridOptions"),
64+
)
65+
def addGrid(selected_theme, dash_grid_options):
66+
try:
67+
selected_theme = json.loads(selected_theme)
68+
except json.JSONDecodeError:
69+
...
70+
71+
if not dash_grid_options:
72+
dash_grid_options = {}
73+
74+
dash_grid_options["theme"] = selected_theme
75+
return dash_grid_options
76+
77+
if __name__ == "__main__":
78+
app.run(debug=True)

tests/examples/themes_legacy.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import dash_ag_grid as dag
2+
from dash import Dash, Input, Output, State, dcc, html
3+
4+
"""
5+
Note the custom assets_folder in this app!
6+
Change the `className` theme to one of the options in that folder to see the
7+
legacy theme in action.
8+
"""
9+
10+
app = Dash(__name__, assets_folder="assets_legacy_themes/")
11+
12+
rowData = [
13+
{"weight": 0.074657, "date": "01/01/2024"},
14+
{"weight": 0.06948567, "date": "02/01/2024"},
15+
{"weight": 0.02730574, "date": "03/01/2024"},
16+
{"weight": 0.0182345, "date": "04/01/2024"},
17+
]
18+
19+
columnDefs = [
20+
{"field": "weight", "cellDataType": "percentage"},
21+
{"field": "date", "cellDataType": "dateString"},
22+
]
23+
24+
app.layout = html.Div(
25+
[
26+
dag.AgGrid(
27+
id="grid",
28+
columnDefs=columnDefs,
29+
rowData=rowData,
30+
defaultColDef={"filter": True, "editable": True},
31+
dashGridOptions={"theme": "legacy"},
32+
className="ag-theme-material",
33+
),
34+
],
35+
)
36+
37+
if __name__ == "__main__":
38+
app.run(debug=True)

0 commit comments

Comments
 (0)