source: src/components/custom-date-range-picker/use-date-range-picker.ts@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import { useState, useCallback } from 'react';
2// utils
3import { fDate } from 'src/utils/format-time';
4//
5import { shortDateLabel } from './utils';
6import { DateRangePickerProps } from './types';
7
8// ----------------------------------------------------------------------
9
10type ReturnType = DateRangePickerProps;
11
12export default function useDateRangePicker(start: Date | null, end: Date | null): ReturnType {
13 const [open, setOpen] = useState(false);
14
15 const [endDate, setEndDate] = useState(end);
16
17 const [startDate, setStartDate] = useState(start);
18
19 const error = start && end ? new Date(start).getTime() > new Date(end).getTime() : false;
20
21 const onOpen = useCallback(() => {
22 setOpen(true);
23 }, []);
24
25 const onClose = useCallback(() => {
26 setOpen(false);
27 }, []);
28
29 const onChangeStartDate = useCallback((newValue: Date | null) => {
30 setStartDate(newValue);
31 }, []);
32
33 const onChangeEndDate = useCallback(
34 (newValue: Date | null) => {
35 if (error) {
36 setEndDate(null);
37 }
38 setEndDate(newValue);
39 },
40 [error]
41 );
42
43 const onReset = useCallback(() => {
44 setStartDate(null);
45 setEndDate(null);
46 }, []);
47
48 return {
49 startDate,
50 endDate,
51 onChangeStartDate,
52 onChangeEndDate,
53 //
54 open,
55 onOpen,
56 onClose,
57 onReset,
58 //
59 selected: !!startDate && !!endDate,
60 error,
61 //
62 label: `${fDate(startDate)} - ${fDate(endDate)}`,
63 shortLabel: shortDateLabel(startDate, endDate),
64 //
65 setStartDate,
66 setEndDate,
67 };
68}
Note: See TracBrowser for help on using the repository browser.