Simon Egersand
2017-07-27 f31f3dcb730f75328dba3c1ff8769deb6598809c
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          */
38         value?: Date;
39         /*
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          */
43         defaultValue?: Date;
44         /*
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          */
49         dateFormat?: boolean|string;
50         /*
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          */
55         timeFormat?: boolean|string;
56         /*
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         /*
92          The default view to display when the picker is shown. ('years', 'months', 'days', 'time')
93          */
94         viewMode?: ViewMode|number;
95         /*
96          Extra class names for the component markup.
97          */
98         className?: string;
99         /*
100          Defines additional attributes for the input element of the component.
101          */
102         inputProps?: React.HTMLProps<HTMLInputElement>;
103         /*
104          Define the dates that can be selected. The function receives (currentDate, selectedDate)
105          and should return a true or false whether the currentDate is valid or not. See selectable dates.
106          */
107         isValidDate?: (currentDate: any, selectedDate: any) => boolean;
108         /*
109          Customize the way that the days are shown in the day picker. The accepted function has
110          the selectedDate, the current date and the default calculated props for the cell,
111          and must return a React component. See appearance customization
112          */
113         renderDay?: (props: any, currentDate: any, selectedDate: any) => JSX.Element;
114         /*
115          Customize the way that the months are shown in the month picker.
116          The accepted function has the selectedDate, the current date and the default calculated
117          props for the cell, the month and the year to be shown, and must return a
118          React component. See appearance customization
119          */
120         renderMonth?: (props: any, month: number, year: number, selectedDate: any) => JSX.Element;
121         /*
122          Customize the way that the years are shown in the year picker.
123          The accepted function has the selectedDate, the current date and the default calculated
124          props for the cell, the year to be shown, and must return a React component.
125          See appearance customization
126          */
127         renderYear?: (props: any, year: number, selectedDate: any) => JSX.Element;
128         /*
129          Whether to use moment's strict parsing when parsing input.
130          */
131         strictParsing?: boolean;
132         /*
133          When true, once the day has been selected, the react-datetime will be automatically closed.
134          */
135         closeOnSelect?: boolean;
136         /*
137          Allow to add some constraints to the time selector. It accepts an object with the format
138          {hours:{ min: 9, max: 15, step:2}} so the hours can't be lower than 9 or higher than 15, and
139          it will change adding or subtracting 2 hours everytime the buttons are clicked. The constraints
140          can be added to the hours, minutes, seconds and milliseconds.
141          */
142         timeConstraints?: TimeConstraints;
143         /*
144          When true, keep the picker open when click event is triggered outside of component. When false,
145          close it.
146          */
147         disableOnClickOutside?: boolean;
148     }
149
150     export interface DatetimepickerState {
151         updateOn: string;
152         inputFormat: string;
153         viewDate: Moment;
154         selectedDate: Moment;
155         inputValue: string;
156         open: boolean;
157     }
158 }
159
160 declare class ReactDatetimeClass extends Component<ReactDatetimeClass.DatetimepickerProps, ReactDatetimeClass.DatetimepickerState> {}