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