diff --git a/src/components/Table/Pagination.svelte b/src/components/Table/Pagination.svelte new file mode 100644 index 00000000..1d4e9648 --- /dev/null +++ b/src/components/Table/Pagination.svelte @@ -0,0 +1,132 @@ + + + + + diff --git a/src/components/Table/Table.stories.svelte b/src/components/Table/Table.stories.svelte index c15b6d4a..855a609d 100644 --- a/src/components/Table/Table.stories.svelte +++ b/src/components/Table/Table.stories.svelte @@ -9,6 +9,8 @@ // @ts-ignore import truncateDocs from './stories/docs/truncate.md?raw'; // @ts-ignore + import paginateDocs from './stories/docs/paginate.md?raw'; + // @ts-ignore import searchDocs from './stories/docs/search.md?raw'; // @ts-ignore import filterDocs from './stories/docs/filter.md?raw'; @@ -80,6 +82,18 @@ }}" /> +Reporters Without Borders", + }}" +/> + Reporters Without Borders", }}" /> diff --git a/src/components/Table/Table.svelte b/src/components/Table/Table.svelte index b61440a1..ce457be9 100644 --- a/src/components/Table/Table.svelte +++ b/src/components/Table/Table.svelte @@ -132,10 +132,10 @@ /** Import local helpers */ import Block from '../Block/Block.svelte'; + import Pagination from './Pagination.svelte'; import { filterArray, paginateArray, - numberWithCommas, uniqueAttr, isNumeric, } from './utils.js'; @@ -150,15 +150,13 @@ let filterValue = ''; $: filteredData = filterArray(data, searchText, filterField, filterValue); $: sortedData = sortArray(filteredData, sortField, sortDirection); - $: currentPageData = () => { - if (truncated) { - return showAll ? sortedData : sortedData.slice(0, truncateLength + 1); - } else if (paginated) { - return paginateArray(sortedData, pageSize, pageNumber); - } else { - return sortedData; - } - }; + $: currentPageData = truncated + ? showAll + ? sortedData + : sortedData.slice(0, truncateLength + 1) + : paginated + ? paginateArray(sortedData, pageSize, pageNumber) + : sortedData; // Estimate the text alignment of our fields. Strings go left. Numbers go right. function getAlignment(value) { @@ -191,18 +189,6 @@ sortDirection = sortDirection === 'ascending' ? 'descending' : 'ascending'; } - function goToPreviousPage() { - if (pageNumber > 1) { - pageNumber -= 1; - } - } - - function goToNextPage() { - if (pageNumber < Math.ceil(filteredData.length / pageSize)) { - pageNumber += 1; - } - } - function sortArray(array, column, direction) { if (!sortable) return array; return array.sort((a, b) => { @@ -318,7 +304,7 @@ - {#each currentPageData() as item, idx} + {#each currentPageData as item, idx} {#each includedFields as field} {/if} {#if paginated} - - {/if} + {/if} @@ -554,55 +506,4 @@ cursor: pointer; } } - - nav.pagination { - display: flex; - justify-content: center; - align-items: center; - margin-top: 1rem; - font-size: 1rem; - font-family: $font-family-display; - font-weight: 400; - button { - padding: 5px 10px; - border: 1px solid; - border-color: $tr-contrast-grey; - border-radius: 8px; - background: $white; - color: $tr-medium-grey; - cursor: pointer; - width: 40px; - height: 40px; - &:disabled { - border-color: $tr-light-grey; - color: $tr-light-grey; - cursor: default; - } - .icon-wrapper { - display: flex; - align-items: center; - justify-content: center; - white-space: nowrap; - transition: all 0.15s ease; - .icon { - height: 1rem; - width: 1rem; - fill: currentColor; - } - } - } - .label { - margin: 0 1rem; - } - } - - .visually-hidden { - clip: rect(0 0 0 0); - clip-path: inset(50%); - height: 1px; - overflow: hidden; - position: absolute; - white-space: nowrap; - width: 1px; - } diff --git a/src/components/Table/stories/docs/paginate.md b/src/components/Table/stories/docs/paginate.md new file mode 100644 index 00000000..6fb7ad3a --- /dev/null +++ b/src/components/Table/stories/docs/paginate.md @@ -0,0 +1,13 @@ +When you table has lots of rows you should consider breaking it up into pages. This can be done by setting the `paginated` option. + +When it is enabled, readers can leaf through the data using a set of buttons below the table. By default there are 25 records per page. You can change the number by adjusting the `pageSize` option. + +This is a good option when publishing large tables for readers to explore. It works well with interactive features like searching and filters. + +```svelte + +``` diff --git a/src/components/Table/utils.js b/src/components/Table/utils.js index a0aa825c..ac5f8095 100644 --- a/src/components/Table/utils.js +++ b/src/components/Table/utils.js @@ -16,12 +16,6 @@ export function paginateArray(array, pageSize, pageNumber) { return array.slice((pageNumber - 1) * pageSize, pageNumber * pageSize); } -export function numberWithCommas(n) { - const parts = n.toString().split('.'); - parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); - return parts.join('.'); -} - export function uniqueAttr(array, attr) { return array.map((e) => e[attr]).filter(unique); }