Skip to content

Commit

Permalink
feat: ✨ columnwidth in gardenmeta (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
espenkalle authored Oct 4, 2023
1 parent 24a0422 commit 7356cd6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 85 deletions.
1 change: 1 addition & 0 deletions apps/test-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function App() {
columnStart: 0,
rowCount: 10000,
validGroupingOptions: ['RFOC', 'PartitionKey'],
columnWidth: 150,
};
},
getBlockAsync: async (a) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/garden/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/workspace-garden",
"version": "5.0.0",
"version": "5.1.0",
"type": "module",
"sideEffects": false,
"license": "MIT",
Expand Down
148 changes: 75 additions & 73 deletions packages/garden/src/lib/components/ExpandProvider/ExpandProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
import { createContext, PropsWithChildren, useReducer } from 'react';
import { defaultGardenPackageWidth } from '../../hooks';

type State = {
expandedColumns: number[];
widths: number[];
};
export enum ActionType {
EXPAND_COLUMN,
}

type ExpandColumn = {
index: number;
type: ActionType.EXPAND_COLUMN;
};

type Action = ExpandColumn;
const expandReducer = (state: State, action: Action): State => {
switch (action.type) {
case ActionType.EXPAND_COLUMN: {
if (state.expandedColumns.findIndex((s) => s === action.index) !== -1) {
const newWidths = [...state.widths];
newWidths[action.index] = defaultGardenPackageWidth;
return {
expandedColumns: state.expandedColumns.filter((s) => s !== action.index),
widths: newWidths,
};
} else {
const newWidths = [...state.widths];
newWidths[action.index] = 500;
return {
expandedColumns: [...state.expandedColumns, action.index],
widths: newWidths,
};
}
}

default:
return { expandedColumns: [], widths: [] };
}
};

type DispatchAction = (action: Action) => void;
const ExpandContext = createContext<State>({
expandedColumns: [],
widths: [],
});

function createExpandDispatchContext() {
return createContext<DispatchAction | undefined>(undefined);
}
const ExpandDispatchContext = createExpandDispatchContext();

type ExpandProviderProps = {
initialWidths: number[];
};

const ExpandProvider = (props: PropsWithChildren<ExpandProviderProps>) => {
const { initialWidths, children } = props;

const [state, dispatch] = useReducer(expandReducer, {
expandedColumns: [],
widths: initialWidths,
});

return (
<ExpandContext.Provider value={state}>
<ExpandDispatchContext.Provider value={dispatch}>{children}</ExpandDispatchContext.Provider>
</ExpandContext.Provider>
);
};

export { ExpandProvider, ExpandContext, ExpandDispatchContext };
import { createContext, PropsWithChildren, useReducer } from 'react';

type State = {
expandedColumns: number[];
widths: number[];
};
export enum ActionType {
EXPAND_COLUMN,
}

type ExpandColumn = {
index: number;
type: ActionType.EXPAND_COLUMN;
};

type Action = ExpandColumn;
const expandReducer =
(defaultColumnWidth: number) =>
(state: State, action: Action): State => {
switch (action.type) {
case ActionType.EXPAND_COLUMN: {
if (state.expandedColumns.findIndex((s) => s === action.index) !== -1) {
const newWidths = [...state.widths];
newWidths[action.index] = defaultColumnWidth;
return {
expandedColumns: state.expandedColumns.filter((s) => s !== action.index),
widths: newWidths,
};
} else {
const newWidths = [...state.widths];
newWidths[action.index] = defaultColumnWidth + 200;
return {
expandedColumns: [...state.expandedColumns, action.index],
widths: newWidths,
};
}
}

default:
return { expandedColumns: [], widths: [] };
}
};

type DispatchAction = (action: Action) => void;
const ExpandContext = createContext<State>({
expandedColumns: [],
widths: [],
});

function createExpandDispatchContext() {
return createContext<DispatchAction | undefined>(undefined);
}
const ExpandDispatchContext = createExpandDispatchContext();

type ExpandProviderProps = {
initialWidths: number[];
defaultColumnWidth: number;
};

const ExpandProvider = (props: PropsWithChildren<ExpandProviderProps>) => {
const { initialWidths, defaultColumnWidth, children } = props;

const [state, dispatch] = useReducer(expandReducer(defaultColumnWidth), {
expandedColumns: [],
widths: initialWidths,
});

return (
<ExpandContext.Provider value={state}>
<ExpandDispatchContext.Provider value={dispatch}>{children}</ExpandDispatchContext.Provider>
</ExpandContext.Provider>
);
};

export { ExpandProvider, ExpandContext, ExpandDispatchContext };
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const VirtualContainer = <TContext,>({
}

const amountOfColumns = data.columnCount;
const widths = useItemWidths(data.columnCount);
const columnWidth = data.columnWidth || 300;
const widths = useItemWidths(data.columnCount, columnWidth);

//TODO: Handle widths = 0 better
if (widths.length === 0 || amountOfColumns !== widths.length) {
Expand All @@ -64,7 +65,7 @@ export const VirtualContainer = <TContext,>({
<>
{/* <ReactQueryDevtools /> */}
<StyledVirtualContainer id={'garden_root'}>
<ExpandProvider initialWidths={widths}>
<ExpandProvider initialWidths={widths} defaultColumnWidth={columnWidth}>
<VirtualGarden
context={context}
getSubgroupItems={dataSource.getSubgroupItems}
Expand Down
10 changes: 2 additions & 8 deletions packages/garden/src/lib/hooks/useItemWidths.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { useEffect, useState } from 'react';

export const defaultGardenPackageWidth = 300;

export function useItemWidths(columnCount: number) {
export function useItemWidths(columnCount: number, itemWidth: number) {
const [widths, setWidths] = useState<number[]>([]);

/**
* How to calculate longest width?
*/
useEffect(() => {
if (columnCount > 0) {
setWidths(new Array(columnCount).fill(defaultGardenPackageWidth));
setWidths(new Array(columnCount).fill(itemWidth));
}
}, [columnCount]);

Expand Down
1 change: 1 addition & 0 deletions packages/garden/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type GardenMeta = {
rowCount: number;
allGroupingOptions: GroupingOption[];
validGroupingOptions: string[];
columnWidth?: number;
};

export type GetHeaderBlockRequestArgs = Pick<
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace-fusion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@equinor/workspace-fusion",
"version": "5.1.0",
"version": "5.2.0",
"type": "module",
"sideEffects": false,
"license": "MIT",
Expand Down

0 comments on commit 7356cd6

Please sign in to comment.