Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

Commit 3b68f64

Browse files
anishaswainportante
authored andcommitted
move AntD datepicker to Patternfly
1 parent 93542f1 commit 3b68f64

File tree

4 files changed

+71
-134
lines changed

4 files changed

+71
-134
lines changed

src/components/DatePicker/index.js

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,66 @@
1-
import React, { Component } from 'react';
2-
import { DatePicker } from 'antd';
1+
import React, { useState } from 'react';
2+
import {
3+
Split,
4+
SplitItem,
5+
DatePicker,
6+
Button,
7+
isValidDate,
8+
yyyyMMddFormat,
9+
} from '@patternfly/react-core';
310
import { connect } from 'dva';
4-
import moment from 'moment';
5-
import PropTypes from 'prop-types';
6-
import './index.less';
711

8-
const { RangePicker } = DatePicker;
12+
const PFDatePicker = props => {
13+
const { dispatch, onChangeCallback } = props;
14+
const [from, setFrom] = useState('');
915

10-
@connect(({ global, datastore }) => ({
11-
indices: datastore.indices,
12-
selectedDateRange: global.selectedDateRange,
13-
}))
14-
class AntdDatePicker extends Component {
15-
static propTypes = {
16-
onChangeCallback: PropTypes.func,
17-
};
16+
const toValidator = date =>
17+
isValidDate(from) && date >= from
18+
? ''
19+
: '"To" date must be greater than or equal to the "From" date';
1820

19-
static defaultProps = {
20-
onChangeCallback: () => {},
21+
const onFromChange = (_str, date) => {
22+
setFrom(date);
2123
};
2224

23-
handleChange = (dates, dateStrings) => {
24-
const { dispatch, onChangeCallback } = this.props;
25+
const getDataOnChange = (fromDate, toDate) => {
2526
dispatch({
2627
type: 'global/updateSelectedDateRange',
2728
payload: {
28-
start: dateStrings[0],
29-
end: dateStrings[1],
29+
start: String(yyyyMMddFormat(fromDate)),
30+
end: String(yyyyMMddFormat(toDate)),
3031
},
31-
}).then(() => {
32-
onChangeCallback();
3332
});
3433
};
3534

36-
// Since we have no data before or after a certain
37-
// time range, we need to refrain user from selecting
38-
// such dates in the date picker.
39-
disabledDate = current => {
40-
const { indices } = this.props;
41-
if (indices.length === 0) {
42-
return false;
43-
}
44-
if (current.isBefore(moment(indices[indices.length - 1]).startOf('month'))) {
45-
return true;
35+
const onToChange = (_str, date) => {
36+
if (isValidDate(date)) {
37+
getDataOnChange(from, date);
4638
}
47-
if (current.isAfter(moment().endOf('day'))) {
48-
return true;
49-
}
50-
return false;
5139
};
5240

53-
render() {
54-
const { selectedDateRange, style } = this.props;
55-
56-
return (
57-
<RangePicker
58-
separator="to"
59-
style={style}
60-
onChange={this.handleChange}
61-
value={[
62-
selectedDateRange.start ? moment(selectedDateRange.start) : moment().subtract(7, 'day'),
63-
selectedDateRange.end ? moment(selectedDateRange.end) : moment(),
64-
]}
65-
disabledDate={this.disabledDate}
66-
size="default"
67-
ranges={{
68-
Today: [moment(), moment()],
69-
'Last week': [moment().subtract(7, 'day'), moment()],
70-
'Last month': [
71-
moment()
72-
.subtract(1, 'month')
73-
.startOf('month'),
74-
moment().startOf('month'),
75-
],
76-
}}
77-
/>
78-
);
79-
}
80-
}
41+
return (
42+
<>
43+
<Split style={{ marginBottom: '15px' }}>
44+
<SplitItem>
45+
<DatePicker onChange={onFromChange} aria-label="Start date" placeholder="YYYY-MM-DD" />
46+
</SplitItem>
47+
<SplitItem style={{ padding: '6px 12px 0 12px' }}>to</SplitItem>
48+
<SplitItem>
49+
<DatePicker
50+
onChange={onToChange}
51+
isDisabled={!isValidDate(from)}
52+
rangeStart={from}
53+
validators={[toValidator]}
54+
aria-label="End date"
55+
placeholder="YYYY-MM-DD"
56+
/>
57+
</SplitItem>
58+
<SplitItem style={{ marginLeft: '8px' }}>
59+
<Button onClick={() => onChangeCallback()}>Filter</Button>
60+
</SplitItem>
61+
</Split>
62+
</>
63+
);
64+
};
8165

82-
export default AntdDatePicker;
66+
export default connect()(PFDatePicker);

src/components/DatePicker/index.less

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/e2e/controllers.e2e.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ describe('controller page component', () => {
120120
});
121121

122122
test('should sort last modified column chronologically ascending', async () => {
123+
await page.reload({ waitUntil: ['networkidle0', 'domcontentloaded'] });
123124
await page.waitForSelector('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
124125
await page.click('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
125126
await page.waitForSelector('table > tbody > tr:nth-child(1) > td:nth-child(1) > span');
@@ -133,6 +134,9 @@ describe('controller page component', () => {
133134
});
134135

135136
test('should sort last modified column chronologically descending', async () => {
137+
await page.reload({ waitUntil: ['networkidle0', 'domcontentloaded'] });
138+
await page.waitForSelector('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
139+
await page.click('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
136140
await page.waitForSelector('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
137141
await page.click('table > thead > tr > th:nth-child(2) > div:nth-child(1) > span');
138142
await page.waitForSelector('table > tbody > tr:nth-child(1) > td:nth-child(1) > span');
@@ -218,16 +222,27 @@ describe('controller page component', () => {
218222
});
219223

220224
test('should display the date picker component on click', async () => {
221-
await page.waitForSelector('article > div > span > span > input:nth-child(1)');
222-
await page.click('article > div > span > span > input:nth-child(1)');
225+
await page.waitForSelector(
226+
'.pf-l-split__item:nth-child(1) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button > svg > path'
227+
);
228+
await page.click(
229+
'.pf-l-split__item:nth-child(1) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button > svg > path'
230+
);
223231
});
224232

225233
test('should change the selected dates after picking them from the calendar', async () => {
226234
await page.waitForSelector(
227-
'div.ant-calendar-footer.ant-calendar-range-bottom > div > div > span:nth-child(2)'
235+
'.pf-l-split__item:nth-child(1) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button'
236+
);
237+
await page.click(
238+
'.pf-l-split__item:nth-child(1) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button'
239+
);
240+
241+
await page.waitForSelector(
242+
'.pf-l-split__item:nth-child(3) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button > svg'
228243
);
229244
await page.click(
230-
'div.ant-calendar-footer.ant-calendar-range-bottom > div > div > span:nth-child(2)'
245+
'.pf-l-split__item:nth-child(3) > .pf-c-date-picker > .pf-c-date-picker__input > .pf-c-input-group > .pf-c-button > svg'
231246
);
232247
});
233248
});

src/pages/Controllers/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
1515
import { faStar } from '@fortawesome/free-solid-svg-icons';
1616

17-
import AntdDatePicker from '@/components/DatePicker';
17+
import PFDatePicker from '@/components/DatePicker';
1818
import Table from '@/components/Table';
1919
import { getDiffDate } from '@/utils/moment_constants';
2020

@@ -24,8 +24,7 @@ import { getDiffDate } from '@/utils/moment_constants';
2424
selectedDateRange: global.selectedDateRange,
2525
favoriteControllers: user.favoriteControllers,
2626
username: auth.username,
27-
loadingControllers:
28-
loading.effects['dashboard/fetchControllers'] || loading.effects['datastore/fetchMonthIndices'],
27+
loadingControllers: loading.effects['datastore/fetchMonthIndices'],
2928
}))
3029
class Controllers extends Component {
3130
constructor(props) {
@@ -205,7 +204,7 @@ class Controllers extends Component {
205204
/>
206205
) : (
207206
<CardBody>
208-
<AntdDatePicker
207+
<PFDatePicker
209208
style={{ width: 400, marginBottom: 16 }}
210209
onChangeCallback={this.fetchControllers}
211210
/>

0 commit comments

Comments
 (0)