Skip to content

Commit

Permalink
feat: make newItemLayout optional in reflowLayout (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
yndx-birman authored Sep 5, 2024
1 parent 1af4306 commit 4309e8e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
20 changes: 14 additions & 6 deletions src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,20 @@ export class DashKit extends React.PureComponent<DashKitInnerProps> {
return UpdateManager.removeItem({id, config, itemsStateAndParams});
}

static reflowLayout(
newLayoutItem: ConfigLayout,
layout: ConfigLayout[],
groups?: DashKitGroup[],
) {
return reflowLayout(newLayoutItem, layout, getReflowGroupsConfig(groups));
static reflowLayout({
newLayoutItem,
layout,
groups,
}: {
newLayoutItem?: ConfigLayout;
layout: ConfigLayout[];
groups?: DashKitGroup[];
}) {
return reflowLayout({
newLayoutItem,
layout,
reflowLayoutOptions: getReflowGroupsConfig(groups),
});
}

metaRef = React.createRef<GridLayout>();
Expand Down
8 changes: 4 additions & 4 deletions src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ export const DashKitGroupsShowcase: React.FC = () => {

setConfig({
...config,
layout: DashKit.reflowLayout(
copyItem,
config.layout.filter(({i}) => i !== itemId),
layout: DashKit.reflowLayout({
newLayoutItem: copyItem,
layout: config.layout.filter(({i}) => i !== itemId),
groups,
),
}),
});
};

Expand Down
33 changes: 22 additions & 11 deletions src/utils/update-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,15 @@ function changeGroupParams({
return update(itemsStateAndParams, obj);
}

export function reflowLayout(
newLayoutItem: ConfigLayout,
layout: ConfigLayout[],
reflowLayoutOptions: ReflowLayoutOptions,
) {
export function reflowLayout({
newLayoutItem,
layout,
reflowLayoutOptions,
}: {
newLayoutItem?: ConfigLayout;
layout: ConfigLayout[];
reflowLayoutOptions: ReflowLayoutOptions;
}) {
const byGroup: Record<string, ConfigLayout[]> = {};
const reducer = (
memo: Record<string, number>,
Expand All @@ -421,7 +425,10 @@ export function reflowLayout(
};

const orderById = layout.reduce<Record<string, number>>(reducer, {});
reducer(orderById, newLayoutItem, layout.length, layout, true);

if (newLayoutItem) {
reducer(orderById, newLayoutItem, layout.length, layout, true);
}

const {defaultProps} = reflowLayoutOptions;

Expand Down Expand Up @@ -489,11 +496,11 @@ export class UpdateManager {
const newLayoutItem = {...saveDefaultLayout, i: newItem.id};

if (options.reflowLayoutOptions) {
newLayout = reflowLayout(
newLayout = reflowLayout({
newLayoutItem,
config.layout.map((t) => ({...t, ...(byId[t.i] || {})})),
options.reflowLayoutOptions,
);
layout: config.layout.map((t) => ({...t, ...(byId[t.i] || {})})),
reflowLayoutOptions: options.reflowLayoutOptions,
});
} else {
newLayout = [
...config.layout.map((t) => ({...t, ...(byId[t.i] || {})})),
Expand All @@ -511,7 +518,11 @@ export class UpdateManager {
const newLayoutItem = {...saveDefaultLayout, i: newItem.id};

if (options.reflowLayoutOptions) {
newLayout = reflowLayout(newLayoutItem, config.layout, options.reflowLayoutOptions);
newLayout = reflowLayout({
newLayoutItem,
layout: config.layout,
reflowLayoutOptions: options.reflowLayoutOptions,
});
} else {
newLayout = [...config.layout, newLayoutItem];
}
Expand Down

0 comments on commit 4309e8e

Please sign in to comment.