I have a dmc.Accordion component in my UI, grouped together with a checkbox. I want to enable/disable the accordion if the checkbox is checked or not. The problem is, dmc.accordion doesn’t have a disable property unlike other dcc components which have the option to enable / disable them (i.e dcc checklist).
One workaround is to set css property ‘pointer-events’ to ‘none’ (as show below) , but is there a better way to do this ?
For dmc-accordion , the following callback updates the styles property of component, by setting pointer-events to none for control element of dmc.accordion.
Minimal Working Example:
from dash import Dash, dcc, html, Input, Output, State, callback
from dash.exceptions import PreventUpdate
import dash_mantine_components as dmc
app = Dash(__name__)
app.layout = html.Div(
children=[
dmc.Group(spacing='xs',position='left',grow=False,style={'gap':'1px'},
children=[
dmc.Checkbox(id='checkbox',size='xs',style={'marginLeft':'5px',},checked=False),
dmc.Accordion(id='dmc-accordion',
children=[
dmc.AccordionItem(
[
dmc.AccordionControl("Customization",style={'color':'white'}),
dmc.AccordionPanel(
dcc.Checklist(inputStyle={'marginRight':'7px'},
options=[
{'label': 'New York City', 'value': 'New York City'},
{'label': 'Montreal', 'value': 'Montreal'},
{'label': 'San Francisco', 'value': 'San Francisco',"disabled":True},
],
value=['Montreal']
),
),
],
value="customization",
),],
chevronPosition='right',variant='filled',
styles={ 'control':{'opacity':'1'},
'item':{'backgroundColor':'transparent',
"&[data-active]":{'backgroundColor':'transparent'}}}
)
],
)
],style={'backgroundColor': 'green'}
)
@app.callback(
Output('dmc-accordion','styles'),
Input('checkbox', 'checked'),
State('dmc-accordion', 'styles'),
prevent_initial_call=True
)
def menu_enable(checked,styles):
print(f'checkbox: {checked} , styles {styles}')
if(checked):
styles['control'] = {'pointer-events':'none','opacity':'0.5'}
else:
styles['control']['pointer-events'] = 'auto'
styles['control']['opacity'] = '1'
return styles
if __name__ == "__main__":
app.run_server(debug=True)
Running the app:
As you can see , this doesn’t disable the accordion entirely as I can still select/deselect options from inside.
Is there a better way to do this? Im not familiar with javascript but can we manipulate DOM of dmc accordion to enable/disable it ? Any help would be appreciated , thanks!