Add setting for table row count and improve height calculation

This commit is contained in:
Andrew Kingston 2021-03-24 19:06:02 +00:00
parent bc562ffd6b
commit 124ff6b089
2 changed files with 61 additions and 42 deletions

View File

@ -1533,6 +1533,12 @@
"key": "showAutoColumns", "key": "showAutoColumns",
"defaultValue": false "defaultValue": false
}, },
{
"type": "number",
"label": "Row Count",
"key": "rowCount",
"defaultValue": 8
},
{ {
"type": "select", "type": "select",
"label": "Theme", "label": "Theme",

View File

@ -8,7 +8,7 @@
export let dataProvider export let dataProvider
export let columns export let columns
export let showAutoColumns export let showAutoColumns
export let rowCount = 8 export let rowCount
const component = getContext("component") const component = getContext("component")
const { styleable } = getContext("sdk") const { styleable } = getContext("sdk")
@ -16,12 +16,26 @@
let sortColumn let sortColumn
let sortOrder let sortOrder
$: styles = makeStyles($component.styles, rowCount)
$: rows = dataProvider?.rows ?? [] $: rows = dataProvider?.rows ?? []
$: sortedRows = sortRows(rows, sortColumn, sortOrder) $: sortedRows = sortRows(rows, sortColumn, sortOrder)
$: loaded = dataProvider?.loaded ?? false $: loaded = dataProvider?.loaded ?? false
$: schema = dataProvider?.schema ?? {} $: schema = dataProvider?.schema ?? {}
$: fields = getFields(schema, columns, showAutoColumns) $: fields = getFields(schema, columns, showAutoColumns)
const makeStyles = (styles, rowCount) => {
if (!rowCount) {
return styles
}
return {
...styles,
normal: {
...styles.normal,
height: `${37 + rowCount * 56}px`,
},
}
}
const sortRows = (rows, sortColumn, sortOrder) => { const sortRows = (rows, sortColumn, sortOrder) => {
if (!sortColumn || !sortOrder) { if (!sortColumn || !sortOrder) {
return rows return rows
@ -52,7 +66,6 @@
} }
let columns = [] let columns = []
Object.entries(schema).forEach(([field, fieldSchema]) => { Object.entries(schema).forEach(([field, fieldSchema]) => {
console.log(fieldSchema)
if (showAutoColumns || !fieldSchema?.autocolumn) { if (showAutoColumns || !fieldSchema?.autocolumn) {
columns.push(field) columns.push(field)
} }
@ -61,50 +74,48 @@
} }
</script> </script>
<div use:styleable={$component.styles}> <div
<div lang="en"
lang="en" dir="ltr"
dir="ltr" class={`spectrum ${size || 'spectrum--medium'} ${theme || 'spectrum--light'}`}
class={`spectrum ${size || 'spectrum--medium'} ${theme || 'spectrum--light'}`} use:styleable={styles}>
style={`height: ${rowCount * 55}px;`}> <table class="spectrum-Table">
<table class="spectrum-Table"> <thead class="spectrum-Table-head">
<thead class="spectrum-Table-head"> <tr>
<tr> {#each fields as field}
<th
class="spectrum-Table-headCell is-sortable"
class:is-sorted-desc={sortColumn === field && sortOrder === 'Descending'}
class:is-sorted-asc={sortColumn === field && sortOrder === 'Ascending'}
on:click={() => sortBy(field)}>
<div class="spectrum-Table-headCell-content">
{schema[field]?.name}
<svg
class="spectrum-Icon spectrum-UIIcon-ArrowDown100 spectrum-Table-sortedIcon"
class:visible={sortColumn === field}
focusable="false"
aria-hidden="true">
<use xlink:href="#spectrum-css-icon-Arrow100" />
</svg>
</div>
</th>
{/each}
</tr>
</thead>
<tbody class="spectrum-Table-body">
{#each sortedRows as row}
<tr class="spectrum-Table-row">
{#each fields as field} {#each fields as field}
<th <td class="spectrum-Table-cell" tabindex="0">
class="spectrum-Table-headCell is-sortable" <div class="spectrum-Table-cell-content">
class:is-sorted-desc={sortColumn === field && sortOrder === 'Descending'} <CellRenderer schema={schema[field]} value={row[field]} />
class:is-sorted-asc={sortColumn === field && sortOrder === 'Ascending'}
on:click={() => sortBy(field)}>
<div class="spectrum-Table-headCell-content">
{schema[field]?.name}
<svg
class="spectrum-Icon spectrum-UIIcon-ArrowDown100 spectrum-Table-sortedIcon"
class:visible={sortColumn === field}
focusable="false"
aria-hidden="true">
<use xlink:href="#spectrum-css-icon-Arrow100" />
</svg>
</div> </div>
</th> </td>
{/each} {/each}
</tr> </tr>
</thead> {/each}
<tbody class="spectrum-Table-body"> </tbody>
{#each sortedRows as row} </table>
<tr class="spectrum-Table-row">
{#each fields as field}
<td class="spectrum-Table-cell" tabindex="0">
<div class="spectrum-Table-cell-content">
<CellRenderer schema={schema[field]} value={row[field]} />
</div>
</td>
{/each}
</tr>
{/each}
</tbody>
</table>
</div>
</div> </div>
<style> <style>
@ -119,6 +130,8 @@
z-index: 1; z-index: 1;
} }
th { th {
vertical-align: bottom;
height: 36px;
position: sticky; position: sticky;
top: 0; top: 0;
background-color: var(--spectrum-global-color-gray-100); background-color: var(--spectrum-global-color-gray-100);