fix(striker-ui): trigger file input field with icon button

This commit is contained in:
Tsu-ba-me 2021-11-16 13:10:11 -05:00
parent ed723734c4
commit c2f38a3f00

View File

@ -1,6 +1,7 @@
import { Box, IconButton } from '@material-ui/core'; import { Box, IconButton, Input, InputLabel } from '@material-ui/core';
import AddIcon from '@material-ui/icons/Add'; import AddIcon from '@material-ui/icons/Add';
import { makeStyles } from '@material-ui/styles'; import { makeStyles } from '@material-ui/styles';
import { useRef } from 'react';
import ICON_BUTTON_STYLE from '../../lib/consts/ICON_BUTTON_STYLE'; import ICON_BUTTON_STYLE from '../../lib/consts/ICON_BUTTON_STYLE';
import { Panel } from '../Panels'; import { Panel } from '../Panels';
@ -9,10 +10,19 @@ import { HeaderText } from '../Text';
const useStyles = makeStyles(() => ({ const useStyles = makeStyles(() => ({
addFileButton: ICON_BUTTON_STYLE, addFileButton: ICON_BUTTON_STYLE,
addFileInput: {
display: 'none',
},
})); }));
const Files = (): JSX.Element => { const Files = (): JSX.Element => {
const classes = useStyles(); const classes = useStyles();
const addFileInputRef = useRef<HTMLInputElement>();
// Let the icon button trigger the invisible input element.
const onAddFileButtonClick = () => {
addFileInputRef.current?.click();
};
return ( return (
<Panel> <Panel>
@ -21,9 +31,22 @@ const Files = (): JSX.Element => {
<HeaderText text="Files" /> <HeaderText text="Files" />
</Box> </Box>
<Box> <Box>
<IconButton className={classes.addFileButton}> <form encType="multipart/form-data">
<AddIcon /> <InputLabel htmlFor="add-file-input">
</IconButton> <Input
className={classes.addFileInput}
id="add-file-input"
ref={addFileInputRef}
type="file"
/>
<IconButton
className={classes.addFileButton}
onClick={onAddFileButtonClick}
>
<AddIcon />
</IconButton>
</InputLabel>
</form>
</Box> </Box>
</Box> </Box>
<Spinner /> <Spinner />