Skip to content

Commit

Permalink
Added custom style support to custom column controls in AutoTable
Browse files Browse the repository at this point in the history
  • Loading branch information
MillanWangGadget committed Sep 22, 2024
1 parent 2850612 commit ea71cd1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
24 changes: 24 additions & 0 deletions packages/react/.changeset/swift-birds-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@gadgetinc/react": patch
---

Added custom style support to custom column controls in AutoTable

```jsx
const MyAutoTable = () => {
const columns = [
{
header: "Custom header",
field: "fieldApiIdentifier",
style: { backgroundColor: "red" },
},
{
header: "Custom cell renderer",
render: ({ record }) => <div>{record.name}</div>,
style: { backgroundColor: "blue" },
},
];

return <AutoTable model={api.myModel} columns={columns} />;
};
```
3 changes: 2 additions & 1 deletion packages/react/spec/auto/PolarisAutoTable.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ export const CustomCell = {
},
"rt",
{
header: "Custom cell",
style: { backgroundColor: "#abcdef", width: "320px", maxWidth: "320px" },
header: "Custom cell with custom style",
render: ({ record }) => {
return (
<div>
Expand Down
12 changes: 12 additions & 0 deletions packages/react/spec/auto/hooks/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,15 @@ describe("helper functions for useTable hook", () => {
"header": "Name",
"identifier": "name",
"sortable": true,
"style": undefined,
"type": "String",
},
{
"field": "age",
"header": "Age",
"identifier": "age",
"sortable": true,
"style": undefined,
"type": "Number",
},
]
Expand Down Expand Up @@ -385,13 +387,15 @@ describe("helper functions for useTable hook", () => {
"header": "Some cool name",
"identifier": "name",
"sortable": true,
"style": undefined,
"type": "String",
},
{
"field": "age",
"header": "Age",
"identifier": "age",
"sortable": false,
"style": undefined,
"type": "Number",
},
]
Expand All @@ -412,6 +416,7 @@ describe("helper functions for useTable hook", () => {
"header": "Address",
"identifier": "address",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -420,6 +425,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "owner",
"relationshipType": "HasOne",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -428,6 +434,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "products",
"relationshipType": "HasMany",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -436,6 +443,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "manufacturer",
"relationshipType": "BelongsTo",
"sortable": true,
"style": undefined,
"type": "String",
},
]
Expand All @@ -455,6 +463,7 @@ describe("helper functions for useTable hook", () => {
"header": "Address",
"identifier": "address",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -463,6 +472,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "owner.name",
"relationshipType": "HasOne",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -471,6 +481,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "products.edges.node.sku",
"relationshipType": "HasMany",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -479,6 +490,7 @@ describe("helper functions for useTable hook", () => {
"identifier": "manufacturer.country",
"relationshipType": "BelongsTo",
"sortable": true,
"style": undefined,
"type": "String",
},
]
Expand Down
16 changes: 16 additions & 0 deletions packages/react/spec/auto/hooks/useTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,41 +126,47 @@ describe("useTable hook", () => {
"header": "Id",
"identifier": "id",
"sortable": false,
"style": undefined,
"type": "ID",
},
{
"field": "name",
"header": "Name",
"identifier": "name",
"sortable": true,
"style": undefined,
"type": "String",
},
{
"field": "inventoryCount",
"header": "Inventory count",
"identifier": "inventoryCount",
"sortable": true,
"style": undefined,
"type": "Number",
},
{
"field": "anything",
"header": "Anything",
"identifier": "anything",
"sortable": true,
"style": undefined,
"type": "JSON",
},
{
"field": "description",
"header": "Description",
"identifier": "description",
"sortable": true,
"style": undefined,
"type": "RichText",
},
{
"field": "category",
"header": "Category",
"identifier": "category",
"sortable": true,
"style": undefined,
"type": "Enum",
},
{
Expand All @@ -169,27 +175,31 @@ describe("useTable hook", () => {
"identifier": "startsAt",
"includeTime": undefined,
"sortable": true,
"style": undefined,
"type": "DateTime",
},
{
"field": "isChecked",
"header": "Is checked",
"identifier": "isChecked",
"sortable": true,
"style": undefined,
"type": "Boolean",
},
{
"field": "metafields",
"header": "Metafields",
"identifier": "metafields",
"sortable": true,
"style": undefined,
"type": "JSON",
},
{
"field": "roles",
"header": "Roles",
"identifier": "roles",
"sortable": false,
"style": undefined,
"type": "RoleAssignments",
},
{
Expand All @@ -198,27 +208,31 @@ describe("useTable hook", () => {
"identifier": "birthday",
"includeTime": undefined,
"sortable": true,
"style": undefined,
"type": "DateTime",
},
{
"field": "color",
"header": "Color",
"identifier": "color",
"sortable": true,
"style": undefined,
"type": "Color",
},
{
"field": "secretKey",
"header": "Secret key",
"identifier": "secretKey",
"sortable": false,
"style": undefined,
"type": "EncryptedString",
},
{
"field": "mustBeLongString",
"header": "Must be long string",
"identifier": "mustBeLongString",
"sortable": true,
"style": undefined,
"type": "String",
},
]
Expand Down Expand Up @@ -526,6 +540,7 @@ describe("useTable hook", () => {
"header": "Name",
"identifier": "name",
"sortable": true,
"style": undefined,
"type": "String",
},
{
Expand All @@ -534,6 +549,7 @@ describe("useTable hook", () => {
"identifier": "000-000-000-000-1",
"render": [Function],
"sortable": false,
"style": undefined,
"type": "CustomRenderer",
},
]
Expand Down
25 changes: 23 additions & 2 deletions packages/react/src/auto/polaris/PolarisAutoTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ const PolarisAutoTableComponent = <
hasZebraStriping={props.hasZebraStriping}
condensed={props.condensed}
>
<PolarisIndexTableCellStyleOverride />

{rows &&
columns &&
rows.map((row, index) => {
Expand All @@ -236,8 +238,8 @@ const PolarisAutoTableComponent = <
selected={selection.recordIds.includes(row.id as string)}
>
{columns.map((column) => (
<IndexTable.Cell key={column.identifier}>
<div style={{ maxWidth: "200px" }}>
<IndexTable.Cell key={column.identifier} className="Gadget_PolarisAutoTable_IndexTableCell">
<div style={{ ...defaultCellStyle, ...column.style }}>
{column.type == "CustomRenderer" ? (
(row[column.identifier] as ReactNode)
) : (
Expand All @@ -251,10 +253,29 @@ const PolarisAutoTableComponent = <
})}
</IndexTable>
</BlockStack>
<PolarisIndexTableCellStyleOverride />
</AutoTableContext.Provider>
);
};

const defaultCellStyle: React.CSSProperties = {
maxWidth: "200px",
padding: "0.375rem",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
} as const;

const PolarisIndexTableCellStyleOverride = () => {
// !important to override the default padding from a class that gets applied afterwards
const css = /*css*/ `
.Gadget_PolarisAutoTable_IndexTableCell {
padding: 0px !important;
}
`;
return <style>{css}</style>;
};

const disablePaginatedSelectAllButton = {
paginatedSelectAllActionText: "", // Empty string to hide the select all button. We only allow selection on the current page
};
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/use-table/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ export const getTableColumns = (spec: Pick<TableSpec, "fieldMetadataTree" | "tar
field: targetColumn.header,
type: "CustomRenderer",
sortable: false,
style: targetColumn.style,
});

continue;
}

const { header, field: columnPath, sortable } = getCellDetailColumnByColumnValue(targetColumn);
const { header, field: columnPath, sortable, style } = getCellDetailColumnByColumnValue(targetColumn);
const { firstField, targetField, isHasMany, isHasOneOrBelongsTo } = getFieldInformationByColumnPath(spec.fieldMetadataTree, columnPath);

const column: TableColumn = {
Expand All @@ -173,6 +174,7 @@ export const getTableColumns = (spec: Pick<TableSpec, "fieldMetadataTree" | "tar
field: columnPath,
type: targetField.fieldType,
sortable: isColumnSortable(targetField, sortable),
style,
};

// The column path doesn't specify a related field, so we need to get the default display field
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/use-table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export type TableColumn = {
includeTime?: boolean;
/** Custom render function */
render?: CustomCellRenderer;
/** Custom style for the cells in the column */
style?: React.CSSProperties;
};

export type TableRow = Record<string, ColumnValueType | ReactNode>;
Expand Down Expand Up @@ -101,6 +103,7 @@ export type RelatedFieldColumn = {
export type CustomCellColumn = {
header: string;
render: CustomCellRenderer;
style?: React.CSSProperties;
};

export type CustomCellRenderer = (props: { record: GadgetRecord<any>; index: number }) => ReactNode;
Expand All @@ -109,6 +112,7 @@ export type CellDetailColumn = {
header?: string;
field: string;
sortable?: boolean;
style?: React.CSSProperties;
};

export type FieldMetadataFragmentWithRelationshipConfig = FieldMetadataFragment & {
Expand Down

0 comments on commit ea71cd1

Please sign in to comment.