Skip to content

Commit

Permalink
Merge pull request #309 from 10up/upkeep/component-media-toolbar
Browse files Browse the repository at this point in the history
upkeep: add TS support to `MediaToolbar` component
  • Loading branch information
fabiankaegy authored Apr 16, 2024
2 parents 04c49b2 + 0b5101d commit a7df65a
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { __ } from '@wordpress/i18n';
import { MediaReplaceFlow, MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import PropTypes from 'prop-types';
import { useMedia } from '../../hooks/use-media';
import type { Attachment } from '@wordpress/core-data';

interface MediaToolbarProps {
/**
* Callback to handle the selection of a media.
*/
onSelect: (media: Attachment) => void;

/**
* Callback to handle the removal of a media.
*/
onRemove: (event: React.MouseEvent<HTMLButtonElement>) => void;

/**
* Wether or not the Remove Image button should be shown.
*/
isOptional?: boolean;

/**
* The ID of the media, in this case the image.
*/
id: number;
}

/**
* MediaToolbar
Expand All @@ -15,9 +37,7 @@ import { useMedia } from '../../hooks/use-media';
* @param {object} props options
* @returns {React.ReactElement} markup of the ToolbarGroup
*/
export const MediaToolbar = (props) => {
const { onSelect, onRemove, isOptional = false, id } = props;

export const MediaToolbar: React.FC<MediaToolbarProps> = ( { onSelect, onRemove, isOptional = false, id } ) => {
const hasImage = !!id;
const { media } = useMedia(id);

Expand Down Expand Up @@ -51,14 +71,3 @@ export const MediaToolbar = (props) => {
</ToolbarGroup>
);
};

MediaToolbar.defaultProps = {
isOptional: false,
};

MediaToolbar.propTypes = {
id: PropTypes.number.isRequired,
onSelect: PropTypes.func.isRequired,
onRemove: PropTypes.func.isRequired,
isOptional: PropTypes.bool,
};

0 comments on commit a7df65a

Please sign in to comment.