Skip to content

Commit

Permalink
allow passing options to the ContentsUploadModal, in order to use it …
Browse files Browse the repository at this point in the history
…elsewhere
  • Loading branch information
erral committed Mar 14, 2024
1 parent 0deb876 commit 0395a92
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class ContentsUploadModal extends Component {
open: PropTypes.bool.isRequired,
onOk: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired,
multiple: PropTypes.bool,
minSize: PropTypes.number,
maxSize: PropTypes.number,
accept: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]),
onDrop: PropTypes.func,
};

/**
Expand Down Expand Up @@ -205,6 +213,32 @@ class ContentsUploadModal extends Component {
* @returns {string} Markup for the component.
*/
render() {
const dropzoneOptions = {};

const {
multiple = true,
minSize = null,
maxSize = null,
accept = null,
disabled = false,
} = this.props;

if (multiple) {
dropzoneOptions['multiple'] = multiple;
}
if (minSize) {
dropzoneOptions['minSize'] = minSize;
}
if (maxSize) {
dropzoneOptions['maxSize'] = maxSize;
}
if (accept) {
dropzoneOptions['accept'] = accept;
}
if (disabled) {
dropzoneOptions['disabled'] = disabled;
}

return (
this.props.open && (
<Modal className="contents-upload-modal" open={this.props.open}>
Expand All @@ -221,10 +255,10 @@ class ContentsUploadModal extends Component {
</Dimmer>
<Modal.Content>
<Dropzone
onDrop={this.onDrop}
onDrop={this.props.onDrop ?? this.onDrop}
className="dropzone"
noDragEventsBubbling={true}
multiple={true}
{...dropzoneOptions}
>
{({ getRootProps, getInputProps }) => (
<div {...getRootProps({ className: 'dashed' })}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,89 @@ describe('ContentsUploadModal', () => {
await waitFor(() => {});
expect(json).toMatchSnapshot();
});

it('renders a contents upload modal component that only allows images', async () => {
const store = mockStore({
content: {
create: {
loading: false,
loaded: true,
},
},
intl: {
locale: 'en',
messages: {},
},
});
const component = renderer.create(
<Provider store={store}>
<ContentsUploadModal
pathname="/blog"
open={false}
onOk={() => {}}
onCancel={() => {}}
accept={['image/*']}
/>
</Provider>,
);
const json = component.toJSON();
await waitFor(() => {});
expect(json).toMatchSnapshot();
});
it('renders a contents upload modal component that only allows 10MB files', async () => {
const store = mockStore({
content: {
create: {
loading: false,
loaded: true,
},
},
intl: {
locale: 'en',
messages: {},
},
});
const component = renderer.create(
<Provider store={store}>
<ContentsUploadModal
pathname="/blog"
open={false}
onOk={() => {}}
onCancel={() => {}}
maxSize={1000000}
/>
</Provider>,
);
const json = component.toJSON();
await waitFor(() => {});
expect(json).toMatchSnapshot();
});
it('renders a contents upload modal component that only allows 1 file', async () => {
const store = mockStore({
content: {
create: {
loading: false,
loaded: true,
},
},
intl: {
locale: 'en',
messages: {},
},
});
const component = renderer.create(
<Provider store={store}>
<ContentsUploadModal
pathname="/blog"
open={false}
onOk={() => {}}
onCancel={() => {}}
multiple={false}
/>
</Provider>,
);
const json = component.toJSON();
await waitFor(() => {});
expect(json).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ContentsUploadModal renders a contents upload modal component 1`] = `null`;

exports[`ContentsUploadModal renders a contents upload modal component that only allows 1 file 1`] = `null`;

exports[`ContentsUploadModal renders a contents upload modal component that only allows 10MB files 1`] = `null`;

exports[`ContentsUploadModal renders a contents upload modal component that only allows images 1`] = `null`;

0 comments on commit 0395a92

Please sign in to comment.