Skip to content

[906] chore: clear search results when focused outside the Search widget #909

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

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/big-owls-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ensembleui/react-runtime": patch
---

Clear results for Search widget when focused outside the widget
19 changes: 5 additions & 14 deletions packages/runtime/src/widgets/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
Expression,
} from "@ensembleui/react-framework";
import { Select as SelectComponent } from "antd";
import { get, isEmpty, isNil, isNull, isObject, isString } from "lodash-es";
import { get, isEmpty, isNil, isObject, isString } from "lodash-es";
import { WidgetRegistry } from "../registry";
import type {
EnsembleWidgetProps,
Expand Down Expand Up @@ -53,7 +53,6 @@ export const Search: React.FC<SearchProps> = ({
...rest
}) => {
const [searchValue, setSearchValue] = useState<string | null>(null);
const [hasCleared, setHasCleared] = useState<boolean>(false);
const [value, setValue] = useState<unknown>();

const { namedData } = useTemplateData({
Expand Down Expand Up @@ -87,7 +86,7 @@ export const Search: React.FC<SearchProps> = ({
);

const renderOptions = useMemo(() => {
if (hasCleared && isNil(searchValue)) return [];
if (isEmpty(searchValue)) return [];

let dropdownOptions: JSX.Element[] = [];

Expand All @@ -112,19 +111,11 @@ export const Search: React.FC<SearchProps> = ({
}

return dropdownOptions;
}, [
itemTemplate,
namedData,
extractValue,
values?.id,
searchValue,
hasCleared,
]);
}, [itemTemplate, namedData, extractValue, values?.id, searchValue]);

useDebounce(
() => {
if (onSearchAction?.callback && !isNull(searchValue)) {
setHasCleared(false);
if (onSearchAction?.callback && !isEmpty(searchValue)) {
onSearchAction.callback({ search: searchValue });
}
},
Expand All @@ -143,6 +134,7 @@ export const Search: React.FC<SearchProps> = ({
(selectedValue: unknown): void => {
if (isObject(itemTemplate) && !isEmpty(namedData)) {
setValue(selectedValue);
setSearchValue(null);
const selectedOption = namedData.find(
(option) => extractValue(option) === selectedValue,
);
Expand All @@ -157,7 +149,6 @@ export const Search: React.FC<SearchProps> = ({

const handleClear = useCallback(() => {
setSearchValue(null);
setHasCleared(true);
onClearAction?.callback();
}, [onClearAction]);

Expand Down
Loading