Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 packages/media-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@wordpress/dataviews": "file:../dataviews",
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/private-apis": "file:../private-apis"
},
"publishConfig": {
Expand Down
49 changes: 44 additions & 5 deletions packages/media-utils/src/components/media-upload-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
store as coreStore,
} from '@wordpress/core-data';
import { resolveSelect } from '@wordpress/data';
import { Modal, DropZone } from '@wordpress/components';
import { Modal, DropZone, FormFileUpload, Button } from '@wordpress/components';
import { upload as uploadIcon } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -290,6 +291,23 @@ export function MediaUploadModal( {
onClose?.();
}, [ onClose ] );

// Use onUpload if provided, otherwise fall back to uploadMedia
const handleUpload = onUpload || uploadMedia;

const handleFileSelect = useCallback(
( event: React.ChangeEvent< HTMLInputElement > ) => {
const files = event.target.files;
if ( files && files.length > 0 ) {
const filesArray = Array.from( files );
handleUpload( {
allowedTypes,
filesList: filesArray,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, for another PR if you want. Looks like onUpload and uploadMedia both support onError - just thinking for when folks try to upload videos or other non-allowed files.

} );
}
},
[ allowedTypes, handleUpload ]
);

const paginationInfo = useMemo(
() => ( {
totalItems,
Expand All @@ -305,20 +323,42 @@ export function MediaUploadModal( {
[]
);

// Build accept attribute from allowedTypes
const acceptTypes = useMemo( () => {
if ( allowedTypes.includes( '*' ) ) {
return undefined;
}
return allowedTypes.join( ',' );
}, [ allowedTypes ] );

if ( ! isOpen ) {
return null;
}

// Use onUpload if provided, otherwise fall back to uploadMedia
const handleUpload = onUpload || uploadMedia;

return (
<Modal
title={ title }
onRequestClose={ handleModalClose }
isDismissible={ isDismissible }
className={ modalClass }
size="fill"
headerActions={
<FormFileUpload
accept={ acceptTypes }
multiple
onChange={ handleFileSelect }
__next40pxDefaultSize
render={ ( { openFileDialog } ) => (
<Button
onClick={ openFileDialog }
icon={ uploadIcon }
__next40pxDefaultSize
>
{ __( 'Upload media' ) }
</Button>
) }
/>
}
>
<DropZone
onFilesDrop={ ( files ) => {
Expand All @@ -341,7 +381,6 @@ export function MediaUploadModal( {
handleUpload( {
allowedTypes,
filesList: filteredFiles,
multiple,
} );
}
} }
Expand Down
Loading