Skip to content

Commit a2dc3a3

Browse files
committed
feat(time-slice): allow always absolute formatting via null formatInput prop
1 parent 9de1fe4 commit a2dc3a3

2 files changed

Lines changed: 73 additions & 9 deletions

File tree

src/timeslice/time-slice.stories.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,54 @@ export const Basic = () => {
5858
)
5959
}
6060

61+
export const Absolute = () => {
62+
const [dateRange, setDateRange] = React.useState<TimeSliceProps['dateRange']>(
63+
{
64+
startDate: undefined,
65+
endDate: undefined
66+
}
67+
)
68+
69+
const onDateRangeChange = (dateRange: TimeSliceProps['dateRange']) => {
70+
setDateRange(dateRange)
71+
}
72+
73+
return (
74+
<>
75+
<TimeSlice.Root onDateRangeChange={onDateRangeChange} formatInput={null}>
76+
<TimeSlice.Input style={{ border: '1px solid black', width: '100%' }} />
77+
<TimeSlice.Portal
78+
style={{ border: '1px solid black', backgroundColor: 'white' }}
79+
>
80+
<TimeSlice.Shortcut duration={{ minutes: 15 }} asChild>
81+
<div className="focus:bg-gray-100">15 minutes</div>
82+
</TimeSlice.Shortcut>
83+
<TimeSlice.Shortcut
84+
className="focus:bg-gray-100"
85+
duration={{ hours: 1 }}
86+
>
87+
<div>1 hour</div>
88+
</TimeSlice.Shortcut>
89+
<TimeSlice.Shortcut
90+
className="focus:bg-gray-100"
91+
duration={{ days: 1 }}
92+
>
93+
<div>1 day</div>
94+
</TimeSlice.Shortcut>
95+
<TimeSlice.Shortcut
96+
className="focus:bg-gray-100"
97+
duration={{ years: 1 }}
98+
>
99+
<div>1 year</div>
100+
</TimeSlice.Shortcut>
101+
</TimeSlice.Portal>
102+
</TimeSlice.Root>
103+
104+
<pre>{JSON.stringify(dateRange, null, 2)}</pre>
105+
</>
106+
)
107+
}
108+
61109
export const DataDog = () => {
62110
const [dateRange, setDateRange] = React.useState<TimeSliceProps['dateRange']>(
63111
{

src/timeslice/time-slice.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ type TimeSliceProps = ScopedProps<{
5151
defaultOpen?: boolean
5252
onOpenChange?: (open: boolean) => void
5353
setOpen?: (open: boolean) => void
54-
formatInput?: (args: {
55-
startDate?: Date
56-
endDate?: Date
57-
isRelative: boolean
58-
}) => string
54+
formatInput?:
55+
| ((args: {
56+
startDate?: Date
57+
endDate?: Date
58+
isRelative: boolean
59+
}) => string)
60+
| null
5961
dateRange?: DateRange
6062
defaultDateRange?: DateRange
6163
onDateRangeChange?: (range: DateRange) => void
@@ -129,10 +131,24 @@ const TimeSlice: React.FC<TimeSliceProps> = ({
129131
[timeZone]
130132
)
131133

132-
const formatInputValue = useMemo(
133-
() => formatInputProp || defaultFormatInput,
134-
[formatInputProp, defaultFormatInput]
135-
)
134+
const formatInputValue = useMemo(() => {
135+
if (formatInputProp) {
136+
return formatInputProp
137+
}
138+
139+
if (formatInputProp === null) {
140+
return (args: {
141+
startDate?: Date
142+
endDate?: Date
143+
isRelative: boolean
144+
}) => {
145+
if (!args.startDate || !args.endDate) return ''
146+
return buildSegments(args.startDate, args.endDate, timeZone).text
147+
}
148+
}
149+
150+
return defaultFormatInput
151+
}, [formatInputProp, defaultFormatInput])
136152

137153
const focusPortal = useCallback(() => {
138154
if (portalContentRef.current) {

0 commit comments

Comments
 (0)