Skip to content

Commit 09d9895

Browse files
authored
Merge pull request #361 from Alexsus87/bugfix/pagination-controlled
Fixed controlled mode on Pagination component
2 parents 251f88d + b640f6f commit 09d9895

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Precise UI Changelog
22

3+
## 2.1.6
4+
5+
- Fixed controlled mode on Pagination component
6+
37
## 2.1.5
48

59
- Improved trigger and added CNAME to kitchen sink

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "precise-ui",
3-
"version": "2.1.5",
3+
"version": "2.1.6",
44
"description": "Precise UI React component library powered by Styled Components.",
55
"keywords": [
66
"react",
@@ -125,4 +125,4 @@
125125
"engines": {
126126
"node": ">=10.0.0"
127127
}
128-
}
128+
}

src/components/Pagination/index.test.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,22 @@ describe('<Pagination />', () => {
6262
);
6363
expect(wrapper.find('.foo')).toHaveLength(2);
6464
});
65+
66+
it('should respect the `value` property', () => {
67+
const wrapper = enzyme.mount(
68+
<Pagination size={3}>
69+
<div className="foo">First</div>
70+
<div className="foo">Second</div>
71+
<div className="foo">3</div>
72+
<div className="foo">4</div>
73+
<div className="foo">5</div>
74+
<div className="foo">6</div>
75+
<div className="foo">7</div>
76+
</Pagination>,
77+
);
78+
79+
wrapper.setProps({value: 2})
80+
81+
expect(wrapper.find('.foo')).toHaveLength(1);
82+
});
6583
});

src/components/Pagination/index.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ export class Pagination extends React.Component<PaginationProps, PaginationState
108108
};
109109
}
110110

111+
static getDerivedStateFromProps(props: PaginationProps, state: PaginationState) {
112+
const { value } = props;
113+
114+
if (typeof value === 'number' && state.current !== value) {
115+
return {
116+
current: value,
117+
};
118+
}
119+
120+
return state;
121+
}
122+
111123
private handlePageChange = ({ page }: PaginationBarPageChangedEvent) => {
112124
const { onChange, value } = this.props;
113125

0 commit comments

Comments
 (0)