Skip to content

Commit 0609d4d

Browse files
committed
Improve styling for DateRangeInput
1 parent fefa9bb commit 0609d4d

2 files changed

Lines changed: 52 additions & 62 deletions

File tree

src/components/DateRangeInput/index.tsx

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import {
33
_cs,
44
randomString,
55
isDefined,
6+
isNotDefined,
67
} from '@togglecorp/fujs';
78
import {
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

103105
function 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 && (

src/components/DateRangeInput/styles.css

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
.input {
66
--color: var(--tui-color-text);
7+
flex-grow: 1;
8+
min-width: unset;
79
color: var(--color);
810

911
&.empty {
@@ -19,23 +21,6 @@
1921
--color: var(--tui-color-negative-on-dark);
2022
}
2123
}
22-
23-
&::-webkit-calendar-picker-indicator {
24-
display: none;
25-
}
26-
}
27-
28-
.separator {
29-
padding: 0 var(--tui-spacing-extra-small);
30-
}
31-
32-
.start-date-input {
33-
flex-shrink: 0;
34-
width: auto;
35-
}
36-
37-
.end-date-input {
38-
flex-grow: 1;
3924
}
4025
}
4126

0 commit comments

Comments
 (0)