Skip to content

Commit

Permalink
wip - Dropzone Component #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Apr 1, 2019
1 parent 4c853db commit 374078f
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 2 deletions.
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"prop-types": "^15.6.2",
"react": "^16.8.1",
"react-dom": "^16.8.1",
"react-dropzone": "^10.1.0",
"react-loading-skeleton": "^1.1.1",
"react-router-dom": "^4.3.1",
"yup": "^0.26.10"
Expand Down
153 changes: 153 additions & 0 deletions resources/js/ui/Dropzone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useDropzone } from 'react-dropzone';

import { Grid, RootRef, Typography, withStyles } from '@material-ui/core';
import classNames from 'classnames';

function Dropzone(props) {
const { classes } = props;

const [files, setFiles] = useState([]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
accept: 'image/*',
maxSize: 1 * 1000 * 1000,
onDrop: acceptedFiles => {
setFiles(
files.concat(
acceptedFiles.map(file =>
Object.assign(file, {
url: URL.createObjectURL(file),
}),
),
),
);
},
});

const { ref, ...rootProps } = getRootProps();

useEffect(
() => () => {
// Make sure to revoke the data uris to avoid memory leaks.
files.forEach(file => URL.revokeObjectURL(file.preview));
},
[files],
);

return (
<RootRef rootRef={ref}>
<Grid
{...rootProps}
container
spacing={8}
justify="center"
alignItems="center"
className={classes.root}
>
<input {...getInputProps()} />

{files.length > 0 ? (
files
.map((file, key) => (
<Grid item key={key}>
<img
src={file.url}
className={classNames(
classes.file,
classes.image,
)}
/>

<Typography
color="primary"
className={classes.removeLink}
>
Remove File
</Typography>
</Grid>
))
.concat([
<Grid item key="addFile">
<div
className={classNames(
classes.file,
classes.addFile,
)}
>
<Typography
className={classNames(
classes.text,
classes.textIcon,
)}
>
+
</Typography>
</div>

<Typography>&nbsp;</Typography>
</Grid>,
])
) : (
<Grid item>
<Typography className={classes.text}>
{isDragActive
? `Drag files here`
: `Drag 'n' drop some files here, or click to select files`}
</Typography>
</Grid>
)}
</Grid>
</RootRef>
);
}

Dropzone.propTypes = {
onDrop: PropTypes.func,
};

const styles = theme => ({
root: {
border: `2px dashed ${theme.palette.grey[500]}`,
backgroundColor: theme.palette.grey[200],
minHeight: 250,
},

text: {
color: theme.palette.grey[500],
},

textIcon: {
fontSize: 75,
fontWeight: 'bold',
textAlign: 'center',
},

removeLink: {
textAlign: 'center',
'&:hover': {
cursor: 'pointer',
},
},

file: {
border: `3px solid ${theme.palette.grey[500]}`,
borderRadius: '5%',
width: 120,
height: 120,
},

image: {
'&:hover': {
filter: 'blur(4px)',
},
},

addFile: {
'&:hover': {
cursor: 'pointer',
},
},
});

export default withStyles(styles)(Dropzone);
1 change: 1 addition & 0 deletions resources/js/ui/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import loadable from '@loadable/component';

export const Dropzone = loadable(() => import('./Dropzone'));
export const Modal = loadable(() => import('./Modal'));
export const Skeleton = loadable(() => import('./Skeleton'));
export const Snackbar = loadable(() => import('./Snackbar'));
Expand Down
2 changes: 1 addition & 1 deletion resources/js/views/__backoffice/users/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Profile, Account, Avatar } from './Forms';
class Create extends Component {
state = {
loading: false,
activeStep: 0,
activeStep: 2,
formValues: [],
errors: {},
message: {},
Expand Down
4 changes: 3 additions & 1 deletion resources/js/views/__backoffice/users/Forms/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PropTypes from 'prop-types';

import { Button, Grid, Typography, withStyles } from '@material-ui/core';

import { Dropzone } from '../../../../ui';

const Avatar = props => {
const { classes, values, errors, handleSubmit, handleSkip } = props;

Expand All @@ -12,7 +14,7 @@ const Avatar = props => {
Avatar Upload
</Typography>

<div className={classes.root} />
<Dropzone />

<div className={classes.sectionSpacer} />

Expand Down

0 comments on commit 374078f

Please sign in to comment.