Skip to content

Commit

Permalink
Merge pull request #8 from bryanmylee/rename-useTable
Browse files Browse the repository at this point in the history
`table.createViewModel`
  • Loading branch information
bryanmylee authored May 13, 2022
2 parents ae29e65 + c9558da commit 5f7eaf9
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-headless-table",
"version": "0.4.1",
"version": "0.5.0",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/createTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import type { AnyPlugins } from './types/TablePlugin';
import type { ReadOrWritable } from './utils/store';
import { getDuplicates } from './utils/array';
import { createViewModel, type TableViewModel } from './createViewModel';

export class Table<Item, Plugins extends AnyPlugins = AnyPlugins> {
data: ReadOrWritable<Item[]>;
Expand Down Expand Up @@ -50,6 +51,10 @@ export class Table<Item, Plugins extends AnyPlugins = AnyPlugins> {
group(def: GroupColumnInit<Item, Plugins>): GroupColumn<Item, Plugins> {
return new GroupColumn(def);
}

createViewModel(columns: Column<Item, Plugins>[]): TableViewModel<Item, Plugins> {
return createViewModel(this, columns);
}
}

export const createTable = <Item, Plugins extends AnyPlugins = AnyPlugins>(
Expand Down
15 changes: 12 additions & 3 deletions src/lib/useTable.ts → src/lib/createViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ import type {
} from './types/TablePlugin';
import { nonUndefined } from './utils/filter';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface TableViewModel<Item, Plugins extends AnyPlugins = AnyPlugins> {
dataColumns: DataColumn<Item, Plugins>[];
visibleColumns: Readable<DataColumn<Item, Plugins>[]>;
headerRows: Readable<HeaderRow<Item, Plugins>[]>;
originalRows: Readable<BodyRow<Item, Plugins>[]>;
rows: Readable<BodyRow<Item, Plugins>[]>;
pageRows: Readable<BodyRow<Item, Plugins>[]>;
pluginStates: PluginStates<Plugins>;
}

export interface TableState<Item, Plugins extends AnyPlugins = AnyPlugins> {
data: ReadOrWritable<Item[]>;
columns: Column<Item, Plugins>[];
Expand All @@ -23,10 +32,10 @@ export interface TableState<Item, Plugins extends AnyPlugins = AnyPlugins> {
pageRows: Readable<BodyRow<Item>[]>;
}

export const useTable = <Item, Plugins extends AnyPlugins = AnyPlugins>(
export const createViewModel = <Item, Plugins extends AnyPlugins = AnyPlugins>(
table: Table<Item, Plugins>,
columns: Column<Item, Plugins>[]
) => {
): TableViewModel<Item, Plugins> => {
const { data, plugins } = table;

const dataColumns = getDataColumns(columns);
Expand Down
1 change: 0 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export { default as Render } from '$lib/components/Render.svelte';
export { Subscribe } from 'svelte-subscribe';
// table core
export { createTable } from '$lib/createTable';
export { useTable } from '$lib/useTable';
export { createRender, type RenderConfig } from '$lib/render';
2 changes: 1 addition & 1 deletion src/lib/plugins/useColumnFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BodyRow } from '$lib/bodyRows';
import type { TablePlugin, NewTablePropSet, DeriveRowsFn } from '$lib/types/TablePlugin';
import { derived, writable, type Readable, type Writable } from 'svelte/store';
import type { RenderConfig } from '$lib/render';
import type { TableState } from '$lib/useTable';
import type { TableState } from '$lib/createViewModel';

export interface ColumnFiltersState<Item> {
filterValues: Writable<Record<string, unknown>>;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tableComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ElementHook,
PluginTablePropSet,
} from './types/TablePlugin';
import type { TableState } from './useTable';
import type { TableState } from './createViewModel';

export interface TableComponentInit {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/Label.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TableState } from '$lib/useTable';
import type { TableState } from '$lib/createViewModel';
import type { RenderConfig } from '$lib/render';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/TablePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BodyRow, BodyRowAttributes } from '$lib/bodyRows';
import type { DataColumn } from '$lib/columns';
import type { HeaderCell, HeaderCellAttributes } from '$lib/headerCells';
import type { HeaderRow, HeaderRowAttributes } from '$lib/headerRows';
import type { TableState } from '$lib/useTable';
import type { TableState } from '$lib/createViewModel';
import type { Readable } from 'svelte/store';

export type TablePlugin<Item, PluginState, ColumnOptions, TablePropSet extends AnyTablePropSet> = (
Expand Down
4 changes: 2 additions & 2 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { derived, readable } from 'svelte/store';
import { Render, Subscribe, createTable, createRender, useTable } from '$lib';
import { Render, Subscribe, createTable, createRender } from '$lib';
import {
useColumnFilters,
useColumnOrder,
Expand Down Expand Up @@ -134,7 +134,7 @@
}),
]);
const { visibleColumns, headerRows, rows, pageRows, pluginStates } = useTable(table, columns);
const { visibleColumns, headerRows, pageRows, pluginStates } = table.createViewModel(columns);
const { sortKeys } = pluginStates.sort;
const { filterValues } = pluginStates.filter;
Expand Down

0 comments on commit 5f7eaf9

Please sign in to comment.