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 3 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
27 changes: 23 additions & 4 deletions src/components/EditorWidgets/Date/DateControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,35 @@ 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.
isValid = datetime => (moment.isMoment(datetime) || datetime instanceof Date || datetime === '');
Copy link
Contributor

Choose a reason for hiding this comment

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

isValid has a specific use for widget controls, which we're not trying to use here, so can we change this to something else, maybe isValidDate?


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

Choose a reason for hiding this comment

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

We do still need to handle the empty case here, in case the user wants to empty the field.

onChange(datetime);
} else {

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

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

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

if (parsedDate.isValid()) {
const formattedValue = parsedDate.format(this.format);
onChange(formattedValue);
}
}

setInactiveStyle();
};

render() {
const { includeTime, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
const format = this.format;
Expand All @@ -53,7 +72,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