135 lines
3.3 KiB
JavaScript
135 lines
3.3 KiB
JavaScript
var Datepicker;
|
|
import './calendar.js';
|
|
export default window.Datepicker = Datepicker = class Datepicker extends React.Component {
|
|
constructor(props) {
|
|
var ref, ref1, s, v, value;
|
|
super(props);
|
|
value = null;
|
|
console.log('datepicker_typeof', typeof props.value, props.value);
|
|
v = props.value;
|
|
|
|
if (v != null && v !== '' && v !== '0000-00-00' && v !== '0' && v !== 0) {
|
|
s = v.toString().split('-');
|
|
|
|
if (s.length === 3) {
|
|
value = new Date(s[0], s[1] - 1, s[2]);
|
|
} else {
|
|
value = new Date(parseInt(v) * 1000);
|
|
}
|
|
}
|
|
|
|
this.state = {
|
|
name: (ref = props.name) != null ? ref : '',
|
|
value: value,
|
|
placeholder: (ref1 = props.placeholder) != null ? ref1 : '',
|
|
timepicker: props.timepicker === true
|
|
};
|
|
this.input = null;
|
|
this.calendar = null;
|
|
this.onCancel = this.onCancel.bind(this);
|
|
this.onSubmit = this.onSubmit.bind(this);
|
|
this.openCalendar = this.openCalendar.bind(this);
|
|
}
|
|
|
|
openCalendar() {
|
|
return this.calendar.open();
|
|
}
|
|
|
|
getValue() {
|
|
var c, d, date, month, res, year;
|
|
d = typeof value !== "undefined" && value !== null ? value : this.state.value;
|
|
res = '';
|
|
|
|
if (d != null && d !== '') {
|
|
date = (c = d.getDate()) < 10 ? "0" + c : c;
|
|
month = (c = d.getMonth() + 1) < 10 ? "0" + c : c;
|
|
year = d.getFullYear();
|
|
res = [year, month, date].join("-");
|
|
|
|
if (this.state.timepicker) {
|
|
res = d.getTime() / 1000;
|
|
}
|
|
} else {
|
|
res = '0000-00-00';
|
|
|
|
if (this.state.timepicker) {
|
|
res = 0;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
onCancel() {
|
|
return this.calendar.close();
|
|
}
|
|
|
|
onSubmit(value) {
|
|
this.setState({
|
|
value
|
|
});
|
|
|
|
if (value != null) {
|
|
this.input.value = this.formatValue(value);
|
|
}
|
|
|
|
return this.calendar.close();
|
|
}
|
|
|
|
formatValue(value) {
|
|
var c, d, date, hours, minutes, month, res, year;
|
|
d = value != null && value !== void 0 ? value : this.state.value;
|
|
console.log('formatValue', value, this.state.value);
|
|
res = '';
|
|
|
|
if (d != null && d !== '') {
|
|
date = (c = d.getDate()) < 10 ? "0" + c : c;
|
|
month = (c = d.getMonth() + 1) < 10 ? "0" + c : c;
|
|
year = d.getFullYear();
|
|
res = [date, month, year].join(".");
|
|
|
|
if (this.state.timepicker) {
|
|
hours = (c = d.getHours()) < 10 ? "0" + c : c;
|
|
minutes = (c = d.getHours()) < 10 ? "0" + c : c;
|
|
res += " " + hours + ":" + minutes;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
render() {
|
|
var _this = this;
|
|
|
|
var name, placeholder, timepicker, value;
|
|
({
|
|
name,
|
|
placeholder,
|
|
timepicker
|
|
} = this.state);
|
|
value = this.formatValue();
|
|
return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", {
|
|
ref: function (i) {
|
|
return _this.input = i;
|
|
},
|
|
type: "text",
|
|
name: name,
|
|
value: value,
|
|
placeholder: placeholder,
|
|
className: "i",
|
|
autoComplete: "off",
|
|
onClick: this.openCalendar
|
|
}), /*#__PURE__*/React.createElement(Calendar, {
|
|
onCancel: this.onCancel,
|
|
onSubmit: this.onSubmit,
|
|
value: this.getValue(),
|
|
timepicker: timepicker,
|
|
ref: function (i) {
|
|
return _this.calendar = i;
|
|
}
|
|
}));
|
|
}
|
|
|
|
};
|
|
//# sourceMappingURL=datepicker.js.map
|