Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/components/GridItem/GridItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ type GridItemProps = {
adjustedWidgetLayout?: ConfigLayout;
}) => void;
gridLayout?: ReactGridLayoutProps;
id?: string;
id: string;
item: ConfigItem;
isDragging?: boolean;
isDraggedOut?: boolean;
layout?: ConfigLayout[];
layout: ConfigLayout[];

forwardedRef?: React.Ref<HTMLDivElement>;
forwardedPluginRef?: (pluginRef: PluginRef) => void;
Expand Down
16 changes: 2 additions & 14 deletions src/components/Item/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import React from 'react';

import {prepareItem} from '../../hocs/prepareItem';
import type {ConfigItem} from '../../shared/types';
import type {PluginRef, PluginWidgetProps} from '../../typings';
import {cn} from '../../utils/cn';
import type {RegisterManager} from '../../utils/register-manager';

import type {ItemProps} from './types';

import './Item.scss';

const b = cn('dashkit-item');

type ItemProps = {
registerManager: RegisterManager;
rendererProps: Omit<PluginWidgetProps, 'onBeforeLoad'>;
type: string;
isPlaceholder?: boolean;
forwardedPluginRef?: (pluginRef: PluginRef) => void;
onItemRender?: (item: ConfigItem) => void;
onItemMountChange?: (item: ConfigItem, meta: {isAsync: boolean; isMounted: boolean}) => void;
item: ConfigItem;
};

// TODO: getDerivedStateFromError и заглушка с ошибкой
const Item: React.FC<ItemProps> = ({
registerManager,
Expand Down
22 changes: 22 additions & 0 deletions src/components/Item/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {ConfigItem, ItemParams} from '../../shared/types';
import type {PluginRef, PluginWidgetProps} from '../../typings';
import type {RegisterManager} from '../../utils/register-manager';

export type RendererProps = Omit<
PluginWidgetProps<ItemParams>,
'onBeforeLoad' | 'width' | 'height'
> & {
width?: number;
height?: number;
};

export type ItemProps = {
forwardedPluginRef?: (ref: PluginRef) => void;
isPlaceholder?: boolean;
item: ConfigItem;
registerManager: RegisterManager;
rendererProps: RendererProps;
type: string;
onItemRender?: (item: ConfigItem) => void;
onItemMountChange?: (item: ConfigItem, meta: {isAsync: boolean; isMounted: boolean}) => void;
};
4 changes: 2 additions & 2 deletions src/components/MobileLayout/MobileLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class MobileLayout extends React.PureComponent<

_memoLayout = this.context.layout;
_memoForwardedPluginRef: Array<(refObject: PluginRef) => void> = [];
_memoAdjustWidgetLayout: Record<string, (props: {needSetDefault: boolean}) => void> = {};
_memoAdjustWidgetLayout: Record<string, (props: {needSetDefault?: boolean}) => void> = {};

state: MobileLayoutState = {
itemsWithActiveAutoheight: {},
Expand Down Expand Up @@ -131,7 +131,7 @@ export default class MobileLayout extends React.PureComponent<
return this._memoForwardedPluginRef[refIndex];
}

adjustWidgetLayout(id: string, {needSetDefault}: {needSetDefault: boolean}) {
adjustWidgetLayout(id: string, {needSetDefault}: {needSetDefault?: boolean}) {
if (needSetDefault) {
const indexesOfItemsWithActiveAutoheight = {
...this.state.itemsWithActiveAutoheight,
Expand Down
11 changes: 7 additions & 4 deletions src/context/DashKitContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ItemStateAndParams,
ItemStateAndParamsChangeOptions,
} from '../shared';
import type {PluginRef, ReactGridLayoutProps} from '../typings';
import type {ContextProps, PluginRef, ReactGridLayoutProps, SettingsProps} from '../typings';

type DashkitPropsPassedToCtx = Pick<
DashKitProps,
Expand Down Expand Up @@ -42,7 +42,10 @@ type TemporaryLayout = {
dragProps: ItemDragProps;
};

export type DashKitCtxShape = DashkitPropsPassedToCtx & {
export type DashKitCtxShape = Omit<DashkitPropsPassedToCtx, 'context' | 'settings'> & {
context: ContextProps;
settings: SettingsProps;

registerManager: RegisterManager;
forwardedMetaRef: React.ForwardedRef<any>;

Expand All @@ -57,12 +60,12 @@ export type DashKitCtxShape = DashkitPropsPassedToCtx & {
) => void;
revertToOriginalLayout: (widgetId: string) => void;

itemsState?: Record<string, ItemState>;
itemsState: Record<string, ItemState>;
itemsParams: Record<string, ItemParams>;
onItemStateAndParamsChange: (
id: string,
stateAndParams: ItemStateAndParams,
options: ItemStateAndParamsChangeOptions,
options?: ItemStateAndParamsChangeOptions,
) => void;

getItemsMeta: (pluginsRefs: Array<PluginRef>) => Array<Promise<any>>;
Expand Down
93 changes: 51 additions & 42 deletions src/hocs/prepareItem.js → src/hocs/prepareItem.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
import React from 'react';

import isEqual from 'lodash/isEqual';
import PropTypes from 'prop-types';

import type {ItemProps, RendererProps} from '../components/Item/types';
import {DashKitContext} from '../context';
import type {ConfigItem, ConfigLayout} from '../shared';
import type {
ItemStateAndParams,
ItemStateAndParamsChangeOptions,
} from '../shared/types/state-and-params';
import type {PluginRef, PluginWidgetProps, ReactGridLayoutProps} from '../typings';

type PrepareItemProps = {
gridLayout?: ReactGridLayoutProps;
adjustWidgetLayout: PluginWidgetProps['adjustWidgetLayout'];
layout: ConfigLayout[];
id: string;
item: ConfigItem;
shouldItemUpdate?: boolean;
width?: number;
height?: number;
transform?: string;
isPlaceholder?: boolean;

onItemRender?: (item: ConfigItem) => void;
onItemMountChange?: (item: ConfigItem, meta: {isAsync: boolean; isMounted: boolean}) => void;

forwardedPluginRef?: (ref: PluginRef) => void;
};

export function prepareItem(
WrappedComponent: React.ComponentType<ItemProps>,
): React.ComponentClass<PrepareItemProps> {
return class PrepareItem extends React.Component<PrepareItemProps> {
static contextType = DashKitContext;
context!: React.ContextType<typeof DashKitContext>;

export function prepareItem(Component) {
return class PrepareItem extends React.Component {
static propTypes = {
gridLayout: PropTypes.object,
adjustWidgetLayout: PropTypes.func.isRequired,
layout: PropTypes.array,
id: PropTypes.string,
item: PropTypes.object,
shouldItemUpdate: PropTypes.bool,
width: PropTypes.number,
height: PropTypes.number,
transform: PropTypes.string,
isPlaceholder: PropTypes.bool,

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

forwardedPluginRef: PropTypes.any,
};
_currentRenderProps: RendererProps = {} as RendererProps;

shouldComponentUpdate(nextProps) {
shouldComponentUpdate(nextProps: PrepareItemProps) {
const {width, height, transform} = this.props;
const {width: widthNext, height: heightNext, transform: transformNext} = nextProps;
if (
!nextProps.shouldItemUpdate &&
width === widthNext &&
height === heightNext &&
transform === transformNext
) {
return false;
}
return true;
}

static contextType = DashKitContext;
return (
nextProps.shouldItemUpdate ||
width !== widthNext ||
height !== heightNext ||
transform !== transformNext
);
}

_onStateAndParamsChange = (stateAndParams, options) => {
_onStateAndParamsChange = (
stateAndParams: ItemStateAndParams,
options?: ItemStateAndParamsChangeOptions,
) => {
this.context.onItemStateAndParamsChange(this.props.id, stateAndParams, options);
};

_currentRenderProps = {};
getRenderProps = () => {
const {id, width, height, item, adjustWidgetLayout, layout, isPlaceholder, gridLayout} =
this.props;
getRenderProps = (): RendererProps => {
const {id, width, height, item, adjustWidgetLayout, layout, gridLayout} = this.props;
const {itemsState, itemsParams, registerManager, settings, context, editMode} =
this.context;
const {data, defaults, namespace} = item;

const rendererProps = {
const rendererProps: RendererProps = {
data,
editMode,
params: itemsParams[id],
Expand All @@ -69,16 +79,15 @@ export function prepareItem(Component) {
layout,
gridLayout: gridLayout || registerManager.gridLayout,
adjustWidgetLayout,
isPlaceholder,
};

const changedProp = Object.entries(rendererProps).find(([key, value]) => {
// Checking gridLayoout deep as groups gridProperties method has tendancy to creat new objects
if (key === 'gridLayout') {
return !isEqual(this._currentRenderProps[key], value);
return !isEqual(this._currentRenderProps[key as keyof RendererProps], value);
}

return this._currentRenderProps[key] !== value;
return this._currentRenderProps[key as keyof RendererProps] !== value;
});

if (changedProp) {
Expand All @@ -95,7 +104,7 @@ export function prepareItem(Component) {
const {type} = item;

return (
<Component
<WrappedComponent
forwardedPluginRef={forwardedPluginRef}
rendererProps={this.getRenderProps()}
registerManager={registerManager}
Expand Down
4 changes: 2 additions & 2 deletions src/typings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export interface WidgetLayout extends AdditionalWidgetLayout {
h: number;
x: number;
y: number;
minW: number;
minH: number;
minW?: number;
minH?: number;
maxW?: number;
maxH?: number;
}
5 changes: 4 additions & 1 deletion src/typings/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '../shared';

import type {ContextProps, SettingsProps, WidgetLayout} from './common';
import type {CompactType} from './config';

export interface PluginWidgetProps<T = StringParams> {
id: string;
Expand All @@ -31,7 +32,9 @@ export interface PluginWidgetProps<T = StringParams> {
settings: SettingsProps;
context: ContextProps;
layout: WidgetLayout[];
gridLayout: ReactGridLayout.ReactGridLayoutProps;
gridLayout: Omit<ReactGridLayout.ReactGridLayoutProps, 'compactType'> & {
compactType?: CompactType;
};
adjustWidgetLayout: (data: {
widgetId: string;
needSetDefault?: boolean;
Expand Down
6 changes: 4 additions & 2 deletions src/utils/register-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReactGridLayout from 'react-grid-layout';

import type {Plugin, PluginDefaultLayout, Settings} from '../typings';
import type {CompactType, Plugin, PluginDefaultLayout, Settings} from '../typings';

interface RegisterManagerDefaultLayout {
x: number;
Expand Down Expand Up @@ -29,7 +29,9 @@ export class RegisterManager {
minW: 4,
minH: 2,
};
private _gridLayout: ReactGridLayout.ReactGridLayoutProps = {
private _gridLayout: Omit<ReactGridLayout.ReactGridLayoutProps, 'compactType'> & {
compactType?: CompactType;
} = {
rowHeight: 18,
cols: 36,
margin: [2, 2],
Expand Down
Loading