Add loading state to new row component

This commit is contained in:
Andrew Kingston 2023-04-26 19:23:06 +01:00
parent 78c50df4bd
commit 5b03ce0566
1 changed files with 39 additions and 10 deletions

View File

@ -27,13 +27,14 @@
columnHorizontalInversionIndex, columnHorizontalInversionIndex,
} = getContext("grid") } = getContext("grid")
let visible = false
let isAdding = false let isAdding = false
let newRow = {} let newRow = {}
let offset = 0 let offset = 0
$: firstColumn = $stickyColumn || $renderedColumns[0] $: firstColumn = $stickyColumn || $renderedColumns[0]
$: width = GutterWidth + ($stickyColumn?.width || 0) $: width = GutterWidth + ($stickyColumn?.width || 0)
$: $tableId, (isAdding = false) $: $tableId, (visible = false)
$: invertY = shouldInvertY(offset, $rowVerticalInversionIndex, $renderedRows) $: invertY = shouldInvertY(offset, $rowVerticalInversionIndex, $renderedRows)
const shouldInvertY = (offset, inversionIndex, rows) => { const shouldInvertY = (offset, inversionIndex, rows) => {
@ -45,7 +46,8 @@
const addRow = async () => { const addRow = async () => {
// Blur the active cell and tick to let final value updates propagate // Blur the active cell and tick to let final value updates propagate
$focusedCellAPI?.blur() isAdding = true
$focusedCellId = null
await tick() await tick()
// Create row // Create row
@ -60,17 +62,19 @@
$focusedCellId = `${savedRow._id}-${firstColumn.name}` $focusedCellId = `${savedRow._id}-${firstColumn.name}`
} }
} }
isAdding = false
} }
const clear = () => { const clear = () => {
isAdding = false isAdding = false
visible = false
$focusedCellId = null $focusedCellId = null
$hoveredRowId = null $hoveredRowId = null
document.removeEventListener("keydown", handleKeyPress) document.removeEventListener("keydown", handleKeyPress)
} }
const startAdding = async () => { const startAdding = async () => {
if (isAdding) { if (visible) {
return return
} }
@ -95,7 +99,7 @@
// Update state and select initial cell // Update state and select initial cell
newRow = {} newRow = {}
isAdding = true visible = true
$hoveredRowId = NewRowID $hoveredRowId = NewRowID
if (firstColumn) { if (firstColumn) {
$focusedCellId = `${NewRowID}-${firstColumn.name}` $focusedCellId = `${NewRowID}-${firstColumn.name}`
@ -115,7 +119,7 @@
} }
const handleKeyPress = e => { const handleKeyPress = e => {
if (!isAdding) { if (!visible) {
return return
} }
if (e.key === "Escape") { if (e.key === "Escape") {
@ -137,7 +141,7 @@
</script> </script>
<!-- Only show new row functionality if we have any columns --> <!-- Only show new row functionality if we have any columns -->
{#if isAdding} {#if visible}
<div <div
class="container" class="container"
class:floating={offset > 0} class:floating={offset > 0}
@ -148,6 +152,9 @@
<div class="sticky-column" transition:fade={{ duration: 130 }}> <div class="sticky-column" transition:fade={{ duration: 130 }}>
<GutterCell on:expand={addViaModal} rowHovered> <GutterCell on:expand={addViaModal} rowHovered>
<Icon name="Add" color="var(--spectrum-global-color-gray-500)" /> <Icon name="Add" color="var(--spectrum-global-color-gray-500)" />
{#if isAdding}
<div in:fade={{ duration: 130 }} class="loading-overlay" />
{/if}
</GutterCell> </GutterCell>
{#if $stickyColumn} {#if $stickyColumn}
{@const cellId = `${NewRowID}-${$stickyColumn.name}`} {@const cellId = `${NewRowID}-${$stickyColumn.name}`}
@ -161,7 +168,14 @@
{updateValue} {updateValue}
rowIdx={0} rowIdx={0}
{invertY} {invertY}
/> >
{#if $stickyColumn?.schema?.autocolumn}
<div class="readonly-overlay">Can't edit auto column</div>
{/if}
{#if isAdding}
<div in:fade={{ duration: 130 }} class="loading-overlay" />
{/if}
</DataCell>
{/if} {/if}
</div> </div>
<div class="normal-columns" transition:fade={{ duration: 130 }}> <div class="normal-columns" transition:fade={{ duration: 130 }}>
@ -183,7 +197,10 @@
{invertY} {invertY}
> >
{#if column?.schema?.autocolumn} {#if column?.schema?.autocolumn}
<div class="readonly">Can't edit auto column</div> <div class="readonly-overlay">Can't edit auto column</div>
{/if}
{#if isAdding}
<div in:fade={{ duration: 130 }} class="loading-overlay" />
{/if} {/if}
</DataCell> </DataCell>
{/key} {/key}
@ -192,7 +209,7 @@
</GridScrollWrapper> </GridScrollWrapper>
</div> </div>
<div class="buttons" transition:fade={{ duration: 130 }}> <div class="buttons" transition:fade={{ duration: 130 }}>
<Button size="M" cta on:click={addRow}>Save</Button> <Button size="M" cta on:click={addRow} disabled={isAdding}>Save</Button>
<Button size="M" secondary newStyles on:click={clear}>Cancel</Button> <Button size="M" secondary newStyles on:click={clear}>Cancel</Button>
</div> </div>
</div> </div>
@ -268,7 +285,7 @@
} }
/* Readonly cell overlay */ /* Readonly cell overlay */
.readonly { .readonly-overlay {
position: absolute; position: absolute;
top: 0; top: 0;
left: 0; left: 0;
@ -283,4 +300,16 @@
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
} }
/* Overlay while row is being added */
.loading-overlay {
position: absolute;
top: 0;
left: 0;
height: var(--row-height);
width: 100%;
z-index: 1;
background: var(--spectrum-global-color-gray-400);
opacity: 0.25;
}
</style> </style>