-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathinput.tsx
More file actions
208 lines (189 loc) · 7.32 KB
/
Copy pathinput.tsx
File metadata and controls
208 lines (189 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as React from 'react'
import { cn } from '@/lib/utils'
import { Icon } from '@/components/ui/icon'
interface InputProps extends Omit<React.ComponentProps<'input'>, 'size'> {
size?: 'xs' | 'sm';
variant?: 'default' | 'rename' | 'rename-selected';
stepper?: boolean;
onStepperChange?: (value: string) => void;
/**
* Disable the implicit ArrowUp/ArrowDown numeric-stepping behavior.
* Use for text-only inputs (e.g. search fields) where pressing arrow keys
* should be left to the parent for navigation/highlighting instead of
* incrementing the value.
*/
disableKeyboardStep?: boolean;
}
const Input = React.forwardRef<HTMLInputElement, InputProps>(function Input({
className,
type,
size = 'xs',
variant = 'default',
onKeyDown,
value,
onChange,
stepper = false,
onStepperChange,
disableKeyboardStep = false,
min,
max,
step = '1',
...props
}, forwardedRef) {
const internalRef = React.useRef<HTMLInputElement>(null);
const inputRef = (forwardedRef || internalRef) as React.RefObject<HTMLInputElement>;
const sizeClasses = {
xs: 'h-8 text-xs px-2 py-1 rounded-lg',
sm: 'h-10 text-sm px-3 py-1.5 rounded-xl',
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
// Only handle arrow keys for numeric inputs
if (!disableKeyboardStep && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
const currentValue = typeof value === 'string' ? value : String(value || '');
// Check if the value is a valid number or empty (treat empty as 0)
const numValue = currentValue === '' ? 0 : parseFloat(currentValue);
if (!isNaN(numValue) && isFinite(numValue)) {
e.preventDefault();
const increment = e.shiftKey ? 10 : 1;
let newValue = e.key === 'ArrowUp'
? numValue + increment
: numValue - increment;
// Respect min/max constraints
if (min !== undefined) {
const minValue = Number(min);
if (!isNaN(minValue)) {
newValue = Math.max(newValue, minValue);
}
}
if (max !== undefined) {
const maxValue = Number(max);
if (!isNaN(maxValue)) {
newValue = Math.min(newValue, maxValue);
}
}
// Create a synthetic event to trigger onChange
if (inputRef.current && onChange) {
const inputElement = inputRef.current;
// Set the value on the input element
inputElement.value = String(newValue);
// Create event with the input element as both target and currentTarget
const syntheticEvent = {
target: inputElement,
currentTarget: inputElement,
} as unknown as React.ChangeEvent<HTMLInputElement>;
onChange(syntheticEvent);
}
return;
}
}
// Call original onKeyDown if provided
onKeyDown?.(e);
};
const handleIncrement = () => {
const currentValue = Number(value) || 0;
const stepValue = Number(step);
const maxValue = max ? Number(max) : Infinity;
const newValue = Math.min(currentValue + stepValue, maxValue);
if (onStepperChange) {
onStepperChange(String(newValue));
} else if (onChange && inputRef.current) {
const inputElement = inputRef.current;
inputElement.value = String(newValue);
const syntheticEvent = {
target: inputElement,
currentTarget: inputElement,
} as unknown as React.ChangeEvent<HTMLInputElement>;
onChange(syntheticEvent);
}
};
const handleDecrement = () => {
const currentValue = Number(value) || 0;
const stepValue = Number(step);
const minValue = min ? Number(min) : -Infinity;
const newValue = Math.max(currentValue - stepValue, minValue);
if (onStepperChange) {
onStepperChange(String(newValue));
} else if (onChange && inputRef.current) {
const inputElement = inputRef.current;
inputElement.value = String(newValue);
const syntheticEvent = {
target: inputElement,
currentTarget: inputElement,
} as unknown as React.ChangeEvent<HTMLInputElement>;
onChange(syntheticEvent);
}
};
if (stepper) {
return (
<div className="group relative w-full">
<input
ref={inputRef}
type={type}
data-slot="input"
min={min}
max={max}
step={step}
className={cn(
'file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground bg-input border-transparent w-full min-w-0 border transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:font-medium disabled:cursor-not-allowed disabled:opacity-50',
'focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-0',
'',
'aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive',
sizeClasses[size],
stepper && !props.disabled && 'pr-8',
className
)}
value={value}
onChange={onChange}
onKeyDown={handleKeyDown}
{...props}
/>
{!props.disabled && (
<div className="absolute right-px top-px bottom-px items-center rounded-r-[10px] hidden group-hover:flex px-1.5">
<div className="flex flex-col -gap-px">
<button
type="button"
tabIndex={-1}
className="p-0 opacity-50 hover:opacity-100 transition-opacity cursor-pointer"
onClick={handleIncrement}
>
<Icon name="chevronUp" className="size-2.5" />
</button>
<button
type="button"
tabIndex={-1}
className="p-0 opacity-50 hover:opacity-100 transition-opacity cursor-pointer -mt-0.5"
onClick={handleDecrement}
>
<Icon name="chevronDown" className="size-2.5" />
</button>
</div>
</div>
)}
</div>
);
}
const variantClasses = {
default: cn(
'file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground bg-input border-transparent w-full min-w-0 border transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:font-medium disabled:cursor-not-allowed disabled:opacity-50',
'focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[0px]',
'aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive',
'[appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none',
sizeClasses[size],
),
rename: 'bg-black/5 dark:bg-white/10 rounded px-1 py-0.5 outline-none min-w-0 text-xs font-medium text-foreground placeholder:text-muted-foreground',
'rename-selected': 'bg-white/20 rounded px-1 py-0.5 outline-none min-w-0 text-xs font-medium text-white placeholder:text-white/40',
};
return (
<input
ref={inputRef}
type={type}
data-slot="input"
className={cn(variantClasses[variant], className)}
value={value}
onChange={onChange}
onKeyDown={handleKeyDown}
{...props}
/>
)
});
export { Input }