Skip to content

Commit

Permalink
feat(data-table): add striped prop (tusen-ai#1591)
Browse files Browse the repository at this point in the history
* feat(data-table): striped data table.

* test(data-table): add striped test, only checking class.

* docs(CHANGELOG, demo): add en demo docs, add CHANGELOG

Co-authored-by: 07akioni <07akioni2@gmail.com>
  • Loading branch information
2 people authored and boya1 committed Nov 16, 2021
1 parent 33a7f94 commit cbd0703
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Pending

### Feats

- `n-data-table` add `striped` prop, closes [#1552](https://github.com/TuSimple/naive-ui/issues/1552).

## 2.20.3 (2021-11-15)

### Fixes
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Pending

### Feats

- `n-data-table` 新增 `striped` 属性,关闭 [#1552](https://github.com/TuSimple/naive-ui/issues/1552)

## 2.20.3 (2021-11-15)

### Fixes
Expand Down
1 change: 1 addition & 0 deletions src/data-table/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ virtual
custom-filter-menu
tree
flex-height
striped
```

## API
Expand Down
61 changes: 61 additions & 0 deletions src/data-table/demos/enUS/striped.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Striped

Using `striped` prop to make it easier to distinguish rows.

```html
<n-data-table :columns="columns" :data="data" :striped="true" />
```

```js
import { defineComponent } from 'vue'

const createColumns = () => {
return [
{
type: 'selection'
},
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
]
}

const createData = () => [
{
key: 0,
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park'
},
{
key: 1,
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park'
},
{
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park'
}
]

export default defineComponent({
setup () {
return {
data: createData(),
columns: createColumns()
}
}
})
```
1 change: 1 addition & 0 deletions src/data-table/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tree
flex-height
fixed-column-debug
scroll-debug
striped
```

## API
Expand Down
61 changes: 61 additions & 0 deletions src/data-table/demos/zhCN/striped.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 条纹

使用 `striped` 属性渲染条纹,使得表格明暗交替。

```html
<n-data-table :columns="columns" :data="data" :striped="true" />
```

```js
import { defineComponent } from 'vue'

const createColumns = () => {
return [
{
type: 'selection'
},
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
]
}

const createData = () => [
{
key: 0,
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park'
},
{
key: 1,
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park'
},
{
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park'
}
]

export default defineComponent({
setup () {
return {
data: createData(),
columns: createColumns()
}
}
})
```
7 changes: 6 additions & 1 deletion src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export const dataTableProps = {
type: Boolean as PropType<boolean | undefined>,
default: undefined
},
striped: {
type: Boolean as PropType<boolean | undefined>,
default: false
},
scrollX: [Number, String] as PropType<string | number>,
defaultCheckedRowKeys: {
type: Array as PropType<RowKey[]>,
Expand Down Expand Up @@ -502,7 +506,8 @@ export default defineComponent({
[`${mergedClsPrefix}-data-table--single-line`]: this.singleLine,
[`${mergedClsPrefix}-data-table--single-column`]: this.singleColumn,
[`${mergedClsPrefix}-data-table--loading`]: this.loading,
[`${mergedClsPrefix}-data-table--flex-height`]: this.flexHeight
[`${mergedClsPrefix}-data-table--flex-height`]: this.flexHeight,
[`${mergedClsPrefix}-data-table--striped`]: this.striped
}
]}
style={this.cssVars as CSSProperties}
Expand Down
3 changes: 3 additions & 0 deletions src/data-table/src/styles/index.cssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ export default c([
cM('loading', [
cB('data-table-wrapper', 'opacity: var(--opacity-loading);')
]),
cM('striped', [
cB('data-table-tr:nth-child(even)', [cB('data-table-td', 'background-color: var(--td-color-hover);')])
]),
cM('single-column', [
cB('data-table-td', {
borderBottom: '0 solid var(--merged-border-color)'
Expand Down
27 changes: 27 additions & 0 deletions src/data-table/tests/DataTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1183,4 +1183,31 @@ describe('props.columns', () => {
'width: 200px; min-width: 200px'
)
})

it('should work with `striped` prop', async () => {
const columns: DataTableColumns = [
{
title: 'Name',
key: 'name',
width: 100
},
{
title: 'Age',
key: 'age',
width: 200
}
]
const data = new Array(5).fill(0).map((_, index) => {
return {
name: index,
age: index
}
})
const wrapper = mount(() => (
<NDataTable columns={columns} data={data} striped={true} />
))
expect(wrapper.findAll('.n-data-table')[0].attributes('class')).toContain(
'n-data-table--striped'
)
})
})

0 comments on commit cbd0703

Please sign in to comment.