Browse Source

fix: fixed rowSelection hidden bug in resizable Table #2036 (#2040)

Co-authored-by: shijia.me <[email protected]>
Shi Jia 1 year ago
parent
commit
498ceefbb4

+ 8 - 0
cypress/e2e/table.spec.js

@@ -246,4 +246,12 @@ describe('table', () => {
         cy.get('button').contains('点击全选').click();
         cy.get('.semi-table-thead .semi-checkbox-checked').should('exist')
     });
+
+    it('test hidden rowSelection in resizable Table', () => {
+        cy.visit('http://localhost:6006/iframe.html?id=table--fixed-row-selection-hidden-resizable&viewMode=story');
+        cy.get('.semi-button').eq(0).click();
+        cy.get('.semi-table-column-selection').should('not.exist');
+        cy.get('.semi-button').eq(0).click();
+        cy.get('.semi-table-column-selection').should('exist');
+    });
 });

+ 1 - 1
packages/semi-ui/table/ResizableTable.tsx

@@ -52,7 +52,7 @@ const ResizableTable = (props: TableProps = {}, ref: React.MutableRefObject<Tabl
         newColumns.unshift({ key: strings.DEFAULT_KEY_COLUMN_EXPAND, width: numbers.DEFAULT_WIDTH_COLUMN_EXPAND });
     }
 
-    if (props.rowSelection && !find(rawColumns, item => item.key === strings.DEFAULT_KEY_COLUMN_SELECTION)) {
+    if (props.rowSelection && !get(props.rowSelection, 'hidden') && !find(rawColumns, item => item.key === strings.DEFAULT_KEY_COLUMN_SELECTION)) {
         newColumns.unshift({
             width: get(props, 'rowSelection.width', numbers.DEFAULT_WIDTH_COLUMN_SELECTION),
             key: strings.DEFAULT_KEY_COLUMN_SELECTION,

+ 2 - 1
packages/semi-ui/table/_story/table.stories.jsx

@@ -107,7 +107,8 @@ export {
     ShowHeader,
     KeepDOM,
     SortIcon,
-    FixedAllDisabledAndSelected
+    FixedAllDisabledAndSelected,
+    FixedRowSelectionHiddenResizable
 } from './v2';
 export { default as FixSelectAll325 } from './Demos/rowSelection';
 

+ 106 - 0
packages/semi-ui/table/_story/v2/FixedRowSelectionHiddenResizable/index.tsx

@@ -0,0 +1,106 @@
+import React, { useMemo, useState } from 'react';
+import { Table, Avatar, Button, Space } from '@douyinfe/semi-ui';
+import * as dateFns from 'date-fns';
+import { IconMore } from '@douyinfe/semi-icons';
+
+const DAY = 24 * 60 * 60 * 1000;
+const figmaIconUrl = 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/figma-icon.png';
+
+/**
+ * test with cypress, don't modify this story
+ * @returns 
+ */
+export default function App() {
+    const [hidden, setHidden] = useState(false);
+    const columns = [
+        {
+            title: '标题',
+            dataIndex: 'name',
+            width: 300,
+            resize: false,
+            render: (text, record, index) => {
+                return (
+                    <div>
+                        <Avatar size="small" shape="square" src={figmaIconUrl} style={{ marginRight: 12 }}></Avatar>
+                        {text}
+                    </div>
+                );
+            },
+            filters: [
+                {
+                    text: 'Semi Design 设计稿',
+                    value: 'Semi Design 设计稿',
+                },
+                {
+                    text: 'Semi D2C 设计稿',
+                    value: 'Semi D2C 设计稿',
+                },
+            ],
+            onFilter: (value, record) => record.name.includes(value),
+        },
+        {
+            title: '大小',
+            dataIndex: 'size',
+            width: 200,
+            sorter: (a, b) => (a.size - b.size > 0 ? 1 : -1),
+            render: text => `${text} KB`,
+        },
+        {
+            title: '所有者',
+            width: 200,
+            dataIndex: 'owner',
+            render: (text, record, index) => {
+                return (
+                    <div>
+                        <Avatar size="small" color={record.avatarBg} style={{ marginRight: 4 }}>
+                            {typeof text === 'string' && text.slice(0, 1)}
+                        </Avatar>
+                        {text}
+                    </div>
+                );
+            },
+        },
+        {
+            title: '更新日期',
+            dataIndex: 'updateTime',
+            sorter: (a, b) => (a.updateTime - b.updateTime > 0 ? 1 : -1),
+            render: value => {
+                return dateFns.format(new Date(value), 'yyyy-MM-dd');
+            },
+        },
+        {
+            title: '操作列',
+            dataIndex: 'operate',
+            fixed: 'right',
+            width: 100,
+            resize: false,
+            render: () => {
+                return <IconMore />;
+            },
+        },
+    ];
+
+    const data = useMemo(() => {
+        const _data = [];
+        for (let i = 0; i < 46; i++) {
+            const isSemiDesign = i % 2 === 0;
+            const randomNumber = (i * 1000) % 199;
+            _data.push({
+                key: '' + i,
+                name: isSemiDesign ? `Semi Design 设计稿${i}.fig` : `Semi D2C 设计稿${i}.fig`,
+                owner: isSemiDesign ? '姜鹏志' : '郝宣',
+                size: randomNumber,
+                updateTime: new Date('2024-01-24').valueOf() + randomNumber * DAY,
+                avatarBg: isSemiDesign ? 'grey' : 'red',
+            });
+        }
+        return _data;
+    }, []);
+
+    return (
+        <Space vertical align="start">
+            <Button onClick={() => setHidden(!hidden)}>toggle hidden rowSelection</Button>
+            <Table columns={columns} dataSource={data} resizable bordered rowSelection={{ hidden }} />
+        </Space>
+    );
+}

+ 1 - 0
packages/semi-ui/table/_story/v2/index.js

@@ -28,3 +28,4 @@ export { default as ShowHeader } from './ShowHeader';
 export { default as KeepDOM } from './KeepDOM';
 export { default as SortIcon } from './SortIcon';
 export { default as FixedAllDisabledAndSelected } from './FixedAllDisabledAndSelected';
+export { default as FixedRowSelectionHiddenResizable } from './FixedRowSelectionHiddenResizable';