Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Calendar: Use isInvalidDate to disallow date selections #1685

Merged
merged 1 commit into from
Apr 9, 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
15 changes: 7 additions & 8 deletions docs/components/packages/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ The date in human-readable format. Displayed in the text input.

A string error message, shown to the user.

### `invalidDays`

- Type: One of type: enum, func
- Default: null

(Coming Soon) Optionally invalidate certain days. `past`, `future`, `none`, or function are accepted.
A function will be passed to react-dates' `isOutsideRange` prop

### `onUpdate`

- **Required**
Expand All @@ -51,6 +43,13 @@ A function called upon selection of a date or input change.

The date format in moment.js-style tokens.

### `isInvalidDate`

- Type: Function
- Default: null

A function to determine if a day on the calendar is not valid

`DateRange` (component)
=======================

Expand Down
4 changes: 3 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 2.0.0 (unreleased)
# 2.0.0 (Unreleased)
- Chart legend component now uses withInstanceId HOC so the ids used in several HTML elements are unique.
- Chart component now accepts data with negative values.
- Chart component: new prop `filterParam` used to detect selected items in the current query. If there are, they will be displayed in the chart even if their values are 0.
- Expand search results and allow searching when input is refocused in autocompleter.
- Animation Slider: Remove `focusOnChange` in favor of `onExited` so consumers can pass a function to be executed after a transition has finished.
- SearchListControl: Add `onSearch` callback prop to let the parent component know about search changes.
- Calendar: Expose `isInvalidDate` prop to `DatePicker` to indicated invalid days that are not selectable.
- Calendar: Expose `isInvalidDate` prop to `DateRange` and remove the `invalidDays` prop.

# 1.6.0
- Chart component: new props `emptyMessage` and `baseValue`. When an empty message is provided, it will be displayed on top of the chart if there are no values different than `baseValue`.
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@woocommerce/components",
"version": "1.6.0",
"version": "2.0.0",
"description": "UI components for WooCommerce.",
"author": "Automattic",
"license": "GPL-2.0-or-later",
Expand Down
18 changes: 7 additions & 11 deletions packages/components/src/calendar/date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ class DatePicker extends Component {
}

render() {
const { date, text, dateFormat, error } = this.props;
// @todo Make upstream Gutenberg change to invalidate certain days.
// const isOutsideRange = getOutsideRange( invalidDays );
const { date, text, dateFormat, error, isInvalidDate } = this.props;

return (
<Dropdown
position="bottom center"
Expand Down Expand Up @@ -98,6 +97,7 @@ class DatePicker extends Component {
<WpDatePicker
currentDate={ date }
onChange={ partial( this.onDateChange, onToggle ) }
isInvalidDate={ isInvalidDate }
/>
</div>
</Section>
Expand All @@ -120,14 +120,6 @@ DatePicker.propTypes = {
* A string error message, shown to the user.
*/
error: PropTypes.string,
/**
* (Coming Soon) Optionally invalidate certain days. `past`, `future`, `none`, or function are accepted.
* A function will be passed to react-dates' `isOutsideRange` prop
*/
invalidDays: PropTypes.oneOfType( [
PropTypes.oneOf( [ 'past', 'future', 'none' ] ),
PropTypes.func,
] ),
/**
* A function called upon selection of a date or input change.
*/
Expand All @@ -136,6 +128,10 @@ DatePicker.propTypes = {
* The date format in moment.js-style tokens.
*/
dateFormat: PropTypes.string.isRequired,
/**
* A function to determine if a day on the calendar is not valid
*/
isInvalidDate: PropTypes.func,
};

export default DatePicker;
16 changes: 6 additions & 10 deletions packages/components/src/calendar/date-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { validateDateInputForRange } from '@woocommerce/date';
*/
import DateInput from './input';
import phrases from './phrases';
import { getOutsideRange } from './utils';

const isRTL = () => document.documentElement.dir === 'rtl';

Expand Down Expand Up @@ -96,9 +95,8 @@ class DateRange extends Component {
shortDateFormat,
isViewportMobile,
isViewportSmall,
invalidDays,
isInvalidDate,
} = this.props;
const isOutsideRange = getOutsideRange( invalidDays );
const isDoubleCalendar = isViewportMobile && ! isViewportSmall;
return (
<div
Expand Down Expand Up @@ -148,7 +146,9 @@ class DateRange extends Component {
endDate={ before }
orientation={ 'horizontal' }
numberOfMonths={ isDoubleCalendar ? 2 : 1 }
isOutsideRange={ isOutsideRange }
isOutsideRange={ ( date ) => {
return isInvalidDate && isInvalidDate( date.toDate() );
} }
minimumNights={ 0 }
hideKeyboardShortcutsPanel
noBorder
Expand Down Expand Up @@ -192,13 +192,9 @@ DateRange.propTypes = {
*/
focusedInput: PropTypes.string,
/**
* Optionally invalidate certain days. `past`, `future`, `none`, or function are accepted.
* A function will be passed to react-dates' `isOutsideRange` prop
* A function to determine if a day on the calendar is not valid
*/
invalidDays: PropTypes.oneOfType( [
PropTypes.oneOf( [ 'past', 'future', 'none' ] ),
PropTypes.func,
] ),
isInvalidDate: PropTypes.func,
/**
* A function called upon selection of a date.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/calendar/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const MyDateRange = withState( {
onUpdate={ onRangeUpdate }
shortDateFormat={ dateFormat }
focusedInput={ focusedInput }
invalidDays="future"
isInvalidDate={ date => moment().isBefore( moment( date ), 'date' ) }
/>
</Section>

Expand All @@ -49,7 +49,7 @@ const MyDateRange = withState( {
error={ afterError }
onUpdate={ onDatePickerUpdate }
dateFormat={ dateFormat }
invalidDays="none"
isInvalidDate={ date => moment( date ).day() === 1 }
/>
</Section>
</div>
Expand Down
20 changes: 0 additions & 20 deletions packages/components/src/calendar/utils.js

This file was deleted.

11 changes: 8 additions & 3 deletions packages/components/src/filters/advanced/date-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isoDateFormat, toMoment } from '@woocommerce/date';
*/
import DatePicker from '../../calendar/date-picker';
import { textContent } from './utils';
import moment from 'moment';

const dateStringFormat = __( 'MMM D, YYYY', 'woocommerce-admin' );
const dateFormat = __( 'MM/DD/YYYY', 'woocommerce-admin' );
Expand Down Expand Up @@ -126,6 +127,10 @@ class DateFilter extends Component {
}
}

isFutureDate( dateString ) {
return moment().isBefore( moment( dateString ), 'day' );
}

getFilterInputs() {
const { filter } = this.props;
const { before, beforeText, beforeError, after, afterText, afterError } = this.state;
Expand All @@ -141,7 +146,7 @@ class DateFilter extends Component {
error={ afterError }
onUpdate={ partial( this.onRangeDateChange, 'after' ) }
dateFormat={ dateFormat }
invalidDays="none"
isInvalidDate={ this.isFutureDate }
/>
),
before: (
Expand All @@ -151,7 +156,7 @@ class DateFilter extends Component {
error={ beforeError }
onUpdate={ partial( this.onRangeDateChange, 'before' ) }
dateFormat={ dateFormat }
invalidDays="none"
isInvalidDate={ this.isFutureDate }
/>
),
span: <span className="separator" />,
Expand All @@ -166,7 +171,7 @@ class DateFilter extends Component {
error={ beforeError }
onUpdate={ this.onSingleDateChange }
dateFormat={ dateFormat }
invalidDays="none"
isInvalidDate={ this.isFutureDate }
/>
);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/components/src/filters/date/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Component, Fragment } from '@wordpress/element';
import { TabPanel, Button } from '@wordpress/components';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import moment from 'moment';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,6 +35,10 @@ class DatePickerContent extends Component {
}
}

isFutureDate( dateString ) {
return moment().isBefore( moment( dateString ), 'day' );
}

render() {
const {
period,
Expand Down Expand Up @@ -89,7 +94,7 @@ class DatePickerContent extends Component {
after={ after }
before={ before }
onUpdate={ onUpdate }
invalidDays="future"
isInvalidDate={ this.isFutureDate }
focusedInput={ focusedInput }
afterText={ afterText }
beforeText={ beforeText }
Expand Down