Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: item life cycle callbacks for mobile #208

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions src/components/GridItem/GridItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class GridItem extends React.PureComponent {
forwardedPluginRef: PropTypes.any,
isPlaceholder: PropTypes.bool,

onItemMountChange: PropTypes.func,
onItemRender: PropTypes.func,

// from react-grid-layout:
children: PropTypes.node,
className: PropTypes.string,
Expand Down Expand Up @@ -146,36 +149,6 @@ class GridItem extends React.PureComponent {
});
};

onMountChange = (isMounted) => {
if (isMounted) {
this._inited = true;

this.props.onItemMountChange?.(this.props.item, {
isAsync: this._isAsyncItem,
isMounted: isMounted,
});

if (!this._isAsyncItem) {
this.props.onItemRender?.(this.props.item);
}
} else {
this.props.onItemMountChange?.(this.props.item, {
isAsync: this._isAsyncItem,
isMounted: isMounted,
});
}
};

onBeforeLoad = () => {
this._isAsyncItem = true;

return this.onLoad;
};

onLoad = () => {
this.props.onItemRender?.(this.props.item);
};

render() {
// из-за бага, что Grid Items unmounts при изменении static, isDraggable, isResaizable
// https://github.com/STRML/react-grid-layout/issues/721
Expand Down Expand Up @@ -244,8 +217,8 @@ class GridItem extends React.PureComponent {
adjustWidgetLayout={this.props.adjustWidgetLayout}
layout={this.props.layout}
forwardedPluginRef={this.props.forwardedPluginRef}
onMountChange={this.onMountChange}
onBeforeLoad={this.onBeforeLoad}
onItemMountChange={this.props.onItemMountChange}
onItemRender={this.props.onItemRender}
/>
</div>
{!noOverlay && this.renderOverlay()}
Expand Down
46 changes: 38 additions & 8 deletions src/components/Item/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,47 @@ const Item = ({
type,
isPlaceholder,
forwardedPluginRef,
onMountChange,
onItemRender,
onItemMountChange,
item,
}) => {
const _isAsyncItem = React.useRef(false);
flops marked this conversation as resolved.
Show resolved Hide resolved

const isRegisteredType = registerManager.check(type);

React.useLayoutEffect(() => {
if (isRegisteredType && !isPlaceholder) {
onMountChange?.(true);
onItemMountChange?.(item, {
isAsync: _isAsyncItem.current,
isMounted: true,
});

if (!_isAsyncItem.current) {
onItemRender?.(item);
}

return () => {
onMountChange?.(false);
onItemMountChange?.(item, {
isAsync: _isAsyncItem.current,
isMounted: false,
});
};
}
}, []);
}, [item]);
flops marked this conversation as resolved.
Show resolved Hide resolved

const onLoad = React.useCallback(() => {
onItemRender?.(item);
}, [item, onItemRender]);
flops marked this conversation as resolved.
Show resolved Hide resolved

const onBeforeLoad = React.useCallback(() => {
_isAsyncItem.current = true;

return onLoad;
}, [onLoad]);

const itemRendererProps = React.useMemo(() => {
return {...rendererProps, onBeforeLoad};
}, [rendererProps, onBeforeLoad]);

if (!isRegisteredType) {
console.warn(`type [${type}] не зарегистрирован`);
Expand All @@ -39,14 +68,14 @@ const Item = ({
<div className={b('placeholder')}>
{registerManager
.getItem(type)
.placeholderRenderer?.(rendererProps, forwardedPluginRef) || null}
.placeholderRenderer?.(itemRendererProps, forwardedPluginRef) || null}
</div>
);
}

return (
<div className={b('renderer')}>
{registerManager.getItem(type).renderer(rendererProps, forwardedPluginRef)}
{registerManager.getItem(type).renderer(itemRendererProps, forwardedPluginRef)}
</div>
);
};
Expand All @@ -57,8 +86,9 @@ Item.propTypes = {
registerManager: PropTypes.object,
type: PropTypes.string,
isPlaceholder: PropTypes.bool,
onMountChange: PropTypes.func,
onBeforeLoad: PropTypes.func,
onItemRender: PropTypes.func,
onItemMountChange: PropTypes.func,
item: PropTypes.object,
};

export default prepareItem(Item);
33 changes: 2 additions & 31 deletions src/components/MobileLayout/MobileLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,6 @@ export default class MobileLayout extends React.PureComponent {
return this._memoAdjustWidgetLayout[index];
};

onMountChange = (isMounted) => {
if (isMounted) {
this._inited = true;

this.context.onItemMountChange?.(this.props.item, {
isAsync: this._isAsyncItem,
isMounted: isMounted,
});

if (!this._isAsyncItem) {
this.context.onItemRender?.(this.props.item);
}
} else {
this.context.onItemMountChange?.(this.props.item, {
isAsync: this._isAsyncItem,
isMounted: isMounted,
});
}
};

onBeforeLoad = () => {
this._isAsyncItem = true;

return this.onLoad;
};

onLoad = () => {
this.context.onItemRender?.(this.props.item);
};

render() {
const {config, layout} = this.context;

Expand All @@ -141,7 +111,8 @@ export default class MobileLayout extends React.PureComponent {
adjustWidgetLayout={this.getMemoAdjustWidgetLayout(index)}
forwardedPluginRef={this.getMemoForwardRefCallback(index)}
onMountChange={this.onMountChange}
onBeforeLoad={this.onBeforeLoad}
onItemMountChange={this.context.onItemMountChange}
onItemRender={this.context.onItemRender}
/>
</div>
);
Expand Down
24 changes: 9 additions & 15 deletions src/hocs/prepareItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export function prepareItem(Component) {
height: PropTypes.number,
transform: PropTypes.string,
isPlaceholder: PropTypes.bool,
onBeforeLoad: PropTypes.func,

onItemRender: PropTypes.func,
onItemMountChange: PropTypes.func,

forwardedPluginRef: PropTypes.any,
onMountChange: PropTypes.func,
};

shouldComponentUpdate(nextProps) {
Expand All @@ -44,16 +45,7 @@ export function prepareItem(Component) {

_currentRenderProps = {};
getRenderProps = () => {
const {
id,
width,
height,
item,
adjustWidgetLayout,
layout,
isPlaceholder,
onBeforeLoad,
} = this.props;
const {id, width, height, item, adjustWidgetLayout, layout, isPlaceholder} = this.props;
const {itemsState, itemsParams, registerManager, settings, context, editMode} =
this.context;
const {data, defaults, namespace} = item;
Expand All @@ -64,7 +56,6 @@ export function prepareItem(Component) {
params: itemsParams[id],
state: itemsState[id],
onStateAndParamsChange: this._onStateAndParamsChange,
onBeforeLoad,
width,
height,
id,
Expand All @@ -90,7 +81,8 @@ export function prepareItem(Component) {
};

render() {
const {item, isPlaceholder, forwardedPluginRef, onMountChange} = this.props;
const {item, isPlaceholder, forwardedPluginRef, onItemMountChange, onItemRender} =
this.props;
const {registerManager} = this.context;
const {type} = item;

Expand All @@ -101,7 +93,9 @@ export function prepareItem(Component) {
registerManager={registerManager}
type={type}
isPlaceholder={isPlaceholder}
onMountChange={onMountChange}
onItemMountChange={onItemMountChange}
onItemRender={onItemRender}
item={item}
/>
);
}
Expand Down
Loading