Portal/rsconcept/frontend/src/components/ui/TextInput.tsx
2025-02-05 12:12:46 +03:00

75 lines
1.6 KiB
TypeScript

import clsx from 'clsx';
import { CProps } from '@/components/props';
import ErrorField from './ErrorField';
import Label from './Label';
interface TextInputProps extends CProps.Editor, CProps.ErrorProcessing, CProps.Colors, CProps.Input {
/** Indicates that padding should be minimal. */
dense?: boolean;
/** Capture enter key. */
allowEnter?: boolean;
}
function preventEnterCapture(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key === 'Enter') {
event.preventDefault();
}
}
/**
* Displays a customizable input with a label.
*/
function TextInput({
id,
label,
dense,
noBorder,
noOutline,
allowEnter,
disabled,
className,
colors = 'clr-input',
onKeyDown,
error,
...restProps
}: TextInputProps) {
return (
<div
className={clsx(
{
'flex flex-col': !dense,
'flex items-center gap-3': dense
},
dense && className
)}
>
<Label text={label} htmlFor={id} />
<input
id={id}
className={clsx(
'min-w-0 py-2',
'leading-tight truncate hover:text-clip',
{
'px-3': !noBorder || !disabled,
'flex-grow max-w-full': dense,
'mt-2': !dense && !!label,
'border': !noBorder,
'clr-outline': !noOutline
},
colors,
!dense && className
)}
onKeyDown={!allowEnter && !onKeyDown ? preventEnterCapture : onKeyDown}
disabled={disabled}
{...restProps}
/>
<ErrorField className='mt-1' error={error} />
</div>
);
}
export default TextInput;