2022-02-10 20:51:50 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @module DateDropdown
|
|
|
|
* DateDropdown components are used to display a dropdown of months and years to handle date selection
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <DateDropdown @handleDateSelection={this.actionFromParent} @name={{"startTime"}}/>
|
|
|
|
* ```
|
|
|
|
* @param {function} handleDateSelection - is the action from the parent that the date picker triggers
|
|
|
|
* @param {string} [name] - optional argument passed from date dropdown to parent function
|
2022-02-17 20:17:59 +00:00
|
|
|
* @param {string} [submitText] - optional argument to change submit button text
|
2022-02-10 20:51:50 +00:00
|
|
|
*/
|
2022-02-17 16:10:56 +00:00
|
|
|
export default class DateDropdown extends Component {
|
2022-02-17 20:17:59 +00:00
|
|
|
currentDate = new Date();
|
|
|
|
currentYear = this.currentDate.getFullYear(); // integer of year
|
|
|
|
currentMonth = this.currentDate.getMonth(); // index of month
|
|
|
|
|
|
|
|
@tracked allowedMonthMax = 12;
|
|
|
|
@tracked disabledYear = null;
|
2022-02-10 20:51:50 +00:00
|
|
|
@tracked startMonth = null;
|
|
|
|
@tracked startYear = null;
|
|
|
|
|
|
|
|
months = Array.from({ length: 12 }, (item, i) => {
|
|
|
|
return new Date(0, i).toLocaleString('en-US', { month: 'long' });
|
|
|
|
});
|
|
|
|
years = Array.from({ length: 5 }, (item, i) => {
|
|
|
|
return new Date().getFullYear() - i;
|
|
|
|
});
|
|
|
|
|
|
|
|
@action
|
2022-02-17 20:17:59 +00:00
|
|
|
selectStartMonth(month, event) {
|
2022-02-10 20:51:50 +00:00
|
|
|
this.startMonth = month;
|
2022-02-17 20:17:59 +00:00
|
|
|
// disables months if in the future
|
|
|
|
this.disabledYear = this.months.indexOf(month) >= this.currentMonth ? this.currentYear : null;
|
|
|
|
event.close();
|
2022-02-10 20:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-02-17 20:17:59 +00:00
|
|
|
selectStartYear(year, event) {
|
2022-02-10 20:51:50 +00:00
|
|
|
this.startYear = year;
|
2022-02-17 20:17:59 +00:00
|
|
|
this.allowedMonthMax = year === this.currentYear ? this.currentMonth : 12;
|
|
|
|
event.close();
|
2022-02-10 20:51:50 +00:00
|
|
|
}
|
2022-02-14 18:27:09 +00:00
|
|
|
|
|
|
|
@action
|
|
|
|
saveDateSelection() {
|
|
|
|
this.args.handleDateSelection(this.startMonth, this.startYear, this.args.name);
|
|
|
|
}
|
2022-02-10 20:51:50 +00:00
|
|
|
}
|