Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onSearch to ComboBox #1010

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/inputs/internal/ComboBoxBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ListBox } from "src/inputs/internal/ListBox";
import { keyToValue, Value, valueToKey } from "src/inputs/Value";
import { BeamFocusableProps } from "src/interfaces";
import { getFieldWidth } from "src/inputs/utils";
import { useDebounce } from "use-debounce";

/** Base props for either `SelectField` or `MultiSelectField`. */
export interface ComboBoxBaseProps<O, V extends Value> extends BeamFocusableProps, PresentationFieldProps {
Expand Down Expand Up @@ -61,6 +62,8 @@ export interface ComboBoxBaseProps<O, V extends Value> extends BeamFocusableProp
hideErrorMessage?: boolean;
/* Allows input to wrap to multiple lines */
multiline?: boolean;
/* Callback for user searches */
onSearch?: (search: string) => void;
}

/**
Expand Down Expand Up @@ -90,6 +93,7 @@ export function ComboBoxBase<O, V extends Value>(props: ComboBoxBaseProps<O, V>)
getOptionValue: propOptionValue,
getOptionMenuLabel: propOptionMenuLabel,
fullWidth = fieldProps?.fullWidth ?? false,
onSearch,
...otherProps
} = props;
const labelStyle = otherProps.labelStyle ?? fieldProps?.labelStyle ?? "above";
Expand Down Expand Up @@ -269,8 +273,11 @@ export function ComboBoxBase<O, V extends Value>(props: ComboBoxBaseProps<O, V>)
onSelectionChange,
});

const [debouncedSearch] = useDebounce(searchValue, 300);

// Reset inputValue when closed or selected changes
useEffect(() => {
if (debouncedSearch) return;
if (state.isOpen && multiselect) {
// While the multiselect is open, let the user keep typing
setFieldState((prevState) => ({
Expand All @@ -285,7 +292,12 @@ export function ComboBoxBase<O, V extends Value>(props: ComboBoxBaseProps<O, V>)
inputValue: getInputValue(selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly),
}));
}
}, [state.isOpen, selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly]);
}, [state.isOpen, selectedOptions, getOptionLabel, multiselect, nothingSelectedText, isReadOnly, debouncedSearch]);

// Call on search callback when the user types in the input field
useEffect(() => {
onSearch?.(debouncedSearch ?? "");
}, [onSearch, debouncedSearch]);

// For the most part, the returned props contain `aria-*` and `id` attributes for accessibility purposes.
const {
Expand Down
Loading