2023-06-19 17:08:12 +02:00
|
|
|
import {
|
|
|
|
forwardRef,
|
|
|
|
useRef,
|
|
|
|
useImperativeHandle,
|
|
|
|
KeyboardEvent,
|
|
|
|
useLayoutEffect,
|
|
|
|
} from "react";
|
2023-05-31 18:27:29 +02:00
|
|
|
import clsx from "clsx";
|
|
|
|
|
|
|
|
import "./TextField.scss";
|
|
|
|
|
|
|
|
export type TextFieldProps = {
|
|
|
|
value?: string;
|
|
|
|
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
onClick?: () => void;
|
|
|
|
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
|
|
|
|
|
|
readonly?: boolean;
|
|
|
|
fullWidth?: boolean;
|
2023-06-19 17:08:12 +02:00
|
|
|
selectOnRender?: boolean;
|
2023-05-31 18:27:29 +02:00
|
|
|
|
|
|
|
label?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
|
|
|
|
(
|
2023-06-19 17:08:12 +02:00
|
|
|
{
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
label,
|
|
|
|
fullWidth,
|
|
|
|
placeholder,
|
|
|
|
readonly,
|
|
|
|
selectOnRender,
|
|
|
|
onKeyDown,
|
|
|
|
},
|
2023-05-31 18:27:29 +02:00
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const innerRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => innerRef.current!);
|
|
|
|
|
2023-06-19 17:08:12 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (selectOnRender) {
|
|
|
|
innerRef.current?.select();
|
|
|
|
}
|
|
|
|
}, [selectOnRender]);
|
|
|
|
|
2023-05-31 18:27:29 +02:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx("ExcTextField", {
|
|
|
|
"ExcTextField--fullWidth": fullWidth,
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
innerRef.current?.focus();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="ExcTextField__label">{label}</div>
|
|
|
|
<div
|
|
|
|
className={clsx("ExcTextField__input", {
|
|
|
|
"ExcTextField__input--readonly": readonly,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
readOnly={readonly}
|
|
|
|
type="text"
|
|
|
|
value={value}
|
|
|
|
placeholder={placeholder}
|
|
|
|
ref={innerRef}
|
|
|
|
onChange={(event) => onChange?.(event.target.value)}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|