diff --git a/packages/react/.changeset/swift-birds-tap.md b/packages/react/.changeset/swift-birds-tap.md new file mode 100644 index 000000000..12f6a272f --- /dev/null +++ b/packages/react/.changeset/swift-birds-tap.md @@ -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 }) =>
{record.name}
, + style: { backgroundColor: "blue" }, + }, + ]; + + return ; +}; +``` diff --git a/packages/react/spec/auto/PolarisAutoTable.stories.jsx b/packages/react/spec/auto/PolarisAutoTable.stories.jsx index d0602de3e..7dfc92ca7 100644 --- a/packages/react/spec/auto/PolarisAutoTable.stories.jsx +++ b/packages/react/spec/auto/PolarisAutoTable.stories.jsx @@ -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 (
diff --git a/packages/react/spec/auto/hooks/helpers.spec.ts b/packages/react/spec/auto/hooks/helpers.spec.ts index e5d203f39..3796ce2a8 100644 --- a/packages/react/spec/auto/hooks/helpers.spec.ts +++ b/packages/react/spec/auto/hooks/helpers.spec.ts @@ -346,6 +346,7 @@ describe("helper functions for useTable hook", () => { "header": "Name", "identifier": "name", "sortable": true, + "style": undefined, "type": "String", }, { @@ -353,6 +354,7 @@ describe("helper functions for useTable hook", () => { "header": "Age", "identifier": "age", "sortable": true, + "style": undefined, "type": "Number", }, ] @@ -385,6 +387,7 @@ describe("helper functions for useTable hook", () => { "header": "Some cool name", "identifier": "name", "sortable": true, + "style": undefined, "type": "String", }, { @@ -392,6 +395,7 @@ describe("helper functions for useTable hook", () => { "header": "Age", "identifier": "age", "sortable": false, + "style": undefined, "type": "Number", }, ] @@ -412,6 +416,7 @@ describe("helper functions for useTable hook", () => { "header": "Address", "identifier": "address", "sortable": true, + "style": undefined, "type": "String", }, { @@ -420,6 +425,7 @@ describe("helper functions for useTable hook", () => { "identifier": "owner", "relationshipType": "HasOne", "sortable": true, + "style": undefined, "type": "String", }, { @@ -428,6 +434,7 @@ describe("helper functions for useTable hook", () => { "identifier": "products", "relationshipType": "HasMany", "sortable": true, + "style": undefined, "type": "String", }, { @@ -436,6 +443,7 @@ describe("helper functions for useTable hook", () => { "identifier": "manufacturer", "relationshipType": "BelongsTo", "sortable": true, + "style": undefined, "type": "String", }, ] @@ -455,6 +463,7 @@ describe("helper functions for useTable hook", () => { "header": "Address", "identifier": "address", "sortable": true, + "style": undefined, "type": "String", }, { @@ -463,6 +472,7 @@ describe("helper functions for useTable hook", () => { "identifier": "owner.name", "relationshipType": "HasOne", "sortable": true, + "style": undefined, "type": "String", }, { @@ -471,6 +481,7 @@ describe("helper functions for useTable hook", () => { "identifier": "products.edges.node.sku", "relationshipType": "HasMany", "sortable": true, + "style": undefined, "type": "String", }, { @@ -479,6 +490,7 @@ describe("helper functions for useTable hook", () => { "identifier": "manufacturer.country", "relationshipType": "BelongsTo", "sortable": true, + "style": undefined, "type": "String", }, ] diff --git a/packages/react/spec/auto/hooks/useTable.spec.tsx b/packages/react/spec/auto/hooks/useTable.spec.tsx index baf5237e1..619b5619a 100644 --- a/packages/react/spec/auto/hooks/useTable.spec.tsx +++ b/packages/react/spec/auto/hooks/useTable.spec.tsx @@ -126,6 +126,7 @@ describe("useTable hook", () => { "header": "Id", "identifier": "id", "sortable": false, + "style": undefined, "type": "ID", }, { @@ -133,6 +134,7 @@ describe("useTable hook", () => { "header": "Name", "identifier": "name", "sortable": true, + "style": undefined, "type": "String", }, { @@ -140,6 +142,7 @@ describe("useTable hook", () => { "header": "Inventory count", "identifier": "inventoryCount", "sortable": true, + "style": undefined, "type": "Number", }, { @@ -147,6 +150,7 @@ describe("useTable hook", () => { "header": "Anything", "identifier": "anything", "sortable": true, + "style": undefined, "type": "JSON", }, { @@ -154,6 +158,7 @@ describe("useTable hook", () => { "header": "Description", "identifier": "description", "sortable": true, + "style": undefined, "type": "RichText", }, { @@ -161,6 +166,7 @@ describe("useTable hook", () => { "header": "Category", "identifier": "category", "sortable": true, + "style": undefined, "type": "Enum", }, { @@ -169,6 +175,7 @@ describe("useTable hook", () => { "identifier": "startsAt", "includeTime": undefined, "sortable": true, + "style": undefined, "type": "DateTime", }, { @@ -176,6 +183,7 @@ describe("useTable hook", () => { "header": "Is checked", "identifier": "isChecked", "sortable": true, + "style": undefined, "type": "Boolean", }, { @@ -183,6 +191,7 @@ describe("useTable hook", () => { "header": "Metafields", "identifier": "metafields", "sortable": true, + "style": undefined, "type": "JSON", }, { @@ -190,6 +199,7 @@ describe("useTable hook", () => { "header": "Roles", "identifier": "roles", "sortable": false, + "style": undefined, "type": "RoleAssignments", }, { @@ -198,6 +208,7 @@ describe("useTable hook", () => { "identifier": "birthday", "includeTime": undefined, "sortable": true, + "style": undefined, "type": "DateTime", }, { @@ -205,6 +216,7 @@ describe("useTable hook", () => { "header": "Color", "identifier": "color", "sortable": true, + "style": undefined, "type": "Color", }, { @@ -212,6 +224,7 @@ describe("useTable hook", () => { "header": "Secret key", "identifier": "secretKey", "sortable": false, + "style": undefined, "type": "EncryptedString", }, { @@ -219,6 +232,7 @@ describe("useTable hook", () => { "header": "Must be long string", "identifier": "mustBeLongString", "sortable": true, + "style": undefined, "type": "String", }, ] @@ -526,6 +540,7 @@ describe("useTable hook", () => { "header": "Name", "identifier": "name", "sortable": true, + "style": undefined, "type": "String", }, { @@ -534,6 +549,7 @@ describe("useTable hook", () => { "identifier": "000-000-000-000-1", "render": [Function], "sortable": false, + "style": undefined, "type": "CustomRenderer", }, ] diff --git a/packages/react/src/auto/polaris/PolarisAutoTable.tsx b/packages/react/src/auto/polaris/PolarisAutoTable.tsx index 349a4fe44..8ca6f4624 100644 --- a/packages/react/src/auto/polaris/PolarisAutoTable.tsx +++ b/packages/react/src/auto/polaris/PolarisAutoTable.tsx @@ -223,6 +223,8 @@ const PolarisAutoTableComponent = < hasZebraStriping={props.hasZebraStriping} condensed={props.condensed} > + + {rows && columns && rows.map((row, index) => { @@ -236,8 +238,8 @@ const PolarisAutoTableComponent = < selected={selection.recordIds.includes(row.id as string)} > {columns.map((column) => ( - -
+ +
{column.type == "CustomRenderer" ? ( (row[column.identifier] as ReactNode) ) : ( @@ -251,10 +253,29 @@ const PolarisAutoTableComponent = < })} + ); }; +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 ; +}; + const disablePaginatedSelectAllButton = { paginatedSelectAllActionText: "", // Empty string to hide the select all button. We only allow selection on the current page }; diff --git a/packages/react/src/use-table/helpers.tsx b/packages/react/src/use-table/helpers.tsx index 22291cd3f..53370c686 100644 --- a/packages/react/src/use-table/helpers.tsx +++ b/packages/react/src/use-table/helpers.tsx @@ -159,12 +159,13 @@ export const getTableColumns = (spec: Pick; @@ -101,6 +103,7 @@ export type RelatedFieldColumn = { export type CustomCellColumn = { header: string; render: CustomCellRenderer; + style?: React.CSSProperties; }; export type CustomCellRenderer = (props: { record: GadgetRecord; index: number }) => ReactNode; @@ -109,6 +112,7 @@ export type CellDetailColumn = { header?: string; field: string; sortable?: boolean; + style?: React.CSSProperties; }; export type FieldMetadataFragmentWithRelationshipConfig = FieldMetadataFragment & {