Skip to content

Commit

Permalink
feat: close keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardocasares committed Oct 29, 2020
1 parent 00a979c commit c38b985
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/pages/search.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { useState, useRef, useEffect, ChangeEvent } from "react";
import debounce from "debounce";
import {
useState,
useRef,
useEffect,
ChangeEvent,
KeyboardEventHandler,
} from "react";
import { Box } from "@/components/Box";
import { Heading } from "@/components/Typography";
import { SearchInput } from "@/components/SearchInput";
import { StationCardList } from "@/components/StationCardList";
import { useTranslation } from "@/lib/hooks/useTranslation";
import debounce from "debounce";

export const Index = () => {
const { t } = useTranslation();
Expand All @@ -14,8 +20,15 @@ export const Index = () => {
const showList = Q.length >= 3;
const onChange = (e: ChangeEvent<HTMLInputElement>) => setQ(e.target.value);
const debouncedOnChange = debounce(onChange, 350);
const closeKeyboard: KeyboardEventHandler<HTMLInputElement> = (e) => {
if (e.key === "Enter") {
input.current.blur();
}
};

useEffect(() => input.current.focus(), []);
useEffect(() => {
input.current.focus();
}, []);

return (
<Box p={["var(--sz4)"]}>
Expand All @@ -24,6 +37,7 @@ export const Index = () => {
<SearchInput
ref={input}
type="search"
onKeyPress={closeKeyboard}
onChange={debouncedOnChange}
placeholder={t.search.placeholder}
/>
Expand Down

1 comment on commit c38b985

@vercel
Copy link

@vercel vercel bot commented on c38b985 Oct 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.