Skip to content
Merged
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 src/packages/table/demo.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Demo10 from './demos/taro/demo10'
import Demo11 from './demos/taro/demo11'
import Demo12 from './demos/taro/demo12'
import Demo13 from './demos/taro/demo13'
import Demo14 from './demos/h5/demo14'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

导入路径错误,应从 Taro 特定的目录导入。

- import Demo14 from './demos/h5/demo14'
+ import Demo14 from './demos/taro/demo14'
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import Demo14 from './demos/h5/demo14'
import Demo14 from './demos/taro/demo14'


const TableDemo = () => {
const [translated] = useTranslate({
Expand All @@ -33,6 +34,7 @@ const TableDemo = () => {
stickyHeader: '固定表头',
stickyLeftColumn: '固定左列',
stickyRightColumn: '固定右列',
customRow: '自定义行',
},
'en-US': {
basic: 'Basic usage',
Expand All @@ -50,6 +52,7 @@ const TableDemo = () => {
stickyHeader: 'sticky header',
stickyLeftColumn: 'sticky left column',
stickyRightColumn: 'sticky right column',
customRow: 'Custom Row',
},
})

Expand Down Expand Up @@ -94,6 +97,8 @@ const TableDemo = () => {
<Demo12 />
<h2>{translated.stickyRightColumn}</h2>
<Demo13 />
<h2>{translated.customRow}</h2>
<Demo14 />
</div>
</>
)
Expand Down
5 changes: 5 additions & 0 deletions src/packages/table/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Demo10 from './demos/h5/demo10'
import Demo11 from './demos/h5/demo11'
import Demo12 from './demos/h5/demo12'
import Demo13 from './demos/h5/demo13'
import Demo14 from './demos/h5/demo14'

const TableDemo = () => {
const [translated] = useTranslate({
Expand All @@ -30,6 +31,7 @@ const TableDemo = () => {
stickyHeader: '固定表头',
stickyLeftColumn: '固定左列',
stickyRightColumn: '固定右列',
customRow: '自定义行',
},
'en-US': {
basic: 'Basic Usage',
Expand All @@ -46,6 +48,7 @@ const TableDemo = () => {
stickyHeader: 'Sticky Header',
stickyLeftColumn: 'Sticky Left Column',
stickyRightColumn: 'Sticky Rright Column',
customRow: 'Custom Row',
},
})

Expand Down Expand Up @@ -77,6 +80,8 @@ const TableDemo = () => {
<Demo12 />
<h2>{translated.stickyRightColumn}</h2>
<Demo13 />
<h2>{translated.customRow}</h2>
<Demo14 />
</div>
)
}
Expand Down
79 changes: 79 additions & 0 deletions src/packages/table/demos/h5/demo14.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { useState } from 'react'
import { Table } from '@nutui/nutui-react'

interface TableColumnProps {
key: string
title?: string
align?: string
sorter?: ((a: any, b: any) => number) | boolean | string
render?: (rowData: any, rowIndex: number) => string | React.ReactNode
fixed?: 'left' | 'right'
width?: number
}

const Demo14 = () => {
const [columns] = useState<Array<TableColumnProps>>([
{
title: '姓名',
key: 'name',
align: 'center',
},
{
title: '性别',
key: 'gender',
},
{
title: '学历',
key: 'record',
},
])

const [data] = useState([
{
name: 'Tom',
gender: '男',
record: '小学',
},
{
height: '50px',
text: '这里是自定义行',
Component: (props: any) => {
return (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom:
'1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)
},
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
},
{
name: 'Lucy',
gender: '女',
record: '本科',
},
{
name: 'Jack',
gender: '男',
record: '高中',
},
])

return <Table columns={columns} data={data} />
}
export default Demo14
79 changes: 79 additions & 0 deletions src/packages/table/demos/taro/demo14.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { useState } from 'react'
import { Table } from '@nutui/nutui-react-taro'

interface TableColumnProps {
key: string
title?: string
align?: string
sorter?: ((a: any, b: any) => number) | boolean | string
render?: (rowData: any, rowIndex: number) => string | React.ReactNode
fixed?: 'left' | 'right'
width?: number
}

const Demo14 = () => {
const [columns] = useState<Array<TableColumnProps>>([
{
title: '姓名',
key: 'name',
align: 'center',
},
{
title: '性别',
key: 'gender',
},
{
title: '学历',
key: 'record',
},
])
Comment on lines +5 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优化列定义

columns 数组是常量,不需要使用 useState 来定义。可以直接定义为常量数组以减少不必要的状态管理复杂性。

-  const [columns] = useState<Array<TableColumnProps>>([
+  const columns: Array<TableColumnProps> = [
...
-  ])
+  ]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [columns] = useState<Array<TableColumnProps>>([
{
title: '姓名',
key: 'name',
align: 'center',
},
{
title: '性别',
key: 'gender',
},
{
title: '学历',
key: 'record',
},
])
const columns: Array<TableColumnProps> = [
{
title: '姓名',
key: 'name',
align: 'center',
},
{
title: '性别',
key: 'gender',
},
{
title: '学历',
key: 'record',
},
]


const [data] = useState([
{
name: 'Tom',
gender: '男',
record: '小学',
},
{
height: '100px',
text: '这里是自定义行',
Component: (props: any) => {
return (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom:
'1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)
},
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优化自定义组件的实现

data 数组中定义的自定义组件提取到外部,以增强代码的可读性和可维护性。

+ const CustomRow = (props: any) => (
+   <div style={{ height: props.height }}>
+     <div
+       style={{
+         position: 'absolute',
+         height: props.height,
+         width: '100%',
+         display: 'flex',
+         justifyContent: 'center',
+         alignItems: 'center',
+         borderBottom: '1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
+       }}
+     >
+       {props.text}
+     </div>
+   </div>
+ )
...
-      Component: (props: any) => {
-        return (
-          <div style={{ height: props.height }}>
-            <div
-              style={{
-                position: 'absolute',
-                height: props.height,
-                width: '100%',
-                display: 'flex',
-                justifyContent: 'center',
-                alignItems: 'center',
-                borderBottom:
-                  '1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
-              }}
-            >
-              {props.text}
-            </div>
-          </div>
-        )
-      },
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
height: '100px',
text: '这里是自定义行',
Component: (props: any) => {
return (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom:
'1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)
},
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
height: '100px',
text: '这里是自定义行',
Component: CustomRow,
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
```
```tsx
const CustomRow = (props: any) => (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom: '1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)

},
{
name: 'Lucy',
gender: '女',
record: '本科',
},
{
name: 'Jack',
gender: '男',
record: '高中',
},
])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优化数据定义

data 数组是常量,不需要使用 useState 来定义。可以直接定义为常量数组以减少不必要的状态管理复杂性。

-  const [data] = useState([
+  const data = [
...
-  ])
+  ]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [data] = useState([
{
name: 'Tom',
gender: '男',
record: '小学',
},
{
height: '100px',
text: '这里是自定义行',
Component: (props: any) => {
return (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom:
'1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)
},
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
},
{
name: 'Lucy',
gender: '女',
record: '本科',
},
{
name: 'Jack',
gender: '男',
record: '高中',
},
])
const data = [
{
name: 'Tom',
gender: '男',
record: '小学',
},
{
height: '100px',
text: '这里是自定义行',
Component: (props: any) => {
return (
<div style={{ height: props.height }}>
<div
style={{
position: 'absolute',
height: props.height,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderBottom:
'1px solid var(--nutui-table-border-color, var(--nutui-black-3, rgba(0, 0, 0, 0.06)))',
}}
>
{props.text}
</div>
</div>
)
},
rowRender: (item: any) => {
const { Component, ...rest } = item
return <Component {...rest} />
},
},
{
name: 'Lucy',
gender: '女',
record: '本科',
},
{
name: 'Jack',
gender: '男',
record: '高中',
},
]


return <Table columns={columns} data={data} />
}
export default Demo14
4 changes: 4 additions & 0 deletions src/packages/table/table.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export const Table: FunctionComponent<
}

const renderBodyTds = (item: any, rowIndex: number) => {
const { rowRender } = item
if (rowRender && typeof rowRender === 'function') {
return rowRender(item, rowIndex)
}
return sortDataItem().map(([value, render]) => {
return (
<div
Expand Down
4 changes: 4 additions & 0 deletions src/packages/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export const Table: FunctionComponent<
}

const renderBodyTds = (item: any, rowIndex: number) => {
const { rowRender } = item
if (rowRender && typeof rowRender === 'function') {
return rowRender(item, rowIndex)
}
Comment on lines +125 to +128
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

需要增加键盘访问性支持。

在文件 src/packages/table/table.tsx 中,发现存在 onClick 事件,但缺少相应的键盘事件处理程序(如 onKeyUponKeyDownonKeyPress)。为了确保组件的可访问性,请添加这些键盘事件处理程序。

  • onClick 事件位置:
    • onClick={() => handleSorterClick(item)}
Analysis chain

自定义行渲染逻辑正确。

但需要增加键盘访问性支持。

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the onClick event in the table has corresponding keyboard event handlers.

# Test: Search for onClick events in the file. Expect: Corresponding onKeyUp, onKeyDown, or onKeyPress events.
rg --type typescript $'onClick' src/packages/table/table.tsx

Length of output: 94



Script:

#!/bin/bash
# Description: Verify the onClick event in the table has corresponding keyboard event handlers.

# Test: Search for onClick events in the file. Expect: Corresponding onKeyUp, onKeyDown, or onKeyPress events.
rg 'onClick' src/packages/table/table.tsx

Length of output: 91



Script:

#!/bin/bash
# Description: Verify the presence of keyboard event handlers corresponding to the onClick event in the table component.

# Test: Search for onKeyUp, onKeyDown, or onKeyPress events in the file.
rg 'on(KeyUp|KeyDown|KeyPress)' src/packages/table/table.tsx

Length of output: 62

return sortDataItem().map(([value, render]) => {
return (
<div
Expand Down