|
1
|
"use client" |
|
2
|
|
|
3
|
import { clx } from "@/utils/clx" |
|
4
|
import * as React from "react" |
|
5
|
|
|
6
|
const ALLOWED_KEYS = ["ArrowDown", "ArrowUp", "ArrowLeft", "ArrowRight"] |
|
7
|
|
|
8
|
export const DatePickerClearButton = React.forwardRef< |
|
9
|
HTMLButtonElement, |
|
10
|
React.ComponentPropsWithoutRef<"button"> |
|
11
|
>(({ type = "button", className, children, ...props }, ref) => { |
|
12
|
/** |
|
13
|
* Allows the button to be used with only the keyboard. |
|
14
|
* Otherwise the wrapping component will hijack the event. |
|
15
|
*/ |
|
16
|
const stopPropagation = (e: React.KeyboardEvent) => { |
|
17
|
if (!ALLOWED_KEYS.includes(e.key)) { |
|
18
|
e.stopPropagation() |
|
19
|
} |
|
20
|
} |
|
21
|
|
|
22
|
return ( |
|
23
|
<button |
|
24
|
ref={ref} |
|
25
|
type={type} |
|
26
|
className={clx( |
|
27
|
"text-ui-fg-muted transition-fg flex size-full items-center justify-center outline-none", |
|
28
|
"hover:bg-ui-button-transparent-hover", |
|
29
|
"focus-visible:bg-ui-bg-interactive focus-visible:text-ui-fg-on-color", |
|
30
|
className |
|
31
|
)} |
|
32
|
aria-label="Clear date" |
|
33
|
onKeyDown={stopPropagation} |
|
34
|
{...props} |
|
35
|
> |
|
36
|
{children} |
|
37
|
</button> |
|
38
|
) |
|
39
|
}) |
|
40
|
DatePickerClearButton.displayName = "DatePickerClearButton" |