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

Allow strings for disabled days time picker #152

Merged
merged 2 commits into from
Oct 8, 2019
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
3 changes: 2 additions & 1 deletion packages/pf3-component-mapper/demo/demo-schemas/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ const output = {
component: components.DATE_PICKER,
isClearable: true,
disabledDays: [
'today',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this line actually means? Is it that you can't select today in the picker? Would it disable any date befor today if I write before: 'today' below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using this component. There is a link in the componentApi docs for date time picker

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this doesn't really answer my question, but I tested it and it works:

                  disabledDays: [{
                    after: 'today'
                  }]

This doesn't allow me to select any date in the future and that's the thing I want the most from this PR 😉

new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 5),
{
before: new Date(),
before: 'Tue Oct 08 2019 10:23:03 GMT+0200 (Central European Summer Time)',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Overlay } from 'patternfly-react';
import PropTypes from 'prop-types';
import PickerInput from './picker-input';
import PopoverRoot from './popover-root';
import { createDisabledDays } from './helpers';

const selectValidDate = (newDate, disabledDays) => {
const { after, before } = disabledDays.find(item => typeof item === 'object' && !(item instanceof Date)) || {};
Expand Down Expand Up @@ -107,6 +108,7 @@ export class DateTimePicker extends React.Component {
disabledDays,
isClearable,
} = this.props;
const cleanDisabledDays = createDisabledDays(disabledDays);
return (
<div style={{ position: 'relative' }} ref={ this.wrapperRef } >
<PickerInput
Expand Down Expand Up @@ -142,7 +144,7 @@ export class DateTimePicker extends React.Component {
locale={ locale }
todayButtonLabel={ todayButtonLabel }
showTodayButton={ showTodayButton }
disabledDays={ disabledDays }
disabledDays={ cleanDisabledDays }
/>
</Overlay>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const createDateObject = value => {
if (value === 'today') {
return new Date();
}

if (typeof value === 'string') {
return new Date(value);
}

return value;
};

export const createDisabledDays = disabledDays => disabledDays.map(item => {
if (typeof item === 'object' && !(item instanceof Date) && !Array.isArray(item)) {

return Object.keys(item).reduce((acc, curr) => ({
...acc,
[curr]: createDateObject(item[curr]),
}), {});
}

return createDateObject(item);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createDisabledDays } from '../../../form-fields/date-time-picker/helpers';

describe('<DateTimePicker /> helpers', () => {
it('should convert string into Date object', () => {
const inputValue = [
'Tue Oct 08 2019 13:27:20 GMT+0200 (Central European Summer Time)',
'1995-12-18T03:24:00',
new Date(),
];
const expectedValue = [
expect.any(Date),
expect.any(Date),
expect.any(Date),
];

const output = createDisabledDays(inputValue);
expect(output).toEqual(expectedValue);
expect(output[0].getFullYear()).toEqual(2019);
expect(output[1].getFullYear()).toEqual(1995);
expect(output[1].getDay()).toEqual(1);
expect(output[1].getMonth()).toEqual(11);
});

it('should return Date object if used alias today', () => {
const inputValue = [
'today',
];
const expectedValue = [
expect.any(Date),
];

const output = createDisabledDays(inputValue);
expect(output).toEqual(expectedValue);
});

it('should return range object with correct keys', () => {
const inputValue = [
{
before: 'Tue Oct 08 2019 13:27:20 GMT+0200 (Central European Summer Time)',
after: '1995-12-18T03:24:00',

},
new Date(),
];
const expectedValue = [
{
before: expect.any(Date),
after: expect.any(Date),
},
expect.any(Date),
];

const output = createDisabledDays(inputValue);
expect(output).toEqual(expectedValue);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This component is using [react-day-picker](https://react-day-picker.js.org/docs/
|todayButtonLabel|string|Label for today button|
|showTodayButton|bool|show/hide today button|
|isDisabled|bool|disable component|
|disabledDays|array|Mark specific days or a range of days as disabled. [More info](https://react-day-picker.js.org/examples/disabled)|
|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`|
|closeOnDaySelect|bool|Close the calendar popover after selecting date.|

<ExampleLink to='date-picker' />
Expand Down