diff --git a/src/components/ui/Input.tsx b/src/components/ui/Input.tsx index 2060c6b7..306f7a01 100644 --- a/src/components/ui/Input.tsx +++ b/src/components/ui/Input.tsx @@ -1,4 +1,5 @@ -import { forwardRef } from "react" +import { forwardRef, useCallback, useMemo } from "react" +import type { ReactElement } from "react" import { cn } from "~/lib/utils" @@ -12,8 +13,11 @@ type InputProps = { error?: string help?: React.ReactNode multiline?: TMultiline + renderInput?: (props: Omit, "renderInput">) => ReactElement } & React.ComponentPropsWithRef +export type CustomInputProps = Omit, "renderInput"> + export const Input = forwardRef(function Input< TMultiline extends boolean = false, >( @@ -26,6 +30,7 @@ export const Input = forwardRef(function Input< error, help, multiline, + renderInput, ...inputProps }: InputProps, ref: TMultiline extends true @@ -34,7 +39,32 @@ export const Input = forwardRef(function Input< ) { const hasAddon = !!addon const hasPrefix = !!prefix - const Component = (multiline ? "textarea" : "input") as any + + const inputComponentProps = useMemo( + () => + ({ + ...inputProps, + ref, + className: cn( + "input", + hasAddon && `has-addon`, + hasPrefix && `has-prefix`, + isBlock && `is-block`, + className, + ), + } as any), + [className, hasAddon, hasPrefix, inputProps, isBlock, ref], + ) + + const renderInputComponent = useCallback(() => { + if (renderInput) { + return renderInput(inputComponentProps) + } else if (multiline) { + return