-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.tsx
45 lines (41 loc) · 1.61 KB
/
input.tsx
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
import { type InputHTMLAttributes, type ReactElement, cloneElement, forwardRef } from "react";
import { cn } from "./cn";
export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
// any other prop goes here
startIcon?: ReactElement;
endIcon?: ReactElement;
wrapperClassName?: string;
}
const Input = forwardRef<HTMLInputElement, InputProps>(
({ startIcon, endIcon, className, type, wrapperClassName, ...rest }, ref) => (
<div className={cn("relative", wrapperClassName)}>
<input
type={type}
className={cn(
"peer flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm transition-colors file:border-0 file:bg-transparent file:text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-60",
startIcon && "pl-9",
endIcon && "pr-9",
className,
)}
ref={ref}
{...rest}
/>
{startIcon &&
cloneElement(startIcon as ReactElement, {
className: cn(
"absolute top-2.5 left-3 size-4 text-muted-foreground peer-focus-visible:text-primary peer-disabled:opacity-60",
startIcon.props.className,
),
})}
{endIcon &&
cloneElement(endIcon as ReactElement, {
className: cn(
"absolute top-2.5 right-3 size-4 text-muted-foreground peer-focus-visible:text-primary peer-disabled:opacity-60",
endIcon.props.className,
),
})}
</div>
),
);
Input.displayName = "Input";
export { Input };