@@ -13,7 +13,8 @@ import {deepMap} from "./utils"
1313
1414interface State {
1515 tree : ImmutableTree ,
16- config : Config
16+ config : Config ,
17+ expanded : boolean
1718}
1819
1920const defaultTree : JsonGroup = {
@@ -71,6 +72,8 @@ const parseJsCodeFromPython = (v: string) => {
7172
7273class ConditionTree extends StreamlitComponentBase < State > {
7374
75+ private debouncedSetStreamlitValue : _ . DebouncedFunc < ( tree : ImmutableTree ) => void > ;
76+
7477 public constructor ( props : ComponentProps ) {
7578 super ( props ) ;
7679
@@ -91,8 +94,11 @@ class ConditionTree extends StreamlitComponentBase<State> {
9194 }
9295 }
9396
94- this . state = { config, tree}
97+ this . state = { config, tree, expanded : false }
9598 this . setStreamlitValue ( tree )
99+
100+ // Create a debounced version of setStreamlitValue
101+ this . debouncedSetStreamlitValue = _ . debounce ( this . setStreamlitValue , 300 ) ;
96102 }
97103
98104 public render = ( ) : ReactNode => {
@@ -126,33 +132,42 @@ class ConditionTree extends StreamlitComponentBase<State> {
126132 </ div >
127133 )
128134 }
135+
136+ componentDidMount = ( ) => {
137+ this . setHeight ( )
138+ }
129139
130140 componentDidUpdate = ( ) => {
131- // Class to apply custom css on rule_groups with a single child
132- document
133- . querySelectorAll ( '.rule_group>.group--children:has(> :nth-child(1):last-child)' )
134- . forEach ( ( x ) => x . classList . add ( 'single-child' ) )
135-
136- this . setHeight ( )
141+ // ... (existing code for CSS class)
142+
143+ // Check if the tree has changed
144+ const currentTree = QbUtils . getTree ( this . state . tree )
145+ const isEmpty = ! currentTree . children1 || ! currentTree . children1 . length
146+
147+ if ( ! isEmpty && ! this . state . expanded ) {
148+ this . setState ( { expanded : true } , this . setHeight )
149+ } else {
150+ this . setHeight ( )
151+ }
137152 }
138153
139- componentDidMount = ( ) => {
140- this . setHeight ( )
154+ componentWillUnmount = ( ) => {
155+ this . debouncedSetStreamlitValue . cancel ( ) ;
141156 }
142157
143158 private setHeight = ( ) => {
144159 // Set frame height
145160 const height = Math . max (
146161 document . body . scrollHeight + 20 ,
147- this . props . args [ 'min_height' ]
162+ this . state . expanded ? this . props . args [ 'min_height' ] : 150
148163 ) ;
149164 Streamlit . setFrameHeight ( height ) ;
150165 }
151166
152167 private onChange = ( immutableTree : ImmutableTree ) => {
153168 this . setState ( { tree : immutableTree } )
154- this . setStreamlitValue ( immutableTree )
155- }
169+ this . debouncedSetStreamlitValue ( immutableTree )
170+ }
156171
157172 private setStreamlitValue = ( tree : ImmutableTree ) => {
158173 const exportFunc = exportFunctions [ this . props . args [ 'return_type' ] ]
0 commit comments