Widget should set default values through onChange

Otherwise, default value may not get persisted
This commit is contained in:
Cássio Zen 2016-12-01 16:28:33 -02:00
parent 047f84405a
commit 782c386d3e
2 changed files with 17 additions and 5 deletions

View File

@ -2,6 +2,12 @@ import React, { PropTypes } from 'react';
import DateTime from 'react-datetime'; import DateTime from 'react-datetime';
export default class DateControl extends React.Component { export default class DateControl extends React.Component {
componentDidMount() {
if (!this.props.value) {
this.props.onChange(new Date());
}
}
handleChange = (datetime) => { handleChange = (datetime) => {
this.props.onChange(datetime); this.props.onChange(datetime);
}; };
@ -9,7 +15,7 @@ export default class DateControl extends React.Component {
render() { render() {
return (<DateTime return (<DateTime
timeFormat={false} timeFormat={false}
value={this.props.value || new Date()} value={this.props.value}
onChange={this.handleChange} onChange={this.handleChange}
/>); />);
} }
@ -17,5 +23,5 @@ export default class DateControl extends React.Component {
DateControl.propTypes = { DateControl.propTypes = {
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
value: PropTypes.object, value: PropTypes.object, // eslint-disable-line
}; };

View File

@ -2,16 +2,22 @@ import React, { PropTypes } from 'react';
import DateTime from 'react-datetime'; import DateTime from 'react-datetime';
export default class DateTimeControl extends React.Component { export default class DateTimeControl extends React.Component {
handleChange = datetime => { componentDidMount() {
if (!this.props.value) {
this.props.onChange(new Date());
}
}
handleChange = (datetime) => {
this.props.onChange(datetime); this.props.onChange(datetime);
}; };
render() { render() {
return <DateTime value={this.props.value || new Date()} onChange={this.handleChange}/>; return <DateTime value={this.props.value} onChange={this.handleChange} />;
} }
} }
DateTimeControl.propTypes = { DateTimeControl.propTypes = {
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
value: PropTypes.object, value: PropTypes.object, // eslint-disable-line
}; };