Skip to content

fix(common): table unsorting will be in descending order #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

hujiahao-hjh
Copy link
Contributor

What this PR does / why we need it:

Fix bug of table unsorting will be in descending order.

Does this PR introduce a user interface change?

❎ No

ChangeLog

Language Changelog
🇺🇸 English
🇨🇳 中文

Does this PR need be patched to older version?

✅ Yes(version is required)
release/1.5-alpha1

Which issue(s) this PR fixes:

Fixes # https://erda-org.erda.cloud/erda/dop/projects/387/issues/all?id=252476&issueFilter__urlQuery=eyJzdGF0ZXMiOls0NDAyLDcxMDQsNzEwNSw0NDAzLDQ0MDQsNzEwNiw0NDA2LDQ0MDcsNDQxMiw0NTM4LDQ0MTMsNDQxNCw0NDE1LDQ0MTZdLCJhc3NpZ25lZUlEcyI6WyIxMDAxMjE0Il19&issueTable__urlQuery=eyJwYWdlTm8iOjEsICJwYWdlU2l6ZSI6MTB9&issueViewGroup__urlQuery=eyJ2YWx1ZSI6ImthbmJhbiIsImNoaWxkcmVuVmFsdWUiOnsia2FuYmFuIjoiZGVhZGxpbmUifX0%3D&iterationID=680&type=BUG

@hujiahao-hjh hujiahao-hjh added the bugfix Used in pr label Nov 24, 2021
@@ -250,7 +250,7 @@ function WrappedTable<T extends object = any>({
);
}, [allColumns, sorterMenu, sort, onRow]);

let data = [...dataSource] as T[];
let data = (dataSource && [...dataSource]) || ([] as T[]);
Copy link
Contributor

Choose a reason for hiding this comment

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

let data: T[] = dataSource ? [...dataSource]) : []

@@ -283,7 +283,7 @@ function WrappedTable<T extends object = any>({
size="small"
pagination={false}
onChange={onChange}
dataSource={data}
dataSource={data || []}
Copy link
Contributor

Choose a reason for hiding this comment

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

u set default value above.

@@ -60,7 +60,7 @@ function WrappedTable<T extends object = any>({
pagination: paginationProps,
onChange,
slot,
dataSource = [],
dataSource,
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont understand why remove this default value. Then leads code below has to be like dataSource?.length || 0

@hujiahao-hjh hujiahao-hjh force-pushed the hotfix/table-unsorting-will-be-in-descending-order branch from a081a4c to fb76c04 Compare November 25, 2021 02:28
@hujiahao-hjh hujiahao-hjh force-pushed the hotfix/table-unsorting-will-be-in-descending-order branch from fb76c04 to 4e28e0e Compare November 25, 2021 02:29
@daskyrk
Copy link
Contributor

daskyrk commented Nov 25, 2021

/approve

@erda-bot erda-bot added the approved Auto tagged by robot label Nov 25, 2021
@erda-bot erda-bot merged commit 6e2a380 into erda-project:master Nov 25, 2021
@daskyrk
Copy link
Contributor

daskyrk commented Nov 25, 2021

/cherry-pick release/1.5-alpha1

@erda-bot
Copy link
Member

Automated cherry pick failed, please resolve the confilcts and create PR manually.
Details:

Auto-merging shell/app/common/components/table/index.tsx
CONFLICT (content): Merge conflict in shell/app/common/components/table/index.tsx
diff --cc shell/app/common/components/table/index.tsx
index f55eda5d,5220e1b1..00000000
--- a/shell/app/common/components/table/index.tsx
+++ b/shell/app/common/components/table/index.tsx
@@@ -122,19 -63,22 +122,19 @@@ function WrappedTable<T extends object 
    dataSource = [],
    onRow,
    rowSelection,
 -  hideHeader,
    ...props
  }: IProps<T>) {
 -  const [columns, setColumns] = React.useState<Array<ColumnProps<T>>>(allColumns);
 +  const [columns, setColumns] = React.useState<Array<IColumnProps<T>>>(allColumns);
    const [sort, setSort] = React.useState<SorterResult<T>>({});
 -  const sortCompareRef = React.useRef<((a: T, b: T) => number) | null>(null);
 +  const sortCompareRef = React.useRef() as { current: (a, b) => number };
    const [defaultPagination, setDefaultPagination] = React.useState<TablePaginationConfig>({
      current: 1,
-     total: dataSource.length,
+     total: dataSource?.length || 0,
      ...PAGINATION,
    });
 -  const isFrontendPaging = !(paginationProps && paginationProps.current) && paginationProps !== false; // Determine whether front-end paging
 +  const [goToVisible, setGoToVisible] = React.useState(false);
  
 -  const pagination: TablePaginationConfig = isFrontendPaging
 -    ? defaultPagination
 -    : (paginationProps as TablePaginationConfig);
 +  const pagination: TablePaginationConfig = paginationProps || defaultPagination;
    const { current = 1, pageSize = PAGINATION.pageSize } = pagination;
  
    React.useEffect(() => {
@@@ -174,14 -135,17 +174,19 @@@
  
        const onSort = (order?: 'ascend' | 'descend') => {
          setSort({ ...sorter, order });
++<<<<<<< HEAD
 +        if (column.sorter?.compare) {
++=======
+         const { sorter: columnSorter } = column as { sorter: { compare: (a: T, b: T) => number } };
+         if (order && columnSorter?.compare) {
++>>>>>>> 6e2a3802 (fix(common): table unsorting will be in descending order (#2107))
            sortCompareRef.current = (a: T, b: T) => {
              if (order === 'ascend') {
 -              return columnSorter?.compare?.(a, b);
 +              return column.sorter?.compare?.(a, b);
              } else {
 -              return columnSorter?.compare?.(b, a);
 +              return column.sorter?.compare?.(b, a);
              }
            };
 -        } else {
 -          sortCompareRef.current = null;
          }
          onTableChange({ pageNo: 1, sorter: { ...sorter, order } });
        };
@@@ -278,88 -250,7 +283,92 @@@
      );
    }, [allColumns, sorterMenu, sort, onRow]);
  
++<<<<<<< HEAD
 +  const paginationCenterRender = (
 +    <Popover
 +      content={
 +        <PaginationJump
 +          pagination={pagination}
 +          onTableChange={onTableChange}
 +          hiddenPopover={() => setGoToVisible(false)}
 +        />
 +      }
 +      trigger="click"
 +      getPopupContainer={(triggerNode) => triggerNode.parentElement as HTMLElement}
 +      placement="top"
 +      overlayClassName="pagination-jump"
 +      visible={goToVisible}
 +      onVisibleChange={setGoToVisible}
 +    >
 +      <div className="pagination-center bg-hover mx-1 px-3 rounded" onClick={() => setGoToVisible(true)}>
 +        {pagination.total ? pagination.current : 0} /{' '}
 +        {(pagination.total && pagination.pageSize && Math.ceil(pagination.total / pagination.pageSize)) || 0}
 +      </div>
 +    </Popover>
 +  );
 +
 +  const paginationItemRender = (
 +    page: number,
 +    type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
 +    originalElement: React.ReactElement<HTMLElement>,
 +  ) => {
 +    const { total } = pagination;
 +    switch (type) {
 +      case 'prev':
 +        return (
 +          <div className="bg-hover" onClick={() => current > 1 && onTableChange({ pageNo: current - 1 })}>
 +            {originalElement}
 +          </div>
 +        );
 +      case 'next':
 +        return (
 +          <div
 +            className="bg-hover"
 +            onClick={() => total && current < Math.ceil(total / pageSize) && onTableChange({ pageNo: current + 1 })}
 +          >
 +            {originalElement}
 +          </div>
 +        );
 +      case 'page':
 +        if (page === 1) {
 +          return paginationCenterRender;
 +        }
 +        return null;
 +      default:
 +        return null;
 +    }
 +  };
 +
 +  const pageSizeMenu = (
 +    <Menu>
 +      {(pagination?.pageSizeOptions || Pagination?.defaultProps?.pageSizeOptions || []).map((item: number) => {
 +        return (
 +          <Menu.Item key={item} onClick={() => onTableChange({ pageNo: 1, pageSize: item })}>
 +            <span className="fake-link mr-1">{i18n.t('{size} items / page', { size: item })}</span>
 +          </Menu.Item>
 +        );
 +      })}
 +    </Menu>
 +  );
 +
 +  const batchMenu = () => {
 +    return (
 +      <Menu>
 +        {(rowSelection?.actions || []).map((item: IRowActions) => {
 +          return (
 +            <Menu.Item key={item.key} onClick={() => item.onClick()} disabled={item.disabled}>
 +              {item.name}
 +            </Menu.Item>
 +          );
 +        })}
 +      </Menu>
 +    );
 +  };
 +
 +  let data = dataSource;
++=======
+   let data: T[] = dataSource ? [...dataSource] : [];
++>>>>>>> 6e2a3802 (fix(common): table unsorting will be in descending order (#2107))
  
    if (sortCompareRef.current) {
      data = data.sort(sortCompareRef.current);

@daskyrk
Copy link
Contributor

daskyrk commented Nov 25, 2021

1.5-alpha2 will be released today, not need to pick.

laojun pushed a commit to laojun/erda-ui that referenced this pull request Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Auto tagged by robot bugfix Used in pr
Development

Successfully merging this pull request may close these issues.

4 participants