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

Table: Create table components #695

Merged
merged 35 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3844c8d
feat(table): add pharos table component
jialin-he Feb 23, 2024
ed0dc5b
feat(table): add more to tables
jialin-he Feb 27, 2024
ba2cba8
feat(table): implement page size dropdown
jialin-he Feb 29, 2024
b98a7d8
feat(table): add react storybook
jialin-he Mar 1, 2024
7376c7b
feat(table): add unit tests to table
jialin-he Mar 2, 2024
0bd9327
docs(table): add changeset
jialin-he Mar 2, 2024
fbed790
feat(table): update storybook
jialin-he Mar 2, 2024
0efbf1e
Merge branch 'develop' into feat/pharos-table
jialin-he Mar 2, 2024
d182e5a
feat(table): improve storybook reactivity
jialin-he Mar 2, 2024
b2612d0
feat(table): add accessibility update
jialin-he Mar 4, 2024
f3861a1
feat(table): use pharos color
jialin-he Mar 4, 2024
1a99376
feat(table): fix bug
jialin-he Mar 5, 2024
31f2164
feat(table): fix build issue
jialin-he Mar 5, 2024
878db09
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 5, 2024
1bcc071
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 5, 2024
a05a7e2
feat(table): fix pagination height and remove any type
jialin-he Mar 6, 2024
9dc90ce
feat(table): add unit test for start and end page number
jialin-he Mar 6, 2024
c124c8d
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he Mar 7, 2024
a54bfba
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he Mar 7, 2024
0a1f1fe
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he Mar 7, 2024
01f4343
feat(table): update row data type
jialin-he Mar 7, 2024
9bef87b
feat(table): update unit test
jialin-he Mar 12, 2024
10aa6bb
feat(table): add hide caption visually attribute
jialin-he Mar 12, 2024
50d1727
Merge branch 'develop' into feat/pharos-table
jialin-he Mar 12, 2024
7d8920f
feat(table): update visually hidden css
jialin-he Mar 12, 2024
0671f2d
feat(table): use hidden mixin
jialin-he Mar 12, 2024
10a15ef
feat(table): fix lint and bug
jialin-he Mar 12, 2024
1f1b2ac
feat(table): fix unit tests
jialin-he Mar 12, 2024
c38abda
Merge branch 'develop' into feat/pharos-table
jialin-he Mar 13, 2024
c135b75
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 13, 2024
e416547
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he Mar 13, 2024
4b6fcde
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 13, 2024
bf20c8e
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 13, 2024
b010390
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he Mar 13, 2024
2161b85
feat(table): update attribute name and add unit test
jialin-he Mar 13, 2024
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
5 changes: 5 additions & 0 deletions .changeset/orange-fireants-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ithaka/pharos': minor
---

Create table component
2 changes: 2 additions & 0 deletions .storybook/initComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
PharosSidenavSection,
PharosTabs,
PharosTab,
PharosTable,
PharosTabPanel,
PharosTextInput,
PharosTextarea,
Expand Down Expand Up @@ -86,6 +87,7 @@ registerComponents('storybook', [
PharosSidenavSection,
PharosTabs,
PharosTab,
PharosTable,
PharosTabPanel,
PharosTextInput,
PharosTextarea,
Expand Down
1 change: 1 addition & 0 deletions packages/pharos-site/initComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if (typeof window !== `undefined`) {
pharos.PharosSidenavSection,
pharos.PharosTabs,
pharos.PharosTab,
pharos.PharosTable,
pharos.PharosTabPanel,
pharos.PharosTextInput,
pharos.PharosTextarea,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { PharosTable } from '../../react-components';
import { configureDocsPage } from '@config/docsPageConfig';
import { PharosContext } from '../../utils/PharosContext';
import { defaultArgs } from './storyArgs';

export default {
title: 'Components/Table',
component: PharosTable,
decorators: [
(Story) => (
<PharosContext.Provider value={{ prefix: 'storybook' }}>
<Story />
</PharosContext.Provider>
),
],
parameters: {
docs: { page: configureDocsPage('table') },
options: { selectedPanel: 'addon-controls' },
},
};

export const Base = {
render: (args) => (
<PharosTable
columns={args.columns}
rowData={args.rowData}
showPagination={args.showPagination}
caption={'An example table'}
></PharosTable>
),
args: {
...defaultArgs,
showPagination: false,
},
};

export const WithPagination = {
render: (args) => (
<PharosTable
columns={args.columns}
rowData={args.rowData}
showPagination={args.showPagination}
totalResults={5}
pageSizeOptions={[2, 4]}
caption={'An example table'}
></PharosTable>
),
args: {
...defaultArgs,
showPagination: true,
},
};
45 changes: 45 additions & 0 deletions packages/pharos/src/components/table/pharos-table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@use '../../utils/scss/mixins';

:host {
display: inline-flex;
flex-direction: column;
}

.table {
border-collapse: collapse;

th {
border: 1px solid var(--pharos-color-marble-gray-base);
padding: var(--pharos-spacing-1-x);
}

td {
border: 1px solid var(--pharos-color-marble-gray-base);
padding: var(--pharos-spacing-1-x);
}
}

.table-controls {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: var(--pharos-spacing-one-half-x);

.item-per-page-wrapper {
display: flex;
align-items: center;
column-gap: var(--pharos-spacing-one-half-x);

.item-per-page-selector {
width: 75px;
}
}

.pagination {
height: var(--pharos-spacing-one-and-a-half-x);
}
}
Comment on lines +22 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we try to use a more BEM-like approach rather than deeply nested selectors. Because this isn't automated and is somewhat subjective currently, let's let it stand as a "TODO" for later 😄


.visually-hidden:not(:focus, :active) {
@include mixins.hidden;
}
174 changes: 174 additions & 0 deletions packages/pharos/src/components/table/pharos-table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { fixture, expect } from '@open-wc/testing';
import type { TemplateResult } from 'lit';

import { html } from 'lit/static-html.js';

import type { PharosTable } from './pharos-table';
import type { PharosLink } from '../link/pharos-link';
import type { PharosPagination } from '../pagination/pharos-pagination';
import type { PharosSelect } from '../select/pharos-select';

describe('pharos-table', () => {
daneah marked this conversation as resolved.
Show resolved Hide resolved
let component: PharosTable, componentWithPagination: PharosTable;
const columns = [
{
name: 'Item',
field: 'item',
},
{
name: 'Filename',
field: 'filename',
},
];
const rowData = [
{
item: 1,
filename: '12345.jpg',
expired_date: '2020-1-1',
created_on: '2010-1-1',
university: 'University of Michigan',
},
{
item: 2,
filename: '123456.jpg',
expired_date: '2020-1-1',
created_on: '2010-1-1',
university: 'University of Michigan',
},
];

beforeEach(async () => {
component = await fixture(html`
<test-pharos-table
.columns="${columns}"
.rowData="${rowData}"
.totalResults="${2}"
caption="test table"
>
</test-pharos-table>
`);

componentWithPagination = await fixture(html`
<test-pharos-table
.columns="${columns}"
.rowData="${rowData}"
.showPagination="${true}"
.totalResults="${2}"
.pageSizeOptions="${[1, 2]}"
caption="test table with pagination"
>
</test-pharos-table>
`);
});

it('is accessible', async () => {
await expect(component).to.be.accessible();
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to add a unit test like this one which verifies it throws an error when there is no caption?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

it('is accessible with pagination', async () => {
await expect(componentWithPagination).to.be.accessible();
});

it('has the correct number of rows', async () => {
const rows = Array.prototype.slice.call(
component.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(3);
});

it('renders rows according to page size', async () => {
const rows = Array.prototype.slice.call(
componentWithPagination.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(2);
});

it('shows correct page start number according to page size', async () => {
let pageNumber: HTMLElement | null =
componentWithPagination.renderRoot.querySelector(`.page-number-display`);
expect(pageNumber?.innerText).contains('Displaying 1-1 of 2');

const selectDropdown = componentWithPagination.renderRoot.querySelector(
`pharos-select`
) as PharosSelect;
selectDropdown['_select'].value = '2';
selectDropdown['_select'].dispatchEvent(new Event('change'));

await componentWithPagination.updateComplete;

pageNumber = componentWithPagination.renderRoot.querySelector(`.page-number-display`);
expect(pageNumber?.innerText).contains('Displaying 1-2 of 2');
});

it('updates correctly after page size selection', async () => {
let rows = Array.prototype.slice.call(
componentWithPagination.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(2);

const selectDropdown = componentWithPagination.renderRoot.querySelector(
`pharos-select`
) as PharosSelect;
selectDropdown['_select'].value = '2';
selectDropdown['_select'].dispatchEvent(new Event('change'));

await componentWithPagination.updateComplete;

rows = Array.prototype.slice.call(
componentWithPagination.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(3);
});

it('fires a custom event when going to previous and next page', async () => {
let prevWasFired = false;
let nextWasFired = false;
const handlePrevPage = (): void => {
prevWasFired = true;
};
const handleNextPage = (): void => {
nextWasFired = true;
};
componentWithPagination.addEventListener('pharos-table-prev-page', handlePrevPage);
componentWithPagination.addEventListener('pharos-table-next-page', handleNextPage);

const pagination = componentWithPagination.renderRoot.querySelector(
`pharos-pagination`
) as PharosPagination;
const nextPageLink = pagination.renderRoot.querySelector(`.next`) as PharosLink;
nextPageLink.click();
await componentWithPagination.updateComplete;

expect(nextWasFired).to.be.true;

const prevPageLink = pagination.renderRoot.querySelector(`.prev`) as PharosLink;
prevPageLink.click();
await componentWithPagination.updateComplete;

expect(prevWasFired).to.be.true;
});
});

it('throws an error if caption is not provided', async () => {
let errorThrown = false;
try {
await fixture(
html`<test-pharos-table
.columns="${[]}"
.rowData="${[]}"
.showPagination="${true}"
.totalResults="${2}"
.pageSizeOptions="${[1, 2]}"
>
</test-pharos-table> `
);
} catch (error) {
if (error instanceof Error) {
errorThrown = true;
expect(error?.message).to.be.equal(
'Table must have an accessible name. Please provide a caption for the table using the `caption` attribute. You can hide the caption visually by setting the `hide-caption-visually` property.'
);
}
}
expect(errorThrown).to.be.true;
});
Loading
Loading