spaCy/website/src/components/table.js

99 lines
3.0 KiB
JavaScript
Raw Normal View History

import React from 'react'
import classNames from 'classnames'
import Icon from './icon'
import { isString } from './util'
import classes from '../styles/table.module.sass'
function isNum(children) {
return isString(children) && /^\d+[.,]?[\dx]+?(|x|ms|mb|gb|k|m)?$/i.test(children)
}
2020-08-18 15:39:40 +03:00
function getCellContent(cellChildren) {
const icons = {
2020-08-18 15:39:40 +03:00
'✅': { name: 'yes', variant: 'success', 'aria-label': 'positive' },
'❌': { name: 'no', variant: 'error', 'aria-label': 'negative' },
}
2020-08-18 15:39:40 +03:00
let children = isString(cellChildren) ? [cellChildren] : cellChildren
if (Array.isArray(children)) {
return children.map((child, i) => {
if (isString(child)) {
const icon = icons[child.trim()]
if (icon) {
const props = { ...icon, inline: i < children.length, 'aria-hidden': undefined }
return <Icon {...props} key={i} />
}
// Work around prettier auto-escape
if (child.startsWith('\\')) return child.slice(1)
}
return child
})
}
return children
}
2020-07-06 19:14:57 +03:00
function isDividerRow(children) {
2020-07-06 23:22:37 +03:00
if (children.length && children[0].props && children[0].props.name == 'td') {
2020-07-06 19:14:57 +03:00
const tdChildren = children[0].props.children
2020-07-09 20:43:25 +03:00
if (tdChildren && !Array.isArray(tdChildren) && tdChildren.props) {
2020-07-06 19:14:57 +03:00
return tdChildren.props.name === 'em'
}
}
return false
}
function isFootRow(children) {
2020-08-17 22:38:20 +03:00
const rowRegex = /^(RETURNS|YIELDS|CREATES|PRINTS|EXECUTES)/
if (children.length && children[0].props.name === 'td') {
const cellChildren = children[0].props.children
if (
cellChildren &&
cellChildren.props &&
cellChildren.props.children &&
isString(cellChildren.props.children)
) {
return rowRegex.test(cellChildren.props.children)
}
}
return false
}
2019-09-28 15:23:03 +03:00
export const Table = ({ fixed, className, ...props }) => {
const tableClassNames = classNames(classes.root, className, {
[classes.fixed]: fixed,
})
return <table className={tableClassNames} {...props} />
}
export const Th = props => <th className={classes.th} {...props} />
export const Tr = ({ evenodd = true, children, ...props }) => {
const foot = isFootRow(children)
2020-07-06 19:14:57 +03:00
const isDivider = isDividerRow(children)
const trClasssNames = classNames({
[classes.tr]: evenodd,
[classes.footer]: foot,
2020-07-06 19:14:57 +03:00
[classes.divider]: isDivider,
'table-footer': foot,
})
return (
<tr className={trClasssNames} {...props}>
{children}
</tr>
)
}
export const Td = ({ num, nowrap, className, children, ...props }) => {
const content = getCellContent(children)
const tdClassNames = classNames(classes.td, className, {
[classes.num]: num || isNum(children),
[classes.nowrap]: nowrap,
})
return (
<td className={tdClassNames} {...props}>
{content}
</td>
)
}