Skip to content

Commit

Permalink
Merge pull request #6009 from cfpb/ans_filters_datepicker
Browse files Browse the repository at this point in the history
Filterable list control: Convert date inputs into datepicker fields
  • Loading branch information
anselmbradford committed Sep 21, 2020
2 parents ff8efea + cf97048 commit b622e80
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
8 changes: 8 additions & 0 deletions cfgov/unprocessed/css/enhancements/forms.less
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ input {
margin-top: 0;
}

/*
* Sets minimum height when an input is a datepicker.
* This is for an issue in iOS Safari where the input is collapsed.
*/
input[type=date] {
min-height: unit( 35px / @base-font-size-px, rem );
}

/* topdoc
name: EOF
eof: true
Expand Down
17 changes: 13 additions & 4 deletions cfgov/unprocessed/js/modules/util/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import ERROR_MESSAGES from '../../config/error-messages-config';
*/
function date( field, currentStatus ) {
const status = currentStatus || {};
let valueToEval = field.value;

/* TODO: Merge this into the regex checks.
This converts numbers from any other format to MM/DD/YYYY */
if ( !isNaN( field.valueAsNumber ) ) {
const date = new Date( field.valueAsNumber );
valueToEval = `${ date.getUTCMonth() + 1 }/${ date.getUTCDate() }/${ date.getUTCFullYear() }`;
}

/* Date regexes match the date patterns that are
allowed in cfgov/v1/forms.py FilterableDateField */
Expand All @@ -32,15 +40,16 @@ function date( field, currentStatus ) {
const dayMonthYearRegex =
/^(?:\d{1}|\d{2})(?:\-|\/)(?:\d{1}|\d{2})(?:\-|\/)(?:\d{4}|\d{2})$/;

const inputIsValid = yearRegex.test( field.value ) ||
monthYearRegex.test( field.value ) ||
dayMonthYearRegex.test( field.value );
const inputIsValid = yearRegex.test( valueToEval ) ||
monthYearRegex.test( valueToEval ) ||
dayMonthYearRegex.test( valueToEval );

if ( field.value && !inputIsValid ) {
if ( valueToEval && !inputIsValid ) {
status.msg = status.msg || '';
status.msg += ERROR_MESSAGES.DATE.INVALID;
status.date = false;
}

return status;
}

Expand Down
5 changes: 3 additions & 2 deletions cfgov/v1/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def validate_after_1900(date):
'%m-%d-%y', # 10-25-16, 9-1-16
'%m/%d/%Y', # 10/25/2016, 9/1/2016
'%m-%d-%Y', # 10-25-2016, 9-1-2016
'%Y-%m-%d', # 2016-10-25, 2016-9-1
'%m/%Y', # 10/2016, 7/2017
'%m-%Y', # 10-2016, 7-2017
'%m/%y', # 10/16, 4/18
Expand All @@ -36,7 +37,7 @@ def validate_after_1900(date):

default_widget_attrs = {
'class': 'a-text-input a-text-input__full',
'type': 'text',
'type': 'date',
'placeholder': 'mm/dd/yyyy',
'data-type': 'date'
}
Expand Down Expand Up @@ -102,7 +103,7 @@ class FilterableListForm(forms.Form):
})
)

preferred_datetime_format = '%m/%d/%Y'
preferred_datetime_format = '%Y-%m-%d'

def __init__(self, *args, **kwargs):
self.filterable_pages = kwargs.pop('filterable_pages')
Expand Down
22 changes: 14 additions & 8 deletions cfgov/v1/util/date_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
from dateutil.relativedelta import relativedelta


# This utility file exists to support date input fields that are typeable
# text inputs, which occur in browsers that don't support the date input field
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

# For more information on date formatting, see the python documentation here:
# https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

full_date_patterns = (
'%m/%d/%Y', # 10/25/2016, 9/1/2016
'%m-%d-%Y', # 10-25-2016, 9-1-2016
'%m/%d/%y', # 10/25/16, 9/1/16
'%m-%d-%y', # 10-25-16, 9-1-16
'%m/%d/%Y', # 10/25/2016, 09/01/2016
'%m-%d-%Y', # 10-25-2016, 09-01-2016
'%Y-%m-%d', # 2016-10-25, 2016-09-01
'%m/%d/%y', # 10/25/16, 09/01/16
'%m-%d-%y', # 10-25-16, 09-01-16
)

month_year_date_patterns = (
'%m/%Y', # 10/2016, 7/2017
'%m-%Y', # 10-2016, 7-2017
'%m/%y', # 10/16, 4/18
'%m-%y', # 10-16, 4-18
'%m/%Y', # 10/2016, 07/2017
'%m-%Y', # 10-2016, 07-2017
'%Y-%m', # 2016-10, 2017-07
'%m/%y', # 10/16, 04/18
'%m-%y', # 10-16, 04-18
)

year_date_patterns = (
Expand Down
4 changes: 2 additions & 2 deletions test/cypress/integration/components/filterable-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe( 'Filterable List', () => {
filterableList.open();
filterableList.showFilters();
filterableList.filterForm().should( 'be.visible' );
filterableList.setFromDate( '01/01/2010' );
filterableList.setToDate( '01/01/2020' );
filterableList.setFromDate( '2010-01-01' );
filterableList.setToDate( '2020-01-01' );
filterableList.applyFilters();
filterableList.filterNotification().should( 'be.visible' );
} );
Expand Down

0 comments on commit b622e80

Please sign in to comment.