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 > ) {
@@ -124,6 +126,7 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
124126 onChange,
125127 name,
126128 value,
129+ placeholder,
127130 } = props ;
128131
129132 const [ tempDate , setTempDate ] = React . useState < Partial < Value > > ( {
@@ -278,6 +281,33 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
278281 1 ,
279282 ) ;
280283
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+
281311 return (
282312 < >
283313 < InputContainer
@@ -330,50 +360,25 @@ function DateRangeInput<N extends NameType>(props: Props<N>) {
330360 readOnly = { readOnly }
331361 uiMode = { uiMode }
332362 input = { (
333- < >
334- < RawInput < string >
335- name = "startDate"
336- className = { _cs (
337- styles . input ,
338- styles . startDateInput ,
339- ! ! error && styles . errored ,
340- ! ( tempDate . startDate ?? value ?. startDate ) && styles . empty ,
341- inputClassName ,
342- ) }
343- value = { tempDate . startDate ?? value ?. startDate }
344- // NOTE: Make this required to hide clear button on firefox
345- required = { ! ! ( tempDate . startDate ?? value ?. startDate ) }
346- elementRef = { inputElementRef }
347- readOnly
348- uiMode = { uiMode }
349- disabled = { disabled }
350- onFocus = { setShowCalendarTrue }
351- type = "date"
352- />
353- < div className = { styles . separator } >
354- -
355- </ div >
356- < RawInput < string >
357- name = "startDate"
358- className = { _cs (
359- styles . input ,
360- styles . endDateInput ,
361- ! ! error && styles . errored ,
362- ! value ?. endDate && styles . empty ,
363- inputClassName ,
364- ) }
365- elementRef = { inputElementRef }
366- readOnly
367- onClick = { setShowCalendarTrue }
368- // NOTE: Make this required to hide clear button on firefox
369- required = { ! ! value ?. endDate }
370- value = { value ?. endDate }
371- uiMode = { uiMode }
372- disabled = { disabled }
373- onFocus = { setShowCalendarTrue }
374- type = "date"
375- />
376- </ >
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+ />
377382 ) }
378383 />
379384 { ! readOnly && showCalendar && (
0 commit comments