2018-08-17 13:49:15 +09:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { Components, registerComponent } from 'meteor/vulcan:core';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
2019-01-26 21:21:09 +09:00
|
|
|
const isEmptyValue = value =>
|
|
|
|
typeof value === 'undefined' ||
|
|
|
|
value === null ||
|
|
|
|
value === '' ||
|
|
|
|
(Array.isArray(value) && value.length === 0);
|
2018-08-17 13:49:15 +09:00
|
|
|
|
|
|
|
class DateComponent2 extends PureComponent {
|
|
|
|
state = {
|
|
|
|
year: null,
|
|
|
|
month: null,
|
|
|
|
day: null,
|
2019-01-26 21:21:09 +09:00
|
|
|
};
|
2018-08-17 13:49:15 +09:00
|
|
|
|
2019-01-26 21:21:09 +09:00
|
|
|
updateDate = date => {
|
2018-08-17 13:49:15 +09:00
|
|
|
const { value, path } = this.props;
|
|
|
|
let newDate;
|
|
|
|
this.setState(date, () => {
|
|
|
|
const { year, month, day } = this.state;
|
|
|
|
if (isEmptyValue(value)) {
|
|
|
|
if (year && month && day) {
|
|
|
|
// wait until we have all three values to update the date
|
2019-01-26 21:21:09 +09:00
|
|
|
newDate = moment()
|
|
|
|
.year(year)
|
|
|
|
.month(month)
|
|
|
|
.date(day);
|
2018-08-17 13:49:15 +09:00
|
|
|
this.props.updateCurrentValues({ [path]: newDate.toDate() });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// update current date
|
|
|
|
newDate = moment(this.props.value);
|
|
|
|
if (year) newDate.year(year);
|
|
|
|
if (month) newDate.month(month);
|
|
|
|
if (day) newDate.date(day);
|
|
|
|
this.props.updateCurrentValues({ [path]: newDate.toDate() });
|
|
|
|
}
|
|
|
|
});
|
2019-01-26 21:21:09 +09:00
|
|
|
};
|
2018-08-17 13:49:15 +09:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { path, value } = this.props;
|
|
|
|
const months = moment.months();
|
|
|
|
const mDate = !isEmptyValue(value) && moment(value);
|
|
|
|
|
|
|
|
const monthProperties = {
|
|
|
|
label: 'Month',
|
|
|
|
name: `${path}.month`,
|
|
|
|
layout: 'vertical',
|
|
|
|
options: months.map((m, i) => ({ label: m, value: m })),
|
2019-01-26 21:21:09 +09:00
|
|
|
value: (mDate && mDate.format('MMMM')) || '',
|
2018-08-17 13:49:15 +09:00
|
|
|
onChange: (name, value) => {
|
|
|
|
this.updateDate({ month: value });
|
2019-01-26 21:21:09 +09:00
|
|
|
},
|
2018-12-31 15:22:17 +09:00
|
|
|
};
|
2018-08-17 13:49:15 +09:00
|
|
|
|
|
|
|
const dayProperties = {
|
|
|
|
label: 'Day',
|
|
|
|
name: `${path}.day`,
|
|
|
|
layout: 'vertical',
|
|
|
|
maxLength: 2,
|
2019-01-26 21:21:09 +09:00
|
|
|
value: (mDate && mDate.format('DD')) || '',
|
|
|
|
onBlur: e => {
|
2018-08-17 13:49:15 +09:00
|
|
|
this.updateDate({ day: e.target.value });
|
2019-01-26 21:21:09 +09:00
|
|
|
},
|
2018-12-31 15:22:17 +09:00
|
|
|
};
|
2018-08-17 13:49:15 +09:00
|
|
|
|
|
|
|
const yearProperties = {
|
|
|
|
label: 'Year',
|
|
|
|
name: `${path}.year`,
|
|
|
|
layout: 'vertical',
|
|
|
|
maxLength: 4,
|
2019-01-26 21:21:09 +09:00
|
|
|
value: (mDate && mDate.format('YYYY')) || '',
|
|
|
|
onBlur: e => {
|
2018-08-17 13:49:15 +09:00
|
|
|
this.updateDate({ year: e.target.value });
|
2019-01-26 21:21:09 +09:00
|
|
|
},
|
2018-12-31 15:22:17 +09:00
|
|
|
};
|
2018-08-17 13:49:15 +09:00
|
|
|
|
|
|
|
return (
|
2019-01-26 21:21:09 +09:00
|
|
|
<Components.FormItem {...this.props.inputProperties}>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<Components.FormComponentSelect
|
|
|
|
inputProperties={monthProperties}
|
|
|
|
datatype={[{ type: String }]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div style={{ marginLeft: 10, width: 60 }}>
|
|
|
|
<Components.FormComponentText inputProperties={dayProperties} />
|
|
|
|
</div>
|
|
|
|
<div style={{ marginLeft: 10, width: 80 }}>
|
|
|
|
<Components.FormComponentText inputProperties={yearProperties} />
|
|
|
|
</div>
|
2018-08-17 13:49:15 +09:00
|
|
|
</div>
|
2019-01-26 21:21:09 +09:00
|
|
|
</Components.FormItem>
|
2018-08-17 13:49:15 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DateComponent2;
|
|
|
|
|
2019-01-26 21:21:09 +09:00
|
|
|
registerComponent('FormComponentDate2', DateComponent2);
|