-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.tsx
47 lines (43 loc) · 1.64 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
46
47
import { type InputHTMLAttributes, type ReactElement, cloneElement, forwardRef } from "react";
import type { Simplify } from "type-fest";
import { cn } from "./cn";
export type InputProps = Simplify<
InputHTMLAttributes<HTMLInputElement> & {
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 };