Skip to content

Commit

Permalink
daterangepicker: typings for v3.0.1 (DefinitelyTyped#25476)
Browse files Browse the repository at this point in the history
* daterangepicker: typings for v3.0.1

See release notes for v3.0.0: https://github.com/dangrossman/daterangepicker/releases/tag/v3.0.0

* fix react-bootstrap-daterangepicker

* fix linting errors

* support for .data('daterangepicker')
  • Loading branch information
thorn0 authored and sheetalkamat committed May 8, 2018
1 parent c5907dd commit 2c008db
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 179 deletions.
142 changes: 89 additions & 53 deletions types/daterangepicker/daterangepicker-tests.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,116 @@
import moment = require("moment")
import daterangepicker = require("daterangepicker");
import moment = require('moment');
import daterangepicker = require('daterangepicker');

function tests_simple() {
$('#daterange').daterangepicker();
$('input[name="daterange"]').daterangepicker({
timePicker: true,
timePickerIncrement: 30,
locale: {
format: 'MM/DD/YYYY h:mm A'
}
});
$('input[name="daterange"]')
.daterangepicker({
timePicker: true,
timePickerIncrement: 30,
locale: {
format: 'MM/DD/YYYY h:mm A'
},
maxSpan: { days: 10 },
applyButtonClasses: 'my-apply-class',
cancelButtonClasses: 'my-cancel-class',
showDropdowns: true,
maxYear: 3000,
minYear: 2000
})
.data('daterangepicker')
.remove();

$('#reportrange').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
Today: [moment(), moment()],
Yesterday: [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
'Last Month': [
moment()
.subtract(1, 'month')
.startOf('month'),
moment()
.subtract(1, 'month')
.endOf('month')
]
}
});

$('input[name="datefilter"]').on('apply.daterangepicker', function (ev, picker) {
$(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
$('input[name="datefilter"]').on('apply.daterangepicker', function(ev, picker) {
$(this).val(
`${picker.startDate.format('MM/DD/YYYY')} - ${picker.endDate.format('MM/DD/YYYY')}`
);
});


$('input[name="datefilter"]').on('cancel.daterangepicker', function (ev, picker) {
$('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) {
$(this).val('');
});

$('#demo').daterangepicker({
"startDate": "05/06/2016",
"endDate": "05/12/2016"
}, function (start: moment.Moment, end: moment.Moment, label: string) {
console.log("New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')");
});

$(function() {

function cb(start: moment.Moment, end: moment.Moment) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
cb(moment().subtract(29, 'days'), moment());
$('#demo').daterangepicker(
{
startDate: '05/06/2016',
endDate: '05/12/2016'
},
(start: moment.Moment, end: moment.Moment, label: string) => {
console.log(
"New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')"
);
}
);

$('#reportrange').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
$(() => {
function cb(start: moment.Moment, end: moment.Moment) {
$('#reportrange span').html(
`${start.format('MMMM D, YYYY')} - ${end.format('MMMM D, YYYY')}`
);
}
}, cb);
cb(moment().subtract(29, 'days'), moment());

$('#reportrange').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()]
},
showCustomRangeLabel: false
}, cb);
$('#reportrange').daterangepicker(
{
ranges: {
Today: [moment(), moment()],
Yesterday: [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [
moment()
.subtract(1, 'month')
.startOf('month'),
moment()
.subtract(1, 'month')
.endOf('month')
]
}
},
cb
);

$('#reportrange').daterangepicker(
{
ranges: {
Today: [moment(), moment()],
Yesterday: [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()]
},
showCustomRangeLabel: false
},
cb
);

$('#endDate').daterangepicker({
singleDatePicker: true,
startDate: moment()
$('#endDate').daterangepicker({
singleDatePicker: true,
startDate: moment()
});
});
});
}

declare const host: HTMLElement;
function test_from_amd() {
var picker = new daterangepicker(host);
console.log(picker.startDate.format("YYYY-MM-DD"));
const picker = new daterangepicker(host);
console.log(picker.startDate.format('YYYY-MM-DD'));
}
116 changes: 70 additions & 46 deletions types/daterangepicker/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for Date Range Picker v2.1.30
// Type definitions for Date Range Picker 3.0
// Project: http://www.daterangepicker.com/
// Definitions by: SirMartin <https://github.com/SirMartin>
// Steven Masala <https://github.com/smasala>
Expand All @@ -7,62 +7,81 @@
// TypeScript Version: 2.3

/// <reference types="jquery"/>
import moment = require("moment");
import moment = require('moment');

declare global {
interface JQuery {
daterangepicker(settings?: daterangepicker.Settings): JQuery;
daterangepicker(settings?: daterangepicker.Settings, callback?: daterangepicker.DataRangePickerCallback): JQuery;
daterangepicker: ((
options?: daterangepicker.Options,
callback?: daterangepicker.DataRangePickerCallback
) => JQuery) & { defaultOptions?: daterangepicker.Options };
data(key: 'daterangepicker'): daterangepicker | undefined;
}
}

declare const daterangepicker: daterangepicker.DateRangePicker;
declare class daterangepicker {
constructor(
element: HTMLElement,
options?: daterangepicker.Options,
callback?: daterangepicker.DataRangePickerCallback
);

declare namespace daterangepicker {
type DataRangePickerCallback = (start: moment.Moment, end: moment.Moment, label: string | null) => any;
startDate: moment.Moment;
endDate: moment.Moment;
container: JQuery;

interface DateRangePicker {
new (element: HTMLElement, settings?: daterangepicker.Settings, callback?: DataRangePickerCallback): DateRangePicker;
setStartDate(date: daterangepicker.DateOrString): void;
setEndDate(date: daterangepicker.DateOrString): void;
remove(): void;
}

startDate: moment.Moment;
endDate: moment.Moment;
container: JQuery;
declare namespace daterangepicker {
type DataRangePickerCallback = (
start: moment.Moment,
end: moment.Moment,
label: string | null
) => void;

setStartDate(date: Date | moment.Moment | string): void;
setEndDate(date: Date | moment.Moment | string): void;
remove(): void;
}
type DateOrString = string | moment.Moment | Date;

interface DatepickerEventObject extends JQueryEventObject {
date: Date;
format(format?: string): string;
}

interface Settings {
interface Options {
/**
* The start of the initially selected date range
*/
startDate?: string | moment.Moment | Date;
startDate?: DateOrString;
/**
* The end of the initially selected date range
*/
endDate?: string | moment.Moment | Date;
endDate?: DateOrString;
/**
* The earliest date a user may select
* The earliest date a user may select
*/
minDate?: string | moment.Moment | Date;
minDate?: DateOrString;
/**
* The latest date a user may select
*/
maxDate?: string | moment.Moment | Date;
maxDate?: DateOrString;
/**
* The maximum span between the selected start and end dates. Can have any property you can add to a moment object (i.e. days, months)
*/
dateLimit?: any;
maxSpan?: moment.MomentInput | moment.Duration;
/**
* Show year and month select boxes above calendars to jump to a specific month and year
*/
showDropdowns?: boolean;
/**
* The minimum year shown in the dropdowns when `showDropdowns` is set to true.
*/
minYear?: number;
/**
* The maximum year shown in the dropdowns when `showDropdowns` is set to true.
*/
maxYear?: number;
/**
* Show localized week numbers at the start of each week on the calendars
*/
Expand Down Expand Up @@ -90,27 +109,37 @@ declare namespace daterangepicker {
/**
* Set predefined date ranges the user can select from.Each key is the label for the range, and its value an array with two dates representing the bounds of the range.
*/
ranges?: any;
ranges?: { [name: string]: [DateOrString, DateOrString] };
/**
* Whether to show the 'Custom Range' label or just pre-defined ranges
*/
showCustomRangeLabel?: boolean;
/**
* (string: 'left'/'right'/'center') Whether the picker appears aligned to the left, to the right, or centered under the HTML element it's attached to
* Normally, if you use the `ranges` option to specify pre-defined date ranges, calendars
* for choosing a custom date range are not shown until the user clicks "Custom Range".
* When this option is set to true, the calendars for choosing a custom date range are always shown instead.
*/
opens?: string;
alwaysShowCalendars?: boolean;
/**
* (string: 'down' or 'up') Whether the picker appears below (default) or above the HTML element it's attached to
* Whether the picker appears aligned to the left, to the right, or centered under the HTML element it's attached to
*/
drops?: string;
opens?: 'left' | 'right' | 'center';
/**
* Whether the picker appears below (default) or above the HTML element it's attached to
*/
drops?: 'down' | 'up';
/**
* CSS class names that will be added to all buttons in the picker
*/
buttonClasses?: string[];
/**
* CSS class string that will be added to the apply button
*/
applyClass?: string;
applyButtonClasses?: string;
/**
* CSS class string that will be added to the cancel button
*/
cancelClass?: string;
* CSS class string that will be added to the cancel button
*/
cancelButtonClasses?: string;
/**
* Allows you to provide localized strings for buttons and labels, customize the date display format, and change the first day of week for the calendars.
*/
Expand All @@ -124,33 +153,28 @@ declare namespace daterangepicker {
*/
autoApply?: boolean;
/**
* When enabled, the two calendars displayed will always be for two sequential months (i.e.January and February), and both will be advanced when clicking the left or right arrows above the calendars.When disabled, the two calendars can be individually advanced and display any month/ year.
* When enabled, the two calendars displayed will always be for two sequential months (i.e.
* January and February), and both will be advanced when clicking the left or right arrows
* above the calendars.When disabled, the two calendars can be individually advanced and
* display any month/ year.
*/
linkedCalendars?: boolean;
/**
* jQuery selector of the parent element that the date range picker will be added to, if not provided this will be 'body'
*/
parentEl?: string;
/**
* A function that is passed each date in the two calendars before they are displayed, and may return true or false to indicate whether that date should be available for selection or not.
*/
isInvalidDate?(startDate: string | moment.Moment | Date, endDate?: string | moment.Moment | Date): boolean;
isInvalidDate?(startDate: DateOrString, endDate?: DateOrString): boolean;
/**
* A function that is passed each date in the two calendars before they are displayed, and may return a string or array of CSS class names to apply to that date's calendar cell.
*/
isCustomDate?(date: string | moment.Moment | Date): string | string[] | undefined;
isCustomDate?(date: DateOrString): string | string[] | undefined;
/**
* Indicates whether the date range picker should automatically update the value of an < input > element it's attached to at initialization and when the selected dates change.
*/
autoUpdateInput?: boolean;
/**
* Normally, if you use the ranges option to specify pre- defined date ranges, calendars for choosing a custom date range are not shown until the user clicks "Custom Range".When this option is set to true, the calendars for choosing a custom date range are always shown instead.
*/
alwaysShowCalendars?: boolean;
/**
* Whether to show the 'Custom Range' label or just pre-defined ranges
*/
showCustomRangeLabel?: boolean;
* jQuery selector of the parent element that the date range picker will be added to, if not provided this will be 'body'
*/
parentEl?: string;
}

interface Locale {
Expand Down
Loading

0 comments on commit 2c008db

Please sign in to comment.