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 1 commit
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
Prev Previous commit
Next Next commit
feat(table): add unit tests to table
  • Loading branch information
jialin-he committed Mar 2, 2024
commit 7376c7b7d0c64d005ec02e8e4f1c06baa702457e
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
135 changes: 105 additions & 30 deletions packages/pharos/src/components/table/pharos-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,124 @@ 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;
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 () => {
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',
},
];
component = await fixture(html`
<test-pharos-table .columns="${columns}" .rowData="${rowData}"> </test-pharos-table>
<test-pharos-table .columns="${columns}" .rowData="${rowData}" .totalResults="${2}">
</test-pharos-table>
`);

componentWithPagination = await fixture(html`
<test-pharos-table
.columns="${columns}"
.rowData="${rowData}"
.hidePagination="${false}"
.totalResults="${2}"
.pageSizeOptions="${[1, 2]}"
>
</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('has 2 rows', async () => {
const rows = Array.prototype.slice.call(component.querySelectorAll(`tr`)) as TemplateResult[];
it('is accessible with pagination', async () => {
await expect(componentWithPagination).to.be.accessible();
});

it('has 3 rows', async () => {
const rows = Array.prototype.slice.call(
component.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(3);
});
jialin-he marked this conversation as resolved.
Show resolved Hide resolved

it('render rows according to page size', async () => {
jialin-he marked this conversation as resolved.
Show resolved Hide resolved
const rows = Array.prototype.slice.call(
componentWithPagination.renderRoot.querySelectorAll(`tr`)
) as TemplateResult[];
expect(rows.length).to.be.eq(2);
});

it('update table after page size selection', async () => {
jialin-he marked this conversation as resolved.
Show resolved Hide resolved
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 pwhen go to prev and next page', async () => {
jialin-he marked this conversation as resolved.
Show resolved Hide resolved
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;
});
});
21 changes: 15 additions & 6 deletions packages/pharos/src/components/table/pharos-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,36 @@ export class PharosTable extends ScopedRegistryMixin(PharosElement) {
@property({ type: Boolean, reflect: true, attribute: 'hide-pagination' })
public hidePagination: boolean = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Might just be just personal opinion, but thoughts on making this showPagination instead and defaulting it to false? Setting hidePagination="false" to show the pagination seems like a more complicated mental model than just showPagination="true" or showPagination="false" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I feel showPagination makes more sense, updated!


@property({ type: Number, reflect: true, attribute: 'total-number' })
@property({ type: Number, reflect: true, attribute: 'total-results' })
public totalResults: number = 0;

@property({ type: Array, reflect: true, attribute: 'page-size-options' })
public pageSizeOptions: number[] = [50, 100];

@state()
private _pageSize = 0;
private _pageSize = 50;

@state()
private _currentPage = 1;

protected override firstUpdated(): void {
this._pageSize = this.hidePagination ? this.rowData.length : this.pageSizeOptions[0];
this.totalResults = this.hidePagination ? this.rowData.length : this.totalResults;
this.totalResults =
this.hidePagination || !this.totalResults ? this.rowData.length : this.totalResults;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we care about hidePagination here?

Copy link
Contributor Author

@jialin-he jialin-he Mar 13, 2024

Choose a reason for hiding this comment

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

Oh I guess we don't need it here!

}

protected override updated(): void {
this._pageSize = this.hidePagination ? this.rowData.length : this._pageSize;
this.totalResults = this.hidePagination ? this.rowData.length : this.totalResults;
this.totalResults =
this.hidePagination || !this.totalResults ? this.rowData.length : this.totalResults;
}

public static override get styles(): CSSResultArray {
return [tableStyles];
}

private _onPageSizeChange(event: Event): void {
this._pageSize = Number((event.composedPath()[0] as Element).value);
this._pageSize = Number((event.composedPath()[0] as HTMLInputElement).value);
}

private _pageStartNumber(): number {
Expand Down Expand Up @@ -145,7 +147,14 @@ export class PharosTable extends ScopedRegistryMixin(PharosElement) {
? html`<div class="table-controls">
<div class="item-per-page-wrapper">
<span>Items per page</span>
<pharos-select class="item-per-page-selector" @change=${this._onPageSizeChange}>
<pharos-select
required
hideLabel
name="pharos-table-page-size-select"
class="item-per-page-selector"
@change=${this._onPageSizeChange}
>
<span slot="label">page size</span>
${this._renderPageSizeOptions()}
</pharos-select>
<span
Expand Down
Loading