Forráskód Böngészése

🎨 chore(helper/config/range): 封装到类

晓晓晓晓丶vv 4 éve
szülő
commit
a5e873303a
2 módosított fájl, 35 hozzáadás és 18 törlés
  1. 35 2
      src/helper/config/range.ts
  2. 0 16
      src/helper/index.ts

+ 35 - 2
src/helper/config/range.ts

@@ -1,6 +1,6 @@
 import moment from "moment";
 
-export const RangePicker = {
+export const picker = {
   today: [moment(), moment()],
   week: [moment().subtract(7, "days"), moment()],
   month: [moment().subtract(30, "days"), moment()],
@@ -12,9 +12,42 @@ export const RangePicker = {
   ],
 };
 
-export const RangePickerText = {
+export const pickerText = {
   today: "今天",
   week: "一周前",
   month: "一月前",
   three_month: "3个月前",
 };
+
+/**
+ * 日期picker range可自定义添加
+ * @methods add("本周", [moment(), moment()])
+ * @methods pick(["today", "week"])
+ */
+class RangePicker {
+  private config: Record<string, moment.Moment[]>;
+
+  constructor() {
+    this.config = Object.create({});
+  }
+
+  add(text: string, range: moment.Moment[]) {
+    this.config[text] = range;
+    return this;
+  }
+
+  pick(range: string[]) {
+    range.forEach((r: string) => {
+      const text = (<Record<string, string>>pickerText)[r];
+      this.config[text] = (picker as Record<string, moment.Moment[]>)[r];
+    });
+
+    return this;
+  }
+
+  getRange() {
+    return this.config;
+  }
+}
+
+export default new RangePicker();

+ 0 - 16
src/helper/index.ts

@@ -1,5 +1,4 @@
 const path = require("path");
-import { RangePicker, RangePickerText } from "./config/range";
 
 /**
  * json字符串格式化
@@ -75,18 +74,3 @@ export const formatSelectOptions = <T>(
     };
   });
 };
-
-/**
- * 获取需要筛选的时间区间
- * @param range eg: ["today", "month", "week", "three_month"]
- */
-export const rangePicker = (range: string[]) => {
-  let rangeConfig: Record<string, moment.Moment[]> = Object.create({});
-
-  range.forEach((r: string) => {
-    const text = (<Record<string, string>>RangePickerText)[r];
-    rangeConfig[text] = (RangePicker as Record<string, moment.Moment[]>)[r];
-  });
-
-  return rangeConfig;
-};