ConceptPortal-public/rsconcept/frontend/src/components/input1/text-input.tsx

77 lines
1.7 KiB
TypeScript
Raw Normal View History

import clsx from 'clsx';
2025-03-12 11:55:43 +03:00
import { Label } from './label1';
2025-02-22 14:04:01 +03:00
import { type Editor, type ErrorProcessing, type Titled } from '../props';
2025-02-12 21:36:25 +03:00
2025-03-12 11:55:43 +03:00
import { ErrorField } from './error-field';
2023-07-15 17:46:19 +03:00
2025-02-22 14:04:01 +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. */
2023-12-28 14:04:44 +03:00
dense?: boolean;
/** Capture enter key. */
2023-12-28 14:04:44 +03:00
allowEnter?: boolean;
2023-11-05 16:31:49 +03:00
}
function preventEnterCapture(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key === 'Enter') {
event.preventDefault();
}
2023-07-15 17:46:19 +03:00
}
/**
* Displays a customizable input with a label.
*/
2025-02-07 10:54:47 +03:00
export function TextInput({
2023-12-28 14:04:44 +03:00
id,
label,
dense,
noBorder,
noOutline,
allowEnter,
disabled,
2025-02-22 14:04:01 +03:00
transparent,
className,
onKeyDown,
error,
...restProps
2023-07-20 17:11:03 +03:00
}: TextInputProps) {
2023-07-15 17:46:19 +03:00
return (
2023-12-28 14:04:44 +03:00
<div
className={clsx(
{
2025-02-03 18:17:44 +03:00
'flex flex-col': !dense,
2023-12-28 14:04:44 +03:00
'flex items-center gap-3': dense
},
2023-12-28 14:04:44 +03:00
dense && className
)}
2023-12-28 14:04:44 +03:00
>
<Label text={label} htmlFor={id} />
<input
id={id}
className={clsx(
2025-02-04 23:34:02 +03:00
'min-w-0 py-2',
2023-12-28 14:04:44 +03:00
'leading-tight truncate hover:text-clip',
{
'px-3': !noBorder || !disabled,
'grow max-w-full': dense,
2025-02-05 12:13:05 +03:00
'mt-2': !dense && !!label,
2023-12-28 14:04:44 +03:00
'border': !noBorder,
2025-02-22 14:04:01 +03:00
'clr-outline': !noOutline,
'bg-transparent': transparent,
'clr-input': !transparent
2023-12-28 14:04:44 +03:00
},
!dense && className
)}
onKeyDown={!allowEnter && !onKeyDown ? preventEnterCapture : onKeyDown}
disabled={disabled}
{...restProps}
/>
2025-02-03 18:17:44 +03:00
<ErrorField className='mt-1' error={error} />
2023-12-28 14:04:44 +03:00
</div>
);
2023-07-15 17:46:19 +03:00
}