Skip to content

Commit

Permalink
Allow Gantt client to get information on resized columns
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
  • Loading branch information
lfasani committed Apr 4, 2024
1 parent e2884d6 commit 1142045
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
29 changes: 25 additions & 4 deletions src/components/gantt/use-tablelist-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,49 @@ export const useTableListResize = (

return [
{
id: "TitleColumn",
component: TitleColumn,
width: titleCellWidth,
title: "Name",
},

{
id: "DateStartColumn",
component: DateStartColumn,
width: dateCellWidth,
title: "From",
},

{
id: "DateEndColumn",
component: DateEndColumn,
width: dateCellWidth,
title: "To",
},

{
id: "DependenciesColumn",
component: DependenciesColumn,
width: dependenciesCellWidth,
title: "Dependencies",
},

{
id: "DeleteColumn",
component: DeleteColumn,
width: actionColumnWidth,
canResize: false,
},

{
id: "EditColumn",
component: EditColumn,
width: actionColumnWidth,
canResize: false,
},

{
id: "AddColumn",
component: AddColumn,
width: actionColumnWidth,
canResize: false,
Expand All @@ -92,9 +99,24 @@ export const useTableListResize = (
useEffect(() => {
if (columnsProp) {
setColumns([...columnsProp]);
setTableWidth(columnsProp.reduce((res, { width }) => res + width, 0));
const currentColumnIds = columnsState.map(col => col.id);
const newColumnIds = columnsProp.map(col => col.id);

const widthOfAddedColumns = columnsProp
.filter(col => !currentColumnIds.includes(col.id))
.reduce((res, { width }) => res + width, 0);
const widthOfRemovedColumns = columnsState
.filter(col => !newColumnIds.includes(col.id))
.reduce((res, { width }) => res + width, 0);

setTableWidth(prev =>
Math.min(
prev + widthOfAddedColumns - widthOfRemovedColumns,
columnsProp.reduce((res, { width }) => res + width, 0)
)
);
}
}, []);
}, [columnsProp]);

const [tableResizeEvent, setTableResizeEvent] =
useState<TableResizeEvent | null>(null);
Expand Down Expand Up @@ -182,12 +204,11 @@ export const useTableListResize = (
columnResizeEvent.initialColumnWidth + moveDelta
);
let previousColumnWidth = null;

setColumns((prevColumns: readonly Column[]) => {
const newColumns = prevColumns.map((column, index) => {
if (index === columnResizeEvent.columnIndex) {
previousColumnWidth = column.width;
column.width = newColumnwidth;
return { ...column, width: newColumnwidth };
}
return column;
});
Expand Down
4 changes: 2 additions & 2 deletions src/types/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ export type ColumnProps = {
};

export type Column = {
id?: string;
id: string;
component: ComponentType<ColumnProps>;
width: number;
title?: ReactNode;
Expand All @@ -835,7 +835,7 @@ export type Column = {
export type OnResizeColumn = (
nextColumns: readonly Column[],
columnIndex: number,
nextWidth: number
deltaWidth: number
) => void;
export type ChangeAction =
| {
Expand Down
8 changes: 8 additions & 0 deletions stories/CustomColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DateStartColumn,
Gantt,
OnChangeTasks,
OnResizeColumn,
Task,
TaskOrEmpty,
TitleColumn,
Expand Down Expand Up @@ -169,6 +170,12 @@ export const CustomColumns: React.FC<AppProps> = props => {
setDisplayedColumns(newDisplayedColumns);
};

const onResizeColumn: OnResizeColumn = (newColumns: readonly Column[]) => {
setDisplayedColumns(() => {
return [...newColumns];
});
};

return (
<>
<FormControl>
Expand Down Expand Up @@ -198,6 +205,7 @@ export const CustomColumns: React.FC<AppProps> = props => {
onEditTask={onEditTask}
onClick={handleClick}
tasks={tasks}
onResizeColumn={onResizeColumn}
/>
</>
);
Expand Down
24 changes: 0 additions & 24 deletions stories/CustomPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,6 @@ export const CustomPalette: React.FC<AppProps> = props => {
return null;
};

const columns: readonly Column[] = [
{
component: TitleColumn,
width: 210,
title: "Name",
},
{
component: DateStartColumn,
width: 150,
title: "Date of start",
},
{
component: DateEndColumn,
width: 150,
title: "Date of end",
},
{
component: ProgressColumn,
width: 40,
title: "Progress",
},
];

const handleTaskDelete = (task: Task) => {
const conf = window.confirm("Are you sure about " + task.name + " ?");
if (conf) {
Expand Down Expand Up @@ -174,7 +151,6 @@ export const CustomPalette: React.FC<AppProps> = props => {
return (
<Gantt
{...props}
columns={columns}
onAddTask={onAddTask}
onChangeTasks={onChangeTasks}
onDoubleClick={handleDblClick}
Expand Down

0 comments on commit 1142045

Please sign in to comment.