Skip to content
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

fix(pagination): auto truncate number after entering #2886

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useState, useMemo, forwardRef, useEffect } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import isNaN from 'lodash/isNaN';

import noop from '../_util/noop';
import useConfig from '../hooks/useConfig';
import useControlled from '../hooks/useControlled';
Expand Down Expand Up @@ -75,14 +77,14 @@ const Pagination = forwardRef<HTMLDivElement, PaginationProps>((originalProps, r
return;
}

let nextCurrent = _nextCurrent;
let nextCurrent = Math.trunc(_nextCurrent);
if (isNaN(nextCurrent)) return;
let nextPageSize = _nextPageSize;

if (!nextPageSize && !pageSizeValidator(nextPageSize)) {
nextPageSize =
pageSize ?? (typeof pageSizeOptions[0] === 'number' ? pageSizeOptions[0] : pageSizeOptions[0]?.value);
}

// 边界处理
if (nextCurrent < min) nextCurrent = min;
if (nextCurrent > pageCount) nextCurrent = pageCount;
Expand Down
22 changes: 15 additions & 7 deletions src/pagination/__tests__/pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Pagination test', () => {
expect(() => wrapper.unmount()).not.toThrow();
});

test('click page', () => {
test('click page works fine', () => {
const wrapper = render(<Pagination total={300} pageSize={15} />);

fireEvent.click(wrapper.getByText('20'));
Expand All @@ -22,7 +22,7 @@ describe('Pagination test', () => {
expect(document.querySelector('.t-is-current')).toHaveTextContent('19');
});

test('pageSize', async () => {
test('pageSize works fine', async () => {
const changeFn = vi.fn();
const pageSizeChangeFn = vi.fn();
const { getByText, container, getByDisplayValue } = render(
Expand All @@ -43,7 +43,7 @@ describe('Pagination test', () => {
expect(container.querySelector('.t-pagination__pager').childNodes.length).toBe(5);
expect(document.querySelector('.t-is-current')).toHaveTextContent('5');
});
test('folded', () => {
test('folded works fine', () => {
const changeFn = vi.fn();
const pageSizeChangeFn = vi.fn();
const { getByText, container } = render(
Expand Down Expand Up @@ -76,15 +76,15 @@ describe('Pagination test', () => {
fireEvent.click(container.querySelector('.t-pagination__number--more'));
expect(document.querySelector('.t-is-current')).toHaveTextContent('2');
});
test('theme', () => {
test('theme works fine', () => {
const changeFn = vi.fn();
render(<Pagination total={100} defaultPageSize={5} theme="simple" onChange={changeFn} />);

fireEvent.change(document.querySelector('.t-pagination__jump .t-input__inner'), { target: { value: '5' } });
fireEvent.keyDown(document.querySelector('.t-pagination__jump .t-input__inner'), { keyCode: 13 });
expect(document.querySelector('.t-pagination__jump .t-input__inner').value).toEqual('5');
});
test('totalContent', () => {
test('totalContent works fine', () => {
const changeFn = vi.fn();
const { getByText, rerender } = render(
<Pagination total={100} defaultPageSize={5} totalContent="总条数" onChange={changeFn} />,
Expand All @@ -96,8 +96,7 @@ describe('Pagination test', () => {
rerender(<Pagination total={100} defaultPageSize={5} totalContent={totalContentFn} onChange={changeFn} />);
expect(totalContentFn).toBeCalled();
});

test('jumper', () => {
test('jumper works fine', () => {
render(<Pagination total={300} pageSize={15} showJumper />);

fireEvent.change(document.querySelector('.t-pagination__jump .t-input__inner'), { target: { value: '5' } });
Expand All @@ -112,4 +111,13 @@ describe('Pagination test', () => {
fireEvent.keyDown(document.querySelector('.t-pagination__jump .t-input__inner'), { keyCode: 13 });
expect(document.querySelector('.t-is-current')).toHaveTextContent('1');
});
test('jump input number return integral part', () => {
// 测试输入小数会自动处理整数
const changeFn = vi.fn();
render(<Pagination total={100} defaultPageSize={20} theme="simple" onChange={changeFn} />);

fireEvent.change(document.querySelector('.t-pagination__jump .t-input__inner'), { target: { value: '5.555' } });
fireEvent.keyDown(document.querySelector('.t-pagination__jump .t-input__inner'), { keyCode: 13 });
expect(document.querySelector('.t-pagination__jump .t-input__inner').value).toEqual('5');
});
});
Loading