Fix bug with select placeholders and fix sorting loops causing endless refreshes

This commit is contained in:
Andrew Kingston 2023-03-11 18:28:58 +00:00
parent 57c82c4a5d
commit c1128618fb
5 changed files with 50 additions and 33 deletions

View File

@ -44,7 +44,10 @@
const getFieldText = (value, options, placeholder) => { const getFieldText = (value, options, placeholder) => {
// Always use placeholder if no value // Always use placeholder if no value
if (value == null || value === "") { if (value == null || value === "") {
return placeholder !== false ? "Choose an option" : "" if (placeholder === false) {
return ""
}
return placeholder || "Choose an option"
} }
return getFieldAttribute(getOptionLabel, value, options) return getFieldAttribute(getOptionLabel, value, options)

View File

@ -59,14 +59,16 @@
class:scrolled={scrollLeft > 0} class:scrolled={scrollLeft > 0}
> >
<div class="header row"> <div class="header row">
<!-- Field headers -->
<SheetCell <SheetCell
width="40" width="40"
on:click={$config.allowSelectRows && selectAll} on:click={$config.allowSelectRows && selectAll}
center center
> >
{#if $config.allowSelectRows} {#if $config.allowSelectRows}
<Checkbox value={rowCount && selectedRowCount === rowCount} /> <Checkbox
value={rowCount && selectedRowCount === rowCount}
disabled={!$renderedRows.length}
/>
{/if} {/if}
</SheetCell> </SheetCell>

View File

@ -22,6 +22,20 @@
return [...options, ...columns.map(col => col.name)] return [...options, ...columns.map(col => col.name)]
} }
const updateSortColumn = e => {
sort.update(state => ({
...state,
column: e.detail,
}))
}
const updateSortOrder = e => {
sort.update(state => ({
...state,
order: e.detail,
}))
}
// Ensure we never have a sort column selected that is not visible // Ensure we never have a sort column selected that is not visible
const checkValidSortColumn = (sortColumn, stickyColumn, visibleColumns) => { const checkValidSortColumn = (sortColumn, stickyColumn, visibleColumns) => {
if (!sortColumn) { if (!sortColumn) {
@ -52,8 +66,8 @@
quiet quiet
size="M" size="M"
on:click={() => (open = !open)} on:click={() => (open = !open)}
selected={!!$sort.column} selected={open || $sort.column}
disabled={!$visibleColumns.length} disabled={!columnOptions.length}
> >
Sort Sort
</ActionButton> </ActionButton>
@ -61,8 +75,20 @@
<Popover bind:open {anchor} align="left"> <Popover bind:open {anchor} align="left">
<div class="content"> <div class="content">
<Select bind:value={$sort.column} options={columnOptions} autoWidth /> <Select
<Select bind:value={$sort.order} options={orderOptions} autoWidth /> placeholder="Default"
value={$sort.column}
options={columnOptions}
autoWidth
on:change={updateSortColumn}
/>
<Select
placeholder={null}
value={$sort.order}
options={orderOptions}
autoWidth
on:change={updateSortOrder}
/>
</div> </div>
</Popover> </Popover>

View File

@ -11,10 +11,11 @@ export const createRowsStore = context => {
const filter = writable([]) const filter = writable([])
const loaded = writable(false) const loaded = writable(false)
const fetch = writable(null) const fetch = writable(null)
const sort = writable({ const initialSortState = {
column: null, column: null,
order: null, order: "ascending",
}) }
const sort = writable(initialSortState)
// Enrich rows with an index property // Enrich rows with an index property
const enrichedRows = derived(rows, $rows => { const enrichedRows = derived(rows, $rows => {
@ -44,10 +45,7 @@ export const createRowsStore = context => {
fetch.set(null) fetch.set(null)
// Reset state // Reset state
sort.set({ sort.set(initialSortState)
column: null,
order: null,
})
filter.set([]) filter.set([])
// Create new fetch model // Create new fetch model
@ -59,8 +57,8 @@ export const createRowsStore = context => {
}, },
options: { options: {
filter: [], filter: [],
sortColumn: null, sortColumn: initialSortState.column,
sortOrder: null, sortOrder: initialSortState.order,
limit: 100, limit: 100,
paginate: true, paginate: true,
}, },
@ -73,15 +71,6 @@ export const createRowsStore = context => {
// Hydrate initial data // Hydrate initial data
rowCacheMap = {} rowCacheMap = {}
rows.set([]) rows.set([])
// Update sorting from fetch if required
const $sort = get(sort)
if (!$sort.column) {
sort.set({
column: $fetch.sortColumn,
order: $fetch.sortOrder,
})
}
} }
// Update schema and enrich primary display into schema // Update schema and enrich primary display into schema
@ -180,10 +169,7 @@ export const createRowsStore = context => {
// Refreshes all data // Refreshes all data
const refreshData = () => { const refreshData = () => {
filter.set([]) filter.set([])
sort.set({ sort.set(initialSortState)
column: null,
order: null,
})
} }
// Updates a value of a row // Updates a value of a row

View File

@ -1,7 +1,7 @@
import { derived, get, writable } from "svelte/store" import { derived, get, writable } from "svelte/store"
export const createScrollStores = context => { export const createScrollStores = context => {
const { rows, columns, stickyColumn, bounds, cellHeight } = context const { rows, visibleColumns, stickyColumn, bounds, cellHeight } = context
const padding = 180 const padding = 180
const scroll = writable({ const scroll = writable({
left: 0, left: 0,
@ -28,10 +28,10 @@ export const createScrollStores = context => {
// Derive horizontal limits // Derive horizontal limits
const contentWidth = derived( const contentWidth = derived(
[columns, stickyColumn], [visibleColumns, stickyColumn],
([$columns, $stickyColumn]) => { ([$visibleColumns, $stickyColumn]) => {
let width = 40 + padding + ($stickyColumn?.width || 0) let width = 40 + padding + ($stickyColumn?.width || 0)
$columns.forEach(col => { $visibleColumns.forEach(col => {
width += col.width width += col.width
}) })
return width return width