|
1
|
"use client" |
|
2
|
|
|
3
|
import * as React from "react" |
|
4
|
import Primitive from "react-currency-input-field" |
|
5
|
|
|
6
|
import { Text } from "@/components/text" |
|
7
|
import { clx } from "@/utils/clx" |
|
8
|
import { VariantProps, cva } from "cva" |
|
9
|
|
|
10
|
const currencyInputVariants = cva({ |
|
11
|
base: clx( |
|
12
|
"flex items-center gap-x-1", |
|
13
|
"bg-ui-bg-field hover:bg-ui-bg-field-hover shadow-buttons-neutral placeholder-ui-fg-muted text-ui-fg-base transition-fg relative w-full rounded-md", |
|
14
|
"focus-within:shadow-borders-interactive-with-active" |
|
15
|
), |
|
16
|
variants: { |
|
17
|
size: { |
|
18
|
base: "txt-compact-medium h-8", |
|
19
|
small: "txt-compact-small h-7", |
|
20
|
}, |
|
21
|
}, |
|
22
|
defaultVariants: { |
|
23
|
size: "base", |
|
24
|
}, |
|
25
|
}) |
|
26
|
|
|
27
|
interface CurrencyInputProps |
|
28
|
extends Omit< |
|
29
|
React.ComponentPropsWithoutRef<typeof Primitive>, |
|
30
|
"prefix" | "suffix" | "size" |
|
31
|
>, |
|
32
|
VariantProps<typeof currencyInputVariants> { |
|
33
|
symbol: string |
|
34
|
code: string |
|
35
|
} |
|
36
|
|
|
37
|
/** |
|
38
|
* This component is based on the input element and supports all of its props |
|
39
|
* |
|
40
|
* @excludeExternal |
|
41
|
*/ |
|
42
|
const CurrencyInput = React.forwardRef<HTMLInputElement, CurrencyInputProps>( |
|
43
|
( |
|
44
|
{ |
|
45
|
/** |
|
46
|
* The input's size. |
|
47
|
*/ |
|
48
|
size = "base", |
|
49
|
/** |
|
50
|
* The symbol to show in the input. |
|
51
|
*/ |
|
52
|
symbol, |
|
53
|
/** |
|
54
|
* The currency code to show in the input. |
|
55
|
*/ |
|
56
|
code, |
|
57
|
/** |
|
58
|
* Whether the input is disabled. |
|
59
|
* |
|
60
|
* @keep |
|
61
|
* @defaultValue false |
|
62
|
*/ |
|
63
|
disabled, |
|
64
|
/** |
|
65
|
* A function that is triggered when the input is invalid. |
|
66
|
* |
|
67
|
* @keep |
|
68
|
*/ |
|
69
|
onInvalid, |
|
70
|
className, |
|
71
|
...props |
|
72
|
}: CurrencyInputProps, |
|
73
|
ref |
|
74
|
) => { |
|
75
|
const innerRef = React.useRef<HTMLInputElement>(null) |
|
76
|
|
|
77
|
React.useImperativeHandle<HTMLInputElement | null, HTMLInputElement | null>( |
|
78
|
ref, |
|
79
|
() => innerRef.current |
|
80
|
) |
|
81
|
|
|
82
|
const [valid, setValid] = React.useState(true) |
|
83
|
|
|
84
|
const onInnerInvalid = React.useCallback( |
|
85
|
(event: React.FormEvent<HTMLInputElement>) => { |
|
86
|
setValid(event.currentTarget.validity.valid) |
|
87
|
|
|
88
|
if (onInvalid) { |
|
89
|
onInvalid(event) |
|
90
|
} |
|
91
|
}, |
|
92
|
[onInvalid] |
|
93
|
) |
|
94
|
|
|
95
|
return ( |
|
96
|
<div |
|
97
|
onClick={() => { |
|
98
|
if (innerRef.current) { |
|
99
|
innerRef.current.focus() |
|
100
|
} |
|
101
|
}} |
|
102
|
className={clx( |
|
103
|
"w-full cursor-text justify-between overflow-hidden", |
|
104
|
currencyInputVariants({ size }), |
|
105
|
{ |
|
106
|
"text-ui-fg-disabled !bg-ui-bg-disabled !shadow-buttons-neutral !placeholder-ui-fg-disabled cursor-not-allowed": |
|
107
|
disabled, |
|
108
|
"!shadow-borders-error invalid:!shadow-borders-error": |
|
109
|
props["aria-invalid"] || !valid, |
|
110
|
}, |
|
111
|
className |
|
112
|
)} |
|
113
|
> |
|
114
|
<span |
|
115
|
className={clx("w-fit min-w-[32px] border-r px-2", { |
|
116
|
"py-[9px]": size === "base", |
|
117
|
"py-[5px]": size === "small", |
|
118
|
})} |
|
119
|
role="presentation" |
|
120
|
> |
|
121
|
<Text |
|
122
|
size="small" |
|
123
|
leading="compact" |
|
124
|
className={clx( |
|
125
|
"text-ui-fg-muted pointer-events-none select-none uppercase", |
|
126
|
{ |
|
127
|
"text-ui-fg-disabled": disabled, |
|
128
|
} |
|
129
|
)} |
|
130
|
> |
|
131
|
{code} |
|
132
|
</Text> |
|
133
|
</span> |
|
134
|
<Primitive |
|
135
|
className="h-full min-w-0 flex-1 appearance-none bg-transparent text-right outline-none disabled:cursor-not-allowed" |
|
136
|
disabled={disabled} |
|
137
|
onInvalid={onInnerInvalid} |
|
138
|
ref={innerRef} |
|
139
|
{...props} |
|
140
|
/> |
|
141
|
<span |
|
142
|
className={clx( |
|
143
|
"flex w-fit min-w-[32px] items-center justify-center border-l px-2 text-right", |
|
144
|
{ |
|
145
|
"py-[9px]": size === "base", |
|
146
|
"py-[5px]": size === "small", |
|
147
|
} |
|
148
|
)} |
|
149
|
role="presentation" |
|
150
|
> |
|
151
|
<Text |
|
152
|
size="small" |
|
153
|
leading="compact" |
|
154
|
className={clx("text-ui-fg-muted pointer-events-none select-none", { |
|
155
|
"text-ui-fg-disabled": disabled, |
|
156
|
})} |
|
157
|
> |
|
158
|
{symbol} |
|
159
|
</Text> |
|
160
|
</span> |
|
161
|
</div> |
|
162
|
) |
|
163
|
} |
|
164
|
) |
|
165
|
CurrencyInput.displayName = "CurrencyInput" |
|
166
|
|
|
167
|
export { CurrencyInput } |