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

fix: DatePicker cleanup PropTypes #1131

Merged
merged 5 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"react-focus-lock": "^2.3.1",
"react-overlays": "^3.2.0",
"react-popper": "^2.2.3",
"react-required-if": "^1.0.3",
"shortid": "^2.2.14",
"tabbable": "^4.0.0"
},
Expand Down
7 changes: 6 additions & 1 deletion src/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isEnabledDate } from '../utils/dateUtils';
import moment from 'moment';
import Popover from '../Popover/Popover';
import PropTypes from 'prop-types';
import requiredIf from 'react-required-if';
import { validDateLookup } from './_validDateLookup';
import { DATEPICKER_TODAY_ACTIONS_TYPES, FORM_MESSAGE_TYPES } from '../utils/constants';
import React, { Component } from 'react';
Expand Down Expand Up @@ -541,7 +542,10 @@ DatePicker.propTypes = {
*/
todayAction: PropTypes.shape({
type: PropTypes.oneOf(DATEPICKER_TODAY_ACTIONS_TYPES),
label: PropTypes.string.isRequired
label: requiredIf(PropTypes.string, todayAction => {
const todayActionType = todayAction?.type?.trim();
return todayActionType === 'select' || todayActionType === 'navigate';
})
}),
/** An object identifying a validation message. The object will include properties for `state` and `text`;
* _e.g._, \`{ state: \'warning\', text: \'This is your last warning\' }\` */
Expand Down Expand Up @@ -572,6 +576,7 @@ DatePicker.defaultProps = {
defaultValue: '',
dateFormat: null,
locale: 'en',
localizedText: Calendar.defaultProps.localizedText,
todayAction: {
type: 'none'
},
Expand Down
12 changes: 10 additions & 2 deletions src/utils/CustomPropTypes/CustomPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ const i18n = (obj) => {
const valueKeys = Object.keys(value);

if (valueKeys.length !== shapeKeys.length) {
let missingKeys = shapeKeys.filter(key => !valueKeys.some(k => key === k));
return new Error(`The ${location} \`${propFullName}\` supplied to \`${componentName}\` only has ${valueKeys.length} strings when ${shapeKeys.length} were expected. Missing ${missingKeys.map(key => `\`${key}\``).join(', ')}.`);
let missMatchType = '';
let missMatchKeys = [];
if (valueKeys.length < shapeKeys.length) {
missMatchType = 'Missing';
missMatchKeys = shapeKeys.filter(key => !valueKeys.some(k => key === k));
} else {
missMatchType = 'Extra';
missMatchKeys = valueKeys.filter(key => !shapeKeys.some(k => key === k));
}
return new Error(`The ${location} \`${propFullName}\` supplied to \`${componentName}\` has ${valueKeys.length} string(s) when ${shapeKeys.length} were expected. ${missMatchType} ${missMatchKeys.map(key => `\`${key}\``).join(', ')}.`);
}

return null;
Expand Down