Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom messages as props #15

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/DropNCrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class DropNCrop extends Component {
canvasWidth: PropTypes.string,
className: PropTypes.string,
cropperOptions: PropTypes.object,
customMessage: PropTypes.shape({
instructions: PropTypes.string,
acceptedFileTypes: PropTypes.string,
maxFileSize: PropTypes.string,
fileTypeErrorMessage: PropTypes.string,
fileSizeErrorMessage: PropTypes.string,
}),
instructions: PropTypes.node,
maxFileSize: PropTypes.number,
onChange: PropTypes.func,
Expand All @@ -37,6 +44,11 @@ class DropNCrop extends Component {
viewMode: 0,
autoCropArea: 1,
},
customMessage: {
instructions: 'Drag-n-drop a file or click to add an image',
acceptedFileTypes: 'Accepted file types: ',
maxFileSize: 'Max file size: ',
},
maxFileSize: 3145728,
};

Expand All @@ -59,9 +71,13 @@ class DropNCrop extends Component {
onChange,
maxFileSize,
allowedFileTypes,
customMessage,
} = this.props;
const fileSizeValidation = fileSizeLessThan(maxFileSize)(files);
const fileTypeValidation = fileType(allowedFileTypes)(files);
const fileSizeErrorMessage = customMessage.fileSizeErrorMessage
? customMessage.fileSizeErrorMessage + bytesToSize(maxFileSize)
: null;
const fileSizeValidation = fileSizeLessThan(maxFileSize, fileSizeErrorMessage)(files);
const fileTypeValidation = fileType(allowedFileTypes, customMessage.fileTypeErrorMessage)(files);

if (fileSizeValidation.isValid && fileTypeValidation.isValid) {
const reader = new FileReader();
Expand Down Expand Up @@ -90,6 +106,7 @@ class DropNCrop extends Component {
canvasWidth,
className,
cropperOptions,
customMessage,
instructions,
allowedFileTypes,
maxFileSize,
Expand Down Expand Up @@ -129,17 +146,17 @@ class DropNCrop extends Component {
{!instructions
? <div className="dropzone-instructions">
<div className="dropzone-instructions--main">
Drag-n-drop a file or click to add an image
{customMessage.instructions}
</div>
<div className="dropzone-instructions--sub">
Accepted file types:
{customMessage.acceptedFileTypes}
{' '}
{allowedFileTypes
.map(mimeType => `.${mimeType.split('/')[1]}`)
.join(', ')}
</div>
<div className="dropzone-instructions--sub">
Max file size: {bytesToSize(maxFileSize)}
{customMessage.maxFileSize} {bytesToSize(maxFileSize)}
</div>
</div>
: instructions}
Expand Down