Skip to content

Commit

Permalink
feat: table component
Browse files Browse the repository at this point in the history
* feat: added sticky last column
* feat: added full story
  • Loading branch information
bariskaraca committed Jan 16, 2024
1 parent bdbfd56 commit 96e67ed
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 116 deletions.
141 changes: 83 additions & 58 deletions src/components/table/bl-table.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {html} from 'lit';
import {Meta, Canvas, ArgsTable, Story} from '@storybook/addon-docs';
import {userEvent} from '@storybook/testing-library';
import { UPDATE_STORY_ARGS } from "@storybook/core-events";

<Meta
title="Components/Table/bl-table"
Expand All @@ -17,53 +17,79 @@ import {userEvent} from '@storybook/testing-library';
}}
/>

export const TableTemplate = (args) => html`
<div style="height: 400px;">
<bl-table
select-value=[0,1]
?selectable=${true}
?multiple=${false}
?sortable=${true}
?sticky-first-column=${true}
?sticky-last-column=${false}
@bl-table-sort=${console.log}
>
<bl-table-header ?sticky=${args.stickyHeader}>
<bl-table-row>
${args.headers.map((header) =>
html`<bl-table-header-cell
?sortable=${header.sortable}
sort-key='${header.sortKey}'
style='${header.width?'--bl-table-header-cell-width: '+header.width+';': ''} ${header.minWidth?'--bl-table-header-cell-min-width: '+header.minWidth+';': ''}'
>
${header.text}
</bl-table-header-cell>`
)}
</bl-table-row>
</bl-table-header>
<bl-table-body>
${args.data.map((row) =>
html`
<bl-table-row>
${args.headers.map((header, col_idx) =>
html`
<bl-table-cell ?disable-selection=${col_idx===0&&row.disabled}>
${row[header.key]}
</bl-table-cell>
`
)}
</bl-table-row>
`
)}
</bl-table-body>
</bl-table>
</div>
`;
export const TableTemplate = (args, {id}) => {
const updateArgs = (_args)=>{
window.__STORYBOOK_ADDONS_CHANNEL__.emit(UPDATE_STORY_ARGS, {
storyId: id,
updatedArgs: _args,
});

}
let {data, headers, selectValue} = args;
const onSort = (event) => {
const [sortKey, sortDirection] = event.detail;
console.log("onSort", event.detail);
updateArgs({
...args,
data: data.sort((a, b) => {
return a[sortKey].toString().localeCompare(b[sortKey], 'tr', {sensitivity: 'base'}) * (sortDirection === 'asc' ? -1 : 1);
}),
});

export const focusSecondOption = async ({}) => {
await userEvent.tab();
await userEvent.keyboard('{ArrowRight}');
console.log(data)
}
const onRowSelect = (event) => {
console.log("onRowSelect", event.detail);
updateArgs({
...args,
selectValue: event.detail,
});
}

return html`
<div style="height: 400px;">
<bl-table
select-value=${JSON.stringify(selectValue)}
?selectable=${true}
?multiple=${true}
?sortable=${true}
?sticky-first-column=${true}
?sticky-last-column=${true}
@bl-table-sort=${onSort}
@bl-table-row-select=${onRowSelect}
>
<bl-table-header ?sticky=${args.stickyHeader}>
<bl-table-row>
${args.headers.map((header) =>
html`
<bl-table-header-cell
?sortable=${header.sortable}
sort-key='${header.sortKey}'
style='${header.width ? '--bl-table-header-cell-width: ' + header.width + ';' : ''} ${header.minWidth ? '--bl-table-header-cell-min-width: ' + header.minWidth + ';' : ''}'
>
${header.text}
</bl-table-header-cell>`
)}
</bl-table-row>
</bl-table-header>
<bl-table-body>
${data.map((row) =>
html`
<bl-table-row selection-key="row-${row.id}">
${headers.map((header, col_idx) =>
html`
<bl-table-cell ?disable-selection=${col_idx === 0 && row.disabled}>
${row[header.key]}
</bl-table-cell>
`
)}
</bl-table-row>
`
)}
</bl-table-body>
</bl-table>
</div>
`;
}

# Table Component
Expand All @@ -81,16 +107,16 @@ If you set a list of `values`, options with those values will be selected.
{key: "gender", text: "Gender", width: '85px'},
{key: "ip_address", text: "IP Address", width: '90px', sticky: 'right'},
],
data: Array(1)
.fill([
{
id: 1,
first_name: "Antonella",
last_name: "Bellefonte",
email: "[email protected]",
gender: "Female",
ip_address: "193.108.174.118"
},
selectValue: ["row-1","row-4"],
data: [
{
id: 1,
first_name: "Antonella",
last_name: "Bellefonte",
email: "[email protected]",
gender: "Female",
ip_address: "193.108.174.118"
},
{id:2,first_name:"Wash",last_name:"Carnson",email:"[email protected]",gender:"Male",ip_address:"255.169.128.60"},
{id:3,first_name:"Betteanne",last_name:"Cowgill",email:"[email protected]",gender:"Female",ip_address:"235.237.3.233", disabled: true},
{id:4,first_name:"Lilith",last_name:"Astbury",email:"[email protected]",gender:"Female",ip_address:"46.41.60.65"},
Expand All @@ -110,8 +136,7 @@ If you set a list of `values`, options with those values will be selected.
{id:18,first_name:"Germana",last_name:"Downing",email:"[email protected]",gender:"Female",ip_address:"55.83.134.58"},
{id:19,first_name:"Kizzie",last_name:"Pyzer",email:"[email protected]",gender:"Female",ip_address:"84.92.93.248"},
{id:20,first_name:"Almire",last_name:"Slinn",email:"[email protected]",gender:"Female",ip_address:"195.56.102.224"},
])
.reduce((a, b)=>a.concat(b), []),
],
stickyHeader:true
}}>
{TableTemplate.bind({})}
Expand Down
33 changes: 18 additions & 15 deletions src/components/table/bl-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe("bl-table", () => {
//when
const el = await fixture<BlTable>(
html`
<bl-table selectable select-value="[0]">
<bl-table selectable select-value="${JSON.stringify(["row-1"])}">
<bl-table-header >
<bl-table-row>
<bl-table-header-cell>
Expand All @@ -370,7 +370,7 @@ describe("bl-table", () => {
</bl-table-row>
</bl-table-header>
<bl-table-body>
<bl-table-row>
<bl-table-row selection-key="row-1">
<bl-table-cell>
1
</bl-table-cell>
Expand Down Expand Up @@ -881,7 +881,7 @@ describe("bl-table", () => {
</bl-table-row>
</bl-table-header>
<bl-table-body>
<bl-table-row>
<bl-table-row selection-key="row-1">
<bl-table-cell>
1
</bl-table-cell>
Expand All @@ -901,7 +901,7 @@ describe("bl-table", () => {
193.108.174.118
</bl-table-cell>
</bl-table-row>
<bl-table-row>
<bl-table-row selection-key="row-2">
<bl-table-cell>
2
</bl-table-cell>
Expand Down Expand Up @@ -939,14 +939,17 @@ describe("bl-table", () => {
const ev = await oneEvent(el, "bl-table-row-select");

expect(ev).to.exist;
expect(ev.detail).to.be.deep.equal([0,1]);
expect(ev.detail).to.be.deep.equal([
"row-1",
"row-2",
]);
});

it("should fire bl-table-row-select event when user unchecked on checkbox in header row", async () => {
const el = await fixture<BlTable>(
html`
<bl-table
select-value="[0,1]"
select-value="${JSON.stringify(["row-1", "row-2"])}"
selectable
multiple
>
Expand All @@ -973,7 +976,7 @@ describe("bl-table", () => {
</bl-table-row>
</bl-table-header>
<bl-table-body>
<bl-table-row>
<bl-table-row selection-key="row-1">
<bl-table-cell>
1
</bl-table-cell>
Expand All @@ -993,7 +996,7 @@ describe("bl-table", () => {
193.108.174.118
</bl-table-cell>
</bl-table-row>
<bl-table-row>
<bl-table-row selection-key="row-2">
<bl-table-cell>
2
</bl-table-cell>
Expand Down Expand Up @@ -1064,7 +1067,7 @@ describe("bl-table", () => {
</bl-table-row>
</bl-table-header>
<bl-table-body>
<bl-table-row>
<bl-table-row selection-key="row-1">
<bl-table-cell>
1
</bl-table-cell>
Expand All @@ -1084,7 +1087,7 @@ describe("bl-table", () => {
193.108.174.118
</bl-table-cell>
</bl-table-row>
<bl-table-row>
<bl-table-row selection-key="row-2">
<bl-table-cell>
2
</bl-table-cell>
Expand Down Expand Up @@ -1122,14 +1125,14 @@ describe("bl-table", () => {
const ev = await oneEvent(el, "bl-table-row-select");

expect(ev).to.exist;
expect(ev.detail).to.be.deep.equal([0]);
expect(ev.detail).to.be.deep.equal(["row-1"]);
});

it("should fire bl-table-row-select event when user unchecked on checkbox in first table row", async () => {
const el = await fixture<BlTable>(
html`
<bl-table
select-value="[0,1]"
select-value="${JSON.stringify(["row-1", "row-2"])}"
selectable
multiple
>
Expand All @@ -1156,7 +1159,7 @@ describe("bl-table", () => {
</bl-table-row>
</bl-table-header>
<bl-table-body>
<bl-table-row>
<bl-table-row selection-key="row-1">
<bl-table-cell>
1
</bl-table-cell>
Expand All @@ -1176,7 +1179,7 @@ describe("bl-table", () => {
193.108.174.118
</bl-table-cell>
</bl-table-row>
<bl-table-row>
<bl-table-row selection-key="row-2">
<bl-table-cell>
2
</bl-table-cell>
Expand Down Expand Up @@ -1214,7 +1217,7 @@ describe("bl-table", () => {
const ev = await oneEvent(el, "bl-table-row-select");

expect(ev).to.exist;
expect(ev.detail).to.be.deep.equal([1]);
expect(ev.detail).to.be.deep.equal(["row-2"]);
});
});
});
Loading

0 comments on commit 96e67ed

Please sign in to comment.