diff --git a/src/components/Dashboard/Dashboard.stories.tsx b/src/components/Dashboard/Dashboard.stories.tsx index d0a16ad..29f130d 100644 --- a/src/components/Dashboard/Dashboard.stories.tsx +++ b/src/components/Dashboard/Dashboard.stories.tsx @@ -216,6 +216,51 @@ function NarrowGridStory() { ); } +function DynamicAddItemsStory() { + const [layout, setLayout] = React.useState([{i: 'errors', x: 0, y: 0, w: 2, h: 3}]); + const [items, setItems] = React.useState(operationalItems); + + const handleAddItem = () => { + setItems((currentItems) => { + const itemNumber = currentItems.length + 1; + + return [ + ...currentItems, + { + id: `metric-${itemNumber}`, + content: ( + + {itemNumber * 128} + Dynamically added metric #{itemNumber} + + ), + }, + ]; + }); + }; + + return ( + + Add item + + } + > + { + setLayout(nextLayout); + action('onLayoutChange')(nextLayout); + }} + /> + + ); +} + const meta: Meta = { title: 'Components/Dashboard', component: Dashboard, @@ -246,3 +291,5 @@ export const ControlledLayout: Story = {render: () => } /** Grid columns, row height, gaps and compaction can be adapted to the host container. */ export const CustomGrid: Story = {render: () => }; + +export const AddItems: Story = {render: () => }; diff --git a/src/components/Dashboard/helpers/createDefaultLayout.ts b/src/components/Dashboard/helpers/createDefaultLayout.ts index 38b9302..0e1b43a 100644 --- a/src/components/Dashboard/helpers/createDefaultLayout.ts +++ b/src/components/Dashboard/helpers/createDefaultLayout.ts @@ -6,6 +6,7 @@ const DEFAULT_ITEM_HEIGHT = 4; export const createDefaultLayout = ( items: DashboardRenderItem[], columns: number, + defaultLayout?: ConfigLayout[], ): ConfigLayout[] => { const itemCount = items.length; @@ -14,30 +15,17 @@ export const createDefaultLayout = ( const itemsPerRow = Math.max(Math.floor(columns / width), 1); - return Array.from({length: itemCount}, (_, index) => ({ - i: items[index].id, - x: (index % itemsPerRow) * width, - y: Math.floor(index / itemsPerRow) * height, - w: width, - h: height, - })); -}; - -export const createDefaultLayoutForItem = ( - item: DashboardRenderItem, - columns: number, - index: number, -) => { - const width = Math.min(Math.ceil(columns / 2), columns); - const height = DEFAULT_ITEM_HEIGHT; - - const itemsPerRow = Math.max(Math.floor(columns / width), 1); - - return { - i: item.id, - x: (index % itemsPerRow) * width, - y: Math.floor(index / itemsPerRow) * height, - w: width, - h: DEFAULT_ITEM_HEIGHT, - }; + return Array.from({length: itemCount}, (_, index) => { + if (defaultLayout?.[index]) { + return defaultLayout?.[index]; + } + + return { + i: items[index].id, + x: (index % itemsPerRow) * width, + y: Math.floor(index / itemsPerRow) * height, + w: width, + h: height, + }; + }); }; diff --git a/src/components/Dashboard/hooks/useDashboardConfig.ts b/src/components/Dashboard/hooks/useDashboardConfig.ts index a3387d1..b8301ee 100644 --- a/src/components/Dashboard/hooks/useDashboardConfig.ts +++ b/src/components/Dashboard/hooks/useDashboardConfig.ts @@ -38,7 +38,7 @@ export const useDashboardConfig = ( const cols = gridProps.cols; const layout = useMemo( - () => defaultLayout ?? createDefaultLayout(items, cols), + () => createDefaultLayout(items, cols, defaultLayout), [defaultLayout, items, cols], );