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: input value should follow current #546

Merged
merged 1 commit into from
Dec 11, 2023
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
54 changes: 33 additions & 21 deletions docs/examples/simple.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import React from 'react';
import React, { useState } from 'react';
import Pagination from 'rc-pagination';
import Select from 'rc-select';
import '../../assets/index.less';

export default () => (
<>
<Pagination simple defaultCurrent={1} total={50} />
<br />
<Pagination
simple
defaultCurrent={1}
total={50}
showTotal={(total) => `Total ${total} items`}
/>
<br />
<Pagination
simple
defaultCurrent={1}
total={50}
showSizeChanger
selectComponentClass={Select}
/>
</>
);
export default () => {
const [pageIndex, setPageIndex] = useState(1);

return (
<>
<button onClick={() => setPageIndex((i) => ++i)}>increase</button>
<Pagination
simple
current={pageIndex}
total={50}
onChange={setPageIndex}
/>
<br />
<Pagination simple defaultCurrent={1} total={50} />
<br />
<Pagination
simple
defaultCurrent={1}
total={50}
showTotal={(total) => `Total ${total} items`}
/>
<br />
<Pagination
simple
defaultCurrent={1}
total={50}
showSizeChanger
selectComponentClass={Select}
/>
</>
);
};
6 changes: 5 additions & 1 deletion src/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
import KeyCode from 'rc-util/lib/KeyCode';
import pickAttrs from 'rc-util/lib/pickAttrs';
import warning from 'rc-util/lib/warning';
import React from 'react';
import React, { useEffect } from 'react';
import type { PaginationProps } from './interface';
import zhCN from './locale/zh_CN';
import Options from './Options';
Expand Down Expand Up @@ -89,6 +89,10 @@ const Pagination: React.FC<PaginationProps> = (props) => {

const [internalInputVal, setInternalInputVal] = React.useState(current);

useEffect(() => {
setInternalInputVal(current);
}, [current]);

const hasOnChange = onChange !== noop;
const hasCurrent = 'current' in props;

Expand Down
46 changes: 3 additions & 43 deletions tests/__snapshots__/demo.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1105,49 +1105,9 @@ exports[`Example showTotal 1`] = `
`;

exports[`Example simple 1`] = `
<ul
class="rc-pagination rc-pagination-simple"
>
<li
aria-disabled="true"
class="rc-pagination-prev rc-pagination-disabled"
title="上一页"
>
<button
aria-label="prev page"
class="rc-pagination-item-link"
disabled=""
type="button"
/>
</li>
<li
class="rc-pagination-simple-pager"
title="1/5"
>
<input
size="3"
type="text"
value="1"
/>
<span
class="rc-pagination-slash"
>
/
</span>
5
</li>
<li
aria-disabled="false"
class="rc-pagination-next"
title="下一页"
>
<button
aria-label="next page"
class="rc-pagination-item-link"
type="button"
/>
</li>
</ul>
<button>
increase
</button>
`;

exports[`Example sizer 1`] = `
Expand Down
26 changes: 26 additions & 0 deletions tests/simple.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,30 @@ describe('simple Pagination', () => {
container.querySelector('.rc-pagination-simple-pager'),
).toHaveAttribute('title', '2/3');
});

it('page should displayed correctly when controlled', () => {
const Demo = () => {
const [current, setCurrent] = React.useState(1);

return (
<div>
<button onClick={() => setCurrent((i) => ++i)}>increase</button>
<Pagination
simple
current={current}
total={25}
onChange={setCurrent}
/>
</div>
);
};

const { container } = render(<Demo />);
const input = container.querySelector('.rc-pagination-simple input');
const button = container.querySelector('button');

expect(input).toHaveProperty('value', '1');
fireEvent.click(button);
expect(input).toHaveProperty('value', '2');
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"rc-pagination": ["src/index.ts"]
}
},
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
}
Loading