Skip to content

Commit

Permalink
feat: grid button (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
matttdawson authored Sep 24, 2024
1 parent c7f24b3 commit 786f381
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/components/gridPopoverEdit/GridButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { CellFocusedEvent, ICellEditorParams } from "ag-grid-community";
import { ColDefT, GridCell, SAICellRendererParams } from "../GridCell";
import { GenericCellColDef } from "../gridRender";
import { GridBaseRow } from "../Grid";
import { LuiButton, LuiIcon } from "@linzjs/lui";
import { useEffect, useRef } from "react";

const ButtonCellRenderer = (props: SAICellRendererParams) => {
const { data, node, column, colDef, api } = props;
const inputRef = useRef<HTMLButtonElement>(null);

useEffect(() => {
const checkFocus = (event: CellFocusedEvent) => {
if (event.rowIndex === node.rowIndex && event.column === column) {
inputRef.current?.focus();
}
};
api.addEventListener("cellFocused", checkFocus);
return () => {
api.removeEventListener("cellFocused", checkFocus);
};
}, [api, column, node.rowIndex]);

return (
<LuiButton
ref={inputRef}
className="lui-button-icon-only"
size="sm"
level="text"
onClick={() => {
const selectedRows = [data];
const selectedRowIds = selectedRows.map((r) => r.id);
colDef?.cellEditorParams.onClick?.({ selectedRows, selectedRowIds });
}}
style={{ display: colDef?.cellEditorParams?.visible?.(props) !== false ? "" : "none" }}
>
<LuiIcon name="ic_redo" alt="revert" size="md" />
</LuiButton>
);
};

export interface GridButtonProps<TData> {
visible?: (cellEditorParams: ICellEditorParams) => boolean;
onClick?: (props: { selectedRows: TData[]; selectedRowIds: (string | number)[] }) => void;
}

export const GridButton = <TData extends GridBaseRow>(
colDef: GenericCellColDef<TData, boolean>,
editor: GridButtonProps<TData>,
): ColDefT<TData> => {
return GridCell({
minWidth: 72,
maxWidth: 72,
resizable: false,
headerClass: "GridHeaderAlignCenter",
cellClass: "GridCellAlignCenter",
cellRenderer: ButtonCellRenderer,
cellEditorParams: {
...editor,
},
...colDef,
});
};
1 change: 1 addition & 0 deletions src/components/gridPopoverEdit/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./GridButton";
export * from "./GridEditBoolean";
export * from "./GridPopoutEditMultiSelect";
export * from "./GridPopoutEditMultiSelectGrid";
Expand Down
22 changes: 21 additions & 1 deletion src/stories/grid/GridPopoutEditBoolean.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import { useMemo, useState } from "react";

import "@linzjs/lui/dist/fonts";

import { ColDefT, Grid, GridCell, GridContextProvider, GridEditBoolean, GridUpdatingContextProvider } from "../..";
import {
ColDefT,
Grid,
GridButton,
GridCell,
GridContextProvider,
GridEditBoolean,
GridUpdatingContextProvider,
} from "../..";
import { waitForGridReady } from "../../utils/storybookTestUtil";
import { IFormTestRow } from "./FormTest";

Expand Down Expand Up @@ -39,6 +47,18 @@ const GridPopoutEditBooleanTemplate: StoryFn<typeof Grid> = () => {
field: "id",
headerName: "Id",
}),
GridButton(
{
colId: "button",
headerName: "Button",
},
{
visible: ({ data }) => !!(data.id & 1),
onClick: ({ selectedRowIds }) => {
alert("click " + selectedRowIds);
},
},
),
GridEditBoolean(
{
field: "bold",
Expand Down
2 changes: 1 addition & 1 deletion src/stories/grid/GridPopoverEditDropDown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const GridEditDropDownTemplate: StoryFn<typeof Grid> = (props: GridProps) => {
),
GridPopoverEditDropDown(
{
field: "position4",
colId: "position4",
headerName: "Filtered (object)",
valueGetter: ({ data }) => data?.position4?.desc,
},
Expand Down

0 comments on commit 786f381

Please sign in to comment.