Skip to content

Commit

Permalink
Merge pull request #205 from mattcorner/onAlert-prop
Browse files Browse the repository at this point in the history
Add new 'onAlert' callback prop
  • Loading branch information
Mattia Panzeri authored Jun 17, 2020
2 parents 323a67d + 38c819f commit 6e0f847
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/components/DropzoneAreaBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ class DropzoneAreaBase extends React.PureComponent {
snackbarVariant: 'success',
};

notifyAlert() {
const {onAlert} = this.props;
const {openSnackBar, snackbarMessage, snackbarVariant} = this.state;
if (openSnackBar && onAlert) {
onAlert(snackbarMessage, snackbarVariant);
}
}

handleDropAccepted = async(acceptedFiles, evt) => {
const {fileObjects, filesLimit, getFileAddedMessage, getFileLimitExceedMessage, onAdd, onDrop} = this.props;

Expand All @@ -95,7 +103,7 @@ class DropzoneAreaBase extends React.PureComponent {
openSnackBar: true,
snackbarMessage: getFileLimitExceedMessage(filesLimit),
snackbarVariant: 'error',
});
}, this.notifyAlert);
return;
}

Expand Down Expand Up @@ -126,7 +134,7 @@ class DropzoneAreaBase extends React.PureComponent {
openSnackBar: true,
snackbarMessage: message,
snackbarVariant: 'success',
});
}, this.notifyAlert);
}

handleDropRejected = (rejectedFiles, evt) => {
Expand Down Expand Up @@ -157,7 +165,7 @@ class DropzoneAreaBase extends React.PureComponent {
openSnackBar: true,
snackbarMessage: message,
snackbarVariant: 'error',
});
}, this.notifyAlert);
}

handleRemove = (fileIndex) => (event) => {
Expand All @@ -177,7 +185,7 @@ class DropzoneAreaBase extends React.PureComponent {
openSnackBar: true,
snackbarMessage: getFileRemovedMessage(removedFileObj.file.name),
snackbarVariant: 'info',
});
}, this.notifyAlert);
};

handleCloseSnackbar = () => {
Expand Down Expand Up @@ -506,6 +514,13 @@ DropzoneAreaBase.propTypes = {
* @param {Event} event The react-dropzone drop event.
*/
onDropRejected: PropTypes.func,
/**
* Fired when an alert is triggered.
*
* @param {string} message Alert message.
* @param {string} variant One of "error", "info", "success".
*/
onAlert: PropTypes.func,
};

export default withStyles(styles, {name: 'MuiDropzoneArea'})(DropzoneAreaBase);
2 changes: 2 additions & 0 deletions src/components/DropzoneAreaBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DropzoneAreaBase } from 'material-ui-dropzone';
<DropzoneAreaBase
onAdd={(fileObjs) => console.log('Added Files:', fileObjs)}
onDelete={(fileObj) => console.log('Removed File:', fileObj)}
onAlert={(message, variant) => console.log(`${variant}: ${message}`)}
/>
```

Expand All @@ -20,6 +21,7 @@ import { DropzoneAreaBase } from 'material-ui-dropzone';
acceptedFiles={['image/*']}
dropzoneText={"Drag and drop an image here or click"}
onChange={(files) => console.log('Files:', files)}
onAlert={(message, variant) => console.log(`${variant}: ${message}`)}
/>
```

Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type DropzoneAreaBaseProps = {
onDelete?: (deletedFileObject: FileObject, index: number) => void;
onDrop?: (files: File[], event: DropEvent) => void;
onDropRejected?: (files: File[], event: DropEvent) => void;
onAlert?: (message: string, variant: AlertType) => void;
getFileLimitExceedMessage?: (filesLimit: number) => string;
getFileAddedMessage?: (fileName: string) => string;
getFileRemovedMessage?: (fileName: string) => string;
Expand Down

0 comments on commit 6e0f847

Please sign in to comment.