Skip to content

Commit

Permalink
fix debouncing / inputValue interaction (#84)
Browse files Browse the repository at this point in the history
* disable debounce for inputTracking

* fix with debounce;

* .
  • Loading branch information
m-wrzr authored Jan 2, 2025
1 parent 646b8e3 commit c417e66
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions streamlit_searchbox/frontend/src/Searchbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ class Searchbox extends StreamlitComponentBase<State> {
super(props);

// bind the search function and debounce to avoid too many requests
// this should be bound to the streamlit state, since we still want to
// keep proper track of the `inputValue` state
if (props.args.debounce && props.args.debounce > 0) {
this.callbackSearch = debounce(
this.callbackSearch.bind(this),
this.callbackSearchReturn = debounce(
this.callbackSearchReturn.bind(this),
props.args.debounce,
);
}
Expand All @@ -61,6 +63,14 @@ class Searchbox extends StreamlitComponentBase<State> {
);
};

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

private callbackSearchReturn = (input: string): void => {
streamlitReturn("search", input);
};

/**
* new keystroke on searchbox
* @param input
Expand All @@ -73,7 +83,7 @@ class Searchbox extends StreamlitComponentBase<State> {
option: null,
});

streamlitReturn("search", input);
this.callbackSearchReturn(input);
};

/**
Expand Down Expand Up @@ -132,12 +142,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 +177,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 +201,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 c417e66

Please sign in to comment.