Skip to content

Commit

Permalink
feat: mobile layout render groups
Browse files Browse the repository at this point in the history
  • Loading branch information
flops committed Dec 16, 2024
1 parent 1c0ea1c commit 7c3543a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/components/GridLayout/GridLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ export default class GridLayout extends React.PureComponent {

if (group.render) {
const groupContext = {
isMobile: false,
config,
editMode,
items,
Expand Down
72 changes: 48 additions & 24 deletions src/components/MobileLayout/MobileLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';

import groupBy from 'lodash/groupBy';

import {DEFAULT_GROUP} from '../../constants';
import {DashKitContext} from '../../context';
import {cn} from '../../utils/cn';
import Item from '../Item/Item';
Expand All @@ -26,7 +29,7 @@ export default class MobileLayout extends React.PureComponent<
context!: React.ContextType<typeof DashKitContext>;

pluginsRefs: PlugibRefObject[] = [];
sortedLayoutItems: ReturnType<typeof getSortedConfigItems> | null = null;
sortedLayoutItems: Record<string, ReturnType<typeof getSortedConfigItems>> | null = null;

_memoLayout = this.context.layout;
_memoForwardedPluginRef: Array<(refObject: PlugibRefObject) => void> = [];
Expand All @@ -37,35 +40,53 @@ export default class MobileLayout extends React.PureComponent<
};

render() {
const {config, layout} = this.context;
const {config, layout, groups = [{id: DEFAULT_GROUP}], context, editMode} = this.context;

this.pluginsRefs.length = config.items.length;

const sortedItems = this.getSortedLayoutItems();

return (
<div className={b()}>
{sortedItems.map((item, index) => {
const isItemWithActiveAutoheight =
index in this.state.indexesOfItemsWithActiveAutoheight;

return (
<div
className={b('item', {autoheight: isItemWithActiveAutoheight})}
key={item.id}
>
<Item
id={item.id}
item={item}
layout={layout}
shouldItemUpdate={false}
adjustWidgetLayout={this.getMemoAdjustWidgetLayout(index)}
forwardedPluginRef={this.getMemoForwardRefCallback(index)}
onItemMountChange={this.context.onItemMountChange}
onItemRender={this.context.onItemRender}
/>
</div>
);
{groups.map((group) => {
const groupId = group.id || DEFAULT_GROUP;
const items = sortedItems[groupId];

const children = items.map((item, index) => {
const isItemWithActiveAutoheight =
index in this.state.indexesOfItemsWithActiveAutoheight;

return (
<div
className={b('item', {autoheight: isItemWithActiveAutoheight})}
key={item.id}
>
<Item
id={item.id}
item={item}
layout={layout}
shouldItemUpdate={false}
adjustWidgetLayout={this.getMemoAdjustWidgetLayout(index)}
forwardedPluginRef={this.getMemoForwardRefCallback(index)}
onItemMountChange={this.context.onItemMountChange}
onItemRender={this.context.onItemRender}
/>
</div>
);
});

if (group.render) {
return group.render(groupId, children, {
isMobile: true,
config,
context,
editMode,
items,
layout,
});
}

return children;
})}
</div>
);
Expand All @@ -80,7 +101,10 @@ export default class MobileLayout extends React.PureComponent<

const hasOrderId = Boolean(this.context.config.items.find((item) => item.orderId));

this.sortedLayoutItems = getSortedConfigItems(this.context.config, hasOrderId);
this.sortedLayoutItems = groupBy(
getSortedConfigItems(this.context.config, hasOrderId),
(item) => item.parent || DEFAULT_GROUP,
);

return this.sortedLayoutItems;
}
Expand Down
1 change: 1 addition & 0 deletions src/typings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type AddNewItemOptions = SetItemOptions & {
export interface DashkitGroupRenderProps {
config: Config;
editMode: boolean;
isMobile: boolean;
items: ConfigItem[];
layout: ConfigLayout[];
context: any;
Expand Down

0 comments on commit 7c3543a

Please sign in to comment.