Blame view

天文台pc/tianwentai-ui/node_modules/react-day-picker/src/contexts/Navigation/useNavigationState.ts 853 Bytes
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
  import { startOfMonth } from 'date-fns';
  
  import { useDayPicker } from 'contexts/DayPicker';
  import { useControlledValue } from 'hooks/useControlledValue';
  
  import { getInitialMonth } from './utils/getInitialMonth';
  
  export type NavigationState = [
    /** The month DayPicker is navigating at */
    month: Date,
    /** Go to the specified month. */
    goToMonth: (month: Date) => void
  ];
  
  /** Controls the navigation state. */
  export function useNavigationState(): NavigationState {
    const context = useDayPicker();
    const initialMonth = getInitialMonth(context);
    const [month, setMonth] = useControlledValue(initialMonth, context.month);
  
    const goToMonth = (date: Date) => {
      if (context.disableNavigation) return;
      const month = startOfMonth(date);
      setMonth(month);
      context.onMonthChange?.(month);
    };
  
    return [month, goToMonth];
  }