Skip to content

Commit

Permalink
feat(data-table): add row-props prop
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Jun 7, 2021
1 parent d7b96e6 commit 15ae234
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `n-switch` supports wai-aria.
- `n-menu` supports basic wai-aria.
- `n-divider` supports basic wai-aria.
- `n-data-table` add `row-props` prop.

### Fixes

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `n-switch` 支持 wai-aria
- `n-menu` 部分支持 wai-aria
- `n-divider` 部分支持 wai-aria
- `n-data-table` 新增 `row-props` 属性

### Fixes

Expand Down
24 changes: 13 additions & 11 deletions src/data-table/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ basic
empty
border
size
row-props
merge-cell
filter-and-sorter
select
Expand Down Expand Up @@ -47,16 +48,17 @@ tree
| cascade | `boolean` | `true` | Whether to do cascade checking when using tree data. |
| children-key | `string` | `'children'` | The key of children data in tree data's data entity. |
| columns | `Array<Column>` | `[]` | Columns to display. |
| data | `Array<Object>` | `[]` | Data to display. |
| data | `Array<object>` | `[]` | Data to display. |
| default-checked-row-keys | `Array<string \| number>` | `[]` | |
| indent | `number` | `16` | Indent of row content when using tree data. |
| loading | `boolean` | `false` | |
| max-height | `number \| string` | `undefined` | The max-height of the table content. Can be a CSS value. |
| min-height | `number \| string` | `undefined` | The min-height of the table content. Can be a CSS value. |
| pagination | `false \| Object` | `false` | See [Pagination props](pagination#Props) |
| pagination | `false \| object` | `false` | See [Pagination props](pagination#Props) |
| paging | `boolean` | `true` | If data-table do automatic paging. You may set it to `false` in async usage. |
| row-class-name | `string \| (rowData: Object, rowIndex : number) => string \| Object` | `undefined` | |
| row-key | `(rowData: Object) => number \| string` | `undefined` | Generate the key of the row by row data (if you don't want to set the key) |
| row-class-name | `string \| (rowData: object, rowIndex : number) => string \| object` | `undefined` | |
| row-key | `(rowData: object) => number \| string` | `undefined` | Generate the key of the row by row data (if you don't want to set the key) |
| row-props | `(rowData: object, rowIndex : number) => object` | `undefined` | |
| scroll-x | `number \| string` | `undefined` | If columns are horizontal fixed, scroll-x need to be set |
| single-column | `boolean` | `false` | |
| single-line | `boolean` | `true` | |
Expand Down Expand Up @@ -91,14 +93,14 @@ These methods can help you control table in an uncontrolled manner. However, it'
| align | `'left' \| 'right' \| 'center'` | `'left'` | Text align in column |
| children | `Column[]` | `undefined` | Child nodes of a grouped column |
| className | `string` | `undefined` | |
| colSpan | `(rowData: Object, rowIndex: number) => number` | `undefined` | |
| colSpan | `(rowData: object, rowIndex: number) => number` | `undefined` | |
| defaultFilterOptionValue | `string \| number \| null` | `null` | The default active filter option value in uncontrolled manner. (works when not using multiple filters) |
| defaultFilterOptionValues | `Array<string \| number>` | `[]` | The default active filter option values in uncontrolled manner. (works when there are multiple filters) |
| defaultSortOrder | `'descend' \| 'ascend' \| false` | `false` | The default sort order of the table in uncontrolled manner |
| disabled | `(rowData: Object, rowIndex: number) => boolean` | `() => false` | |
| disabled | `(rowData: object, rowIndex: number) => boolean` | `() => false` | |
| ellipsis | `boolean \| EllipsisProps` | `false` | |
| expandable | `(rowData: Object, rowIndex: number) => boolean` | `undefined` | Whethe the row is expandable. Only works when `type` is `'expand'`. |
| filter | `boolean \| (optionValue: string \| number, rowData: Object) => boolean \| 'default'` | `false` | The filter of the column. If set to `true`, it will only display filter button on the column, which can be used in async status. |
| expandable | `(rowData: object, rowIndex: number) => boolean` | `undefined` | Whethe the row is expandable. Only works when `type` is `'expand'`. |
| filter | `boolean \| (optionValue: string \| number, rowData: object) => boolean \| 'default'` | `false` | The filter of the column. If set to `true`, it will only display filter button on the column, which can be used in async status. |
| filterMode | `'and' \| 'or'` | `'or'` | |
| filterMultiple | `boolean` | `true` | |
| filterOptionValue | `string \| number \| null` | `undefined` | The active filter option value in controlled manner. If not set, the filter of the column works in an uncontrolled manner. (works when not using multiple filters) |
Expand All @@ -107,12 +109,12 @@ These methods can help you control table in an uncontrolled manner. However, it'
| fixed | `'left \| 'right' \| false` | `false` | |
| key | `string \| number` | `undefined` | Unique key of this column, **required** when table's row-key is not set. |
| options | `Array<'all' \| 'none' \| { label: string, key: string \| number, onSelect: (pageData: RowData) => void }>` | `undefined` | Options of custom selection. Only work with `type='selection'` |
| render | `(rowData: Object, rowIndex: number) => VNodeChild` | `undefined` | Render function of column row cell. |
| renderExpand | `(rowData: Object, rowIndex: number) => VNodeChild` | `undefined` | Render function of the expand area. Only works when `type` is `'expand'`. |
| render | `(rowData: object, rowIndex: number) => VNodeChild` | `undefined` | Render function of column row cell. |
| renderExpand | `(rowData: object, rowIndex: number) => VNodeChild` | `undefined` | Render function of the expand area. Only works when `type` is `'expand'`. |
| renderFilterMenu | `() => VNodeChild` | `undefined` | Render function of column filter menu. |
| renderFilterIcon | `(options: { active: boolean, show: boolean }) => VNodeChild` | `undefined` | Render function of column filter icon. |
| renderFilter | `(options: { active: boolean, show: boolean }) => VNodeChild` | `undefined` | Render function of column filter trigger. |
| rowSpan | `(rowData: Object, rowIndex: number) => number` | `undefined` | |
| rowSpan | `(rowData: object, rowIndex: number) => number` | `undefined` | |
| sortOrder | `'descend' \| 'ascend' \| false` | `undefined` | The controlled sort order of the column. If multiple columns' sortOrder is set, the first one will affect. |
| sorter | `boolean \| function \| 'default'` | `false` | The sorter of the column. If set `'default'`, it will use a basic builtin compare function. If set to `true`, it will only display sort icon on the column, which can be used in async status. Otherwise it works like `Array.sort`'s compare function. |
| title | `string \| (() => VNodeChild)` | `undefined` | Can be a render function. |
Expand Down
62 changes: 62 additions & 0 deletions src/data-table/demos/enUS/row-props.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Custom Row Props

If you want to add attr or event handlers to a row, use `row-props` prop.

```html
<n-data-table :columns="columns" :data="data" :row-props="rowProps" />
```

```js
import { defineComponent } from 'vue'
import { useMessage } from 'naive-ui'

export default defineComponent({
setup () {
const message = useMessage()
return {
rowProps: (row) => {
return {
style: 'cursor: pointer;',
onClick: () => {
message.info(row.name)
}
}
},
columns: [
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
],
data: [
{
key: 0,
name: '07akioni',
age: '18',
address: 'Yiheyuan Road'
},
{
key: 1,
name: '08akioni',
age: '14',
address: 'Pingshan Road'
},
{
key: 2,
name: '09akioni',
age: '22',
address: 'Haidian Bridge'
}
]
}
}
})
```
24 changes: 13 additions & 11 deletions src/data-table/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ basic
empty
border
size
row-props
merge-cell
filter-and-sorter
select
Expand Down Expand Up @@ -47,16 +48,17 @@ tree
| cascade | `boolean` | `true` | 在进行树型数据选择的时候是否级联 |
| children-key | `string` | `'children'` | 树形数据下后代节点在数据中的 key |
| columns | `Array<Column>` | `[]` | 需要展示的列 |
| data | `Array<Object>` | `[]` | 需要展示的数据 |
| data | `Array<object>` | `[]` | 需要展示的数据 |
| default-checked-row-keys | `Array<string \| number>` | `[]` | |
| indent | `number` | `16` | 使用树形数据时行内容的缩进 |
| loading | `boolean` | `false` | |
| max-height | `number \| string` | `undefined` | 表格内容的最大高度,可以是 CSS 属性值 |
| min-height | `number \| string` | `undefined` | 表格内容的最低高度,可以是 CSS 属性值 |
| pagination | `false \| Object` | `false` | 属性参考 [Pagination props](pagination#Props) |
| pagination | `false \| object` | `false` | 属性参考 [Pagination props](pagination#Props) |
| paging | `boolean` | `true` | 表格是否自动分页数据,在异步的状况下你可能需要把它设为 `false` |
| row-class-name | `string \| (rowData: Object, index : number) => string \| Object` | `undefined` | |
| row-key | `(rowData: Object) => number \| string` | `undefined` | 通过行数据创建行的 key(如果你不想给每一行加上 key) |
| row-class-name | `string \| (rowData: object, index : number) => string \| object` | `undefined` | |
| row-key | `(rowData: object) => number \| string` | `undefined` | 通过行数据创建行的 key(如果你不想给每一行加上 key) |
| row-props | `(rowData: object, rowIndex : number) => object` | `undefined` | |
| scroll-x | `number \| string` | `undefined` | 表格内容的横向宽度,如果列被水平固定了,则需要设定它 |
| single-column | `boolean` | `false` | |
| single-line | `boolean` | `true` | |
Expand Down Expand Up @@ -91,14 +93,14 @@ tree
| align | `'left' \| 'right' \| 'center'` | `'left'` | 列内的文本排列 |
| children | `Column[]` | `undefined` | 成组列头的子节点 |
| className | `string` | `undefined` | |
| colSpan | `(rowData: Object, rowIndex: number) => number` | `undefined` | |
| colSpan | `(rowData: object, rowIndex: number) => number` | `undefined` | |
| defaultFilterOptionValue | `string \| number \| null` | `null` | 非受控状态下默认的过滤器选项值(过滤器单选时生效) |
| defaultFilterOptionValues | `Array<string \| number>` | `[]` | 非受控状态下默认的过滤器选项值(过滤器多选时生效) |
| defaultSortOrder | `'descend' \| 'ascend' \| false` | `false` | 非受控状态下表格默认的排序方式 |
| disabled | `(rowData: Object, rowIndex: number) => boolean` | `undefined` | |
| disabled | `(rowData: object, rowIndex: number) => boolean` | `undefined` | |
| ellipsis | `boolean \| EllipsisProps` | `false` | |
| expandable | `(rowData: Object, rowIndex: number) => boolean` | `undefined` | 行是否可展开,仅在 `type``'expand'` 时生效 |
| filter | `boolean \| (optionValue: string \| number, rowData: Object) => boolean \| 'default'` | `undefined` | 这一列的过滤方法。如果设为 `true`,表格将只会在这列展示一个排序图标,在异步的时候可能有用。 |
| expandable | `(rowData: object, rowIndex: number) => boolean` | `undefined` | 行是否可展开,仅在 `type``'expand'` 时生效 |
| filter | `boolean \| (optionValue: string \| number, rowData: object) => boolean \| 'default'` | `undefined` | 这一列的过滤方法。如果设为 `true`,表格将只会在这列展示一个排序图标,在异步的时候可能有用。 |
| filterMode | `'and' \| 'or'` | `'or'` | |
| filterMultiple | `boolean` | `true` | |
| filterOptionValue | `string \| number \| null` | `undefined` | 受控状态下,当前激活的过滤器选项值。如果不做设定,这一列的过滤行为将是非受控的(过滤器单选时生效) |
Expand All @@ -107,12 +109,12 @@ tree
| fixed | `'left \| 'right' \| false` | `false` | |
| key | `string \| number` | `undefined` | 这一列的 key,在表格未设定 row-key 的时候是**必须**的。 |
| options | `Array<'all' \| 'none' \| { label: string, key: string \| number, onSelect: (pageData: RowData) => void }>` | `undefined` | 自定义选择项的选项,只对 `type='selection'` 生效 |
| render | `(rowData: Object, rowIndex: number) => VNodeChild` | `undefined` | 渲染函数,渲染这一列的每一行的单元格 |
| renderExpand | `(rowData: Object, rowIndex: number) => VNodeChild` | `undefined` | 展开区域的渲染函数,仅在 `type``'expand'` 的时候生效 |
| render | `(rowData: object, rowIndex: number) => VNodeChild` | `undefined` | 渲染函数,渲染这一列的每一行的单元格 |
| renderExpand | `(rowData: object, rowIndex: number) => VNodeChild` | `undefined` | 展开区域的渲染函数,仅在 `type``'expand'` 的时候生效 |
| renderFilterMenu | `() => VNodeChild` | `undefined` | 渲染函数,渲染这一列的过滤器菜单 |
| renderFilterIcon | `(options: { active: boolean, show: boolean }) => VNodeChild` | `undefined` | 渲染函数,渲染过滤器图标 |
| renderFilter | `(options: { active: boolean, show: boolean }) => VNodeChild` | `undefined` | 渲染函数,渲染过滤器触发元素 |
| rowSpan | `(rowData: Object, rowIndex: number) => number` | `undefined` | |
| rowSpan | `(rowData: object, rowIndex: number) => number` | `undefined` | |
| sortOrder | `'descend' \| 'ascend' \| false` | `undefined` | 受控状态下表格的排序方式。如果多列都设定了有效值,那么只有第一个会生效 |
| sorter | `boolean \| function \| 'default'` | `undefined` | 这一列的排序方法。如果设为 `'default'` 表格将会使用一个内置的排序函数;如果设为 `true`,表格将只会在这列展示一个排序图标,在异步的时候可能有用。其他情况下它工作的方式类似 `Array.sort` 的对比函数 |
| title | `string \| (() => VNodeChild)` | `undefined` | 可以是渲染函数 |
Expand Down
62 changes: 62 additions & 0 deletions src/data-table/demos/zhCN/row-props.demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 自定义行属性

如果你想给行增加一些属性或者事件处理器,使用 `row-props` 属性。

```html
<n-data-table :columns="columns" :data="data" :row-props="rowProps" />
```

```js
import { defineComponent } from 'vue'
import { useMessage } from 'naive-ui'

export default defineComponent({
setup () {
const message = useMessage()
return {
rowProps: (row) => {
return {
style: 'cursor: pointer;',
onClick: () => {
message.info(row.name)
}
}
},
columns: [
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
],
data: [
{
key: 0,
name: '07akioni',
age: '18',
address: 'Yiheyuan Road'
},
{
key: 1,
name: '08akioni',
age: '14',
address: 'Pingshan Road'
},
{
key: 2,
name: '09akioni',
age: '22',
address: 'Haidian Bridge'
}
]
}
}
})
```
17 changes: 10 additions & 7 deletions src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import NMainTable from './MainTable'
import { useCheck } from './use-check'
import { useTableData } from './use-table-data'
import { useScroll } from './use-scroll'
import {
import type {
CreateRowClassName,
CreateRowKey,
OnUpdateCheckedRowKeys,
Expand All @@ -34,9 +34,10 @@ import {
MainTableRef,
DataTableInst,
OnUpdateExpandedRowKeys,
dataTableInjectionKey,
CreateSummary
CreateSummary,
CreateRowProps
} from './interface'
import { dataTableInjectionKey } from './interface'
import style from './styles/index.cssr'
import { useGroupHeader } from './use-group-header'
import { useExpand } from './use-expand'
Expand All @@ -59,6 +60,7 @@ export const dataTableProps = {
default: () => []
},
rowClassName: [String, Function] as PropType<string | CreateRowClassName>,
rowProps: Function as PropType<CreateRowProps>,
rowKey: Function as PropType<CreateRowKey>,
loading: {
type: Boolean,
Expand Down Expand Up @@ -333,6 +335,7 @@ export default defineComponent({
renderExpandRef,
summaryRef: toRef(props, 'summary'),
virtualScrollRef: toRef(props, 'virtualScroll'),
rowPropsRef: toRef(props, 'rowProps'),
checkOptionsRef: computed(() => {
const { value: selectionColumn } = selectionColumnRef
return selectionColumn?.options
Expand Down Expand Up @@ -469,8 +472,8 @@ export default defineComponent({
`${mergedClsPrefix}-data-table`,
{
[`${mergedClsPrefix}-data-table--bordered`]: this.mergedBordered,
[`${mergedClsPrefix}-data-table--bottom-bordered`]: this
.mergedBottomBordered,
[`${mergedClsPrefix}-data-table--bottom-bordered`]:
this.mergedBottomBordered,
[`${mergedClsPrefix}-data-table--single-line`]: this.singleLine,
[`${mergedClsPrefix}-data-table--single-column`]: this.singleColumn
}
Expand All @@ -494,8 +497,8 @@ export default defineComponent({
class={[
`${mergedClsPrefix}-data-table-empty`,
{
[`${mergedClsPrefix}-data-table-empty--hide`]: this
.loading
[`${mergedClsPrefix}-data-table-empty--hide`]:
this.loading
}
]}
>
Expand Down
13 changes: 9 additions & 4 deletions src/data-table/src/TableParts/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default defineComponent({
hasChildrenRef,
firstContentfulColIndexRef,
indentRef,
rowPropsRef,
setHeaderScrollLeft,
doUpdateExpandedRowKeys,
handleTableBodyScroll,
Expand Down Expand Up @@ -254,6 +255,7 @@ export default defineComponent({
hasChildren: hasChildrenRef,
firstContentfulColIndex: firstContentfulColIndexRef,
indent: indentRef,
rowProps: rowPropsRef,
setHeaderScrollLeft,
handleMouseenterTable,
handleVirtualListScroll,
Expand Down Expand Up @@ -314,6 +316,7 @@ export default defineComponent({
showHeader,
hasChildren,
firstContentfulColIndex,
rowProps,
handleMouseenterTable,
handleMouseleaveTable,
renderExpand,
Expand Down Expand Up @@ -383,9 +386,8 @@ export default defineComponent({
const colNodes = cols.map((col, colIndex) => {
if (rowIndex in cordToPass) {
const cordOfRowToPass = cordToPass[rowIndex]
const indexInCordOfRowToPass = cordOfRowToPass.indexOf(
colIndex
)
const indexInCordOfRowToPass =
cordOfRowToPass.indexOf(colIndex)
if (~indexInCordOfRowToPass) {
cordOfRowToPass.splice(indexInCordOfRowToPass, 1)
return null
Expand Down Expand Up @@ -464,7 +466,8 @@ export default defineComponent({
column.type === 'selection',
[`${mergedClsPrefix}-data-table-td--expand`]:
column.type === 'expand',
[`${mergedClsPrefix}-data-table-td--last-col`]: isLastCol,
[`${mergedClsPrefix}-data-table-td--last-col`]:
isLastCol,
[`${mergedClsPrefix}-data-table-td--last-row`]:
isLastRow && !showExpandContent
}
Expand Down Expand Up @@ -529,6 +532,7 @@ export default defineComponent({
</td>
)
})
const props = rowProps ? rowProps(rowData, rowIndex) : undefined
const row = (
<tr
onMouseenter={() => {
Expand All @@ -539,6 +543,7 @@ export default defineComponent({
`${mergedClsPrefix}-data-table-tr`,
createRowClassName(rowData, rowIndex, rowClassName)
]}
{...props}
>
{colNodes}
</tr>
Expand Down

0 comments on commit 15ae234

Please sign in to comment.