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

This commit is contained in:
Eric Liu 2022-06-05 12:27:24 -07:00
commit 91348df434
2 changed files with 17 additions and 11 deletions

View file

@ -155,7 +155,6 @@
}; };
setContext("DataTable", { setContext("DataTable", {
sortHeader,
tableSortable, tableSortable,
batchSelectedIds, batchSelectedIds,
tableRows, tableRows,
@ -344,7 +343,9 @@
<TableHeader <TableHeader
id="{header.key}" id="{header.key}"
style="{formatHeaderWidth(header)}" style="{formatHeaderWidth(header)}"
disableSorting="{header.sort === false}" sortable="{sortable && header.sort !== false}"
sortDirection="{$sortHeader.sortDirection}"
active="{$sortHeader.id === header.key}"
on:click="{() => { on:click="{() => {
dispatch('click', { header }); dispatch('click', { header });

View file

@ -1,6 +1,15 @@
<script> <script>
/** Set to `true` to disable sorting on this specific cell */ /** Set to `true` for the sortable variant */
export let disableSorting = false; export let sortable = false;
/**
* Specify the sort direction
* @type {"none" | "ascending" | "descending"}
*/
export let sortDirection = "none";
/** Set to `true` if the column sorting */
export let active = false;
/** Specify the `scope` attribute */ /** Specify the `scope` attribute */
export let scope = "col"; export let scope = "col";
@ -14,21 +23,17 @@
/** Set an id for the top-level element */ /** Set an id for the top-level element */
export let id = "ccs-" + Math.random().toString(36); export let id = "ccs-" + Math.random().toString(36);
import { getContext } from "svelte";
import ArrowUp from "../icons/ArrowUp.svelte"; import ArrowUp from "../icons/ArrowUp.svelte";
import ArrowsVertical from "../icons/ArrowsVertical.svelte"; import ArrowsVertical from "../icons/ArrowsVertical.svelte";
const { sortHeader, tableSortable } = getContext("DataTable");
$: active = $sortHeader.id === id;
// TODO: translate with id // TODO: translate with id
$: ariaLabel = translateWithId(); $: ariaLabel = translateWithId();
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->
{#if $tableSortable && !disableSorting} {#if sortable}
<th <th
aria-sort="{active ? $sortHeader.sortDirection : 'none'}" aria-sort="{active ? sortDirection : 'none'}"
scope="{scope}" scope="{scope}"
data-header="{id}" data-header="{id}"
{...$$restProps} {...$$restProps}
@ -40,7 +45,7 @@
class:bx--table-sort="{true}" class:bx--table-sort="{true}"
class:bx--table-sort--active="{active}" class:bx--table-sort--active="{active}"
class:bx--table-sort--ascending="{active && class:bx--table-sort--ascending="{active &&
$sortHeader.sortDirection === 'descending'}" sortDirection === 'descending'}"
on:click on:click
> >
<div class:bx--table-header-label="{true}"> <div class:bx--table-header-label="{true}">