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

**Feature:** Add width to table columns #902

Merged
merged 5 commits into from
Jan 29, 2019
Merged
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
69 changes: 69 additions & 0 deletions src/Table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ You can render a simple use-case of the table by specifying a list of records an
/>
```

### With Fixed Layout

Tables perform better with a forced fixed layout, since the browser doesn't have to recalculate positions depending on the contents of the table. Here's what the same table looks like with a fixed layout.

```js
<Table
fixedLayout
data={[{ name: "Max", profession: "Carpenter" }, { name: "Moritz", profession: "Baker" }]}
columns={["name", "profession"]}
/>
```

### Simple Usage without Header

```js
Expand Down Expand Up @@ -78,6 +90,63 @@ const data = [
/>
```

### With custom widths per column

We can customize the look of the table by specifying a `width` property on certain columns.

```jsx
const data = [
{
name: "Mega Deal Dev",
lastUpdated: "2018-06-06",
tags: ["agent-view", "production"],
collaborators: [
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
],
},
{
name: "Mega Deal Dev",
lastUpdated: "2018-06-06",
tags: ["agent-view", "production"],
collaborators: [
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
],
},
{
name: "Toy Bundle",
lastUpdated: "2018-06-06",
tags: ["agent-view", "production"],
collaborators: [
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
{ photo: "https://graph.facebook.com/775278331/picture", name: "Tejas Kumar" },
],
},
]
;<Table
data={data}
columns={[
{ heading: "", cell: dataEntry => <Icon name="Box" color="info" /> },
{ heading: "Name", width: 600, cell: dataEntry => dataEntry.name },
{ heading: "Last updated", cell: dataEntry => dataEntry.lastUpdated },
{
heading: "Tags",
width: 100,
cell: dataEntry => dataEntry.tags.map((tag, tagIndex) => <Chip key={tagIndex}>{tag}</Chip>),
},
{ heading: "Collaborators", cell: dataEntry => <AvatarGroup avatars={dataEntry.collaborators} /> },
]}
onRowClick={(dataEntry, i) => console.log({ dataEntry, i })}
/>
```

### With icons

```jsx
Expand Down
23 changes: 19 additions & 4 deletions src/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ export interface TableProps<T> extends DefaultProps {
iconColor?: (dataEntry: T) => string
/** Remove the header? */
headless?: boolean
/** Fixed Layout for performance */
fixedLayout?: boolean
}

export interface Column<T> {
heading: React.ReactNode
cell: (dataEntry: T, index: number) => React.ReactNode
sortOrder?: "asc" | "desc"
onSortClick?: (order: "asc" | "desc") => void
width?: number
}

const Container = styled("table")(({ theme }) => ({
const Container = styled("table")<{ fixedLayout: TableProps<any>["fixedLayout"] }>(({ theme, fixedLayout }) => ({
width: "100%",
backgroundColor: theme.color.white,
textAlign: "left",
borderCollapse: "collapse",
fontSize: theme.font.size.small,
fontFamily: theme.font.family.main,
tableLayout: fixedLayout ? "fixed" : "initial",
}))

const Tr = styled("tr")<{ hover?: boolean; clickable?: boolean }>(({ hover, theme, clickable }) => ({
Expand Down Expand Up @@ -88,13 +92,21 @@ const ThContent = styled("span")<{ sorted?: boolean }>`
${props => props.sorted && `color: ${props.theme.color.text.light};`};
`

const Td = styled("td")(({ theme }) => ({
const Td = styled("td")<{ cellWidth?: Column<any>["width"] }>(({ theme, cellWidth }) => ({
verticalAlign: "middle",
borderBottom: `1px solid ${theme.color.separators.default}`,
color: theme.color.text.default,
hyphens: "auto",
"&:first-child": {
paddingLeft: theme.space.small,
},
...(cellWidth
? {
width: cellWidth,
wordBreak: "break-all",
wordWrap: "break-word",
}
: {}),
}))

const Actions = styled(Td)(({ theme }) => ({
Expand Down Expand Up @@ -148,6 +160,7 @@ function Table<T>({
icon,
iconColor,
headless,
fixedLayout,
...props
}: TableProps<T>) {
const standardizedColumns: Array<Column<T>> = columns.map(column => {
Expand All @@ -164,7 +177,7 @@ function Table<T>({
const hasIcons: boolean = Boolean(data[0]) && Boolean(icon) && Boolean(icon!(data[0]))

return (
<Container {...props}>
<Container fixedLayout={fixedLayout} {...props}>
{!headless && (
<Thead>
<Tr>
Expand Down Expand Up @@ -234,7 +247,9 @@ function Table<T>({
</IconCell>
)}
{standardizedColumns.map((column, columnIndex) => (
<Td key={columnIndex}>{column.cell(dataEntry, dataEntryIndex)}</Td>
<Td cellWidth={column.width} key={columnIndex}>
{column.cell(dataEntry, dataEntryIndex)}
</Td>
))}
{rowAction}
{onRowClick &&
Expand Down
4 changes: 2 additions & 2 deletions src/Table/__tests__/__snapshots__/Table.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`Table Component Should render 1`] = `
class="css-ar73w8-Messages"
/>
<table
class="css-39fsjl"
class="css-je0z3e"
>
<thead
class="css-x4ot8q"
Expand All @@ -22,7 +22,7 @@ exports[`Table Component Should render 1`] = `
class="css-16kb41"
>
<td
class="css-1lbz3s3"
class="css-1duroev"
colspan="0"
>
There are no records available
Expand Down