-
Notifications
You must be signed in to change notification settings - Fork 16
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
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 ed0dc5b
feat(table): add more to tables
jialin-he ba2cba8
feat(table): implement page size dropdown
jialin-he b98a7d8
feat(table): add react storybook
jialin-he 7376c7b
feat(table): add unit tests to table
jialin-he 0bd9327
docs(table): add changeset
jialin-he fbed790
feat(table): update storybook
jialin-he 0efbf1e
Merge branch 'develop' into feat/pharos-table
jialin-he d182e5a
feat(table): improve storybook reactivity
jialin-he b2612d0
feat(table): add accessibility update
jialin-he f3861a1
feat(table): use pharos color
jialin-he 1a99376
feat(table): fix bug
jialin-he 31f2164
feat(table): fix build issue
jialin-he 878db09
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he 1bcc071
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he a05a7e2
feat(table): fix pagination height and remove any type
jialin-he 9dc90ce
feat(table): add unit test for start and end page number
jialin-he c124c8d
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he a54bfba
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he 0a1f1fe
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he 01f4343
feat(table): update row data type
jialin-he 9bef87b
feat(table): update unit test
jialin-he 10aa6bb
feat(table): add hide caption visually attribute
jialin-he 50d1727
Merge branch 'develop' into feat/pharos-table
jialin-he 7d8920f
feat(table): update visually hidden css
jialin-he 0671f2d
feat(table): use hidden mixin
jialin-he 10a15ef
feat(table): fix lint and bug
jialin-he 1f1b2ac
feat(table): fix unit tests
jialin-he c38abda
Merge branch 'develop' into feat/pharos-table
jialin-he c135b75
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he e416547
Update packages/pharos/src/components/table/pharos-table.test.ts
jialin-he 4b6fcde
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he bf20c8e
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he b010390
Update packages/pharos/src/components/table/pharos-table.ts
jialin-he 2161b85
feat(table): update attribute name and add unit test
jialin-he File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ithaka/pharos': minor | ||
--- | ||
|
||
Create table component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/pharos/src/components/table/PharosTable.react.stories.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
.visually-hidden:not(:focus, :active) { | ||
@include mixins.hidden; | ||
} |
174 changes: 174 additions & 0 deletions
174
packages/pharos/src/components/table/pharos-table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😄