range.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import moment from "moment";
  2. export const picker = {
  3. 本日: [moment(), moment()],
  4. 近7日: [moment().subtract(7, "d"), moment()],
  5. 近14日: [moment().subtract(14, "d"), moment()],
  6. 近30日: [moment().subtract(30, "d"), moment()],
  7. 上季度: [
  8. moment()
  9. .subtract(3, "month")
  10. .startOf("month"),
  11. moment().startOf("month"),
  12. ],
  13. };
  14. export const pickerText = {
  15. today: "今天",
  16. week: "一周前",
  17. month: "一月前",
  18. three_month: "3个月前",
  19. };
  20. /**
  21. * 日期picker range可自定义添加
  22. * @methods add("本周", [moment(), moment()])
  23. * @methods pick(["today", "week"])
  24. */
  25. class RangePicker implements RangePickerClass {
  26. private config: Record<string, moment.Moment[]>;
  27. constructor() {
  28. this.config = Object.create({});
  29. }
  30. add(text: string, range: moment.Moment[]) {
  31. this.config[text] = range;
  32. return this;
  33. }
  34. pick(range: string[]) {
  35. range.forEach((r: string) => {
  36. // const text = (<Record<string, string>>pickerText)[r];
  37. this.config[r] = (picker as Record<string, moment.Moment[]>)[r];
  38. });
  39. return this;
  40. }
  41. getRange() {
  42. return this.config;
  43. }
  44. }
  45. export default new RangePicker();