2024-06-07 20:17:03 +03:00
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-01-28 23:23:03 +03:00
|
|
|
import { CProps } from '@/components/props';
|
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
import ErrorField from './ErrorField';
|
2024-06-07 20:17:03 +03:00
|
|
|
import Label from './Label';
|
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
interface TextInputProps extends CProps.Editor, CProps.ErrorProcessing, CProps.Colors, CProps.Input {
|
2024-11-21 15:09:31 +03:00
|
|
|
/** Indicates that padding should be minimal. */
|
2024-06-07 20:17:03 +03:00
|
|
|
dense?: boolean;
|
2024-11-21 15:09:31 +03:00
|
|
|
|
|
|
|
/** 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:09:31 +03:00
|
|
|
/**
|
|
|
|
* Displays a customizable input with a label.
|
|
|
|
*/
|
2024-06-07 20:17:03 +03:00
|
|
|
function TextInput({
|
|
|
|
id,
|
|
|
|
label,
|
|
|
|
dense,
|
|
|
|
noBorder,
|
|
|
|
noOutline,
|
|
|
|
allowEnter,
|
|
|
|
disabled,
|
|
|
|
className,
|
|
|
|
colors = 'clr-input',
|
|
|
|
onKeyDown,
|
2025-01-31 21:04:21 +03:00
|
|
|
error,
|
2024-06-07 20:17:03 +03:00
|
|
|
...restProps
|
|
|
|
}: TextInputProps) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
{
|
2025-02-03 18:17:07 +03:00
|
|
|
'flex flex-col': !dense,
|
2024-06-07 20:17:03 +03:00
|
|
|
'flex items-center gap-3': dense
|
|
|
|
},
|
|
|
|
dense && className
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Label text={label} htmlFor={id} />
|
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
className={clsx(
|
2025-02-03 18:17:07 +03:00
|
|
|
'min-w-0 py-2 mt-2',
|
2024-06-07 20:17:03 +03:00
|
|
|
'leading-tight truncate hover:text-clip',
|
|
|
|
{
|
|
|
|
'px-3': !noBorder || !disabled,
|
|
|
|
'flex-grow max-w-full': dense,
|
|
|
|
'border': !noBorder,
|
|
|
|
'clr-outline': !noOutline
|
|
|
|
},
|
|
|
|
colors,
|
|
|
|
!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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TextInput;
|