-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.js
144 lines (120 loc) · 3.13 KB
/
table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import styled from "@emotion/styled"
import { cx } from "emotion"
import React from "react"
import { theme } from "../../utils/theme"
import { rhythm, scale } from "../../utils/typography"
function findTableDimensions(cells) {
let maxRows = 0
let columns = 0
let columnRows = 0
cells.forEach(({ props: { header, footer } }) => {
if (header) {
columnRows = 0
columns++
} else if (!footer) {
columnRows++
}
maxRows = columnRows > maxRows ? columnRows : maxRows
})
return { rows: maxRows + 1, columns } // +1 for headers row
}
function getLabels(cells) {
return cells
.filter(({ props: { label, children } }) => label && children)
.map(({ props: { children } }) => children)
}
function* getInfIter(array) {
let index = 0
while (true) {
yield array[index]
index = (index + 1) % array.length
}
}
const GridCell = styled.div`
padding-left: ${rhythm(2 / 3)};
padding-top: ${rhythm(2 / 3)};
&.label {
display: none;
@media (min-width: 500px) {
display: block;
}
}
&.header {
margin-top: ${rhythm(1)};
padding-bottom: ${rhythm(1 / 3)};
font-weight: ${theme.fonts.header.weights.bold};
border-bottom: 2px solid ${theme.colors.primary};
@media (min-width: 500px) {
grid-row: 1;
display: flex;
align-items: flex-end;
justify-content: center;
margin-top: 0;
}
}
&.footer {
grid-row: -1;
grid-column: 1 / -1;
margin-top: ${rhythm(2 / 3)};
padding-top: ${rhythm(1 / 4)};
padding-bottom: ${rhythm(1 / 4)};
font-weight: ${theme.fonts.body.weights.light};
border-top: 2px solid ${theme.colors.secondary};
}
`
const GridTable = styled.div`
${scale(-1 / 4)};
margin-bottom: ${rhythm(1.5)};
@media (min-width: 500px) {
display: grid;
grid-template-columns: repeat(${({ columns }) => columns}, 1fr);
grid-template-rows: repeat(${({ rows }) => rows}, min-content);
grid-auto-flow: column;
}
`
const Flex = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
padding-right: ${rhythm(2 / 3)};
@media (min-width: 500px) {
padding-right: 0;
.desc {
display: none;
}
}
`
export const Table = ({ children }) => {
const cells = React.Children.toArray(children).filter(
({ props: { mdxType } }) => mdxType === "Cell"
)
const { rows, columns } = findTableDimensions(cells)
const labelsIter = getInfIter(getLabels(cells))
const elements = cells.map((cell) => {
const { header, footer, label } = cell.props
if (header || footer || label) {
return cell
}
const { value: labelValue } = labelsIter.next()
return React.cloneElement(cell, { desc: labelValue })
})
return (
<GridTable role="table" rows={rows} columns={columns}>
{elements}
</GridTable>
)
}
export const Cell = ({ header, footer, label, desc, children }) => {
return (
<GridCell role="cell" className={cx({ header, footer, label })}>
{desc ? (
<Flex>
<div className="desc">{desc}</div>
<div>{children}</div>
</Flex>
) : (
children
)}
</GridCell>
)
}