Skip to content

Commit

Permalink
disable debounce for inputTracking
Browse files Browse the repository at this point in the history
  • Loading branch information
m-wrzr committed Jan 2, 2025
1 parent 646b8e3 commit 8c5f075
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ If the rerun should affect the whole app or just the fragment.
debounce: int = 0
```

Delay executing the callback from the react component by `x` milliseconds to avoid too many / redudant requests, i.e. during fast typing.
Delay executing the callback from the react component by `x` milliseconds to avoid too many / redudant requests, i.e. during fast typing. `NOTE:` this is only applied if `edit_after_submit='disabled'`.

```python
min_execution_time: int = 0
Expand Down
19 changes: 12 additions & 7 deletions streamlit_searchbox/frontend/src/Searchbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ class Searchbox extends StreamlitComponentBase<State> {
super(props);

// bind the search function and debounce to avoid too many requests
if (props.args.debounce && props.args.debounce > 0) {
if (
props.args.debounce &&
props.args.debounce > 0 &&
!this.isInputTrackingActive()
) {
this.callbackSearch = debounce(
this.callbackSearch.bind(this),
props.args.debounce,
Expand All @@ -61,6 +65,10 @@ class Searchbox extends StreamlitComponentBase<State> {
);
};

private isInputTrackingActive = (): boolean => {
return this.props.args.edit_after_submit !== "disabled";
};

/**
* new keystroke on searchbox
* @param input
Expand Down Expand Up @@ -132,12 +140,9 @@ class Searchbox extends StreamlitComponentBase<State> {
* @returns
*/
public render = (): ReactNode => {
const editableAfterSubmit =
this.props.args.edit_after_submit !== "disabled";

// always focus the input field to enable edits
const onFocus = () => {
if (editableAfterSubmit && this.state.inputValue) {
if (this.isInputTrackingActive() && this.state.inputValue) {
this.state.inputValue && this.ref.current.select.inputRef.select();
}
};
Expand Down Expand Up @@ -170,7 +175,7 @@ class Searchbox extends StreamlitComponentBase<State> {
inputValue={
// for edit_after_submit we want to disable the tracking
// since the inputValue is equal to the value
editableAfterSubmit ||
this.isInputTrackingActive() ||
// only use this for the initial default value
this.props.args.default_searchterm === this.state.inputValue
? this.state.inputValue
Expand All @@ -194,7 +199,7 @@ class Searchbox extends StreamlitComponentBase<State> {
this.props.args.style_overrides?.dropdown || {},
),
IndicatorSeparator: () => null,
Input: editableAfterSubmit ? Input : components.Input,
Input: this.isInputTrackingActive() ? Input : components.Input,
Option: (props) =>
style.optionHighlighted(
props,
Expand Down

0 comments on commit 8c5f075

Please sign in to comment.