diff --git a/src/components/table/table.tsx b/src/components/table/table.tsx index bb035bd..20b533b 100644 --- a/src/components/table/table.tsx +++ b/src/components/table/table.tsx @@ -12,12 +12,17 @@ export type Column = { sortable?: boolean, group?: string, readonly groupBy?: (rows: DataSetRowNode[]) => DataSetNode[], + readonly groupBy?: (rows: DataSetRowNode[]) => DataSetNode[], }; +export type CellEditor, K extends keyof T> = (cell: { row: number, column: K, value: T[K] }) => JSX.Element; +export type CellEditors> = { [K in keyof T]?: CellEditor }; export type CellEditor, K extends keyof T> = (cell: { row: number, column: K, value: T[K] }) => JSX.Element; export type CellEditors> = { [K in keyof T]?: CellEditor }; export interface TableApi> { + readonly selection: Accessor[]>; + readonly rows: Accessor>; readonly selection: Accessor[]>; readonly rows: Accessor>; readonly columns: Accessor[]>; @@ -25,6 +30,10 @@ export interface TableApi> { clear(): void; } +interface TableContextType> { + readonly rows: Accessor>, + readonly columns: Accessor[]>, + readonly selection: Accessor[]>, interface TableContextType> { readonly rows: Accessor>, readonly columns: Accessor[]>, @@ -46,6 +55,7 @@ type TableProps> = { class?: string, summary?: string, rows: DataSet, + rows: DataSet, columns: Column[], selectionMode?: SelectionMode, children?: CellEditors, @@ -55,18 +65,20 @@ type TableProps> = { export function Table>(props: TableProps) { const [selection, setSelection] = createSignal[]>([]); + const rows = createMemo(() => props.rows); const rows = createMemo(() => props.rows); const columns = createMemo[]>(() => props.columns ?? []); const selectionMode = createMemo(() => props.selectionMode ?? SelectionMode.None); const cellRenderers = createMemo>(() => props.children ?? {}); const context: TableContextType = { - rows, - columns, - selection, - selectionMode, - cellRenderers, - }; + const context: TableContextType = { + rows, + columns, + selection, + selectionMode, + cellRenderers, + }; return @@ -77,10 +89,12 @@ export function Table>(props: TableProps) { ; }; +type InnerTableProps> = { class?: string, summary?: string, rows: DataSet }; type InnerTableProps> = { class?: string, summary?: string, rows: DataSet }; function InnerTable>(props: InnerTableProps) { const table = useTable(); + const table = useTable(); const selectable = createMemo(() => table.selectionMode() !== SelectionMode.None); const columnCount = createMemo(() => table.columns().length); @@ -95,8 +109,9 @@ function InnerTable>(props: InnerTableProps) { { - node => - } + { + node => + } {/* @@ -112,8 +127,11 @@ function InnerTable>(props: InnerTableProps) { function Api>(props: { api: undefined | ((api: TableApi) => any) }) { const table = useTable(); const selectionContext = useSelection(); + const table = useTable(); + const selectionContext = useSelection(); const api: TableApi = { + selection: selectionContext.selection, selection: selectionContext.selection, rows: table.rows, columns: table.columns, @@ -163,6 +181,7 @@ function Head(props: {}) { { ({ id, label, sortable }) => { + const sort = createMemo(() => table.rows().sort()); const sort = createMemo(() => table.rows().sort()); const by = String(id); @@ -200,45 +219,53 @@ function Head(props: {}) { }; function Node>(props: { node: DataSetNode, depth: number, groupedBy?: keyof T }) { - return - { - row => - } + function Node>(props: { node: DataSetNode, depth: number, groupedBy?: keyof T }) { + return + { + row => + } - { - group => - } - ; -} + { + group => + } + ; + } -function Row>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) { - const table = useTable(); - const context = useSelection(); - const columns = table.columns; + function Row>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) { + const table = useTable(); + const context = useSelection(); + const columns = table.columns; - const isSelected = context.isSelected(props.key); + function Row>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) { + const table = useTable(); + const context = useSelection(); + const columns = table.columns; - return - - - context.select([props.key])} on:pointerdown={e => e.stopPropagation()} /> - - + const isSelected = context.isSelected(props.key); - { - ({ id }) => {table.cellRenderers()[id]?.({ row: props.key as number, column: id, value: props.value[id] }) ?? props.value[id]} - } - ; -}; + return + + + context.select([props.key])} on:pointerdown={e => e.stopPropagation()} /> + + -function Group>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode[], depth: number }) { - const table = useTable(); + { + ({ id }) => {table.cellRenderers()[id]?.({ row: props.key as number, column: id, value: props.value[id] }) ?? props.value[id]} + } + ; + }; - return
- {String(props.key)} + function Group>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode[], depth: number }) { + function Group>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode[], depth: number }) { + const table = useTable(); - { - node => - } -
; -}; \ No newline at end of file + return
+ {String(props.key)} + {String(props.key)} + + { + node => + } +
; + }; \ No newline at end of file diff --git a/src/routes/(editor)/experimental/table.tsx b/src/routes/(editor)/experimental/table.tsx index e36c89e..ba4acf8 100644 --- a/src/routes/(editor)/experimental/table.tsx +++ b/src/routes/(editor)/experimental/table.tsx @@ -2,8 +2,8 @@ import { Sidebar } from '~/components/sidebar'; import { Column, createDataSet, DataSetGroupNode, DataSetNode, DataSetRowNode, GroupOptions, SelectionMode, SortOptions, Table } from '~/components/table'; import { createStore } from 'solid-js/store'; import { Person, people } from './experimental.data'; -import css from './table.module.css'; import { createEffect, createMemo, For } from 'solid-js'; +import css from './table.module.css'; export default function TableExperiment() { const columns: Column[] = [