-
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.
feat(Table): sortable columns (#233)
- Loading branch information
1 parent
53d2faf
commit 8addb80
Showing
12 changed files
with
22,655 additions
and
20,013 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
packages/visualizations-react/stories/Table/Sort.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,79 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
import type { TableData, Async, ColumnSortValues, DataFrame } from '@opendatasoft/visualizations'; | ||
import { ColumnSort } from '@opendatasoft/visualizations'; | ||
import { Table } from '../../src'; | ||
import value from './data'; | ||
import options from './options'; | ||
import { fetchData } from './utils'; | ||
|
||
import './custom-style.css'; | ||
|
||
const meta: ComponentMeta<typeof Table> = { | ||
title: 'Table/Sort', | ||
component: Table, | ||
}; | ||
export default meta; | ||
|
||
const data: Async<TableData> = { | ||
value, | ||
loading: false, | ||
}; | ||
|
||
const sortColumn = (sort: [string, ColumnSortValues], key: string) => | ||
sort[0] === key && sort[1] === 'ASC' ? 'DESC' : 'ASC'; | ||
|
||
const Template: ComponentStory<typeof Table> = args => { | ||
const { options: unsortedOptions } = args; | ||
const [sort, setSort] = useState<[string, ColumnSortValues]>(['title', ColumnSort.asc]); | ||
const [records, setRecords] = useState<DataFrame>(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const newRecords = await fetchData({ | ||
size: 5, | ||
data: value, | ||
sort, | ||
page: 1, | ||
}); | ||
setRecords(newRecords); | ||
})(); | ||
}, [setRecords, sort]); | ||
|
||
const sortedData = { value: records, isLoading: false }; | ||
|
||
const sortableColumns = unsortedOptions.columns.map((col, i) => ({ | ||
...col, | ||
sorted: sort[0] === col.key ? sort[1] : undefined, | ||
// cheap way to have some not sortable | ||
onClick: i < 2 ? () => setSort([col.key, sortColumn(sort, col.key)]) : undefined, | ||
})); | ||
|
||
const sortedOptions = { | ||
...options, | ||
columns: sortableColumns, | ||
}; | ||
return <Table data={sortedData} options={sortedOptions} />; | ||
}; | ||
|
||
export const Sort = Template.bind({}); | ||
Sort.args = { | ||
data, | ||
options: { | ||
...options, | ||
}, | ||
}; | ||
|
||
const ColoredTemplate: ComponentStory<typeof Table> = args => ( | ||
<div className="design-system"> | ||
<Template {...args} /> | ||
</div> | ||
); | ||
|
||
export const ColorSort = ColoredTemplate.bind({}); | ||
ColorSort.args = { | ||
data, | ||
options: { | ||
...options, | ||
}, | ||
}; |
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,25 @@ | ||
import type { ColumnSortValues, DataFrame } from '@opendatasoft/visualizations'; | ||
import { ColumnSort } from '@opendatasoft/visualizations'; | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const fetchData = async ({ | ||
size, | ||
page, | ||
data, | ||
sort, | ||
}: { | ||
size: number; | ||
page: number; | ||
data: DataFrame; | ||
sort?: [string, ColumnSortValues]; | ||
}) => { | ||
const startIndex = (page - 1) * size; | ||
const endIndex = startIndex + size; | ||
await setTimeout(() => {}, 300); | ||
const dataFrame: DataFrame = data?.slice(startIndex, endIndex); | ||
if (sort) { | ||
return dataFrame.sort((r1, r2) => | ||
sort[1] === ColumnSort.asc ? r1[sort[0]] - r2[sort[0]] : r2[sort[0]] - r1[sort[0]] | ||
); | ||
} | ||
return dataFrame; | ||
}; |
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
32 changes: 32 additions & 0 deletions
32
packages/visualizations/src/components/Table/Headers/SortButton.svelte
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,32 @@ | ||
<script lang="ts"> | ||
import { ColumnSort, type ColumnSortValues } from '../types'; | ||
import SortIcon from './SortIcon.svelte'; | ||
export let sorted: ColumnSortValues | undefined; | ||
export let labels = { | ||
asc: 'Sort ascending', | ||
desc: 'Sort descending', | ||
}; | ||
</script> | ||
|
||
<button on:click type="button" aria-label={sorted === ColumnSort.asc ? labels.asc : labels.desc}> | ||
<slot /> | ||
<SortIcon {sorted} /> | ||
</button> | ||
|
||
<style> | ||
button { | ||
font-size: inherit; | ||
font-weight: inherit; | ||
text-align: inherit; | ||
border: none; | ||
background: transparent; | ||
box-shadow: none; | ||
cursor: pointer; | ||
width: 100%; | ||
height: 100%; | ||
padding: 0; | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
</style> |
23 changes: 23 additions & 0 deletions
23
packages/visualizations/src/components/Table/Headers/SortIcon.svelte
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,23 @@ | ||
<script lang="ts"> | ||
import { ColumnSort, type ColumnSortValues } from "../types"; | ||
export let sorted: ColumnSortValues | undefined; | ||
</script> | ||
|
||
<svg width="16" height="17" viewBox="0 0 16 17" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<path | ||
d="m12 10.5-4 4-4-4h8zM12" | ||
fill={sorted === ColumnSort.asc ? 'var(--selected)' : 'var(--neutral)'} | ||
/> | ||
<path | ||
d="m12 6.5l-4-4-4 4h8z" | ||
fill={sorted === ColumnSort.desc ? 'var(--selected)' : 'var(--neutral)'} | ||
/> | ||
</svg> | ||
|
||
<style> | ||
svg { | ||
--selected: black; | ||
--neutral: grey; | ||
} | ||
</style> |
3 changes: 3 additions & 0 deletions
3
packages/visualizations/src/components/Table/Headers/index.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,3 @@ | ||
import Headers from './Headers.svelte'; | ||
|
||
export default Headers; |
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
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 |
---|---|---|
|
@@ -27,7 +27,6 @@ | |
{/if} | ||
</div> | ||
|
||
<!-- markup (zero or more items) goes here --> | ||
<style> | ||
h3, | ||
p { | ||
|
8addb80
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
8addb80
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