Portal/rsconcept/frontend/src/components/input/text-input.tsx

71 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import clsx from 'clsx';
2025-02-22 14:03:13 +03:00
import { type Editor, type ErrorProcessing, type Titled } from '../props';
2025-02-12 21:36:03 +03:00
2025-03-12 11:54:32 +03:00
import { ErrorField } from './error-field';
2025-03-12 12:12:45 +03:00
import { Label } from './label';
2024-06-07 20:17:03 +03:00
2025-02-22 14:03:13 +03:00
interface TextInputProps extends Editor, ErrorProcessing, Titled, React.ComponentProps<'input'> {
/** Indicates that the input should be transparent. */
transparent?: boolean;
/** Indicates that padding should be minimal. */
2024-06-07 20:17:03 +03:00
dense?: boolean;
/** Capture enter key. */
2024-06-07 20:17:03 +03:00
allowEnter?: boolean;
}
function preventEnterCapture(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key === 'Enter') {
event.preventDefault();
}
}
/**
* Displays a customizable input with a label.
*/
2025-02-07 10:53:49 +03:00
export function TextInput({
2024-06-07 20:17:03 +03:00
id,
label,
dense,
noBorder,
noOutline,
allowEnter,
disabled,
2025-02-22 14:03:13 +03:00
transparent,
2024-06-07 20:17:03 +03:00
className,
onKeyDown,
error,
2024-06-07 20:17:03 +03:00
...restProps
}: TextInputProps) {
return (
<div
className={clsx(
2025-03-13 01:14:47 +03:00
dense ? 'flex items-center gap-3' : 'flex flex-col', //
2024-06-07 20:17:03 +03:00
dense && className
)}
>
<Label text={label} htmlFor={id} />
<input
id={id}
className={clsx(
2025-02-04 23:33:35 +03:00
'min-w-0 py-2',
2024-06-07 20:17:03 +03:00
'leading-tight truncate hover:text-clip',
2025-03-13 01:14:47 +03:00
transparent ? 'bg-transparent' : 'clr-input',
!noBorder && 'border',
!noOutline && 'clr-outline',
(!noBorder || !disabled) && 'px-3',
dense && 'grow max-w-full',
!dense && !!label && 'mt-2',
2024-06-07 20:17:03 +03:00
!dense && className
)}
onKeyDown={!allowEnter && !onKeyDown ? preventEnterCapture : onKeyDown}
disabled={disabled}
{...restProps}
/>
2025-02-03 18:17:07 +03:00
<ErrorField className='mt-1' error={error} />
2024-06-07 20:17:03 +03:00
</div>
);
}