Skip to content

Commit

Permalink
Merge pull request #50 from bryanmylee:label-with-cells
Browse files Browse the repository at this point in the history
Always pass table component into label
  • Loading branch information
bryanmylee committed Sep 9, 2022
2 parents a8bf6e4 + 99016bc commit 20c4c6e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 38 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
@@ -1,7 +1,7 @@
{
"name": "svelte-headless-table",
"description": "Unopinionated and extensible data tables for Svelte",
"version": "0.13.4",
"version": "0.14.0",
"scripts": {
"dev": "vite dev",
"build": "vite build",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bodyCells.HeaderCell.render.test.ts
Expand Up @@ -39,7 +39,7 @@ const state = {
it('renders dynamic label with state', () => {
const actual = new TestHeaderCell({
id: '0',
label: ({ columns }) => `${columns.length} columns`,
label: (_, { columns }) => `${columns.length} columns`,
colspan: 1,
colstart: 1,
});
Expand All @@ -53,7 +53,7 @@ it('renders dynamic label with state', () => {
it('throws if rendering dynamically without state', () => {
const actual = new TestHeaderCell({
id: '0',
label: ({ columns }) => `${columns.length} columns`,
label: (_, { columns }) => `${columns.length} columns`,
colspan: 1,
colstart: 1,
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bodyCells.ts
Expand Up @@ -101,7 +101,7 @@ export class DataBodyCell<
if (this.state === undefined) {
throw new Error('Missing `state` reference');
}
return this.label({ column: this.column, row: this.row, value: this.value }, this.state);
return this.label(this, this.state);
}

clone(): DataBodyCell<Item, Plugins> {
Expand Down Expand Up @@ -142,7 +142,7 @@ export class DisplayBodyCell<Item, Plugins extends AnyPlugins = AnyPlugins> exte
if (this.state === undefined) {
throw new Error('Missing `state` reference');
}
return this.label({ column: this.column, row: this.row }, this.state);
return this.label(this, this.state);
}

clone(): DisplayBodyCell<Item, Plugins> {
Expand Down
13 changes: 6 additions & 7 deletions src/lib/headerCells.ts
Expand Up @@ -5,10 +5,9 @@ import type { HeaderLabel } from './types/Label';
import type { AnyPlugins } from './types/TablePlugin';
import type { RenderConfig } from './render';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type HeaderCellInit<Item, Plugins extends AnyPlugins = AnyPlugins> = {
id: string;
label: HeaderLabel<Item>;
label: HeaderLabel<Item, Plugins>;
colspan: number;
colstart: number;
};
Expand All @@ -23,10 +22,10 @@ export abstract class HeaderCell<
Item,
Plugins extends AnyPlugins = AnyPlugins
> extends TableComponent<Item, Plugins, 'thead.tr.th'> {
label: HeaderLabel<Item>;
label: HeaderLabel<Item, Plugins>;
colspan: number;
colstart: number;
constructor({ id, label, colspan, colstart }: HeaderCellInit<Item>) {
constructor({ id, label, colspan, colstart }: HeaderCellInit<Item, Plugins>) {
super({ id });
this.label = label;
this.colspan = colspan;
Expand All @@ -39,7 +38,7 @@ export abstract class HeaderCell<
throw new Error('Missing `state` reference');
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.label(this.state as any);
return this.label(this, this.state as any);
}
return this.label;
}
Expand Down Expand Up @@ -152,7 +151,7 @@ export type FlatDisplayHeaderCellInit<Item, Plugins extends AnyPlugins = AnyPlug
FlatHeaderCellInit<Item, Plugins>,
'label'
> & {
label?: HeaderLabel<Item>;
label?: HeaderLabel<Item, Plugins>;
};

export class FlatDisplayHeaderCell<
Expand Down Expand Up @@ -225,7 +224,7 @@ export type GroupDisplayHeaderCellInit<Item, Plugins extends AnyPlugins = AnyPlu
GroupHeaderCellInit<Item, Plugins>,
'label' | 'colspan'
> & {
label?: HeaderLabel<Item>;
label?: HeaderLabel<Item, Plugins>;
colspan?: number;
};

Expand Down
25 changes: 5 additions & 20 deletions src/lib/types/Label.ts
@@ -1,37 +1,22 @@
import type { BodyRow } from '$lib/bodyRows';
import type { DataColumn, FlatColumn } from '$lib/columns';
import type { DataBodyCell, DisplayBodyCell } from '$lib/bodyCells';
import type { TableState } from '$lib/createViewModel';
import type { HeaderCell } from '$lib/headerCells';
import type { RenderConfig } from '$lib/render';
import type { AnyPlugins } from './TablePlugin';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DataLabel<Item, Plugins extends AnyPlugins = AnyPlugins, Value = any> = (
props: DataLabelProps<Item, Plugins, Value>,
cell: DataBodyCell<Item, Plugins, Value>,
state: TableState<Item, Plugins>
) => RenderConfig;

// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
export type DataLabelProps<Item, Plugins extends AnyPlugins = AnyPlugins, Value = any> = {
column: DataColumn<Item, Plugins>;
row: BodyRow<Item, Plugins>;
// Value type does not infer correctly in Svelte
// value: Value;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
};

export type DisplayLabel<Item, Plugins extends AnyPlugins = AnyPlugins> = (
props: DisplayLabelProps<Item, Plugins>,
cell: DisplayBodyCell<Item, Plugins>,
state: TableState<Item, Plugins>
) => RenderConfig;

export type DisplayLabelProps<Item, Plugins extends AnyPlugins = AnyPlugins> = {
column: FlatColumn<Item, Plugins>;
row: BodyRow<Item, Plugins>;
};

// If the function type is removed from the union, generics will not be
// inferred for subtypes.
export type HeaderLabel<Item, Plugins extends AnyPlugins = AnyPlugins> =
| RenderConfig
| ((state: TableState<Item, Plugins>) => RenderConfig);
| ((cell: HeaderCell<Item, Plugins>, state: TableState<Item, Plugins>) => RenderConfig);
13 changes: 9 additions & 4 deletions src/routes/index.svelte
Expand Up @@ -69,7 +69,7 @@
const columns = table.createColumns([
table.display({
id: 'selected',
header: ({ pluginStates }) => {
header: (_, { pluginStates }) => {
const { allPageRowsSelected, somePageRowsSelected } = pluginStates.select;
return createRender(SelectIndicator, {
isSelected: allPageRowsSelected,
Expand Down Expand Up @@ -134,14 +134,19 @@
},
}),
table.group({
header: ({ rows, pageRows }) =>
header: (_, { rows, pageRows }) =>
derived(
[rows, pageRows],
([_rows, _pageRows]) => `Name (${_rows.length} records, ${_pageRows.length} in page)`
),
columns: [
table.column({
header: createRender(Italic, { text: 'First Name' }),
header: (cell) => {
return createRender(
Italic,
derived(cell.props(), (_props) => ({ text: `First Name ${_props.sort.order}` }))
);
},
accessor: 'firstName',
plugins: {
group: {
Expand Down Expand Up @@ -171,7 +176,7 @@
],
}),
table.group({
header: ({ rows }) =>
header: (_, { rows }) =>
createRender(
Italic,
derived(rows, (_rows) => ({ text: `Info (${_rows.length} samples)` }))
Expand Down

1 comment on commit 20c4c6e

@vercel
Copy link

@vercel vercel bot commented on 20c4c6e Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.