From 2dc2609cfdb7032b204a85989861554e653e53e7 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 5 Sep 2024 12:24:38 -0600 Subject: [PATCH] feat: support vertical orientation --- examples/basic.ts | 6 +++++- src/table.tsx | 33 +++++++++++++++++++++++++++++++-- src/types.ts | 23 +++++++++++++++++++++-- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/examples/basic.ts b/examples/basic.ts index 28d8669..4b14b1f 100644 --- a/examples/basic.ts +++ b/examples/basic.ts @@ -1846,12 +1846,13 @@ makeTable({ borderStyle: 'headers-only-with-underline', columns: ['version', 'channel', 'location'], data: versions, - // filter: (row) => /^.+$/.test(row.channel), + filter: (row) => /^.+$/.test(row.channel), headerOptions: { bold: true, color: '#905de8', formatter: 'capitalCase', }, + orientation: 'vertical', overflow: 'wrap', sort: { channel(a, b) { @@ -1867,7 +1868,9 @@ makeTable({ }, }, }) + console.log() + makeTable({ // align: 'center', borderStyle: 'headers-only-with-underline', @@ -1880,6 +1883,7 @@ makeTable({ color: 'blueBright', formatter: 'capitalCase', }, + orientation: 'vertical', overflow: 'wrap', sort: { fullName: 'asc', diff --git a/src/table.tsx b/src/table.tsx index a256fc2..1847d2d 100644 --- a/src/table.tsx +++ b/src/table.tsx @@ -64,6 +64,7 @@ export function Table(props: TableProps) { data, filter, maxWidth, + orientation = 'horizontal', overflow = 'truncate', padding = 1, sort, @@ -134,6 +135,35 @@ export function Table(props: TableProps) { skeleton: BORDER_SKELETONS[config.borderStyle].separator, }) + if (orientation === 'vertical') { + return ( + + {processedData.map((row, index) => { + // Calculate the hash of the row based on its value and position + const key = `row-${sha1(row)}-${index}` + const maxKeyLength = Math.max(...Object.values(headings).map((c) => c.length)) + // Construct a row. + return ( + + {/* print all data in key:value pairs */} + {columns.map((column) => { + const value = (row[column.column] ?? '').toString() + const keyName = (headings[column.key] ?? column.key).toString() + const keyPadding = ' '.repeat(maxKeyLength - keyName.length + padding) + return ( + + {keyName}{keyPadding} + {value} + + ) + })} + + ) + })} + + ) + } + return ( {headerComponent({columns, data: {}, key: 'header'})} @@ -266,7 +296,6 @@ export function Skeleton(props: React.PropsWithChildren & {readonly height?: num * @param options see {@link TableProps} */ export function makeTable(options: TableProps): void { - const {data, ...props} = options || {data: []} - const instance = render() + const instance = render(
) instance.unmount() } diff --git a/src/types.ts b/src/types.ts index ff13775..bf60114 100644 --- a/src/types.ts +++ b/src/types.ts @@ -108,11 +108,11 @@ export type TableProps = { */ headerOptions?: HeaderOptions /** - * Border style for the table. Defaults to 'all'. + * Border style for the table. Defaults to 'all'. Only applies to horizontal orientation. */ borderStyle?: BorderStyle /** - * Align data in columns. Defaults to 'left'. + * Align data in columns. Defaults to 'left'. Only applies to horizontal orientation. */ align?: ColumnAlignment /** @@ -148,6 +148,25 @@ export type TableProps = { * ``` */ sort?: Sort + /** + * The orientation of the table. Defaults to 'horizontal'. + * + * If 'vertical', individual records will be displayed vertically in key:value pairs. + * + * @example + * ``` + * ───────────── + * Name Alice + * Id 36329 + * Age 20 + * ───────────── + * Name Bob + * Id 49032 + * Age 21 + * ───────────── + * ``` + */ + orientation?: 'horizontal' | 'vertical' } export type Config = {