|
1
|
import { Minus, Plus } from "@medusajs/icons" |
|
2
|
import { clx } from "@medusajs/ui" |
|
3
|
import { forwardRef, InputHTMLAttributes } from "react" |
|
4
|
|
|
5
|
type InputProps = Omit< |
|
6
|
InputHTMLAttributes<HTMLInputElement>, |
|
7
|
"type" | "value" | "onChange" | "size" |
|
8
|
> |
|
9
|
|
|
10
|
interface NumberInputProps extends InputProps { |
|
11
|
value: number |
|
12
|
onChange: (value: number) => void |
|
13
|
size?: "base" | "small" |
|
14
|
min?: number |
|
15
|
max?: number |
|
16
|
step?: number |
|
17
|
} |
|
18
|
|
|
19
|
export const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>( |
|
20
|
( |
|
21
|
{ |
|
22
|
value, |
|
23
|
onChange, |
|
24
|
size = "base", |
|
25
|
min = 0, |
|
26
|
max = 100, |
|
27
|
step = 1, |
|
28
|
className, |
|
29
|
disabled, |
|
30
|
...props |
|
31
|
}, |
|
32
|
ref |
|
33
|
) => { |
|
34
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
|
35
|
const newValue = |
|
36
|
event.target.value === "" ? min : Number(event.target.value) |
|
37
|
if ( |
|
38
|
!isNaN(newValue) && |
|
39
|
(max === undefined || newValue <= max) && |
|
40
|
(min === undefined || newValue >= min) |
|
41
|
) { |
|
42
|
onChange(newValue) |
|
43
|
} |
|
44
|
} |
|
45
|
|
|
46
|
const handleIncrement = () => { |
|
47
|
const newValue = value + step |
|
48
|
if (max === undefined || newValue <= max) { |
|
49
|
onChange(newValue) |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
const handleDecrement = () => { |
|
54
|
const newValue = value - step |
|
55
|
if (min === undefined || newValue >= min) { |
|
56
|
onChange(newValue) |
|
57
|
} |
|
58
|
} |
|
59
|
|
|
60
|
return ( |
|
61
|
<div |
|
62
|
className={clx( |
|
63
|
"inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg", |
|
64
|
"[&:has(input:focus)]:shadow-borders-interactive-with-active", |
|
65
|
{ |
|
66
|
"h-7": size === "small", |
|
67
|
"h-8": size === "base", |
|
68
|
}, |
|
69
|
className |
|
70
|
)} |
|
71
|
> |
|
72
|
<input |
|
73
|
ref={ref} |
|
74
|
type="number" |
|
75
|
value={value} |
|
76
|
onChange={handleChange} |
|
77
|
min={min} |
|
78
|
max={max} |
|
79
|
step={step} |
|
80
|
className={clx( |
|
81
|
"flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]", |
|
82
|
"[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none", |
|
83
|
"placeholder:text-ui-fg-muted" |
|
84
|
)} |
|
85
|
{...props} |
|
86
|
/> |
|
87
|
<button |
|
88
|
className={clx( |
|
89
|
"flex items-center justify-center outline-none transition-fg", |
|
90
|
"disabled:cursor-not-allowed disabled:text-ui-fg-muted", |
|
91
|
"focus:bg-ui-bg-field-component-hover", |
|
92
|
"hover:bg-ui-bg-field-component-hover", |
|
93
|
{ |
|
94
|
"size-7": size === "small", |
|
95
|
"size-8": size === "base", |
|
96
|
} |
|
97
|
)} |
|
98
|
type="button" |
|
99
|
onClick={handleDecrement} |
|
100
|
disabled={(min !== undefined && value <= min) || disabled} |
|
101
|
> |
|
102
|
<Minus /> |
|
103
|
<span className="sr-only">{`Decrease by ${step}`}</span> |
|
104
|
</button> |
|
105
|
<button |
|
106
|
className={clx( |
|
107
|
"flex items-center justify-center outline-none transition-fg", |
|
108
|
"disabled:cursor-not-allowed disabled:text-ui-fg-muted", |
|
109
|
"focus:bg-ui-bg-field-hover", |
|
110
|
"hover:bg-ui-bg-field-hover", |
|
111
|
{ |
|
112
|
"size-7": size === "small", |
|
113
|
"size-8": size === "base", |
|
114
|
} |
|
115
|
)} |
|
116
|
type="button" |
|
117
|
onClick={handleIncrement} |
|
118
|
disabled={(max !== undefined && value >= max) || disabled} |
|
119
|
> |
|
120
|
<Plus /> |
|
121
|
<span className="sr-only">{`Increase by ${step}`}</span> |
|
122
|
</button> |
|
123
|
</div> |
|
124
|
) |
|
125
|
} |
|
126
|
) |