mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
parent
918ef46a90
commit
0a927ae512
3 changed files with 265 additions and 54 deletions
|
@ -42,6 +42,25 @@
|
|||
*/
|
||||
export let sortable = false;
|
||||
|
||||
/**
|
||||
* Set to `true` for the expandable variant
|
||||
* Automatically set to `true` if `batchExpansion` is `true`
|
||||
* @type {boolean} [expandable=false]
|
||||
*/
|
||||
export let expandable = false;
|
||||
|
||||
/**
|
||||
* Set to `true` to enable batch expansion
|
||||
* @type {boolean} [batchExpansion=false]
|
||||
*/
|
||||
export let batchExpansion = false;
|
||||
|
||||
/**
|
||||
* Specify the row ids to be expanded
|
||||
* @type {boolean} [expandedRowIds=[]]
|
||||
*/
|
||||
export let expandedRowIds = [];
|
||||
|
||||
/**
|
||||
* Set to `true` to enable a sticky header
|
||||
* @type {boolean} [stickyHeader=false]
|
||||
|
@ -50,6 +69,7 @@
|
|||
|
||||
import { createEventDispatcher, setContext } from "svelte";
|
||||
import { writable, derived } from "svelte/store";
|
||||
import ChevronRight16 from "carbon-icons-svelte/lib/ChevronRight16";
|
||||
import Table from "./Table.svelte";
|
||||
import TableBody from "./TableBody.svelte";
|
||||
import TableCell from "./TableCell.svelte";
|
||||
|
@ -81,6 +101,14 @@
|
|||
},
|
||||
});
|
||||
|
||||
let expanded = false;
|
||||
let parentRowId = null;
|
||||
|
||||
$: expandedRows = expandedRowIds.reduce(
|
||||
(a, id) => ({ ...a, [id]: true }),
|
||||
{}
|
||||
);
|
||||
$: if (batchExpansion) expandable = true;
|
||||
$: tableSortable.set(sortable);
|
||||
$: headerKeys = headers.map(({ key }) => key);
|
||||
$: rows = rows.map((row) => ({
|
||||
|
@ -109,63 +137,124 @@
|
|||
});
|
||||
}
|
||||
}
|
||||
$: props = {
|
||||
headers,
|
||||
rows,
|
||||
};
|
||||
</script>
|
||||
|
||||
<slot props="{props}">
|
||||
<TableContainer title="{title}" description="{description}" {...$$restProps}>
|
||||
<Table
|
||||
zebra="{zebra}"
|
||||
size="{size}"
|
||||
stickyHeader="{stickyHeader}"
|
||||
sortable="{sortable}"
|
||||
>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{#each headers as header, i (header.key)}
|
||||
<TableHeader
|
||||
on:click="{() => {
|
||||
dispatch('click', { header });
|
||||
let active = header.key === $sortHeader.key;
|
||||
let currentSortDirection = active ? $sortHeader.sortDirection : 'none';
|
||||
let sortDirection = sortDirectionMap[currentSortDirection];
|
||||
dispatch('click:header', { header, sortDirection });
|
||||
sortHeader.set({
|
||||
id: sortDirection === 'none' ? null : $thKeys[header.key],
|
||||
key: header.key,
|
||||
sortDirection,
|
||||
});
|
||||
}}"
|
||||
>
|
||||
{header.value}
|
||||
</TableHeader>
|
||||
{/each}
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each sorting ? sortedRows : rows as row, i (row.id)}
|
||||
<TableRow
|
||||
on:click="{() => {
|
||||
dispatch('click', { row });
|
||||
dispatch('click:row', row);
|
||||
}}"
|
||||
<TableContainer title="{title}" description="{description}" {...$$restProps}>
|
||||
<Table
|
||||
zebra="{zebra}"
|
||||
size="{size}"
|
||||
stickyHeader="{stickyHeader}"
|
||||
sortable="{sortable}"
|
||||
>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
{#if expandable}
|
||||
<th
|
||||
scope="col"
|
||||
class:bx--table-expand="{true}"
|
||||
data-previous-value="{expanded ? 'collapsed' : undefined}"
|
||||
>
|
||||
{#each row.cells as cell, j (cell.key)}
|
||||
<TableCell
|
||||
{#if batchExpansion}
|
||||
<button
|
||||
type="button"
|
||||
class:bx--table-expand__button="{true}"
|
||||
on:click="{() => {
|
||||
dispatch('click', { row, cell });
|
||||
dispatch('click:cell', cell);
|
||||
expanded = !expanded;
|
||||
expandedRowIds = expanded ? rows.map((row) => row.id) : [];
|
||||
|
||||
dispatch('click:header--expand', { expanded });
|
||||
}}"
|
||||
>
|
||||
{cell.value}
|
||||
</TableCell>
|
||||
{/each}
|
||||
</TableRow>
|
||||
<ChevronRight16 class="bx--table-expand__svg" />
|
||||
</button>
|
||||
{/if}
|
||||
</th>
|
||||
{/if}
|
||||
{#each headers as header, i (header.key)}
|
||||
<TableHeader
|
||||
on:click="{() => {
|
||||
dispatch('click', { header });
|
||||
let active = header.key === $sortHeader.key;
|
||||
let currentSortDirection = active ? $sortHeader.sortDirection : 'none';
|
||||
let sortDirection = sortDirectionMap[currentSortDirection];
|
||||
dispatch('click:header', { header, sortDirection });
|
||||
sortHeader.set({
|
||||
id: sortDirection === 'none' ? null : $thKeys[header.key],
|
||||
key: header.key,
|
||||
sortDirection,
|
||||
});
|
||||
}}"
|
||||
>
|
||||
{header.value}
|
||||
</TableHeader>
|
||||
{/each}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
</slot>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{#each sorting ? sortedRows : rows as row, i (row.id)}
|
||||
<TableRow
|
||||
class="{expandedRows[row.id] ? 'bx--expandable-row' : ''} {expandable ? 'bx--parent-row' : ''} {expandable && parentRowId === row.id ? 'bx--expandable-row--hover' : ''}"
|
||||
on:click="{() => {
|
||||
dispatch('click', { row });
|
||||
dispatch('click:row', row);
|
||||
}}"
|
||||
>
|
||||
{#if expandable}
|
||||
<TableCell
|
||||
class="bx--table-expand"
|
||||
headers="expand"
|
||||
data-previous-value="{expandedRows[row.id] ? 'collapsed' : undefined}"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class:bx--table-expand__button="{true}"
|
||||
aria-label="{expandedRows[row.id] ? 'Collapse current row' : 'Expand current row'}"
|
||||
on:click="{() => {
|
||||
const rowExpanded = !!expandedRows[row.id];
|
||||
|
||||
expandedRowIds = rowExpanded ? expandedRowIds.filter((id) => id !== row.id) : [...expandedRowIds, row.id];
|
||||
|
||||
dispatch('click:row--expand', {
|
||||
row,
|
||||
expanded: !rowExpanded,
|
||||
});
|
||||
}}"
|
||||
>
|
||||
<ChevronRight16 class="bx--table-expand__svg" />
|
||||
</button>
|
||||
</TableCell>
|
||||
{/if}
|
||||
{#each row.cells as cell, j (cell.key)}
|
||||
<TableCell
|
||||
on:click="{() => {
|
||||
dispatch('click', { row, cell });
|
||||
dispatch('click:cell', cell);
|
||||
}}"
|
||||
>
|
||||
{cell.value}
|
||||
</TableCell>
|
||||
{/each}
|
||||
</TableRow>
|
||||
|
||||
{#if expandable && expandedRows[row.id]}
|
||||
<tr
|
||||
data-child-row
|
||||
class:bx--expandable-row="{true}"
|
||||
on:mouseenter="{() => {
|
||||
parentRowId = row.id;
|
||||
}}"
|
||||
on:mouseleave="{() => {
|
||||
parentRowId = null;
|
||||
}}"
|
||||
>
|
||||
<TableCell colspan="{headers.length + 1}">
|
||||
<div class:bx--child-row-inner-container="{true}">
|
||||
<slot name="expanded-row" row="{row}" />
|
||||
</div>
|
||||
</TableCell>
|
||||
</tr>
|
||||
{/if}
|
||||
{/each}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue