@@ -13,7 +13,8 @@ import {deepMap} from "./utils"
13
13
14
14
interface State {
15
15
tree : ImmutableTree ,
16
- config : Config
16
+ config : Config ,
17
+ expanded : boolean
17
18
}
18
19
19
20
const defaultTree : JsonGroup = {
@@ -71,6 +72,8 @@ const parseJsCodeFromPython = (v: string) => {
71
72
72
73
class ConditionTree extends StreamlitComponentBase < State > {
73
74
75
+ private debouncedSetStreamlitValue : _ . DebouncedFunc < ( tree : ImmutableTree ) => void > ;
76
+
74
77
public constructor ( props : ComponentProps ) {
75
78
super ( props ) ;
76
79
@@ -91,8 +94,11 @@ class ConditionTree extends StreamlitComponentBase<State> {
91
94
}
92
95
}
93
96
94
- this . state = { config, tree}
97
+ this . state = { config, tree, expanded : false }
95
98
this . setStreamlitValue ( tree )
99
+
100
+ // Create a debounced version of setStreamlitValue
101
+ this . debouncedSetStreamlitValue = _ . debounce ( this . setStreamlitValue , 300 ) ;
96
102
}
97
103
98
104
public render = ( ) : ReactNode => {
@@ -126,33 +132,42 @@ class ConditionTree extends StreamlitComponentBase<State> {
126
132
</ div >
127
133
)
128
134
}
135
+
136
+ componentDidMount = ( ) => {
137
+ this . setHeight ( )
138
+ }
129
139
130
140
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
+ }
137
152
}
138
153
139
- componentDidMount = ( ) => {
140
- this . setHeight ( )
154
+ componentWillUnmount = ( ) => {
155
+ this . debouncedSetStreamlitValue . cancel ( ) ;
141
156
}
142
157
143
158
private setHeight = ( ) => {
144
159
// Set frame height
145
160
const height = Math . max (
146
161
document . body . scrollHeight + 20 ,
147
- this . props . args [ 'min_height' ]
162
+ this . state . expanded ? this . props . args [ 'min_height' ] : 150
148
163
) ;
149
164
Streamlit . setFrameHeight ( height ) ;
150
165
}
151
166
152
167
private onChange = ( immutableTree : ImmutableTree ) => {
153
168
this . setState ( { tree : immutableTree } )
154
- this . setStreamlitValue ( immutableTree )
155
- }
169
+ this . debouncedSetStreamlitValue ( immutableTree )
170
+ }
156
171
157
172
private setStreamlitValue = ( tree : ImmutableTree ) => {
158
173
const exportFunc = exportFunctions [ this . props . args [ 'return_type' ] ]
0 commit comments