|
1
|
"use client" |
|
2
|
|
|
3
|
import { Eye, EyeSlash, MagnifyingGlassMini } from "@medusajs/icons" |
|
4
|
import { VariantProps, cva } from "cva" |
|
5
|
import * as React from "react" |
|
6
|
|
|
7
|
import { clx } from "@/utils/clx" |
|
8
|
|
|
9
|
const inputBaseStyles = clx( |
|
10
|
"caret-ui-fg-base bg-ui-bg-field hover:bg-ui-bg-field-hover shadow-borders-base placeholder-ui-fg-muted text-ui-fg-base transition-fg relative w-full appearance-none rounded-md outline-none", |
|
11
|
"focus-visible:shadow-borders-interactive-with-active", |
|
12
|
"disabled:text-ui-fg-disabled disabled:!bg-ui-bg-disabled disabled:placeholder-ui-fg-disabled disabled:cursor-not-allowed", |
|
13
|
"aria-[invalid=true]:!shadow-borders-error invalid:!shadow-borders-error" |
|
14
|
) |
|
15
|
|
|
16
|
const inputVariants = cva({ |
|
17
|
base: clx( |
|
18
|
inputBaseStyles, |
|
19
|
"[&::--webkit-search-cancel-button]:hidden [&::-webkit-search-cancel-button]:hidden [&::-webkit-search-decoration]:hidden" |
|
20
|
), |
|
21
|
variants: { |
|
22
|
size: { |
|
23
|
base: "txt-compact-small h-8 px-2 py-1.5", |
|
24
|
small: "txt-compact-small h-7 px-2 py-1", |
|
25
|
}, |
|
26
|
}, |
|
27
|
defaultVariants: { |
|
28
|
size: "base", |
|
29
|
}, |
|
30
|
}) |
|
31
|
|
|
32
|
interface InputProps |
|
33
|
extends VariantProps<typeof inputVariants>, |
|
34
|
Omit<React.InputHTMLAttributes<HTMLInputElement>, "size"> {} |
|
35
|
|
|
36
|
/** |
|
37
|
* This component is based on the `input` element and supports all of its props |
|
38
|
*/ |
|
39
|
const Input = React.forwardRef<HTMLInputElement, InputProps>( |
|
40
|
( |
|
41
|
{ |
|
42
|
className, |
|
43
|
type, |
|
44
|
/** |
|
45
|
* The input's size. |
|
46
|
*/ |
|
47
|
size = "base", |
|
48
|
...props |
|
49
|
}: InputProps, |
|
50
|
ref |
|
51
|
) => { |
|
52
|
const [typeState, setTypeState] = React.useState(type) |
|
53
|
|
|
54
|
const isPassword = type === "password" |
|
55
|
const isSearch = type === "search" |
|
56
|
|
|
57
|
return ( |
|
58
|
<div className="relative"> |
|
59
|
<input |
|
60
|
ref={ref} |
|
61
|
type={isPassword ? typeState : type} |
|
62
|
className={clx( |
|
63
|
inputVariants({ size: size }), |
|
64
|
{ |
|
65
|
"pl-8": isSearch && size === "base", |
|
66
|
"pr-8": isPassword && size === "base", |
|
67
|
"pl-7": isSearch && size === "small", |
|
68
|
"pr-7": isPassword && size === "small", |
|
69
|
}, |
|
70
|
className |
|
71
|
)} |
|
72
|
{...props} |
|
73
|
/> |
|
74
|
{isSearch && ( |
|
75
|
<div |
|
76
|
className={clx( |
|
77
|
"text-ui-fg-muted pointer-events-none absolute bottom-0 left-0 flex items-center justify-center", |
|
78
|
{ |
|
79
|
"h-8 w-8": size === "base", |
|
80
|
"h-7 w-7": size === "small", |
|
81
|
} |
|
82
|
)} |
|
83
|
role="img" |
|
84
|
> |
|
85
|
<MagnifyingGlassMini /> |
|
86
|
</div> |
|
87
|
)} |
|
88
|
{isPassword && ( |
|
89
|
<div |
|
90
|
className={clx( |
|
91
|
"absolute bottom-0 right-0 flex items-center justify-center border-l", |
|
92
|
{ |
|
93
|
"h-8 w-8": size === "base", |
|
94
|
"h-7 w-7": size === "small", |
|
95
|
} |
|
96
|
)} |
|
97
|
> |
|
98
|
<button |
|
99
|
className="text-ui-fg-muted hover:text-ui-fg-base focus-visible:text-ui-fg-base focus-visible:shadow-borders-interactive-w-focus active:text-ui-fg-base h-fit w-fit rounded-sm outline-none transition-all" |
|
100
|
type="button" |
|
101
|
onClick={() => { |
|
102
|
setTypeState(typeState === "password" ? "text" : "password") |
|
103
|
}} |
|
104
|
> |
|
105
|
<span className="sr-only"> |
|
106
|
{typeState === "password" ? "Show password" : "Hide password"} |
|
107
|
</span> |
|
108
|
{typeState === "password" ? <Eye /> : <EyeSlash />} |
|
109
|
</button> |
|
110
|
</div> |
|
111
|
)} |
|
112
|
</div> |
|
113
|
) |
|
114
|
} |
|
115
|
) |
|
116
|
Input.displayName = "Input" |
|
117
|
|
|
118
|
export { Input, inputBaseStyles } |