Skip to content

Commit

Permalink
Table typescript take two (#682)
Browse files Browse the repository at this point in the history
* convert to typescript

* Change suffix

* Radius optional
  • Loading branch information
wenche authored and vnys committed Nov 13, 2020
1 parent 71d462d commit a6c3d92
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 170 deletions.
25 changes: 0 additions & 25 deletions libraries/core-react/src/Table/Body.jsx

This file was deleted.

12 changes: 12 additions & 0 deletions libraries/core-react/src/Table/Body.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { HTMLAttributes } from 'react'
import styled from 'styled-components'

const TableBase = styled.tbody``

type Props = HTMLAttributes<HTMLTableSectionElement>

export const Body = ({ children, ...props }: Props): JSX.Element => {
return <TableBase {...props}>{children}</TableBase>
}

Body.displayName = 'eds-table-body'
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// @ts-nocheck
import React from 'react'
import PropTypes from 'prop-types'
import React, { HTMLAttributes } from 'react'
import styled, { css } from 'styled-components'
import { getTokens } from './Table.tokens'
import type { Border } from '@equinor/eds-tokens'
import { getTokens, TableCell } from './Table.tokens'
import { typographyTemplate } from '../_common/templates'

const borderTemplate = (borders) =>
const borderTemplate = (borders: { bottom: Border }) =>
Object.keys(borders).reduce((acc, val) => {
const { color, width } = borders[val]
const { color, width }: Border = borders[val] as Border
return `${acc} border-${val}: ${width} solid ${color}; \n`
}, '')

const Base = ({ tokens, as }) => {
type BaseProps = {
tokens: TableCell
as: string
}
const Base = ({ tokens, as }: BaseProps) => {
const { background, height, text, spacings, borders } = tokens
const { typography } = text
const bordersAndBackground =
Expand Down Expand Up @@ -39,8 +42,19 @@ const TableBase = styled.td`
${Base}
`

export const Cell = (props) => {
const { children, as, variant } = props
type Props = {
/** Specifies which td or th to use */
as: 'td' | 'th'
/** Specifies which variant to use */
variant: 'text' | 'icon' | 'numeric' | 'input'
} & HTMLAttributes<HTMLTableCellElement>

export const Cell = ({
children,
as = 'td',
variant = 'text',
...props
}: Props): JSX.Element => {
const tokens = getTokens(as, variant)
return (
<TableBase as={as} tokens={tokens} {...props}>
Expand All @@ -49,24 +63,4 @@ export const Cell = (props) => {
)
}

Cell.propTypes = {
/** @ignore */
className: PropTypes.string,
/** @ignore */
children: PropTypes.node.isRequired,
/** Specifies which td or th to use */
as: PropTypes.oneOf(['td', 'th']),
/** Specifies the scope of th */
// scope: PropTypes.oneOf(['col', 'row']),
/** Specifies which variant to use */
variant: PropTypes.oneOf(['text', 'icon', 'numeric', 'input']),
}

Cell.defaultProps = {
className: '',
// scope: '',
as: 'td',
variant: 'text',
}

Cell.displayName = 'eds-table-cell'
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
// @ts-nocheck
import React from 'react'
import PropTypes from 'prop-types'
import React, { FunctionComponent } from 'react'
import styled, { css } from 'styled-components'
import { getTokens } from './Table.tokens'
import { getTokens, TableCell } from './Table.tokens'
import type { Border } from '@equinor/eds-tokens'

const borderTemplate = (borders) =>
const borderTemplate = (borders: { bottom: Border }): string =>
Object.keys(borders).reduce((acc, val) => {
const { color, width } = borders[val]
const { color, width }: Border = borders[val] as Border
return `${acc} border-${val}: ${width} solid ${color}; \n`
}, '')

const StyledTableHead = styled.thead`
type StyledTableHeadProps = {
token: TableCell
}

const StyledTableHead = styled.thead<StyledTableHeadProps>`
${({ token: { borders, background } }) => css`
${borderTemplate(borders)}
background: ${background};`}
`

export const Head = ({ children, ...props }) => {
export const Head: FunctionComponent = ({ children, ...props }) => {
const token = getTokens('th', 'text')
return (
<StyledTableHead token={token} {...props}>
Expand All @@ -25,15 +28,4 @@ export const Head = ({ children, ...props }) => {
)
}

Head.propTypes = {
/** @ignore */
className: PropTypes.string,
/** @ignore */
children: PropTypes.node.isRequired,
}

Head.defaultProps = {
className: '',
}

Head.displayName = 'eds-table-head'
25 changes: 0 additions & 25 deletions libraries/core-react/src/Table/Row.jsx

This file was deleted.

14 changes: 14 additions & 0 deletions libraries/core-react/src/Table/Row.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { FunctionComponent } from 'react'
import styled from 'styled-components'

const TableBase = styled.tr``

type Props = React.HTMLAttributes<HTMLTableRowElement>

export const Row: FunctionComponent<Props> = (props) => {
const { children } = props

return <TableBase {...props}>{children}</TableBase>
}

Row.displayName = 'EdsTableRow'
27 changes: 0 additions & 27 deletions libraries/core-react/src/Table/Table.jsx

This file was deleted.

23 changes: 0 additions & 23 deletions libraries/core-react/src/Table/Table.tokens.js

This file was deleted.

99 changes: 99 additions & 0 deletions libraries/core-react/src/Table/Table.tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import tableTokens from '@equinor/eds-tokens/components/table/table.json'
import type {
Border,
Clickbound,
Spacing,
Typography,
} from '@equinor/eds-tokens'

const { header, cell } = tableTokens

type Field = {
height: string
background: string
borders: {
outline: Border
}
text: {
color: string
typography: Typography
colorPlaceholder: string
}
spacings: Spacing
}

export type TableCell = {
height: string
background: string
borders: {
bottom: Border
}
text?: {
color: string
typography: Typography
}
spacings: Spacing
clickbound: Clickbound
field?: Field
active: {
background: string
color?: string
typography?: Typography
borders: {
bottom: Border
}
field?: Field
}
disabled: {
background: string
color?: string
typography?: Typography
borders: {
bottom: Border
}
field?: Field
}
focus: {
type?: string
color?: string
width?: string
gap?: string
field?: Field
}
hover: {
background: string
field?: Field
}
}

type Variants = {
header: {
text: TableCell
}
cell: {
text: TableCell
numeric: TableCell
icon: TableCell
input: TableCell
}
}

const variants: Variants = {
header: { text: header.text },
cell: {
text: cell.text,
numeric: cell.monospaced_numeric,
icon: cell.icon,
input: cell.input,
},
}

export const getTokens = (as: string, variant: string): TableCell => {
switch (as) {
case 'th':
return variants.header[variant] as TableCell
case 'td':
default:
return variants.cell[variant] as TableCell
}
}
15 changes: 15 additions & 0 deletions libraries/core-react/src/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { FunctionComponent, HTMLAttributes } from 'react'
import styled from 'styled-components'

const TableBase = styled.table`
border-spacing: 0;
border-collapse: collapse;
`

type Props = HTMLAttributes<HTMLTableElement>

export const Table: FunctionComponent<Props> = ({ children, ...props }) => {
return <TableBase {...props}>{children}</TableBase>
}

Table.displayName = 'EdsTable'
15 changes: 0 additions & 15 deletions libraries/core-react/src/Table/index.js

This file was deleted.

Loading

0 comments on commit a6c3d92

Please sign in to comment.