2025-08-10 15:21:49 +03:00
|
|
|
import type { InputHTMLAttributes, RefObject } from "preact/compat";
|
2025-08-04 23:22:45 +03:00
|
|
|
|
2025-08-14 21:31:09 +03:00
|
|
|
interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "onChange" | "value"> {
|
2025-08-04 23:22:45 +03:00
|
|
|
id?: string;
|
2025-08-03 21:18:18 +03:00
|
|
|
currentValue?: string;
|
2025-08-18 18:43:27 +03:00
|
|
|
onChange?(newValue: string, validity: ValidityState): void;
|
2025-08-05 19:06:47 +03:00
|
|
|
inputRef?: RefObject<HTMLInputElement>;
|
2025-08-03 21:18:18 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 21:31:09 +03:00
|
|
|
export default function FormTextBox({ inputRef, className, type, currentValue, onChange, ...rest}: FormTextBoxProps) {
|
2025-08-14 21:55:16 +03:00
|
|
|
if (type === "number" && currentValue) {
|
|
|
|
|
const { min, max } = rest;
|
2025-08-15 11:21:19 +03:00
|
|
|
const currentValueNum = parseInt(currentValue, 10);
|
|
|
|
|
if (min && currentValueNum < parseInt(String(min), 10)) {
|
2025-08-14 21:55:16 +03:00
|
|
|
currentValue = String(min);
|
2025-08-15 11:21:19 +03:00
|
|
|
} else if (max && currentValueNum > parseInt(String(max), 10)) {
|
2025-08-14 21:55:16 +03:00
|
|
|
currentValue = String(max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 21:18:18 +03:00
|
|
|
return (
|
2025-08-04 21:17:35 +03:00
|
|
|
<input
|
2025-08-05 19:06:47 +03:00
|
|
|
ref={inputRef}
|
|
|
|
|
className={`form-control ${className ?? ""}`}
|
2025-08-14 21:31:09 +03:00
|
|
|
type={type ?? "text"}
|
2025-08-04 21:17:35 +03:00
|
|
|
value={currentValue}
|
2025-08-18 17:37:20 +03:00
|
|
|
onInput={e => {
|
|
|
|
|
const target = e.currentTarget;
|
|
|
|
|
onChange?.(target.value, target.validity);
|
|
|
|
|
}}
|
2025-08-14 21:31:09 +03:00
|
|
|
{...rest}
|
2025-08-09 19:41:49 +03:00
|
|
|
/>
|
2025-08-03 21:18:18 +03:00
|
|
|
);
|
2025-08-14 21:31:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FormTextBoxWithUnit(props: FormTextBoxProps & { unit: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<label class="input-group tn-number-unit-pair">
|
|
|
|
|
<FormTextBox {...props} />
|
|
|
|
|
<span class="input-group-text">{props.unit}</span>
|
|
|
|
|
</label>
|
|
|
|
|
)
|
2025-08-03 21:18:18 +03:00
|
|
|
}
|