-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/sc-46253/sdk-table-pagination (#229)
* feat: add pagination components and stories
- Loading branch information
1 parent
8c8a639
commit 3592c20
Showing
35 changed files
with
40,939 additions
and
18,528 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
56,984 changes: 38,854 additions & 18,130 deletions
56,984
packages/visualizations-react/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
108 changes: 108 additions & 0 deletions
108
packages/visualizations-react/stories/Table/Pagination.stories.tsx
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,108 @@ | ||
import React, { useState } from 'react'; | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
import type { TableProps } from '@opendatasoft/visualizations'; | ||
import { Table } from '../../src'; | ||
import value from './data'; | ||
import options from './options'; | ||
import './pagination.css'; | ||
|
||
const meta: ComponentMeta<typeof Table> = { | ||
title: 'Table/Pagination', | ||
component: Table, | ||
}; | ||
export default meta; | ||
|
||
const data: TableProps['data'] = { | ||
value, | ||
loading: false, | ||
}; | ||
|
||
const sliceDataX = (numberPerPage: number) => (fullData: TableProps['data'], page: number) => { | ||
const startIndex = (page - 1) * numberPerPage; | ||
return fullData.value?.slice(startIndex, startIndex + numberPerPage); | ||
}; | ||
|
||
const makeTemplate = (numberPerPage: number) => { | ||
const sliceData = sliceDataX(numberPerPage); | ||
const PaginatedTemplate: ComponentStory<typeof Table> = args => { | ||
const { data: rawData, options: paginatedOptions } = args; | ||
const { pagination } = paginatedOptions; | ||
const { initial = 0 } = pagination || {}; | ||
const [records, setRecords] = useState(sliceData(rawData, initial || 1)); | ||
if (paginatedOptions.pagination && rawData.value) { | ||
paginatedOptions.pagination.onChangePage = async (page: number) => { | ||
await setTimeout(() => { | ||
setRecords(sliceData(rawData, page)); | ||
}, 300); | ||
}; | ||
} | ||
|
||
const paginatedData = { value: records, isLoading: false }; | ||
return <Table data={paginatedData} options={paginatedOptions} />; | ||
}; | ||
return PaginatedTemplate; | ||
}; | ||
|
||
const paginatedOptions = { | ||
...options, | ||
pagination: { | ||
initial: 2, | ||
recordsPerPage: 3, | ||
totalRecords: value.length, | ||
onChangePage: () => {}, // set in template | ||
}, | ||
}; | ||
const PaginatedTemplate = makeTemplate(3); | ||
export const Paginated = PaginatedTemplate.bind({}); | ||
Paginated.args = { | ||
data, | ||
options: paginatedOptions, | ||
}; | ||
|
||
const longPagesOptions = { | ||
...options, | ||
pagination: { | ||
initial: 1, | ||
recordsPerPage: 10, | ||
totalRecords: value.length, | ||
onChangePage: () => {}, // set in template | ||
}, | ||
}; | ||
const LongPaginatedTemplate = makeTemplate(10); | ||
export const LongPages = LongPaginatedTemplate.bind({}); | ||
LongPages.args = { | ||
data, | ||
options: longPagesOptions, | ||
}; | ||
|
||
const shortPagesOptions = { | ||
...options, | ||
pagination: { | ||
initial: 1, | ||
recordsPerPage: 2, | ||
totalRecords: value.length, | ||
onChangePage: () => {}, // set in template | ||
}, | ||
}; | ||
const ShortPaginatedTemplate = makeTemplate(2); | ||
export const ShortPages = ShortPaginatedTemplate.bind({}); | ||
ShortPages.args = { | ||
data, | ||
options: shortPagesOptions, | ||
}; | ||
|
||
|
||
const StyledPaginated: ComponentStory<typeof Table> = args => ( | ||
<div className="custom-pagination"> | ||
<PaginatedTemplate {...args} /> | ||
</div> | ||
); | ||
|
||
export const CustomStyle = StyledPaginated.bind({}); | ||
CustomStyle.args = { | ||
data, | ||
options: { | ||
...paginatedOptions, | ||
unstyled: 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
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,62 @@ | ||
import type { Column, TableOptions } from '@opendatasoft/visualizations'; | ||
|
||
export const columns: Column[] = [ | ||
{ | ||
title: 'Title', | ||
key: 'title', | ||
dataFormat: 'short-text', | ||
}, | ||
{ | ||
title: 'Price', | ||
key: 'price', | ||
dataFormat: 'number', | ||
options: { | ||
style: 'currency', | ||
currency: 'EUR', | ||
}, | ||
}, | ||
{ | ||
title: 'Content', | ||
key: 'content', | ||
dataFormat: 'long-text', | ||
}, | ||
{ | ||
title: 'Published date', | ||
key: 'datePublished', | ||
dataFormat: 'date', | ||
options: { dateStyle: 'full' }, | ||
}, | ||
{ | ||
title: 'Featured', | ||
key: 'isFeatured', | ||
dataFormat: 'boolean', | ||
}, | ||
{ | ||
title: 'Word count', | ||
key: 'wordCount', | ||
dataFormat: 'number', | ||
}, | ||
{ | ||
title: 'Reading time', | ||
key: 'readingTime', | ||
dataFormat: 'number', | ||
}, | ||
{ | ||
title: 'URL', | ||
key: 'url', | ||
dataFormat: 'url', | ||
}, | ||
]; | ||
|
||
const options: TableOptions = { | ||
columns, | ||
title: 'My table', | ||
subtitle: 'and a subtitle...', | ||
description: 'An aria description', | ||
source: { | ||
href: '', | ||
}, | ||
locale: 'fr', | ||
}; | ||
|
||
export default options; |
14 changes: 14 additions & 0 deletions
14
packages/visualizations-react/stories/Table/pagination.css
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,14 @@ | ||
.custom-pagination .ods-visualizations-table .pagination { | ||
flex-direction: row-reverse; | ||
justify-content: flex-start; | ||
} | ||
|
||
.custom-pagination .ods-visualizations-table .current { | ||
border-radius: 999px; | ||
box-shadow: rgb(85, 91, 255) 0px 0px 0px 1px, rgb(31, 193, 27) 0px 0px 0px 2px, rgb(255, 217, 19) 0px 0px 0px 3px, rgb(255, 156, 85) 0px 0px 0px 4px, rgb(255, 85, 85) 0px 0px 0px 5px; | ||
color: rgba(240, 46, 170, 0.4); | ||
} | ||
|
||
.custom-pagination .ods-visualizations-table .numbering { | ||
color: rgba(240, 46, 170, 0.2); | ||
} |
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
Oops, something went wrong.
3592c20
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.
Coverage for this commit
Coverage Report
3592c20
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.
Coverage for this commit
Coverage Report