mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-23 06:04:32 +01:00
handle pre hydrated text
This commit is contained in:
1 file changed
+14
-5
@@ -25,11 +25,7 @@ function Content({ topResult, onSelect }: SearchBarProps) {
|
|||||||
const search = TankFilters.use((state) => state.search);
|
const search = TankFilters.use((state) => state.search);
|
||||||
|
|
||||||
const input = useRef<HTMLInputElement>(null);
|
const input = useRef<HTMLInputElement>(null);
|
||||||
|
const previousSearch = useRef(search);
|
||||||
useEffect(() => {
|
|
||||||
if (search !== null) return;
|
|
||||||
input.current!.value = "";
|
|
||||||
}, [search]);
|
|
||||||
|
|
||||||
const handleChange = useCallback(() => {
|
const handleChange = useCallback(() => {
|
||||||
if (!input.current) return;
|
if (!input.current) return;
|
||||||
@@ -40,6 +36,19 @@ function Content({ topResult, onSelect }: SearchBarProps) {
|
|||||||
draft.search = sanitized.length === 0 ? null : sanitized;
|
draft.search = sanitized.length === 0 ? null : sanitized;
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Picks up text typed before hydration
|
||||||
|
if (input.current!.value.trim().length > 0) handleChange();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (previousSearch.current !== null && search === null) {
|
||||||
|
input.current!.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
previousSearch.current = search;
|
||||||
|
}, [search]);
|
||||||
const handleKeyDown = useCallback<KeyboardEventHandler>(
|
const handleKeyDown = useCallback<KeyboardEventHandler>(
|
||||||
(event) => {
|
(event) => {
|
||||||
if (event.key !== "Enter" || !topResult || !TankFilters.state.search) {
|
if (event.key !== "Enter" || !topResult || !TankFilters.state.search) {
|
||||||
|
|||||||
Reference in new issue
Block a user