-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use common accession search input field
- Loading branch information
1 parent
063f6fe
commit cb4c52e
Showing
4 changed files
with
139 additions
and
86 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
web-frontend/src/elements/common/AccessionSearchInputField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Button, Input } from 'antd'; | ||
import { Content, Header } from 'antd/es/layout/layout'; | ||
import { | ||
ChangeEvent, | ||
CSSProperties, | ||
KeyboardEvent, | ||
useCallback, | ||
useState, | ||
} from 'react'; | ||
import { createSearchParams, useNavigate } from 'react-router-dom'; | ||
import routes from '../../constants/routes'; | ||
|
||
type InputProps = { | ||
width: CSSProperties['width']; | ||
height: CSSProperties['height']; | ||
}; | ||
|
||
function AccessionSearchInputField({ width, height }: InputProps) { | ||
const [accession, setAccession] = useState<string>(''); | ||
const navigate = useNavigate(); | ||
|
||
const handleOnChange = useCallback((e: ChangeEvent<HTMLInputElement>) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
setAccession(e.target.value.trim()); | ||
}, []); | ||
|
||
const handleOnClick = useCallback( | ||
() => | ||
navigate({ | ||
pathname: routes.accession.path, | ||
search: `?${createSearchParams({ id: accession })}`, | ||
}), | ||
[accession, navigate], | ||
); | ||
|
||
const handleOnKeyDown = useCallback( | ||
(e: KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
handleOnClick(); | ||
} | ||
}, | ||
[handleOnClick], | ||
); | ||
|
||
return ( | ||
<Content style={{ width, height }}> | ||
<Header | ||
style={{ | ||
width, | ||
height, | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
textAlign: 'center', | ||
backgroundColor: 'beige', | ||
}} | ||
> | ||
<Input | ||
type="text" | ||
placeholder="e.g. MSBNK-AAFC-AC000114" | ||
value={accession && accession !== '' ? accession : undefined} | ||
addonBefore="Go to accession:" | ||
onChange={handleOnChange} | ||
onKeyDown={handleOnKeyDown} | ||
allowClear | ||
style={{ width: 500 }} | ||
/> | ||
<Button | ||
children="Search" | ||
onClick={handleOnClick} | ||
disabled={accession.trim() === ''} | ||
style={{ width: 100, marginLeft: 20 }} | ||
/> | ||
</Header> | ||
</Content> | ||
); | ||
} | ||
|
||
export default AccessionSearchInputField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters