Simon Egersand
2018-02-07 63e0b0b2fc6f361b5ac4751eb05bc5983aea5873
commit | author | age
752fa8 1 // Type definitions for react-datetime
KJ 2 // Project: https://github.com/YouCanBookMe/react-datetime
3 // Definitions by: Ivan Verevkin <vereva@x-root.org>
4 //     Updates by: Aaron Spaulding <aaron@sachimp.com>,
5 //                 Karol Janyst <http://github.com/LKay>
6
7 import { Component, ChangeEvent, FocusEvent, FocusEventHandler } from "react";
8 import { Moment } from "moment";
9
10 export = ReactDatetimeClass;
11
12 declare namespace ReactDatetimeClass {
13     /*
14      The view mode can be any of the following strings.
15      */
16     export type ViewMode = "years" | "months" | "days" | "time";
17
18     export interface TimeConstraint {
19         min: number;
20         max: number;
21         step: number;
22     }
23
24     export interface TimeConstraints {
25         hours?: TimeConstraint;
26         minutes?: TimeConstraint;
27         seconds?: TimeConstraint;
28         milliseconds?: TimeConstraint;
29     }
30
31     type EventOrValueHandler<Event> = (event: Event | Moment | string) => void;
32
33     export interface DatetimepickerProps {
34         /*
35          Represents the selected date by the component, in order to use it as a controlled component.
36          This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date.
37          */
3551b7 38         value?: Date | string | Moment;
752fa8 39         /*
KJ 40          Represents the selected date for the component to use it as a uncontrolled component.
41          This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date.
42          */
3551b7 43         defaultValue?: Date | string | Moment;
752fa8 44         /*
KJ 45          Defines the format for the date. It accepts any moment.js date format.
46          If true the date will be displayed using the defaults for the current locale.
47          If false the datepicker is disabled and the component can be used as timepicker.
48          */
3551b7 49         dateFormat?: boolean | string;
752fa8 50         /*
KJ 51          Defines the format for the time. It accepts any moment.js time format.
52          If true the time will be displayed using the defaults for the current locale.
53          If false the timepicker is disabled and the component can be used as datepicker.
54          */
3551b7 55         timeFormat?: boolean | string;
752fa8 56         /*
KJ 57          Whether to show an input field to edit the date manually.
58          */
59         input?: boolean;
60         /*
61          Whether to open or close the picker. If not set react-datetime will open the
62          datepicker on input focus and close it on click outside.
63          */
64         open?: boolean;
65         /*
66          Manually set the locale for the react-datetime instance.
67          Moment.js locale needs to be loaded to be used, see i18n docs.
68          */
69         locale?: string;
70         /*
71          Whether to interpret input times as UTC or the user's local timezone.
72          */
73         utc?: boolean;
74         /*
75          Callback trigger when the date changes. The callback receives the selected `moment` object as
76          only parameter, if the date in the input is valid. If the date in the input is not valid, the
77          callback receives the value of the input (a string).
78          */
79         onChange?: EventOrValueHandler<ChangeEvent<any>>;
80         /*
81          Callback trigger for when the user opens the datepicker.
82          */
83         onFocus?: FocusEventHandler<any>;
84         /*
85          Callback trigger for when the user clicks outside of the input, simulating a regular onBlur.
86          The callback receives the selected `moment` object as only parameter, if the date in the input
87          is valid. If the date in the input is not valid, the callback receives the value of the
88          input (a string).
89          */
90         onBlur?: EventOrValueHandler<FocusEvent<any>>;
91         /*
b8d07e 92          Callback trigger when the view mode changes. The callback receives the selected view mode
SE 93          string ('years', 'months', 'days', 'time') as only parameter.
94          */
95         onViewModeChange?: (viewMode: string) => void;
96         /*
752fa8 97          The default view to display when the picker is shown. ('years', 'months', 'days', 'time')
KJ 98          */
3551b7 99         viewMode?: ViewMode | number;
752fa8 100         /*
KJ 101          Extra class names for the component markup.
102          */
103         className?: string;
104         /*
105          Defines additional attributes for the input element of the component.
106          */
107         inputProps?: React.HTMLProps<HTMLInputElement>;
108         /*
109          Define the dates that can be selected. The function receives (currentDate, selectedDate)
110          and should return a true or false whether the currentDate is valid or not. See selectable dates.
111          */
112         isValidDate?: (currentDate: any, selectedDate: any) => boolean;
113         /*
114          Customize the way that the days are shown in the day picker. The accepted function has
115          the selectedDate, the current date and the default calculated props for the cell,
116          and must return a React component. See appearance customization
117          */
118         renderDay?: (props: any, currentDate: any, selectedDate: any) => JSX.Element;
119         /*
120          Customize the way that the months are shown in the month picker.
121          The accepted function has the selectedDate, the current date and the default calculated
122          props for the cell, the month and the year to be shown, and must return a
123          React component. See appearance customization
124          */
125         renderMonth?: (props: any, month: number, year: number, selectedDate: any) => JSX.Element;
126         /*
127          Customize the way that the years are shown in the year picker.
128          The accepted function has the selectedDate, the current date and the default calculated
129          props for the cell, the year to be shown, and must return a React component.
130          See appearance customization
131          */
132         renderYear?: (props: any, year: number, selectedDate: any) => JSX.Element;
133         /*
134          Whether to use moment's strict parsing when parsing input.
135          */
136         strictParsing?: boolean;
137         /*
138          When true, once the day has been selected, the react-datetime will be automatically closed.
139          */
140         closeOnSelect?: boolean;
141         /*
142          Allow to add some constraints to the time selector. It accepts an object with the format
143          {hours:{ min: 9, max: 15, step:2}} so the hours can't be lower than 9 or higher than 15, and
144          it will change adding or subtracting 2 hours everytime the buttons are clicked. The constraints
145          can be added to the hours, minutes, seconds and milliseconds.
146          */
147         timeConstraints?: TimeConstraints;
148         /*
149          When true, keep the picker open when click event is triggered outside of component. When false,
150          close it.
151          */
152         disableOnClickOutside?: boolean;
153     }
154
155     export interface DatetimepickerState {
156         updateOn: string;
157         inputFormat: string;
158         viewDate: Moment;
159         selectedDate: Moment;
160         inputValue: string;
161         open: boolean;
162     }
163 }
164
165 declare class ReactDatetimeClass extends Component<ReactDatetimeClass.DatetimepickerProps, ReactDatetimeClass.DatetimepickerState> {}