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

145 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-09-17 14:30:26 +09:00
import React, {useState, useRef, useEffect} from 'react';
import styled from 'styled-components';
import {useTranslation} from 'react-i18next';
2020-02-05 15:59:53 +09:00
import {Link, useHistory} from 'react-router-dom';
2020-02-05 17:28:22 +09:00
import {sanitize} from '../util/text';
2020-03-05 22:09:12 +09:00
import {sendQueryEvent} from '../util/analytics';
2019-09-17 14:30:26 +09:00
import {useDeferredState} from '../util/hooks';
import {mobile} from '../util/css';
import Suggestion from './Suggestion';
2020-02-05 15:59:53 +09:00
const Form: React.FC<{
initialValue?: string;
}> = ({initialValue = ''}) => {
const history = useHistory();
2019-09-17 14:30:26 +09:00
const [query, setQuery] = useDeferredState(800, '');
2020-02-05 15:59:53 +09:00
const [inputValue, setInputValue] = useState(initialValue);
2019-09-17 14:30:26 +09:00
const [suggested, setSuggested] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const {t} = useTranslation();
// set input value
2019-12-24 01:57:07 +09:00
function onInputChange(e: React.FormEvent<HTMLInputElement>): void {
2019-09-17 14:30:26 +09:00
const value = e.currentTarget.value;
setInputValue(value);
}
// clear input form and focus on it
2019-12-24 01:57:07 +09:00
function onLogoClick(): void {
2019-09-17 14:30:26 +09:00
setInputValue('');
2020-02-05 15:59:53 +09:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
inputRef.current!.focus();
}
// invoke when user clicked one of the suggested items
2019-12-24 01:57:07 +09:00
function onSuggestionCompleted(name: string): void {
2019-09-17 14:30:26 +09:00
setInputValue(name);
setSuggested(true);
}
2019-09-17 14:30:26 +09:00
const queryGiven = query && query.length > 0;
useEffect(() => {
2020-02-05 15:59:53 +09:00
function onQuery(query: string) {
if (!query || query === '') {
return;
}
2020-03-05 22:09:12 +09:00
sendQueryEvent(query);
2020-02-05 15:59:53 +09:00
history.push(`/s/${query}`);
}
if (query.length === 0) {
2019-09-17 14:30:26 +09:00
setSuggested(false);
2020-02-05 15:59:53 +09:00
} else {
onQuery(query);
}
2020-02-05 15:59:53 +09:00
}, [query, history]);
useEffect(() => {
2020-02-05 17:28:22 +09:00
const modifiedValue = sanitize(inputValue);
2019-09-17 14:30:26 +09:00
setQuery(modifiedValue);
}, [inputValue, setQuery]);
return (
<InputContainer>
2020-02-06 17:21:22 +09:00
<Logo to="/" onClick={onLogoClick}>
<LogoImage src="/logo.svg" />
2020-02-05 15:59:53 +09:00
</Logo>
<InputView
onChange={onInputChange}
value={inputValue}
ref={inputRef}
placeholder={t('placeholder')}
aria-label="search query"
/>
{queryGiven && !suggested ? (
<Suggestion onSubmit={onSuggestionCompleted} query={query} />
) : null}
</InputContainer>
2019-09-17 14:30:26 +09:00
);
};
2019-09-17 14:30:26 +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;
}
2019-09-17 14:30:26 +09:00
`;
2020-02-06 17:21:22 +09:00
const Logo = styled(Link)`
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 12px;
margin-top: 5px;
cursor: pointer;
${mobile} {
font-size: 15px;
}
2020-02-06 17:21:22 +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
}
2019-09-17 14:30:26 +09:00
`;
const InputView = styled.input.attrs({
type: 'text',
autocomplete: 'off',
autocorrect: 'off',
autocapitalize: 'off',
spellcheck: 'false',
})`
width: 100%;
border: none;
outline: none;
text-align: center;
2020-02-06 03:39:06 +09:00
font-family: 'Montserrat', monospace;
font-weight: 600;
2020-02-06 13:16:30 +09:00
font-size: 6rem;
${mobile} {
font-size: 2rem;
}
2020-02-06 17:21:22 +09:00
::placeholder {
color: #c8cdda;
}
2019-09-17 14:30:26 +09:00
`;