ConceptPortal-public/rsconcept/frontend/src/components/ui/TextInput.tsx

64 lines
1.3 KiB
TypeScript
Raw Normal View History

import clsx from 'clsx';
import { CProps } from '../props';
2023-07-15 17:46:19 +03:00
import Label from './Label';
2023-12-28 14:04:44 +03:00
interface TextInputProps extends CProps.Editor, CProps.Colors, CProps.Input {
dense?: boolean;
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
}
2023-07-20 17:11:03 +03:00
function TextInput({
2023-12-28 14:04:44 +03:00
id,
label,
dense,
noBorder,
noOutline,
allowEnter,
disabled,
className,
2023-11-05 16:31:49 +03:00
colors = 'clr-input',
onKeyDown,
...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(
{
2023-12-28 14:04:44 +03:00
'flex flex-col gap-2': !dense,
'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(
'py-2',
'leading-tight truncate hover:text-clip',
{
'px-3': !noBorder || !disabled,
'flex-grow': dense,
'border': !noBorder,
'clr-outline': !noOutline
},
colors,
!dense && className
)}
onKeyDown={!allowEnter && !onKeyDown ? preventEnterCapture : onKeyDown}
disabled={disabled}
{...restProps}
/>
</div>
);
2023-07-15 17:46:19 +03:00
}
2023-12-28 14:04:44 +03:00
export default TextInput;