fix merge issues
This commit is contained in:
parent
0749d5904f
commit
ed8eecea2a
1 changed files with 43 additions and 69 deletions
|
@ -12,17 +12,12 @@ export type Column<T> = {
|
||||||
sortable?: boolean,
|
sortable?: boolean,
|
||||||
group?: string,
|
group?: string,
|
||||||
readonly groupBy?: (rows: DataSetRowNode<keyof T, T>[]) => DataSetNode<keyof T, T>[],
|
readonly groupBy?: (rows: DataSetRowNode<keyof T, T>[]) => DataSetNode<keyof T, T>[],
|
||||||
readonly groupBy?: (rows: DataSetRowNode<keyof T, T>[]) => DataSetNode<keyof T, T>[],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CellEditor<T extends Record<string, any>, K extends keyof T> = (cell: { row: number, column: K, value: T[K] }) => JSX.Element;
|
|
||||||
export type CellEditors<T extends Record<string, any>> = { [K in keyof T]?: CellEditor<T, K> };
|
|
||||||
export type CellEditor<T extends Record<string, any>, K extends keyof T> = (cell: { row: number, column: K, value: T[K] }) => JSX.Element;
|
export type CellEditor<T extends Record<string, any>, K extends keyof T> = (cell: { row: number, column: K, value: T[K] }) => JSX.Element;
|
||||||
export type CellEditors<T extends Record<string, any>> = { [K in keyof T]?: CellEditor<T, K> };
|
export type CellEditors<T extends Record<string, any>> = { [K in keyof T]?: CellEditor<T, K> };
|
||||||
|
|
||||||
export interface TableApi<T extends Record<string, any>> {
|
export interface TableApi<T extends Record<string, any>> {
|
||||||
readonly selection: Accessor<SelectionItem<keyof T, T>[]>;
|
|
||||||
readonly rows: Accessor<DataSet<T>>;
|
|
||||||
readonly selection: Accessor<SelectionItem<keyof T, T>[]>;
|
readonly selection: Accessor<SelectionItem<keyof T, T>[]>;
|
||||||
readonly rows: Accessor<DataSet<T>>;
|
readonly rows: Accessor<DataSet<T>>;
|
||||||
readonly columns: Accessor<Column<T>[]>;
|
readonly columns: Accessor<Column<T>[]>;
|
||||||
|
@ -30,10 +25,6 @@ export interface TableApi<T extends Record<string, any>> {
|
||||||
clear(): void;
|
clear(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TableContextType<T extends Record<string, any>> {
|
|
||||||
readonly rows: Accessor<DataSet<T>>,
|
|
||||||
readonly columns: Accessor<Column<T>[]>,
|
|
||||||
readonly selection: Accessor<SelectionItem<keyof T, T>[]>,
|
|
||||||
interface TableContextType<T extends Record<string, any>> {
|
interface TableContextType<T extends Record<string, any>> {
|
||||||
readonly rows: Accessor<DataSet<T>>,
|
readonly rows: Accessor<DataSet<T>>,
|
||||||
readonly columns: Accessor<Column<T>[]>,
|
readonly columns: Accessor<Column<T>[]>,
|
||||||
|
@ -55,7 +46,6 @@ type TableProps<T extends Record<string, any>> = {
|
||||||
class?: string,
|
class?: string,
|
||||||
summary?: string,
|
summary?: string,
|
||||||
rows: DataSet<T>,
|
rows: DataSet<T>,
|
||||||
rows: DataSet<T>,
|
|
||||||
columns: Column<T>[],
|
columns: Column<T>[],
|
||||||
selectionMode?: SelectionMode,
|
selectionMode?: SelectionMode,
|
||||||
children?: CellEditors<T>,
|
children?: CellEditors<T>,
|
||||||
|
@ -65,20 +55,18 @@ type TableProps<T extends Record<string, any>> = {
|
||||||
export function Table<T extends Record<string, any>>(props: TableProps<T>) {
|
export function Table<T extends Record<string, any>>(props: TableProps<T>) {
|
||||||
const [selection, setSelection] = createSignal<SelectionItem<keyof T, T>[]>([]);
|
const [selection, setSelection] = createSignal<SelectionItem<keyof T, T>[]>([]);
|
||||||
|
|
||||||
const rows = createMemo(() => props.rows);
|
|
||||||
const rows = createMemo(() => props.rows);
|
const rows = createMemo(() => props.rows);
|
||||||
const columns = createMemo<Column<T>[]>(() => props.columns ?? []);
|
const columns = createMemo<Column<T>[]>(() => props.columns ?? []);
|
||||||
const selectionMode = createMemo(() => props.selectionMode ?? SelectionMode.None);
|
const selectionMode = createMemo(() => props.selectionMode ?? SelectionMode.None);
|
||||||
const cellRenderers = createMemo<CellEditors<T>>(() => props.children ?? {});
|
const cellRenderers = createMemo<CellEditors<T>>(() => props.children ?? {});
|
||||||
|
|
||||||
const context: TableContextType<T> = {
|
const context: TableContextType<T> = {
|
||||||
const context: TableContextType<T> = {
|
rows,
|
||||||
rows,
|
columns,
|
||||||
columns,
|
selection,
|
||||||
selection,
|
selectionMode,
|
||||||
selectionMode,
|
cellRenderers,
|
||||||
cellRenderers,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
return <TableContext.Provider value={context}>
|
return <TableContext.Provider value={context}>
|
||||||
<SelectionProvider selection={setSelection} multiSelect={props.selectionMode === SelectionMode.Multiple}>
|
<SelectionProvider selection={setSelection} multiSelect={props.selectionMode === SelectionMode.Multiple}>
|
||||||
|
@ -89,12 +77,10 @@ export function Table<T extends Record<string, any>>(props: TableProps<T>) {
|
||||||
</TableContext.Provider>;
|
</TableContext.Provider>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type InnerTableProps<T extends Record<string, any>> = { class?: string, summary?: string, rows: DataSet<T> };
|
|
||||||
type InnerTableProps<T extends Record<string, any>> = { class?: string, summary?: string, rows: DataSet<T> };
|
type InnerTableProps<T extends Record<string, any>> = { class?: string, summary?: string, rows: DataSet<T> };
|
||||||
|
|
||||||
function InnerTable<T extends Record<string, any>>(props: InnerTableProps<T>) {
|
function InnerTable<T extends Record<string, any>>(props: InnerTableProps<T>) {
|
||||||
const table = useTable<T>();
|
const table = useTable<T>();
|
||||||
const table = useTable<T>();
|
|
||||||
|
|
||||||
const selectable = createMemo(() => table.selectionMode() !== SelectionMode.None);
|
const selectable = createMemo(() => table.selectionMode() !== SelectionMode.None);
|
||||||
const columnCount = createMemo(() => table.columns().length);
|
const columnCount = createMemo(() => table.columns().length);
|
||||||
|
@ -109,9 +95,8 @@ function InnerTable<T extends Record<string, any>>(props: InnerTableProps<T>) {
|
||||||
|
|
||||||
<tbody class={css.main}>
|
<tbody class={css.main}>
|
||||||
<For each={props.rows.value()}>{
|
<For each={props.rows.value()}>{
|
||||||
<For each={props.rows.value()}>{
|
node => <Node node={node} depth={0} />
|
||||||
node => <Node node={node} depth={0} />
|
}</For>
|
||||||
}</For>
|
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
||||||
{/* <Show when={true}>
|
{/* <Show when={true}>
|
||||||
|
@ -127,11 +112,8 @@ function InnerTable<T extends Record<string, any>>(props: InnerTableProps<T>) {
|
||||||
function Api<T extends Record<string, any>>(props: { api: undefined | ((api: TableApi<T>) => any) }) {
|
function Api<T extends Record<string, any>>(props: { api: undefined | ((api: TableApi<T>) => any) }) {
|
||||||
const table = useTable<T>();
|
const table = useTable<T>();
|
||||||
const selectionContext = useSelection<T>();
|
const selectionContext = useSelection<T>();
|
||||||
const table = useTable<T>();
|
|
||||||
const selectionContext = useSelection<T>();
|
|
||||||
|
|
||||||
const api: TableApi<T> = {
|
const api: TableApi<T> = {
|
||||||
selection: selectionContext.selection,
|
|
||||||
selection: selectionContext.selection,
|
selection: selectionContext.selection,
|
||||||
rows: table.rows,
|
rows: table.rows,
|
||||||
columns: table.columns,
|
columns: table.columns,
|
||||||
|
@ -181,7 +163,6 @@ function Head(props: {}) {
|
||||||
|
|
||||||
<For each={table.columns()}>{
|
<For each={table.columns()}>{
|
||||||
({ id, label, sortable }) => {
|
({ id, label, sortable }) => {
|
||||||
const sort = createMemo(() => table.rows().sort());
|
|
||||||
const sort = createMemo(() => table.rows().sort());
|
const sort = createMemo(() => table.rows().sort());
|
||||||
const by = String(id);
|
const by = String(id);
|
||||||
|
|
||||||
|
@ -219,53 +200,46 @@ function Head(props: {}) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function Node<T extends Record<string, any>>(props: { node: DataSetNode<keyof T, T>, depth: number, groupedBy?: keyof T }) {
|
function Node<T extends Record<string, any>>(props: { node: DataSetNode<keyof T, T>, depth: number, groupedBy?: keyof T }) {
|
||||||
function Node<T extends Record<string, any>>(props: { node: DataSetNode<keyof T, T>, depth: number, groupedBy?: keyof T }) {
|
return <Switch>
|
||||||
return <Switch>
|
<Match when={props.node.kind === 'row' ? props.node : undefined}>{
|
||||||
<Match when={props.node.kind === 'row' ? props.node : undefined}>{
|
row => <Row key={row().key} value={row().value} depth={props.depth} groupedBy={props.groupedBy} />
|
||||||
row => <Row key={row().key} value={row().value} depth={props.depth} groupedBy={props.groupedBy} />
|
}</Match>
|
||||||
}</Match>
|
|
||||||
|
|
||||||
<Match when={props.node.kind === 'group' ? props.node : undefined}>{
|
<Match when={props.node.kind === 'group' ? props.node : undefined}>{
|
||||||
group => <Group key={group().key} groupedBy={group().groupedBy} nodes={group().nodes} depth={props.depth} />
|
group => <Group key={group().key} groupedBy={group().groupedBy} nodes={group().nodes} depth={props.depth} />
|
||||||
}</Match>
|
}</Match>
|
||||||
</Switch>;
|
</Switch>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Row<T extends Record<string, any>>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) {
|
function Row<T extends Record<string, any>>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) {
|
||||||
const table = useTable<T>();
|
const table = useTable<T>();
|
||||||
const context = useSelection<T>();
|
const context = useSelection<T>();
|
||||||
const columns = table.columns;
|
const columns = table.columns;
|
||||||
|
|
||||||
function Row<T extends Record<string, any>>(props: { key: keyof T, value: T, depth: number, groupedBy?: keyof T }) {
|
const isSelected = context.isSelected(props.key);
|
||||||
const table = useTable<T>();
|
|
||||||
const context = useSelection<T>();
|
|
||||||
const columns = table.columns;
|
|
||||||
|
|
||||||
const isSelected = context.isSelected(props.key);
|
return <tr class={css.row} style={{ '--depth': props.depth }} use:selectable={{ value: props.value, key: props.key }}>
|
||||||
|
<Show when={table.selectionMode() !== SelectionMode.None}>
|
||||||
|
<th class={css.checkbox}>
|
||||||
|
<input type="checkbox" checked={isSelected()} on:input={() => context.select([props.key])} on:pointerdown={e => e.stopPropagation()} />
|
||||||
|
</th>
|
||||||
|
</Show>
|
||||||
|
|
||||||
return <tr class={css.row} style={{ '--depth': props.depth }} use:selectable={{ value: props.value, key: props.key }}>
|
<For each={columns()}>{
|
||||||
<Show when={table.selectionMode() !== SelectionMode.None}>
|
({ id }) => <td class={'css.cell'}>{table.cellRenderers()[id]?.({ row: props.key as number, column: id, value: props.value[id] }) ?? props.value[id]}</td>
|
||||||
<th class={css.checkbox}>
|
}</For>
|
||||||
<input type="checkbox" checked={isSelected()} on:input={() => context.select([props.key])} on:pointerdown={e => e.stopPropagation()} />
|
</tr>;
|
||||||
</th>
|
};
|
||||||
</Show>
|
|
||||||
|
|
||||||
<For each={columns()}>{
|
function Group<T extends Record<string, any>>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode<keyof T, T>[], depth: number }) {
|
||||||
({ id }) => <td class={'css.cell'}>{table.cellRenderers()[id]?.({ row: props.key as number, column: id, value: props.value[id] }) ?? props.value[id]}</td>
|
const table = useTable();
|
||||||
}</For>
|
|
||||||
</tr>;
|
|
||||||
};
|
|
||||||
|
|
||||||
function Group<T extends Record<string, any>>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode<keyof T, T>[], depth: number }) {
|
return <details open>
|
||||||
function Group<T extends Record<string, any>>(props: { key: keyof T, groupedBy: keyof T, nodes: DataSetNode<keyof T, T>[], depth: number }) {
|
<summary style={{ '--depth': props.depth }}>{String(props.key)}</summary>
|
||||||
const table = useTable();
|
<summary style={{ '--depth': props.depth }}>{String(props.key)}</summary>
|
||||||
|
|
||||||
return <details open>
|
<For each={props.nodes}>{
|
||||||
<summary style={{ '--depth': props.depth }}>{String(props.key)}</summary>
|
node => <Node node={node} depth={props.depth + 1} groupedBy={props.groupedBy} />
|
||||||
<summary style={{ '--depth': props.depth }}>{String(props.key)}</summary>
|
}</For>
|
||||||
|
</details>;
|
||||||
<For each={props.nodes}>{
|
};
|
||||||
node => <Node node={node} depth={props.depth + 1} groupedBy={props.groupedBy} />
|
|
||||||
}</For>
|
|
||||||
</details>;
|
|
||||||
};
|
|
Loading…
Add table
Add a link
Reference in a new issue