|
1
|
"use client" |
|
2
|
|
|
3
|
import * as React from "react" |
|
4
|
import { useDateSegment } from "react-aria" |
|
5
|
import { DateFieldState, DateSegment as Segment } from "react-stately" |
|
6
|
|
|
7
|
import { clx } from "@/utils/clx" |
|
8
|
|
|
9
|
interface DateSegmentProps extends React.ComponentPropsWithoutRef<"div"> { |
|
10
|
segment: Segment |
|
11
|
state: DateFieldState |
|
12
|
} |
|
13
|
|
|
14
|
const DateSegment = ({ segment, state }: DateSegmentProps) => { |
|
15
|
const ref = React.useRef<HTMLDivElement>(null) |
|
16
|
const { segmentProps } = useDateSegment(segment, state, ref) |
|
17
|
|
|
18
|
const isSpacingLiteral = |
|
19
|
segment.type === "literal" && |
|
20
|
(segment.text === ", " || segment.text.trim() === "") |
|
21
|
|
|
22
|
/** |
|
23
|
* We render an empty span with a margin to maintain the correct spacing |
|
24
|
* between date and time segments. The literal between them depends on the |
|
25
|
* locale: ", " in en-US, but a lone " " in fr-FR and many others. A text |
|
26
|
* node made only of whitespace collapses to nothing at a line edge, so |
|
27
|
* both cases take the span. |
|
28
|
*/ |
|
29
|
if (isSpacingLiteral) { |
|
30
|
return <span className="mx-1" /> |
|
31
|
} |
|
32
|
|
|
33
|
return ( |
|
34
|
/** |
|
35
|
* We wrap the segment in a span to prevent the segment from being |
|
36
|
* focused when the user clicks outside of the component. |
|
37
|
* |
|
38
|
* See: https://github.com/adobe/react-spectrum/issues/3164 |
|
39
|
*/ |
|
40
|
<span> |
|
41
|
<div |
|
42
|
ref={ref} |
|
43
|
className={clx( |
|
44
|
"transition-fg outline-none", |
|
45
|
"focus-visible:bg-ui-bg-interactive focus-visible:text-ui-fg-on-color", |
|
46
|
{ |
|
47
|
"text-ui-fg-muted uppercase": segment.isPlaceholder, |
|
48
|
"text-ui-fg-muted": !segment.isEditable && !state.value, |
|
49
|
} |
|
50
|
)} |
|
51
|
{...segmentProps} |
|
52
|
> |
|
53
|
{segment.text} |
|
54
|
</div> |
|
55
|
</span> |
|
56
|
) |
|
57
|
} |
|
58
|
|
|
59
|
export { DateSegment } |