This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/date-time-input-range.js
2020-03-12 13:29:55 -04:00

54 lines
1.1 KiB
JavaScript

import { equal } from "@ember/object/computed";
import Component from "@ember/component";
export default Component.extend({
classNames: ["d-date-time-input-range"],
from: null,
to: null,
onChangeTo: null,
onChangeFrom: null,
currentPanel: "from",
showFromTime: true,
showToTime: true,
error: null,
fromPanelActive: equal("currentPanel", "from"),
toPanelActive: equal("currentPanel", "to"),
_valid(state) {
if (state.to && state.from && state.to < state.from) {
return I18n.t("date_time_picker.errors.to_before_from");
}
return true;
},
actions: {
_onChange(options, value) {
if (this.onChange) {
const state = {
from: this.from,
to: this.to
};
const diff = {};
diff[options.prop] = value;
const newState = Object.assign(state, diff);
const validation = this._valid(newState);
if (validation === true) {
this.set("error", null);
this.onChange(newState);
} else {
this.set("error", validation);
}
}
},
onChangePanel(panel) {
this.set("currentPanel", panel);
}
}
});