Skip to content
This repository was archived by the owner on Oct 19, 2021. It is now read-only.

Commit 55d46f4

Browse files
fix(pageSize): add pageSize prop back in (#70)
1 parent 5e0f2b6 commit 55d46f4

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

components/Pagination.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Pagination extends Component {
2121
totalItems: PropTypes.number.isRequired,
2222
disabled: PropTypes.bool,
2323
page: PropTypes.number,
24+
pageSize: PropTypes.number,
2425
};
2526
static defaultProps = {
2627
backwardText: 'Backward',
@@ -36,14 +37,22 @@ class Pagination extends Component {
3637
static uuid = 0;
3738
state = {
3839
page: this.props.page,
39-
pageSize: this.props.pageSizes[0],
40+
pageSize: this.props.pageSize &&
41+
this.props.pageSizes.includes(this.props.pageSize)
42+
? this.props.pageSize
43+
: this.props.pageSizes[0],
4044
};
41-
componentWillReceiveProps({ pageSizes, page }) {
45+
componentWillReceiveProps({ pageSizes, page, pageSize }) {
4246
if (!equals(pageSizes, this.props.pageSizes)) {
4347
this.setState({ pageSize: pageSizes[0], page: 1 });
4448
}
45-
if (!equals(page, this.props.page)) {
46-
this.setState({ page });
49+
if (page !== this.props.page) {
50+
this.setState({
51+
page,
52+
});
53+
}
54+
if (pageSize !== this.props.pageSize) {
55+
this.setState({ pageSize });
4756
}
4857
}
4958
id = Pagination.uuid++;
@@ -101,9 +110,9 @@ class Pagination extends Component {
101110
onChange={this.handleSizeChange}
102111
value={pageSize}
103112
>
104-
{pageSizes.map(size => (
113+
{pageSizes.map(size =>
105114
<SelectItem key={size} value={size} text={String(size)} />
106-
))}
115+
)}
107116
</Select>
108117
<span className="bx--pagination__text">
109118
{itemsPerPageText}&nbsp;&nbsp;|&nbsp;&nbsp;

components/__tests__/Pagination-test.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ describe('Pagination', () => {
6363
actualPageSize = pageSize;
6464
};
6565
const pager = mount(
66-
<Pagination pageSizes={[5, 10]} totalItems={50} onChange={handler} />
66+
<Pagination
67+
pageSizes={[5, 10]}
68+
totalItems={50}
69+
onChange={handler}
70+
/>
6771
);
6872
expect(pager.state().pageSize).toBe(5);
6973
pager.find('select').simulate('change', { target: { value: 10 } });
@@ -81,7 +85,11 @@ describe('Pagination', () => {
8185
actualPage = page;
8286
};
8387
const pager = mount(
84-
<Pagination pageSizes={[5, 10]} totalItems={50} onChange={handler} />
88+
<Pagination
89+
pageSizes={[5, 10]}
90+
totalItems={50}
91+
onChange={handler}
92+
/>
8593
);
8694
pager.setState({ page: 2 });
8795
expect(pager.state().page).toBe(2);
@@ -97,6 +105,19 @@ describe('Pagination', () => {
97105
pager.setProps({ pageSizes: [3, 6] });
98106
expect(pager.state().page).toEqual(1);
99107
});
108+
it('should default to pageSize if pageSize is provided', () => {
109+
const pager = mount(
110+
<Pagination pageSizes={[5, 10]} pageSize={10} totalItems={50} />
111+
);
112+
expect(pager.state().pageSize).toEqual(10);
113+
});
114+
it('should default to pageSize if on change to pageSize', () => {
115+
const pager = mount(
116+
<Pagination pageSizes={[5, 10]} totalItems={50} />
117+
);
118+
pager.setProps({ pageSize: 10 });
119+
expect(pager.state().pageSize).toEqual(10);
120+
});
100121
});
101122
});
102123

@@ -112,8 +133,12 @@ describe('Pagination', () => {
112133
it('should have two buttons for navigation', () => {
113134
const buttons = right.find('.bx--pagination__button');
114135
expect(buttons.length).toBe(2);
115-
expect(buttons.at(0).hasClass('bx--pagination__button--backward')).toBe(true);
116-
expect(buttons.at(1).hasClass('bx--pagination__button--forward')).toBe(true);
136+
expect(buttons.at(0).hasClass('bx--pagination__button--backward')).toBe(
137+
true
138+
);
139+
expect(buttons.at(1).hasClass('bx--pagination__button--forward')).toBe(
140+
true
141+
);
117142
});
118143
it('should disable backward navigation for the first page', () => {
119144
const buttons = right.find('.bx--pagination__button');
@@ -122,7 +147,11 @@ describe('Pagination', () => {
122147
});
123148
it('should disable forward navigation for the last page', () => {
124149
const smallPage = shallow(
125-
<Pagination className="extra-class" pageSizes={[100]} totalItems={50} />
150+
<Pagination
151+
className="extra-class"
152+
pageSizes={[100]}
153+
totalItems={50}
154+
/>
126155
);
127156
const buttons = smallPage.find('.bx--pagination__button');
128157
expect(buttons.at(0).props().disabled).toBe(true);
@@ -136,7 +165,11 @@ describe('Pagination', () => {
136165
actualPage = page;
137166
};
138167
const pager = mount(
139-
<Pagination pageSizes={[5, 10]} totalItems={50} onChange={handler} />
168+
<Pagination
169+
pageSizes={[5, 10]}
170+
totalItems={50}
171+
onChange={handler}
172+
/>
140173
);
141174
expect(pager.state().page).toBe(1);
142175
pager.find('.bx--pagination__button--forward').simulate('click');
@@ -149,7 +182,11 @@ describe('Pagination', () => {
149182
actualPage = page;
150183
};
151184
const pager = mount(
152-
<Pagination pageSizes={[5, 10]} totalItems={50} onChange={handler} />
185+
<Pagination
186+
pageSizes={[5, 10]}
187+
totalItems={50}
188+
onChange={handler}
189+
/>
153190
);
154191
pager.setState({ page: 2 });
155192
expect(pager.state().page).toBe(2);
@@ -163,10 +200,16 @@ describe('Pagination', () => {
163200
actualPage = page;
164201
};
165202
const pager = mount(
166-
<Pagination pageSizes={[5, 10]} totalItems={50} onChange={handler} />
203+
<Pagination
204+
pageSizes={[5, 10]}
205+
totalItems={50}
206+
onChange={handler}
207+
/>
167208
);
168209
expect(pager.state().page).toBe(1);
169-
pager.find('.bx--text__input').simulate('change', { target: { value: 2 } });
210+
pager
211+
.find('.bx--text__input')
212+
.simulate('change', { target: { value: 2 } });
170213
expect(actualPage).toBe(2);
171214
expect(pager.state().page).toBe(2);
172215
});

0 commit comments

Comments
 (0)