Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): add cell renderer and value parser #561

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions table/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ export type ICell = number | string | String | Cell;

export type Direction = "left" | "right" | "center";

export type ValueParser = (
value: string | number | undefined | null,
) => string | number | undefined | null;

export type Renderer = (value: string) => string;

/** Cell options. */
export interface ICellOptions {
border?: boolean;
colSpan?: number;
rowSpan?: number;
align?: Direction;
value?: ValueParser;
render?: Renderer;
}

/** Cell representation. */
Expand All @@ -36,21 +44,23 @@ export class Cell {

/**
* Cell constructor.
* @param value Cell value.
* @param cellValue Cell value.
*/
public constructor(private value: ICell) {}
public constructor(
private cellValue?: ICell | undefined | null,
) {}

/** Get cell value. */
public toString(): string {
return this.value.toString();
return this.cellValue?.toString() ?? "";
}

/**
* Set cell value.
* @param value Cell or cell value.
*/
public setValue(value: ICell): this {
this.value = value;
public setValue(value: ICell | undefined | null): this {
this.cellValue = value;
return this;
}

Expand Down Expand Up @@ -116,13 +126,31 @@ export class Cell {
return this;
}

/**
* Register cell value parser.
* @param fn Value parser callback function.
*/
public value(fn: ValueParser): this {
this.options.value = fn;
return this;
}

/**
* Register cell renderer. Will be called once for each line in the cell.
* @param fn Cell renderer callback function.
*/
public renderer(fn: Renderer): this {
this.options.render = fn;
return this;
}

/**
* Getter:
*/

/** Check if cell has border. */
public getBorder(): boolean {
return this.options.border === true;
public getBorder(): boolean | undefined {
return this.options.border;
}

/** Get col span. */
Expand All @@ -139,8 +167,18 @@ export class Cell {
: 1;
}

/** Get row span. */
public getAlign(): Direction {
return this.options.align ?? "left";
/** Get cell alignment. */
public getAlign(): Direction | undefined {
return this.options.align;
}

/** Get value parser. */
public getValueParser(): ValueParser | undefined {
return this.options.value;
}

/** Get cell renderer. */
public getRenderer(): Renderer | undefined {
return this.options.render;
}
}
103 changes: 99 additions & 4 deletions table/column.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,164 @@
import { Direction } from "./cell.ts";
import { Direction, Renderer, ValueParser } from "./cell.ts";

export interface ColumnOptions {
border?: boolean;
align?: Direction;
minWidth?: number;
maxWidth?: number;
padding?: number;
headerValue?: ValueParser;
cellValue?: ValueParser;
headerRenderer?: Renderer;
cellRenderer?: Renderer;
}

export class Column {
static from(options: ColumnOptions): Column {
/**
* Create column from existing column or column options.
* @param options Column options.
*/
static from(options: ColumnOptions | Column): Column {
const column = new Column();
column.opts = { ...options };
column.opts = { ...options instanceof Column ? options.opts : options };
return column;
}

protected opts: ColumnOptions = {};
constructor(
protected opts: ColumnOptions = {},
) {}

/**
* Set column options.
* @param options Column options.
*/
options(options: ColumnOptions): this {
Object.assign(this.opts, options);
return this;
}

/**
* Set min column width.
* @param width Min column width.
*/
minWidth(width: number): this {
this.opts.minWidth = width;
return this;
}

/**
* Set max column width.
* @param width Max column width.
*/
maxWidth(width: number): this {
this.opts.maxWidth = width;
return this;
}

/**
* Set column border.
* @param border
*/
border(border = true): this {
this.opts.border = border;
return this;
}

/**
* Set column left and right padding.
* @param padding Padding.
*/
padding(padding: number): this {
this.opts.padding = padding;
return this;
}

/**
* Set column alignment.
* @param direction Column alignment.
*/
align(direction: Direction): this {
this.opts.align = direction;
return this;
}

/**
* Register header value parser.
* @param fn Value parser callback function.
*/
headerValue(fn: ValueParser): this {
this.opts.headerValue = fn;
return this;
}

/**
* Register cell value parser.
* @param fn Value parser callback function.
*/
cellValue(fn: ValueParser): this {
this.opts.cellValue = fn;
return this;
}

/**
* Register header cell renderer. Will be called once for each line in the cell.
* @param fn Cell renderer callback function.
*/
headerRenderer(fn: Renderer): this {
this.opts.headerRenderer = fn;
return this;
}

/**
* Register cell renderer. Will be called once for each line in the cell.
* @param fn Cell renderer callback function.
*/
cellRenderer(fn: Renderer): this {
this.opts.cellRenderer = fn;
return this;
}

/** Get min column width. */
getMinWidth(): number | undefined {
return this.opts.minWidth;
}

/** Get max column width. */
getMaxWidth(): number | undefined {
return this.opts.maxWidth;
}

/** Get column border. */
getBorder(): boolean | undefined {
return this.opts.border;
}

/** Get column padding. */
getPadding(): number | undefined {
return this.opts.padding;
}

/** Get column alignment. */
getAlign(): Direction | undefined {
return this.opts.align;
}

/** Get header value parser. */
getHeaderValueParser(): ValueParser | undefined {
return this.opts.headerValue;
}

/** Get value parser. */
getCellValueParser(): ValueParser | undefined {
return this.opts.cellValue;
}

/** Get header renderer. */
getHeaderRenderer(): Renderer | undefined {
return this.opts.headerRenderer;
}

/** Get cell renderer. */
getCellRenderer(): Renderer | undefined {
return this.opts.cellRenderer;
}
}
Loading