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 typing in date widget #1247

Merged
merged 5 commits into from
Apr 14, 2018
Merged
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
29 changes: 25 additions & 4 deletions src/components/EditorWidgets/Date/DateControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,37 @@ export default class DateControl extends React.Component {
}
}

// Date is valid if datetime is a moment or Date object otherwise it's a string.
// Handle the empty case, if the user wants to empty the field.
isValidDate = datetime => (moment.isMoment(datetime) || datetime instanceof Date || datetime === '');

handleChange = datetime => {
const { onChange } = this.props;
if (!this.format || datetime === '') {
onChange(datetime);
} else {

// Set the date only if the format is valid
if (this.isValidDate(datetime)) {
const formattedValue = moment(datetime).format(this.format);
onChange(formattedValue);
}
};

onBlur = datetime => {
const { setInactiveStyle, onChange } = this.props;

if (!this.isValidDate(datetime)) {
const parsedDate = moment(datetime);

if (parsedDate.isValid()) {
const formattedValue = parsedDate.format(this.format);
onChange(formattedValue);
} else {
window.alert('The date you entered is invalid.');
}
}

setInactiveStyle();
};

render() {
const { includeTime, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
const format = this.format;
Expand All @@ -53,7 +74,7 @@ export default class DateControl extends React.Component {
value={moment(value, format)}
onChange={this.handleChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
onBlur={this.onBlur}
inputProps={{ className: classNameWrapper }}
/>
);
Expand Down