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

Functional components #274

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dist
node_modules
coverage
dist

.DS_Store
*.log*
7,099 changes: 7,099 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"object-hash": "^2.0.3"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
"@types/jest": "26.0.24",
"@types/node": "14.18.23",
"@types/object-hash": "1.3.4",
Expand All @@ -47,4 +48,4 @@
"ink": ">=3.0.0",
"react": ">=16.8.0"
}
}
}
121 changes: 121 additions & 0 deletions src/createRowComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Box } from 'ink'
import { CellProps } from '.'
import { Column, ScalarDict } from './types'
import React from 'react'

/**
* Intersperses a list of elements with another element.
*/
function intersperse<T, I>(
intersperser: (index: number) => I,
elements: T[],
): (T | I)[] {
// Intersparse by reducing from left.
let interspersed: (T | I)[] = elements.reduce((acc, element, index) => {
// Only add element if it's the first one.
if (acc.length === 0) return [element]
// Add the intersparser as well otherwise.
return [...acc, intersperser(index), element]
}, [] as (T | I)[])

return interspersed
}

type RowConfig = {
/**
* Component used to render cells.
*/
cell: (props: CellProps) => JSX.Element
/**
* Tells the padding of each cell.
*/
padding: number
/**
* Component used to render skeleton in the row.
*/
skeleton: {
component: (props: React.PropsWithChildren<{}>) => JSX.Element
/**
* Characters used in skeleton.
* | |
* (left)-(line)-(cross)-(line)-(right)
* | |
*/
left: string
right: string
cross: string
line: string
}
}

type RowProps<T extends ScalarDict> = {
key: string
propKey?: string
data: Partial<T>
columns: Column<T>[]
}

/**
* Constructs a Row element from the configuration.
*/
export function createRowComponent<T extends ScalarDict>(
config: RowConfig,
): (props: RowProps<T>) => JSX.Element {
/* This is a component builder. We return a function. */

const skeleton = config.skeleton
const SkeletonComponent = skeleton.component

/* Row */
return (props) => {
const defKey = props.propKey || props.key
return (
<Box flexDirection="row">
{/* Left */}
<SkeletonComponent>{skeleton.left}</SkeletonComponent>
{/* Data */}
{...intersperse(
(i) => {
const key = `${defKey}-hseparator-${i}`

// The horizontal separator.
return (
<SkeletonComponent key={key}>{skeleton.cross}</SkeletonComponent>
)
},

// Values.
props.columns.map((column, colI) => {
// content
const value = props.data[column.column]

if (value == undefined || value == null) {
const key = `${defKey}-empty-${column.key}`

return (
<config.cell key={key} column={colI}>
{skeleton.line.repeat(column.width)}
</config.cell>
)
} else {
const key = `${defKey}-cell-${column.key}`

// margins
const ml = config.padding
const mr = column.width - String(value).length - config.padding

return (
/* prettier-ignore */
<config.cell key={key} column={colI}>
{`${skeleton.line.repeat(ml)}${String(value)}${skeleton.line.repeat(mr)}`}
</config.cell>
)
}
}),
)}
{/* Right */}
<SkeletonComponent>{skeleton.right}</SkeletonComponent>
</Box>
)
}
}
Loading