1- import React from 'react' ;
1+ import React , { useMemo } from 'react' ;
22import {
33 _cs ,
44 randomString ,
55 isDefined ,
6+ isNotDefined ,
67} from '@togglecorp/fujs' ;
78import {
89 IoCalendarOutline ,
@@ -98,6 +99,7 @@ export interface Props<N extends NameType> extends InheritedProps {
9899 value : Value | undefined | null ;
99100 name : N ;
100101 onChange ?: ( value : Value | undefined , name : N ) => void ;
102+ placeholder ?: string ;
101103}
102104
103105function DateRangeInput < N extends NameType > ( props : Props < N > ) {
@@ -119,11 +121,12 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
119121 uiMode,
120122 inputElementRef,
121123 containerRef : containerRefFromProps ,
122- inputSectionRef,
124+ inputSectionRef : inputSectionRefFromProps ,
123125 inputClassName,
124126 onChange,
125127 name,
126128 value,
129+ placeholder,
127130 } = props ;
128131
129132 const [ tempDate , setTempDate ] = React . useState < Partial < Value > > ( {
@@ -132,9 +135,11 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
132135 } ) ;
133136 const [ calendarMonthSelectionPopupClassName ] = React . useState ( randomString ( 16 ) ) ;
134137 const createdContainerRef = React . useRef < HTMLDivElement > ( null ) ;
138+ const createdInputSectionRef = React . useRef < HTMLDivElement > ( null ) ;
135139 const popupRef = React . useRef < HTMLDivElement > ( null ) ;
136140
137141 const containerRef = containerRefFromProps ?? createdContainerRef ;
142+ const inputSectionRef = inputSectionRefFromProps ?? createdInputSectionRef ;
138143 const [
139144 showCalendar ,
140145 setShowCalendarTrue ,
@@ -171,7 +176,7 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
171176 showCalendar ,
172177 handlePopupBlur ,
173178 popupRef ,
174- containerRef ,
179+ inputSectionRef ,
175180 ) ;
176181
177182 const dateRendererParams = React . useCallback ( ( ) => ( {
@@ -276,6 +281,33 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
276281 1 ,
277282 ) ;
278283
284+ const dateInputLabel = useMemo (
285+ ( ) => {
286+ if (
287+ isNotDefined ( tempDate . startDate )
288+ && isNotDefined ( value ?. startDate )
289+ && isNotDefined ( value ?. endDate )
290+ ) {
291+ return undefined ;
292+ }
293+
294+ const startDateString = tempDate . startDate ?? value ?. startDate ;
295+ const start = isDefined ( startDateString )
296+ ? new Date ( startDateString ) . toLocaleDateString ( )
297+ : '--' ;
298+ const endDateString = value ?. endDate ;
299+ const end = isDefined ( endDateString )
300+ ? new Date ( endDateString ) . toLocaleDateString ( )
301+ : '--' ;
302+
303+ return [
304+ start ,
305+ end ,
306+ ] . join ( ' to ' ) ;
307+ } ,
308+ [ value , tempDate ] ,
309+ ) ;
310+
279311 return (
280312 < >
281313 < InputContainer
@@ -328,55 +360,30 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
328360 readOnly = { readOnly }
329361 uiMode = { uiMode }
330362 input = { (
331- < >
332- < RawInput < string >
333- name = "startDate"
334- className = { _cs (
335- styles . input ,
336- styles . startDateInput ,
337- ! ! error && styles . errored ,
338- ! ( tempDate . startDate ?? value ?. startDate ) && styles . empty ,
339- inputClassName ,
340- ) }
341- value = { tempDate . startDate ?? value ?. startDate }
342- // NOTE: Make this required to hide clear button on firefox
343- required = { ! ! ( tempDate . startDate ?? value ?. startDate ) }
344- elementRef = { inputElementRef }
345- readOnly
346- uiMode = { uiMode }
347- disabled = { disabled }
348- onFocus = { setShowCalendarTrue }
349- type = "date"
350- />
351- < div className = { styles . separator } >
352- to
353- </ div >
354- < RawInput < string >
355- name = "startDate"
356- className = { _cs (
357- styles . input ,
358- styles . endDateInput ,
359- ! ! error && styles . errored ,
360- ! value ?. endDate && styles . empty ,
361- inputClassName ,
362- ) }
363- elementRef = { inputElementRef }
364- readOnly
365- onClick = { setShowCalendarTrue }
366- // NOTE: Make this required to hide clear button on firefox
367- required = { ! ! value ?. endDate }
368- value = { value ?. endDate }
369- uiMode = { uiMode }
370- disabled = { disabled }
371- onFocus = { setShowCalendarTrue }
372- type = "date"
373- />
374- </ >
363+ < RawInput
364+ elementRef = { inputElementRef }
365+ name = "date-range"
366+ value = { dateInputLabel }
367+ readOnly
368+ uiMode = { uiMode }
369+ disabled = { disabled }
370+ onFocus = { setShowCalendarTrue }
371+ onClick = { setShowCalendarTrue }
372+ className = { _cs (
373+ styles . input ,
374+ ! ! error && styles . errored ,
375+ ! ( tempDate . startDate || value ?. startDate || value ?. endDate )
376+ && styles . empty ,
377+ inputClassName ,
378+ ) }
379+ type = "text"
380+ placeholder = { placeholder }
381+ />
375382 ) }
376383 />
377384 { ! readOnly && showCalendar && (
378385 < Popup
379- parentRef = { containerRef }
386+ parentRef = { inputSectionRef }
380387 elementRef = { popupRef }
381388 freeWidth
382389 className = { styles . calendarPopup }
0 commit comments