Skip to content

Commit e44abe8

Browse files
authored
Merge pull request #12 from Welnic/feature/debounce-and-dynamic-height
Feature/debounce and dynamic height
2 parents ef73c6d + 03d53ac commit e44abe8

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

streamlit_condition_tree/frontend/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

streamlit_condition_tree/frontend/src/ConditionTree.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {deepMap} from "./utils"
1313

1414
interface State {
1515
tree: ImmutableTree,
16-
config: Config
16+
config: Config,
17+
expanded: boolean
1718
}
1819

1920
const defaultTree: JsonGroup = {
@@ -71,6 +72,8 @@ const parseJsCodeFromPython = (v: string) => {
7172

7273
class 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

Comments
 (0)