2021-10-31 12:45:34 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-10-31 12:45:34 +01:00
|
|
|
import SearchIcon from '@mui/icons-material/Search';
|
|
|
|
|
import { IconButton, Input } from '@mui/material';
|
|
|
|
|
import CancelIcon from '@mui/icons-material/Cancel';
|
2021-12-19 18:06:27 +03:30
|
|
|
import { useQueryParam, StringParam } from 'use-query-params';
|
2021-10-31 12:45:34 +01:00
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
interface IProps {
|
|
|
|
|
autoOpen?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
|
autoOpen: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AppbarSearch: React.FunctionComponent<IProps> = (props) => {
|
|
|
|
|
const { autoOpen } = props;
|
2021-12-19 18:06:27 +03:30
|
|
|
const [query, setQuery] = useQueryParam('query', StringParam);
|
2021-10-31 12:45:34 +01:00
|
|
|
const [searchOpen, setSearchOpen] = useState(!!query);
|
|
|
|
|
const inputRef = React.useRef<HTMLInputElement>();
|
|
|
|
|
|
|
|
|
|
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
|
|
|
|
setQuery(e.target.value === '' ? undefined : e.target.value);
|
|
|
|
|
}
|
|
|
|
|
const cancelSearch = () => {
|
|
|
|
|
setQuery(null);
|
|
|
|
|
setSearchOpen(false);
|
|
|
|
|
};
|
|
|
|
|
const handleBlur = () => { if (!query) setSearchOpen(false); };
|
|
|
|
|
const openSearch = () => {
|
|
|
|
|
setSearchOpen(true);
|
|
|
|
|
// Put Focus Action at the end of the Callstack so Input actually exists on the dom
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (inputRef && inputRef.current) inputRef.current.focus();
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-12-19 18:06:27 +03:30
|
|
|
|
|
|
|
|
const handleSearchShortcut = (e: KeyboardEvent) => {
|
|
|
|
|
if ((e.code === 'F3') || (e.ctrlKey && e.code === 'KeyF')) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
openSearch();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (autoOpen) {
|
|
|
|
|
openSearch();
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('keydown', handleSearchShortcut);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('keydown', handleSearchShortcut);
|
|
|
|
|
};
|
|
|
|
|
}, [handleSearchShortcut]);
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{searchOpen
|
|
|
|
|
? (
|
|
|
|
|
<Input
|
|
|
|
|
value={query || ''}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
onBlur={handleBlur}
|
|
|
|
|
inputRef={inputRef}
|
|
|
|
|
endAdornment={(
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={cancelSearch}
|
|
|
|
|
>
|
|
|
|
|
<CancelIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2021-12-19 18:06:27 +03:30
|
|
|
) : (
|
|
|
|
|
<IconButton onClick={openSearch}>
|
|
|
|
|
<SearchIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2021-10-31 12:45:34 +01:00
|
|
|
</>
|
|
|
|
|
);
|
2022-03-12 04:46:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AppbarSearch.defaultProps = defaultProps;
|
|
|
|
|
|
|
|
|
|
export default AppbarSearch;
|