Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Handle no table header specified case #371

Merged
merged 1 commit into from
Aug 13, 2021
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
96 changes: 96 additions & 0 deletions src/Table/Table.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Table density is an important characteristic when considering how to present inf
columns={[
{
id: "image",
headerTitle: "img",
render: ({ image }) => (
<img style={{ width: 24, height: 24 }} src={image} />
),
Expand Down Expand Up @@ -324,6 +325,101 @@ Table density is an important characteristic when considering how to present inf
</Story>
</Canvas>

### no table headers

<Canvas>
<Story name="no headers light">
<div style={{ padding: 16 }}>
<Table
keyOn="name"
data={[
{
name: "Alice Howell",
image: require("./table.stories/alice-howell.png"),
},
{
name: "Benjamin Lawrence",
image: require("./table.stories/benjamin-lawrence.png"),
},
{
name: "Cynthia Bowman",
image: require("./table.stories/cynthia-bowman.png"),
},
{
name: "Jeremy Jacobs",
image: require("./table.stories/jeremy-jacobs.png"),
},
{
name: "Jeremy Griffin",
image: require("./table.stories/jeremy-griffin.png"),
},
]}
columns={[
{
id: "image",
render: ({ image }) => (
<img style={{ width: 24, height: 24 }} src={image} />
),
},
{
id: "name",
render: ({ name }) => <>{name}</>,
},
]}
/>
</div>
</Story>
<Story name="no headers dark">
<div
style={{
background: colors.black.base,
borderRadius: 8,
padding: 16,
color: colors.white,
}}
>
<Table
theme="dark"
keyOn="name"
data={[
{
name: "Alice Howell",
image: require("./table.stories/alice-howell.png"),
},
{
name: "Benjamin Lawrence",
image: require("./table.stories/benjamin-lawrence.png"),
},
{
name: "Cynthia Bowman",
image: require("./table.stories/cynthia-bowman.png"),
},
{
name: "Jeremy Jacobs",
image: require("./table.stories/jeremy-jacobs.png"),
},
{
name: "Jeremy Griffin",
image: require("./table.stories/jeremy-griffin.png"),
},
]}
columns={[
{
id: "image",
render: ({ image }) => (
<img style={{ width: 24, height: 24 }} src={image} />
),
},
{
id: "name",
render: ({ name }) => <>{name}</>,
},
]}
/>
</div>
</Story>
</Canvas>

## `td` customization

You can customize how each column's `td` is rendered.
Expand Down
92 changes: 48 additions & 44 deletions src/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,52 +225,56 @@ export function Table<RowShape>({
))}
</colgroup>

<thead>
{React.cloneElement(
headTrElement,
{
className: cx(
css({
...typography.base.xsmall,
borderBottom: border,
borderTop: border,
color:
theme === "light"
? colors.grey.base
: theme === "dark"
? colors.midnight.lighter
: assertUnreachable(theme),
textAlign: "left",
textTransform: "uppercase",
}),
headTrElement.props.className,
),
},
<>
{columns.map(({ headerTitle, id, thAs = "th" }, colIndex) => {
const element = createElementFromAs(thAs);
{columns.filter((c) => c.headerTitle).length > 0 && (
<thead>
{React.cloneElement(
headTrElement,
{
className: cx(
css({
...typography.base.xsmall,
borderBottom: border,
borderTop: border,
color:
theme === "light"
? colors.grey.base
: theme === "dark"
? colors.midnight.lighter
: assertUnreachable(theme),
textAlign: "left",
textTransform: "uppercase",
}),
headTrElement.props.className,
),
},
<>
{columns.map(
({ headerTitle, id, thAs = "th" }, colIndex) => {
const element = createElementFromAs(thAs);

return React.cloneElement(
element,
{
className: css(
css({
fontWeight: 600,
padding,
paddingLeft: colIndex === 0 ? 0 : padding,
paddingRight:
colIndex === columns.length - 1 ? 0 : padding,
}),
element.props.className,
),
key: id,
return React.cloneElement(
element,
{
className: css(
css({
fontWeight: 600,
padding,
paddingLeft: colIndex === 0 ? 0 : padding,
paddingRight:
colIndex === columns.length - 1 ? 0 : padding,
}),
element.props.className,
),
key: id,
},
headerTitle,
);
},
headerTitle,
);
})}
</>,
)}
</thead>
)}
</>,
)}
</thead>
)}
<tbody>
{data.map((item, index) =>
React.cloneElement(
Expand Down