Skip to content

Commit cde5fa2

Browse files
densumeshcdxker
authored andcommitted
feature: add open and onOpenChange as params for search component
1 parent f816e63 commit cde5fa2

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

clients/search-component/src/utils/hooks/modal-context.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export type ModalProps = {
6363
defaultCurrency?: string;
6464
currencyPosition?: currencyPosition;
6565
responsive?: boolean;
66+
open?: boolean;
67+
onOpenChange?: (open: boolean) => void;
6668
debounceMs?: number;
6769
};
6870

@@ -151,17 +153,17 @@ function ModalProvider({
151153
});
152154
const [query, setQuery] = useState("");
153155
const [results, setResults] = useState<ChunkWithHighlights[] | GroupChunk[]>(
154-
[]
156+
[],
155157
);
156158
const [requestID, setRequestID] = useState("");
157159
const [loadingResults, setLoadingResults] = useState(false);
158-
const [open, setOpen] = useState(false);
160+
const [open, setOpen] = useState(props.open ?? false);
159161
const inputRef = useRef<HTMLInputElement>(null);
160162
const [mode, setMode] = useState(props.defaultSearchMode || "search");
161163
const modalRef = useRef<HTMLDivElement>(null);
162164
const [tagCounts, setTagCounts] = useState<CountChunkQueryResponseBody[]>([]);
163165
const [currentTag, setCurrentTag] = useState(
164-
props.tags?.find((t) => t.selected)?.tag || "all"
166+
props.tags?.find((t) => t.selected)?.tag || "all",
165167
);
166168

167169
const trieve = new TrieveSDK({
@@ -177,6 +179,10 @@ function ModalProvider({
177179
}));
178180
}, [onLoadProps]);
179181

182+
useEffect(() => {
183+
props.onOpenChange?.(open);
184+
}, [open]);
185+
180186
const search = async (abortController: AbortController) => {
181187
if (!query) {
182188
setResults([]);
@@ -247,8 +253,8 @@ function ModalProvider({
247253
trieve: trieve,
248254
abortController,
249255
...(tag.tag !== "all" && { tag: tag.tag }),
250-
})
251-
)
256+
}),
257+
),
252258
);
253259
setTagCounts(numberOfRecords);
254260
} catch (e) {
@@ -304,8 +310,7 @@ function ModalProvider({
304310
currentTag,
305311
setCurrentTag,
306312
tagCounts,
307-
}}
308-
>
313+
}}>
309314
{children}
310315
</ModalContext.Provider>
311316
);

0 commit comments

Comments
 (0)