Skip to content

Commit 9d2d1c8

Browse files
authored
Merge pull request #152 from data-driven-forms/allow-strings-for-disabled-days-time-picker
Allow strings for disabled days time picker
2 parents b664b8d + d7ec567 commit 9d2d1c8

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

packages/pf3-component-mapper/demo/demo-schemas/sandbox.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,10 @@ const output = {
405405
component: components.DATE_PICKER,
406406
isClearable: true,
407407
disabledDays: [
408+
'today',
408409
new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 5),
409410
{
410-
before: new Date(),
411+
before: 'Tue Oct 08 2019 10:23:03 GMT+0200 (Central European Summer Time)',
411412
},
412413
],
413414
},

packages/pf3-component-mapper/src/form-fields/date-time-picker/date-time-picker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Overlay } from 'patternfly-react';
33
import PropTypes from 'prop-types';
44
import PickerInput from './picker-input';
55
import PopoverRoot from './popover-root';
6+
import { createDisabledDays } from './helpers';
67

78
const selectValidDate = (newDate, disabledDays) => {
89
const { after, before } = disabledDays.find(item => typeof item === 'object' && !(item instanceof Date)) || {};
@@ -107,6 +108,7 @@ export class DateTimePicker extends React.Component {
107108
disabledDays,
108109
isClearable,
109110
} = this.props;
111+
const cleanDisabledDays = createDisabledDays(disabledDays);
110112
return (
111113
<div style={{ position: 'relative' }} ref={ this.wrapperRef } >
112114
<PickerInput
@@ -142,7 +144,7 @@ export class DateTimePicker extends React.Component {
142144
locale={ locale }
143145
todayButtonLabel={ todayButtonLabel }
144146
showTodayButton={ showTodayButton }
145-
disabledDays={ disabledDays }
147+
disabledDays={ cleanDisabledDays }
146148
/>
147149
</Overlay>
148150
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const createDateObject = value => {
2+
if (value === 'today') {
3+
return new Date();
4+
}
5+
6+
if (typeof value === 'string') {
7+
return new Date(value);
8+
}
9+
10+
return value;
11+
};
12+
13+
export const createDisabledDays = disabledDays => disabledDays.map(item => {
14+
if (typeof item === 'object' && !(item instanceof Date) && !Array.isArray(item)) {
15+
16+
return Object.keys(item).reduce((acc, curr) => ({
17+
...acc,
18+
[curr]: createDateObject(item[curr]),
19+
}), {});
20+
}
21+
22+
return createDateObject(item);
23+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { createDisabledDays } from '../../../form-fields/date-time-picker/helpers';
2+
3+
describe('<DateTimePicker /> helpers', () => {
4+
it('should convert string into Date object', () => {
5+
const inputValue = [
6+
'Tue Oct 08 2019 13:27:20 GMT+0200 (Central European Summer Time)',
7+
'1995-12-18T03:24:00',
8+
new Date(),
9+
];
10+
const expectedValue = [
11+
expect.any(Date),
12+
expect.any(Date),
13+
expect.any(Date),
14+
];
15+
16+
const output = createDisabledDays(inputValue);
17+
expect(output).toEqual(expectedValue);
18+
expect(output[0].getFullYear()).toEqual(2019);
19+
expect(output[1].getFullYear()).toEqual(1995);
20+
expect(output[1].getDay()).toEqual(1);
21+
expect(output[1].getMonth()).toEqual(11);
22+
});
23+
24+
it('should return Date object if used alias today', () => {
25+
const inputValue = [
26+
'today',
27+
];
28+
const expectedValue = [
29+
expect.any(Date),
30+
];
31+
32+
const output = createDisabledDays(inputValue);
33+
expect(output).toEqual(expectedValue);
34+
});
35+
36+
it('should return range object with correct keys', () => {
37+
const inputValue = [
38+
{
39+
before: 'Tue Oct 08 2019 13:27:20 GMT+0200 (Central European Summer Time)',
40+
after: '1995-12-18T03:24:00',
41+
42+
},
43+
new Date(),
44+
];
45+
const expectedValue = [
46+
{
47+
before: expect.any(Date),
48+
after: expect.any(Date),
49+
},
50+
expect.any(Date),
51+
];
52+
53+
const output = createDisabledDays(inputValue);
54+
expect(output).toEqual(expectedValue);
55+
});
56+
});

packages/react-renderer-demo/src/docs-components/component-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ This component is using [react-day-picker](https://react-day-picker.js.org/docs/
118118
|todayButtonLabel|string|Label for today button|
119119
|showTodayButton|bool|show/hide today button|
120120
|isDisabled|bool|disable component|
121-
|disabledDays|array|Mark specific days or a range of days as disabled. [More info](https://react-day-picker.js.org/examples/disabled)|
121+
|disabledDays|array|Mark specific days or a range of days as disabled. [More info](https://react-day-picker.js.org/examples/disabled). In order to store this prop to JSON we allow using string. Any string accepted by Date constructor is valid value. There is an alias for current date: `today`|
122122
|closeOnDaySelect|bool|Close the calendar popover after selecting date.|
123123

124124
<ExampleLink to='date-picker' />

0 commit comments

Comments
 (0)