@@ -6,29 +6,34 @@ import { findNextRangeIndex, generateStyles } from '../../utils';
66import { isBefore , differenceInCalendarDays , addDays , min , isWithinInterval , max } from 'date-fns' ;
77import classnames from 'classnames' ;
88import coreStyles from '../../styles' ;
9- import { Styles } from '../DayCell/types' ;
109import { DisplayMode , DateRange as DateRangeObject } from '../../utilsTypes' ;
10+ import { Preview , Styles } from '../DayCell/types' ;
11+ import { InputRange } from '../../defaultRangesTypes' ;
1112
1213
13- interface DateRangeProps extends CalendarProps {
14+ export interface DateRangeProps extends Omit < CalendarProps , 'onChange' > {
1415 focusedRange : number [ ]
1516 initialFocusedRange : number [ ]
1617 moveRangeOnFirstSelection : boolean
1718 retainEndDateOnFirstSelection : boolean
19+ className : string
20+ onChange : ( ranges : [ { [ key : string ] : DateRangeObject } ] ) => void
21+ ranges : DateRangeObject [ ]
1822}
1923
2024interface DateRangeState {
21- preview : null
25+ preview : Preview | null
2226 focusedRange : number [ ]
2327}
2428
2529class DateRange extends Component < DateRangeProps , DateRangeState > {
26- public static propTypes = { } ;
30+ static propTypes = { } ;
2731 static defaultProps = { }
2832 styles : Styles
2933 calendar : Calendar | null = null
3034
31- constructor ( props : DateRangeProps , context ) {
35+ // FIXME: context
36+ constructor ( props : DateRangeProps , context : any ) {
3237 super ( props , context ) ;
3338 this . state = {
3439 focusedRange : props . initialFocusedRange || [ findNextRangeIndex ( props . ranges ) , 0 ] ,
@@ -37,7 +42,7 @@ class DateRange extends Component<DateRangeProps, DateRangeState> {
3742 this . styles = generateStyles ( [ coreStyles , props . classNames ] ) ;
3843 }
3944
40- calcNewSelection = ( value : Date | DateRangeObject , isSingleValue = true ) => {
45+ calcNewSelection = ( value : Date | DateRangeObject , isSingleValue = true ) : Preview | undefined => {
4146 const focusedRange = this . props . focusedRange || this . state . focusedRange ;
4247 const {
4348 ranges,
@@ -49,34 +54,35 @@ class DateRange extends Component<DateRangeProps, DateRangeState> {
4954 } = this . props ;
5055 const focusedRangeIndex = focusedRange [ 0 ] ;
5156 const selectedRange = ranges [ focusedRangeIndex ] ;
52- if ( ! selectedRange || ! onChange ) return { } ;
57+ if ( ! selectedRange || ! onChange ) return undefined
5358 let { startDate, endDate } = selectedRange ;
5459 const now = new Date ( ) ;
5560 let nextFocusRange ;
61+ // FIXME: get rid of isSingleValue
5662 if ( ! isSingleValue ) {
57- startDate = value . startDate ;
58- endDate = value . endDate ;
63+ startDate = ( value as DateRangeObject ) . startDate ;
64+ endDate = ( value as DateRangeObject ) . endDate ;
5965 } else if ( focusedRange [ 1 ] === 0 ) {
6066 // startDate selection
6167 const dayOffset = differenceInCalendarDays ( endDate || now , startDate ) ;
6268 const calculateEndDate = ( ) => {
6369 if ( moveRangeOnFirstSelection ) {
64- return addDays ( value , dayOffset ) ;
70+ return addDays ( value as Date , dayOffset ) ;
6571 }
6672 if ( retainEndDateOnFirstSelection ) {
67- if ( ! endDate || isBefore ( value , endDate ) ) {
73+ if ( ! endDate || isBefore ( value as Date , endDate ) ) {
6874 return endDate ;
6975 }
7076 return value ;
7177 }
7278 return value || now ;
7379 } ;
74- startDate = value ;
75- endDate = calculateEndDate ( ) ;
80+ startDate = value as Date ;
81+ endDate = calculateEndDate ( ) as Date ;
7682 if ( maxDate ) endDate = min ( [ endDate , maxDate ] ) ;
7783 nextFocusRange = [ focusedRange [ 0 ] , 1 ] ;
7884 } else {
79- endDate = value ;
85+ endDate = value as Date ;
8086 }
8187
8288 // reverse dates if startDate before endDate
@@ -106,13 +112,11 @@ class DateRange extends Component<DateRangeProps, DateRangeState> {
106112 nextFocusRange = [ nextFocusRangeIndex , 0 ] ;
107113 }
108114 return {
109- wasValid : ! ( inValidDatesWithinRange . length > 0 ) ,
110115 range : { startDate, endDate } ,
111- nextFocusRange : nextFocusRange ,
112- } ;
116+ } as Preview ;
113117 } ;
114118
115- setSelection = ( value : Date | DateRange , isSingleValue : boolean ) => {
119+ setSelection = ( value : Date | DateRangeObject , isSingleValue : boolean ) => {
116120 const { onChange, ranges, onRangeFocusChange } = this . props ;
117121 const focusedRange = this . props . focusedRange || this . state . focusedRange ;
118122 const focusedRangeIndex = focusedRange [ 0 ] ;
@@ -135,30 +139,32 @@ class DateRange extends Component<DateRangeProps, DateRangeState> {
135139 this . setState ( { focusedRange } ) ;
136140 this . props . onRangeFocusChange && this . props . onRangeFocusChange ( focusedRange ) ;
137141 } ;
138- updatePreview = ( val ) => {
142+
143+ updatePreview = ( val ?: Preview ) => {
139144 if ( ! val ) {
140145 this . setState ( { preview : null } ) ;
141146 return ;
142147 }
143148 const { rangeColors, ranges } = this . props ;
144149 const focusedRange = this . props . focusedRange || this . state . focusedRange ;
145150 const color = ranges [ focusedRange [ 0 ] ] ?. color || rangeColors [ focusedRange [ 0 ] ] || this . props . color ;
146- this . setState ( { preview : { ...val . range , color } } ) ;
151+ this . setState ( { preview : { ...val . range , color } as Preview } ) ;
147152 } ;
153+
148154 render ( ) {
149155 return (
150156 < Calendar
157+ { ...this . props }
151158 focusedRange = { this . state . focusedRange }
152159 onRangeFocusChange = { this . handleRangeFocusChange }
153160 preview = { this . state . preview }
154161 onPreviewChange = { value => {
155- this . updatePreview ( value ? this . calcNewSelection ( value ) : null ) ;
162+ this . updatePreview ( value ? this . calcNewSelection ( value ) : undefined ) ;
156163 } }
157- { ...this . props }
158164 displayMode = { DisplayMode . DATE_RANGE }
159- className = { classnames ( this . styles . dateRangeWrapper , this . props . className ) }
160- onChange = { this . setSelection }
161- updateRange = { val => this . setSelection ( val , false ) }
165+ classNames = { classnames ( this . styles . dateRangeWrapper , this . props . className ) }
166+ onChange = { ( val ) => this . setSelection ( val as Date , true ) }
167+ updateRange = { ( val ) => this . setSelection ( val as DateRangeObject , false ) }
162168 ref = { target => {
163169 this . calendar = target ;
164170 } }
0 commit comments