Skip to content

Commit

Permalink
Merge pull request #3976 from ProjectMirador/default-args-background-…
Browse files Browse the repository at this point in the history
…plugin

Use ES6 default arguments to avoid deprecated defaultProps on function components
  • Loading branch information
jcoyne authored Nov 6, 2024
2 parents 756da0d + 9820e05 commit 47a97a9
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 117 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
}],
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off",
"react/require-default-props": "off",
"react/require-default-props": [2, {
"functions": "defaultArguments"
}],
"react-hooks/exhaustive-deps": "error",
"testing-library/render-result-naming-convention": "off",
"testing-library/no-render-in-lifecycle": [
Expand Down
2 changes: 1 addition & 1 deletion __tests__/src/components/ThumbnailNavigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Subject({ fixture = manifestJson, ...props }) {
}

Subject.propTypes = {
fixture: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
fixture: PropTypes.object, // eslint-disable-line react/forbid-prop-types
};

jest.mock(
Expand Down
12 changes: 7 additions & 5 deletions setupJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ jest.mock('react-i18next', () => ({
type: '3rdParty',
},
// this mock makes sure any components using the translate HoC receive the t function as a prop
withTranslation: () => (Component) => {
Component.defaultProps = { // eslint-disable-line no-param-reassign
...Component.defaultProps, t: k => k,
};
return Component;
withTranslation: () => (WrappedComponent) => {
/**
*
*/
const I18nAwareComponent = ({ t = (k => k), ...props }) => <WrappedComponent t={t} {...props} />; // eslint-disable-line react/prop-types

return I18nAwareComponent;
},
}));
12 changes: 1 addition & 11 deletions src/components/AudioViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ const StyledAudio = styled('audio')({
});

/** */
export function AudioViewer(props) {
export function AudioViewer({ audioOptions = {}, audioResources = [], captions = [] }) {
/* eslint-disable jsx-a11y/media-has-caption */
/** */
const {
captions, audioOptions, audioResources,
} = props;

return (
<StyledContainer>
Expand All @@ -44,9 +40,3 @@ AudioViewer.propTypes = {
audioResources: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
captions: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types
};

AudioViewer.defaultProps = {
audioOptions: {},
audioResources: [],
captions: [],
};
8 changes: 2 additions & 6 deletions src/components/BackgroundPluginArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import ns from '../config/css-ns';
import { PluginHook } from './PluginHook';

/** invisible area where background plugins can add to */
export const BackgroundPluginArea = props => (
export const BackgroundPluginArea = ({ PluginComponents = [], ...props }) => (
<div className={ns('background-plugin-area')} style={{ display: 'none' }}>
<PluginHook {...props} />
<PluginHook PluginComponents={PluginComponents} {...props} />
</div>
);

BackgroundPluginArea.propTypes = {
PluginComponents: PropTypes.array, // eslint-disable-line react/forbid-prop-types
};

BackgroundPluginArea.defaultProps = {
PluginComponents: [],
};
46 changes: 12 additions & 34 deletions src/components/CompanionWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ const StyledCloseButton = styled(MiradorMenuButton, { name: 'CompanionWindow', s
/**
* CompanionWindow
*/
export function CompanionWindow(props) {
export function CompanionWindow(props) { // eslint-disable-line react/require-default-props
const {
ariaLabel = undefined, classes = {}, direction, paperClassName = '', onCloseClick = () => {}, updateCompanionWindow = undefined, isDisplayed = false,
position = null, t = key => key, title = null, children = undefined, titleControls = null, size = {},
defaultSidebarPanelWidth = 235, defaultSidebarPanelHeight = 201, innerRef = undefined,
} = props;

/** */
const openInNewStyle = () => {
const { direction } = props;
if (direction === 'rtl') return { transform: 'scale(-1, 1)' };
return {};
};
const openInNewStyle = direction === 'rtl' ? { transform: 'scale(-1, 1)' } : {};

/** */
const resizeHandles = () => {
const { direction, position } = props;
const resizeHandles = (() => {
const positions = {
ltr: {
default: 'left',
Expand Down Expand Up @@ -69,12 +70,7 @@ export function CompanionWindow(props) {
}

return base;
};
const {
ariaLabel, classes, paperClassName, onCloseClick, updateCompanionWindow, isDisplayed,
position, t, title, children, titleControls, size,
defaultSidebarPanelWidth, defaultSidebarPanelHeight, innerRef,
} = props;
})();

const isBottom = (position === 'bottom' || position === 'far-bottom');

Expand Down Expand Up @@ -111,7 +107,7 @@ export function CompanionWindow(props) {
width: isBottom ? 'auto' : defaultSidebarPanelWidth,
}}
disableDragging
enableResizing={resizeHandles()}
enableResizing={resizeHandles}
minHeight={50}
minWidth={position === 'left' ? 235 : 100}
>
Expand All @@ -130,7 +126,7 @@ export function CompanionWindow(props) {
aria-label={t('openInCompanionWindow')}
onClick={() => { updateCompanionWindow({ position: 'right' }); }}
>
<OpenInNewIcon style={openInNewStyle()} />
<OpenInNewIcon style={openInNewStyle} />
</MiradorMenuButton>
)
: (
Expand Down Expand Up @@ -208,21 +204,3 @@ CompanionWindow.propTypes = {
titleControls: PropTypes.node,
updateCompanionWindow: PropTypes.func,
};

CompanionWindow.defaultProps = {
ariaLabel: undefined,
children: undefined,
classes: {},
defaultSidebarPanelHeight: 201,
defaultSidebarPanelWidth: 235,
innerRef: undefined,
isDisplayed: false,
onCloseClick: () => { },
paperClassName: '',
position: null,
size: {},
t: key => key,
title: null,
titleControls: null,
updateCompanionWindow: undefined,
};
24 changes: 12 additions & 12 deletions src/components/IIIFIFrameCommunication.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { useEffect } from 'react';
import PropTypes from 'prop-types';

const IIIFIFrameCommunicationDefaultProps = {
'aria-hidden': true,
frameBorder: 0,
height: 1,
name: undefined,
scrolling: undefined,
style: { visibility: 'hidden' },
width: 1,
};

/**
* Handle IIIF Auth token validation using iframe message events
*/
export function IIIFIFrameCommunication({ handleReceiveMessage, ...props }) {
export function IIIFIFrameCommunication({ handleReceiveMessage = undefined, ...props }) {
// Attaches the 'message' event listener to the window.
useEffect(() => {
if (!handleReceiveMessage) return undefined;
Expand All @@ -19,6 +29,7 @@ export function IIIFIFrameCommunication({ handleReceiveMessage, ...props }) {
// iframe "title" attribute is passed in via props for accessibility
// eslint-disable-next-line jsx-a11y/iframe-has-title
<iframe
{...IIIFIFrameCommunicationDefaultProps}
{...props}
/>
);
Expand All @@ -35,14 +46,3 @@ IIIFIFrameCommunication.propTypes = {
style: PropTypes.shape({}),
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

IIIFIFrameCommunication.defaultProps = {
'aria-hidden': true,
frameBorder: 0,
handleReceiveMessage: undefined,
height: 1,
name: undefined,
scrolling: undefined,
style: { visibility: 'hidden' },
width: 1,
};
10 changes: 2 additions & 8 deletions src/components/IIIFThumbnail.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const Image = styled('img', { name: 'IIIFThumbnail', slot: 'image' })(() => ({
* try to load the image (or even calculate that the image url/height/width are)
*/
const LazyLoadedImage = ({
border, placeholder, style = {}, thumbnail, resource, maxHeight, maxWidth, thumbnailsConfig, ...props
border = false, placeholder, style = {}, thumbnail = null,
resource, maxHeight, maxWidth, thumbnailsConfig = {}, ...props
}) => {
const { ref, inView } = useInView();
const [loaded, setLoaded] = useState(false);
Expand Down Expand Up @@ -129,13 +130,6 @@ LazyLoadedImage.propTypes = {
thumbnailsConfig: PropTypes.object, // eslint-disable-line react/forbid-prop-types
};

LazyLoadedImage.defaultProps = {
border: false,
style: {},
thumbnail: null,
thumbnailsConfig: {},
};

/**
* Uses InteractionObserver to "lazy" load canvas thumbnails that are in view.
*/
Expand Down
37 changes: 13 additions & 24 deletions src/components/MiradorMenuButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ const Root = styled(IconButton, { name: 'MiradorMenuButton', slot: 'root' })(({
* This shares the passed in aria-label w/ the Tooltip (as title) and the IconButton
* All props besides icon are spread to the IconButton component
*/
export function MiradorMenuButton(props) {
const { 'aria-label': ariaLabel } = props;
const {
badge,
children,
container,
dispatch,
selected,
BadgeProps,
TooltipProps,
sx,
...iconButtonProps
} = props;

export function MiradorMenuButton({
'aria-label': ariaLabel,
badge = false,
children,
container = null,
dispatch = () => {},
selected = false,
BadgeProps = {},
TooltipProps = {},
sx = {},
...iconButtonProps
}) {
const button = (
<Root
selected={selected}
aria-label={ariaLabel}
{...iconButtonProps}
sx={sx}
size="large"
Expand Down Expand Up @@ -76,13 +75,3 @@ MiradorMenuButton.propTypes = {
sx: PropTypes.object, // eslint-disable-line react/forbid-prop-types
TooltipProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types
};

MiradorMenuButton.defaultProps = {
badge: false,
BadgeProps: {},
container: null,
dispatch: () => {},
selected: false,
sx: {},
TooltipProps: {},
};
2 changes: 1 addition & 1 deletion src/components/MosaicRenderPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MinimalWindow from '../containers/MinimalWindow';
export function MosaicRenderPreview({
t = k => k,
title = '',
windowId = '',
windowId,
}) {
return (
<MinimalWindow
Expand Down
8 changes: 1 addition & 7 deletions src/components/ScrollIndicatedDialogContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ const Root = styled(DialogContent, { name: 'ScrollIndicatedDialogContent', slot:
* ScrollIndicatedDialogContent ~ Inject a style into the DialogContent component
* to indicate there is scrollable content
*/
export function ScrollIndicatedDialogContent(props) {
const { classes, className, ...otherProps } = props;
export function ScrollIndicatedDialogContent({ classes = {}, className = '', ...otherProps }) {
const ourClassName = [className, classes.shadowScrollDialog].join(' ');

return (
Expand All @@ -67,8 +66,3 @@ ScrollIndicatedDialogContent.propTypes = {
}),
className: PropTypes.string,
};

ScrollIndicatedDialogContent.defaultProps = {
classes: {},
className: '',
};
8 changes: 1 addition & 7 deletions src/extend/PluginProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
} from './pluginMapping';

/** */
export default function PluginProvider(props) {
const { plugins, children } = props;
export default function PluginProvider({ plugins = [], children = null }) {
const [pluginMap, setPluginMap] = useState({});

useEffect(() => {
Expand All @@ -29,8 +28,3 @@ PluginProvider.propTypes = {
children: PropTypes.node,
plugins: PropTypes.array, // eslint-disable-line react/forbid-prop-types
};

PluginProvider.defaultProps = {
children: null,
plugins: [],
};

0 comments on commit 47a97a9

Please sign in to comment.