Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/contexts/Navigation/utils/getPreviousMonth.ts 1.13 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  import { addMonths, differenceInCalendarMonths, startOfMonth } from 'date-fns';
  
  /**
   * Returns the next previous the user can navigate to, according to the given
   * options.
   *
   * Please note that the previous month is not always the previous calendar
   * month:
   *
   * - if before the `fromDate` date, is `undefined`;
   * - if the navigation is paged, is the number of months displayed before.
   *
   */
  export function getPreviousMonth(
    startingMonth: Date,
    options: {
      numberOfMonths?: number;
      fromDate?: Date;
      toDate?: Date;
      pagedNavigation?: boolean;
      today?: Date;
      disableNavigation?: boolean;
    }
  ): Date | undefined {
    if (options.disableNavigation) {
      return undefined;
    }
    const { fromDate, pagedNavigation, numberOfMonths = 1 } = options;
    const offset = pagedNavigation ? numberOfMonths : 1;
    const month = startOfMonth(startingMonth);
    if (!fromDate) {
      return addMonths(month, -offset);
    }
    const monthsDiff = differenceInCalendarMonths(month, fromDate);
  
    if (monthsDiff <= 0) {
      return undefined;
    }
  
    // Jump back as the number of months when paged navigation
    return addMonths(month, -offset);
  }