Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onclick to action element #2957

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
Expand Down Expand Up @@ -1074,6 +1075,31 @@ describe("IcDataTables", () => {
});
});

it("should run an event when clicked if the data prop contains actionOnClick key", () => {
mount(
<IcDataTable
columns={COLS}
data={ACTION_DATA_ELEMENTS}
caption="Data tables"
></IcDataTable>
);

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(
<IcDataTable
Expand All @@ -1083,12 +1109,15 @@ describe("IcDataTables", () => {
></IcDataTable>
);

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");

Expand Down Expand Up @@ -4419,4 +4448,4 @@ describe("IcDataTable visual regression tests in high contrast mode", () => {
delay: 500,
});
});
});
});
3 changes: 2 additions & 1 deletion packages/canary-react/src/stories/ic-data-table.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<Canvas withSource="none">
<Story name="Action Element">
Expand Down Expand Up @@ -2115,6 +2115,7 @@ const DATA = [
firstName: {
data: "Joe",
actionElement: `<ic-button variant="icon" size="small aria-label="you can disable tooltips on icon buttons"> <svg aria-label="refresh button" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" > <path d="M0 0h24v24H0V0z" fill="none"></path> <path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"></path> </svg></ic-button>`,
actionOnClick: () => console.log('hello')
},
lastName: "Bloggs",
age: 30,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,9 +1583,9 @@ To set the min and max width of a table cell, set the `table-layout` attribute t
</script>
```

### 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.

<Canvas withSource="none">
<Story name="Action Element">{ActionElement()}</Story>
Expand Down Expand Up @@ -1694,5 +1694,7 @@ export default DataTable;
#### Development Area

<Canvas withSource="none">
<Story name="Dev Area">{DevArea()}</Story>
<Story parameters={{ loki: { skip: true } }} name="Dev Area">
{DevArea()}
</Story>
</Canvas>
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,12 @@ export class DataTable {
<span
class="action-element"
innerHTML={cellValue("actionElement")}
// eslint-disable-next-line react/jsx-no-bind
onClick={
cell.actionOnClick
? () => this.handleClick(cell.actionOnClick)
: undefined
}
></span>
</div>
) : (
Expand Down Expand Up @@ -1864,7 +1870,9 @@ export class DataTable {
});
});
};

private handleClick = (callback: () => void) => {
return callback();
};
render() {
const {
caption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,9 @@ export const ACTION_DATA_ELEMENTS = [
firstName: {
data: "Joe",
actionElement: `<ic-button variant="icon" size="small aria-label="you can disable tooltips on icon buttons"> <svg aria-label="refresh button" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000"> <path d="M0 0h24v24H0V0z" fill="none"></path> <path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"></path> </svg></ic-button>`,
actionOnClick: () => {
console.log("hello");
},
},
lastName: "Bloggs",
age: 31,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6615,6 +6615,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-0"></slot>
</div>
</td>
</tr>
<tr class="table-row">
<td class="table-cell">
Expand Down Expand Up @@ -6645,6 +6650,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-1"></slot>
</div>
</td>
</tr>
<tr class="table-row">
<td class="table-cell">
Expand Down Expand Up @@ -6675,6 +6685,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-2"></slot>
</div>
</td>
</tr>
<tr class="table-row">
<td class="table-cell">
Expand Down Expand Up @@ -6705,6 +6720,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-3"></slot>
</div>
</td>
</tr>
<tr class="table-row">
<td class="table-cell">
Expand Down Expand Up @@ -6735,6 +6755,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-4"></slot>
</div>
</td>
</tr>
<tr class="table-row">
<td class="table-cell">
Expand Down Expand Up @@ -6765,6 +6790,11 @@ exports[`ic-data-table should slot a custom element into a cell 1`] = `
</ic-typography>
</div>
</td>
<td class="table-cell with-overflow">
<div class="data-type-element">
<slot name="actions-5"></slot>
</div>
</td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ const dataWithActionElement = [
name: {
data: name1,
actionElement: `<ic-button size="small" variant="icon" aria-label="you can disable tooltips on icon buttons" disable-tooltip="true"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" > <path d="M0 0h24v24H0V0z" fill="none"></path> <path d="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"></path> </svg></ic-button>`,
actionOnClick: () => console.log("hello"),
},
age: 36,
department: "Accounts",
Expand Down
Loading