Skip to content

Commit

Permalink
feat(react): support future React version by dropping defaultProps on…
Browse files Browse the repository at this point in the history
… functional component (#579)

see: facebook/react#25699

fix #450
  • Loading branch information
100terres authored Jun 30, 2023
1 parent cde24b2 commit 521a125
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions src/view/droppable/connected-droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ import isDragging from '../../state/is-dragging';
import StoreContext from '../context/store-context';
import whatIsDraggedOverFromResult from '../../state/droppable/what-is-dragged-over-from-result';

function getBody(): HTMLElement {
invariant(document.body, 'document.body is not ready');
return document.body;
}

const defaultProps: DefaultProps = {
mode: 'standard',
type: 'DEFAULT',
direction: 'vertical',
isDropDisabled: false,
isCombineEnabled: false,
ignoreContainerClipping: false,
renderClone: null,
getContainerForClone: getBody,
};

const attachDefaultPropsToOwnProps = (ownProps: InternalOwnProps) => {
// We need to assign default props manually because upcoming React version will stop supporting
// defaultProps on functional components.
// see: https://github.com/facebook/react/pull/25699
let mergedProps = { ...ownProps };
let defaultPropKey: keyof typeof defaultProps;
for (defaultPropKey in defaultProps) {
if (ownProps[defaultPropKey] === undefined) {
mergedProps = {
...mergedProps,
[defaultPropKey]: defaultProps[defaultPropKey],
};
}
}

return mergedProps;
};

const isMatchingType = (type: TypeId, critical: Critical): boolean =>
type === critical.droppable.type;

Expand Down Expand Up @@ -138,10 +172,12 @@ export const makeMapStateToProps = (): Selector => {
const selector = (state: State, ownProps: InternalOwnProps): MapProps => {
// not checking if item is disabled as we need the home list to display a placeholder

const id: DroppableId = ownProps.droppableId;
const type: TypeId = ownProps.type;
const isEnabled = !ownProps.isDropDisabled;
const renderClone: DraggableChildrenFn | null = ownProps.renderClone;
const ownPropsWithDefaultProps = attachDefaultPropsToOwnProps(ownProps);
const id: DroppableId = ownPropsWithDefaultProps.droppableId;
const type: TypeId = ownPropsWithDefaultProps.type;
const isEnabled = !ownPropsWithDefaultProps.isDropDisabled;
const renderClone: DraggableChildrenFn | null =
ownPropsWithDefaultProps.renderClone;

if (isDragging(state)) {
const critical: Critical = state.critical;
Expand Down Expand Up @@ -228,22 +264,6 @@ const mapDispatchToProps: DispatchProps = {
updateViewportMaxScroll: updateViewportMaxScrollAction,
};

function getBody(): HTMLElement {
invariant(document.body, 'document.body is not ready');
return document.body;
}

const defaultProps: DefaultProps = {
mode: 'standard',
type: 'DEFAULT',
direction: 'vertical',
isDropDisabled: false,
isCombineEnabled: false,
ignoreContainerClipping: false,
renderClone: null,
getContainerForClone: getBody,
};

// Abstract class allows to specify props and defaults to component.
// All other ways give any or do not let add default props.
// eslint-disable-next-line
Expand All @@ -261,8 +281,20 @@ const ConnectedDroppable = connect(
makeMapStateToProps,
// no dispatch props for droppable
mapDispatchToProps,
// mergeProps - using default
null as any,
// We need to assign default props manually because upcoming React version will stop supporting
// defaultProps on functional components.
// see: https://github.com/facebook/react/pull/25699
(
stateProps: MapProps,
dispatchProps: DispatchProps,
ownProps: InternalOwnProps,
) => {
return {
...attachDefaultPropsToOwnProps(ownProps),
...stateProps,
...dispatchProps,
};
},
{
// Ensuring our context does not clash with consumers
context: StoreContext as any,
Expand All @@ -274,6 +306,4 @@ const ConnectedDroppable = connect(
// FIXME: Typings are really complexe
)(Droppable) as unknown as FunctionComponent<DroppableProps>;

ConnectedDroppable.defaultProps = defaultProps;

export default ConnectedDroppable;

1 comment on commit 521a125

@vercel
Copy link

@vercel vercel bot commented on 521a125 Jun 30, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.