Skip to content

Commit

Permalink
Fix showing JSON columns in Svelte Table (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon authored Jun 26, 2024
1 parent ad2500c commit d71b03a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-mayflies-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latitude-data/svelte": patch
"@latitude-data/client": patch
---

Fix show JSON columns in Svelte table component
16 changes: 13 additions & 3 deletions packages/client/core/src/theme/assets/latitude.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
@tailwind utilities;

@layer base {
/* Chrome, Safari and Opera */
.no-scrollbar::-webkit-scrollbar {
display: none;
}

.no-scrollbar {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
overflow: -moz-scrollbars-none; /* Firefox */
}
.custom-scrollbar {
/** Make scrollbar to not take space **/
overflow: overlay !important;
}
.custom-scrollbar::-webkit-scrollbar {
@apply lat-w-5 lat-h-5;
@apply lat-h-5 lat-w-5;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
@apply lat-bg-gray-400;
@apply lat-rounded-full lat-bg-clip-padding lat-border-solid;
@apply lat-border-transparent lat-border-[6px];
@apply lat-rounded-full lat-border-solid lat-bg-clip-padding;
@apply lat-border-[6px] lat-border-transparent;
}

.custom-scrollbar::-webkit-scrollbar-track {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/core/src/theme/ui/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function tableFooterCssClass({ className }: Props) {

function tableCellCssClass({ className }: Props) {
return cn(
'lat-p-2 lat-align-middle [&:has([role=checkbox])]:lat-pr-0 [&>[role=checkbox]]:lat-translate-y-[2px]',
'lat-p-2 lat-max-w-80 lat-overflow-x-auto no-scrollbar lat-align-middle [&:has([role=checkbox])]:lat-pr-0 [&>[role=checkbox]]:lat-translate-y-[2px]',
className,
)
}
Expand Down
27 changes: 23 additions & 4 deletions packages/client/svelte/src/lib/internal/ui/Table/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<script lang="ts">
import * as Table from '$lib/internal/ui/_table'
import QueryResult from '@latitude-data/query_result'
import QueryResult, {
type Field,
type QueryResultRow,
} from '@latitude-data/query_result'
import { Card } from '$lib'
import { createTable, Render, Subscribe } from 'svelte-headless-table'
import { readable } from 'svelte/store'
Expand All @@ -26,11 +29,27 @@
let className: $$Props['class'] = undefined
export { className as class }
$: table = createTable(readable(data.toArray()))
/**
* This is very similar to QueryResult.toArray
* but please resists your sharp dry instintcs and
* leave this method here.
* We only want to stringify JSON columns in this case
*/
function toArray(fields: Field[], result: QueryResult) {
return result.rows.map((row: unknown[]) =>
row.reduce((acc: Record<string, unknown>, value, i) => {
acc[fields[i]!.name] =
typeof value === 'object' ? JSON.stringify(value) : value
return acc
}, {} as QueryResultRow),
)
}
$: table = createTable(readable(toArray(data.fields, data)))
$: columns = table.createColumns(
data.fields.map((field) =>
table.column({ accessor: field.name, header: field.name })
)
table.column({ accessor: field.name, header: field.name }),
),
)
$: ({ headerRows, pageRows, tableAttrs, tableBodyAttrs } =
table.createViewModel(columns))
Expand Down

0 comments on commit d71b03a

Please sign in to comment.