Vulcan/packages/vulcan-ui-bootstrap/lib/components/forms/Date2.jsx

105 lines
2.8 KiB
React
Raw Normal View History

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);
class DateComponent2 extends PureComponent {
state = {
year: null,
month: null,
day: null,
2019-01-26 21:21:09 +09:00
};
2019-01-26 21:21:09 +09:00
updateDate = date => {
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);
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
};
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')) || '',
onChange: (name, value) => {
this.updateDate({ month: value });
2019-01-26 21:21:09 +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 => {
this.updateDate({ day: e.target.value });
2019-01-26 21:21:09 +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 => {
this.updateDate({ year: e.target.value });
2019-01-26 21:21:09 +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>
</div>
2019-01-26 21:21:09 +09:00
</Components.FormItem>
);
}
}
export default DateComponent2;
2019-01-26 21:21:09 +09:00
registerComponent('FormComponentDate2', DateComponent2);