feat(data-table): support programmatic sorting (#1337)

* refactor(data-table): pass down sortable props instead of using context

* feat(data-table): support programmatic sorting

* test(data-table): assert new props

* docs(data-table): add "Programmatic sorting" example

* refactor(data-table): remove unused tableSortable store

* refactor(data-table): remove unused indices
This commit is contained in:
metonym 2022-06-05 13:37:50 -07:00 committed by GitHub
commit 72c24b83b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 241 additions and 48 deletions

View file

@ -0,0 +1,97 @@
<script>
import { DataTable, Button } from "carbon-components-svelte";
let sortKey = "port";
let sortDirection = "ascending";
$: console.log("sortKey", sortKey);
$: console.log("sortDirection", sortDirection);
</script>
<Button
kind="tertiary"
disabled="{sortKey === 'port' && sortDirection === 'ascending'}"
on:click="{() => {
sortKey = 'port';
sortDirection = 'ascending';
}}"
>
Sort "port" in ascending order
</Button>
<Button
kind="tertiary"
disabled="{sortKey === 'name' && sortDirection === 'descending'}"
on:click="{() => {
sortKey = 'name';
sortDirection = 'descending';
}}"
>
Sort "name" in descending order
</Button>
<Button
kind="ghost"
on:click="{() => {
sortKey = null;
sortDirection = 'none';
}}"
>
Clear sorting
</Button>
<DataTable
sortable
bind:sortKey
bind:sortDirection
headers="{[
{ key: 'name', value: 'Name' },
{ key: 'protocol', value: 'Protocol', sort: false },
{ key: 'port', value: 'Port' },
{ key: 'rule', value: 'Rule' },
]}"
rows="{[
{
id: 'a',
name: 'Load Balancer 3',
protocol: 'HTTP',
port: 3000,
rule: 'Round robin',
},
{
id: 'b',
name: 'Load Balancer 1',
protocol: 'HTTP',
port: 443,
rule: 'Round robin',
},
{
id: 'c',
name: 'Load Balancer 2',
protocol: 'HTTP',
port: 80,
rule: 'DNS delegation',
},
{
id: 'd',
name: 'Load Balancer 6',
protocol: 'HTTP',
port: 3000,
rule: 'Round robin',
},
{
id: 'e',
name: 'Load Balancer 4',
protocol: 'HTTP',
port: 443,
rule: 'Round robin',
},
{
id: 'f',
name: 'Load Balancer 5',
protocol: 'HTTP',
port: 80,
rule: 'DNS delegation',
},
]}"
/>