import { FunctionalComponent, ComponentChild, Ref } from 'preact'; import { forwardRef } from 'preact/compat'; export type DecoratedInputProps = { type?: 'text' | 'email' | 'password'; className?: string; disabled?: boolean; left?: ComponentChild[]; right?: ComponentChild[]; value?: string; placeholder?: string; onChange?: (text: string) => void; onFocus?: (event: FocusEvent) => void; onKeyDown?: (event: KeyboardEvent) => void; autocomplete?: boolean; }; const getClassNames = ( hasLeftDecorations: boolean, hasRightDecorations: boolean ) => { return { container: `flex items-stretch position-relative bg-default border-1 border-solid border-main rounded focus-within:ring-info overflow-hidden ${ !hasLeftDecorations && !hasRightDecorations ? 'px-2 py-1.5' : '' }`, input: `w-full border-0 focus:shadow-none bg-transparent ${ !hasLeftDecorations && hasRightDecorations ? 'pl-2' : '' } ${hasRightDecorations ? 'pr-2' : ''}`, disabled: 'bg-grey-5 cursor-not-allowed', }; }; /** * Input that can be decorated on the left and right side */ export const DecoratedInput: FunctionalComponent = forwardRef( ( { type = 'text', className = '', disabled = false, left, right, value, placeholder = '', onChange, onFocus, onKeyDown, autocomplete = false, }: DecoratedInputProps, ref: Ref ) => { const hasLeftDecorations = Boolean(left?.length); const hasRightDecorations = Boolean(right?.length); const classNames = getClassNames(hasLeftDecorations, hasRightDecorations); return (
{left && (
{left.map((leftChild) => ( <>{leftChild} ))}
)} onChange && onChange((e.target as HTMLInputElement).value) } onFocus={onFocus} onKeyDown={onKeyDown} data-lpignore={type !== 'password' ? true : false} autocomplete={autocomplete ? 'on' : 'off'} ref={ref} /> {right && (
{right.map((rightChild, index) => (
0 ? 'ml-3' : ''}>{rightChild}
))}
)}
); } );