diff --git a/packages/canary-react/src/component-tests/IcDataTable/IcDataTable.cy.tsx b/packages/canary-react/src/component-tests/IcDataTable/IcDataTable.cy.tsx index 2de415190a..83738aaa51 100644 --- a/packages/canary-react/src/component-tests/IcDataTable/IcDataTable.cy.tsx +++ b/packages/canary-react/src/component-tests/IcDataTable/IcDataTable.cy.tsx @@ -87,6 +87,7 @@ const PAGINATION_GO_TO_PAGE_TEXT_FIELD_SELECTOR = ".go-to-page-holder ic-text-field"; const PAGINATION_GO_TO_PAGE_BUTTON_SELECTOR = ".go-to-page-holder ic-button"; const ITEMS_PER_PAGE_SELECTOR = ".items-per-page-input"; +const ACTION_ELEMENT = "action-element"; // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types export const BasicDataTable = (dataTableProps?: any): ReactElement => ( @@ -1074,6 +1075,31 @@ describe("IcDataTables", () => { }); }); + it("should run an event when clicked if the data prop contains actionOnClick key", () => { + mount( + + ); + + cy.spy(window.console, "log").as("spyWinConsoleLog"); + + cy.viewport(1024, 768); + + cy.checkHydrated(DATA_TABLE_SELECTOR); + + cy.findShadowEl(DATA_TABLE_SELECTOR, "td") + .eq(0) + .find("span") + .should(HAVE_CLASS, ACTION_ELEMENT) + .click(); + + cy.get("@spyWinConsoleLog").should("have.callCount", 1); + cy.get("@spyWinConsoleLog").should(HAVE_BEEN_CALLED_WITH, "hello"); + }); + it("should render an element in the table cell if the data prop contains the actionElement key", () => { mount( { > ); + cy.viewport(1024, 768); + cy.checkHydrated(DATA_TABLE_SELECTOR); cy.findShadowEl(DATA_TABLE_SELECTOR, "td") + .eq(0) .find("span") - .should(HAVE_CLASS, "action-element") + .should(HAVE_CLASS, ACTION_ELEMENT) .find("ic-button") .should("be.visible"); @@ -4419,4 +4448,4 @@ describe("IcDataTable visual regression tests in high contrast mode", () => { delay: 500, }); }); -}); +}); \ No newline at end of file diff --git a/packages/canary-react/src/stories/ic-data-table.stories.mdx b/packages/canary-react/src/stories/ic-data-table.stories.mdx index d5166cd25c..772fd1ca59 100644 --- a/packages/canary-react/src/stories/ic-data-table.stories.mdx +++ b/packages/canary-react/src/stories/ic-data-table.stories.mdx @@ -2081,7 +2081,7 @@ const DataTable = () => { ### Action Element -An example with action elements passed in via the data +Action Element and Action OnClick can be passed via the `data` prop to display in certain cells, this is beneficial when dealing with large sets of data. @@ -2115,6 +2115,7 @@ const DATA = [ firstName: { data: "Joe", actionElement: ` `, + actionOnClick: () => console.log('hello') }, lastName: "Bloggs", age: 30, diff --git a/packages/canary-web-components/src/components/ic-data-table/ic-data-table.stories.mdx b/packages/canary-web-components/src/components/ic-data-table/ic-data-table.stories.mdx index a520c452cf..669b005f21 100644 --- a/packages/canary-web-components/src/components/ic-data-table/ic-data-table.stories.mdx +++ b/packages/canary-web-components/src/components/ic-data-table/ic-data-table.stories.mdx @@ -1583,9 +1583,9 @@ To set the min and max width of a table cell, set the `table-layout` attribute t ``` -### Action Element +### Action Element and Action OnClick -The example below demonstrates action elements appearing in table cells after being passed in via the data. +The example below demonstrates action elements and Action OnClicks appearing in table cells after being passed in via the data. When dealing with Large sets of data, its easier to pass in the Elements and Onclick function and keeping the accessibility intact. {ActionElement()} @@ -1694,5 +1694,7 @@ export default DataTable; #### Development Area - {DevArea()} + + {DevArea()} + diff --git a/packages/canary-web-components/src/components/ic-data-table/ic-data-table.tsx b/packages/canary-web-components/src/components/ic-data-table/ic-data-table.tsx index b5d695de5e..c109b00de3 100644 --- a/packages/canary-web-components/src/components/ic-data-table/ic-data-table.tsx +++ b/packages/canary-web-components/src/components/ic-data-table/ic-data-table.tsx @@ -1429,6 +1429,12 @@ export class DataTable { this.handleClick(cell.actionOnClick) + : undefined + } > ) : ( @@ -1864,7 +1870,9 @@ export class DataTable { }); }); }; - + private handleClick = (callback: () => void) => { + return callback(); + }; render() { const { caption, diff --git a/packages/canary-web-components/src/components/ic-data-table/story-data.ts b/packages/canary-web-components/src/components/ic-data-table/story-data.ts index 3141a77094..8e50004f54 100644 --- a/packages/canary-web-components/src/components/ic-data-table/story-data.ts +++ b/packages/canary-web-components/src/components/ic-data-table/story-data.ts @@ -975,6 +975,9 @@ export const ACTION_DATA_ELEMENTS = [ firstName: { data: "Joe", actionElement: ` `, + actionOnClick: () => { + console.log("hello"); + }, }, lastName: "Bloggs", age: 31, diff --git a/packages/canary-web-components/src/components/ic-data-table/test/basic/__snapshots__/ic-data-table.spec.tsx.snap b/packages/canary-web-components/src/components/ic-data-table/test/basic/__snapshots__/ic-data-table.spec.tsx.snap index f68c7aad60..b04f272d39 100644 --- a/packages/canary-web-components/src/components/ic-data-table/test/basic/__snapshots__/ic-data-table.spec.tsx.snap +++ b/packages/canary-web-components/src/components/ic-data-table/test/basic/__snapshots__/ic-data-table.spec.tsx.snap @@ -6615,6 +6615,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ @@ -6645,6 +6650,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ @@ -6675,6 +6685,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ @@ -6705,6 +6720,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ @@ -6735,6 +6755,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ @@ -6765,6 +6790,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = ` + +
+ +
+ diff --git a/packages/canary-web-components/src/components/ic-data-table/test/basic/ic-data-table.spec.tsx b/packages/canary-web-components/src/components/ic-data-table/test/basic/ic-data-table.spec.tsx index a0df95ddcb..117410cfd4 100644 --- a/packages/canary-web-components/src/components/ic-data-table/test/basic/ic-data-table.spec.tsx +++ b/packages/canary-web-components/src/components/ic-data-table/test/basic/ic-data-table.spec.tsx @@ -375,6 +375,7 @@ const dataWithActionElement = [ name: { data: name1, actionElement: ` `, + actionOnClick: () => console.log("hello"), }, age: 36, department: "Accounts",