Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
}
}

&-row-striped {
.@{tablePrefixCls}-cell {
background: #fcf4f4;
}
}
// ================== Cell ==================
&-cell {
background: #f4f4f4;
Expand Down
8 changes: 8 additions & 0 deletions docs/demo/stripe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: stripe
nav:
title: Demo
path: /demo
---

<code src="../examples/stripe.tsx"></code>
46 changes: 46 additions & 0 deletions docs/examples/stripe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';

interface FieldType {
name?: string;
age?: string;
address?: string;
}

const columns: TableProps<FieldType>['columns'] = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
];

const data = [
{ name: 'John', age: '25', address: '1 A Street' },
{ name: 'Fred', age: '38', address: '2 B Street' },
{ name: 'Anne', age: '47', address: '3 C Street' },
{ name: 'Ben', age: '14', address: '4 C Street' },
{ name: 'Hali', age: '34', address: '5 C Street' },
{ name: 'Hama', age: '25', address: '6 C Street' },
];

const Demo = () => (
<div>
<h2>Table with stripe</h2>
<Table columns={columns} data={data} stripe />
</div>
);

export default Demo;
9 changes: 7 additions & 2 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
export interface BodyProps<RecordType> {
data: readonly RecordType[];
measureColumnWidth: boolean;
stripe: boolean;
}

function Body<RecordType>(props: BodyProps<RecordType>) {
if (process.env.NODE_ENV !== 'production') {
devRenderTimes(props);
}

const { data, measureColumnWidth } = props;
const { data, measureColumnWidth, stripe } = props;

const {
prefixCls,
Expand Down Expand Up @@ -96,10 +97,14 @@
if (data.length) {
rows = flattenData.map((item, idx) => {
const { record, indent, index: renderIndex, rowKey } = item;

let className = '';
if (stripe && idx % 2 === 1) {
className = `${prefixCls}-row-striped`;
}

Check warning on line 103 in src/Body/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/Body/index.tsx#L102-L103

Added lines #L102 - L103 were not covered by tests
return (
<BodyRow
classNames={bodyCls}
className={className}
styles={bodyStyles}
key={rowKey}
rowKey={rowKey}
Expand Down
5 changes: 4 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export interface TableProps<RecordType = any>

direction?: Direction;

stripe?: boolean;

sticky?: boolean | TableSticky;

rowHoverable?: boolean;
Expand Down Expand Up @@ -207,6 +209,7 @@ function Table<RecordType extends DefaultRecordType>(
scroll,
tableLayout,
direction,
stripe,

// Additional Part
title,
Expand Down Expand Up @@ -625,7 +628,7 @@ function Table<RecordType extends DefaultRecordType>(

// Body
const bodyTable = (
<Body data={mergedData} measureColumnWidth={fixHeader || horizonScroll || isSticky} />
<Body data={mergedData} stripe={stripe} measureColumnWidth={fixHeader || horizonScroll || isSticky} />
);

const bodyColGroup = (
Expand Down