1
0
mirror of https://github.com/uetchy/namae.git synced 2025-07-02 06:20:02 +09:00
namae/src/components/Form.tsx

146 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-08-31 08:41:53 +09:00
import React, { useState, useRef, useEffect } from 'react';
import styled from 'styled-components';
import { useTranslation } from 'react-i18next';
import { Link, useHistory } from 'react-router-dom';
import { sanitize } from '../util/text';
import { sendQueryEvent } from '../util/analytics';
import { mobile } from '../util/css';
import Suggestion from './Suggestion';
import { useDeferredState } from '../util/hooks';
2020-02-05 15:59:53 +09:00
const Form: React.FC<{
2020-08-31 08:41:53 +09:00
initialValue?: string;
}> = ({ initialValue = '' }) => {
2020-08-31 08:41:53 +09:00
const history = useHistory();
const [inputValue, setInputValue] = useState(initialValue);
const [suggestionQuery, setSuggestionQuery] = useDeferredState(800, '');
const [suggested, setSuggested] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const { t } = useTranslation();
2020-03-06 00:05:54 +09:00
function search(query: string) {
2020-08-31 08:41:53 +09:00
sendQueryEvent(sanitize(query));
history.push(`/s/${query}`);
2020-03-06 00:05:54 +09:00
}
// set input value
2019-12-24 01:57:07 +09:00
function onInputChange(e: React.FormEvent<HTMLInputElement>): void {
2020-08-31 08:41:53 +09:00
setInputValue(e.currentTarget.value);
}
// invoke when user clicked one of the suggested items
2019-12-24 01:57:07 +09:00
function onSuggestionCompleted(name: string): void {
2020-08-31 08:41:53 +09:00
setInputValue(name);
search(name);
setSuggested(true);
}
2020-03-06 00:05:54 +09:00
function onSubmitQuery(e: React.FormEvent) {
2020-08-31 08:41:53 +09:00
e.preventDefault();
inputRef.current!.blur();
2020-03-06 00:05:54 +09:00
if (!inputValue || inputValue === '') {
2020-08-31 08:41:53 +09:00
return;
}
2020-08-31 08:41:53 +09:00
search(inputValue);
2020-03-06 00:05:54 +09:00
}
useEffect(() => {
2020-08-31 08:41:53 +09:00
const modifiedValue = sanitize(inputValue);
setSuggestionQuery(modifiedValue);
}, [inputValue, setSuggestionQuery]);
2020-03-06 00:05:54 +09:00
2020-08-31 08:41:53 +09:00
const queryGiven = suggestionQuery && suggestionQuery !== '';
return (
<InputContainer>
<InputHeader>
<Logo to="/">
<LogoImage src="/logo.svg" />
</Logo>
</InputHeader>
2020-03-26 16:47:56 +09:00
<form onSubmit={onSubmitQuery} action="/s" role="search">
2020-03-06 00:05:54 +09:00
<InputView
onChange={onInputChange}
value={inputValue}
ref={inputRef}
placeholder={t('placeholder')}
2020-03-26 16:47:56 +09:00
aria-label="Search"
2020-03-06 00:05:54 +09:00
/>
</form>
{queryGiven && !suggested ? (
2020-03-06 00:05:54 +09:00
<Suggestion onSubmit={onSuggestionCompleted} query={suggestionQuery} />
) : null}
</InputContainer>
2020-08-31 08:41:53 +09:00
);
};
2020-08-31 08:41:53 +09:00
export default Form;
const InputContainer = styled.div`
2020-02-06 17:21:22 +09:00
display: flex;
flex-direction: column;
align-items: center;
2020-02-06 13:16:30 +09:00
padding: 30px;
transform: translateY(40px);
2020-02-06 13:16:30 +09:00
border-radius: 50px;
box-shadow: 0 10px 50px 0 #858efb;
background: #ffffff;
${mobile} {
2020-02-06 13:16:30 +09:00
padding: 20px;
transform: translateY(20px);
2020-02-06 13:16:30 +09:00
border-radius: 30px;
}
2020-08-31 08:41:53 +09:00
`;
const InputHeader = styled.div`
2020-02-06 17:21:22 +09:00
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin: 12px 0 5px 0;
2020-08-31 08:41:53 +09:00
`;
const Logo = styled(Link)`
cursor: pointer;
${mobile} {
font-size: 15px;
}
2020-08-31 08:41:53 +09:00
`;
2020-02-05 15:59:53 +09:00
2020-02-06 17:21:22 +09:00
const LogoImage = styled.img`
width: 140px;
2020-02-06 03:39:06 +09:00
2020-02-06 17:21:22 +09:00
${mobile} {
width: 90px;
2020-02-06 03:39:06 +09:00
}
2020-08-31 08:41:53 +09:00
`;
const InputView = styled.input.attrs({
2020-03-07 13:16:01 +09:00
type: 'search',
2020-04-14 16:12:38 +09:00
enterkeyhint: 'search',
autocomplete: 'off',
autocorrect: 'off',
autocapitalize: 'off',
spellcheck: 'false',
autofocus: '',
})`
width: 100%;
border: none;
outline: none;
text-align: center;
2020-06-20 18:19:05 +09:00
font-family: 'Montserrat', sans-serif;
2020-02-06 03:39:06 +09:00
font-weight: 600;
2020-02-06 13:16:30 +09:00
font-size: 6rem;
2020-03-07 13:16:01 +09:00
appearance: none;
${mobile} {
font-size: 2rem;
}
2020-02-06 17:21:22 +09:00
::placeholder {
color: #c8cdda;
}
2020-08-31 08:41:53 +09:00
`;