| 
									
										
										
										
											2025-08-21 15:11:08 +03:00
										 |  |  | import { useEffect, type InputHTMLAttributes, type RefObject } from "preact/compat"; | 
					
						
							| 
									
										
										
										
											2025-08-04 23:22:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-19 21:41:05 +03:00
										 |  |  | interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "onChange" | "onBlur" | "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-18 20:41:33 +03:00
										 |  |  |     onBlur?(newValue: string): void; | 
					
						
							| 
									
										
										
										
											2025-08-05 19:06:47 +03:00
										 |  |  |     inputRef?: RefObject<HTMLInputElement>; | 
					
						
							| 
									
										
										
										
											2025-08-03 21:18:18 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-21 15:11:08 +03:00
										 |  |  | export default function FormTextBox({ inputRef, className, type, currentValue, onChange, onBlur, autoFocus, ...rest}: FormTextBoxProps) { | 
					
						
							|  |  |  |     useEffect(() => { | 
					
						
							|  |  |  |         if (autoFocus) { | 
					
						
							|  |  |  |             inputRef?.current?.focus(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-20 18:34:57 +03:00
										 |  |  |     function applyLimits(value: string) { | 
					
						
							|  |  |  |         if (type === "number") { | 
					
						
							|  |  |  |             const { min, max } = rest; | 
					
						
							|  |  |  |             const currentValueNum = parseInt(value, 10); | 
					
						
							|  |  |  |             if (min && currentValueNum < parseInt(String(min), 10)) { | 
					
						
							|  |  |  |                 return String(min); | 
					
						
							|  |  |  |             } else if (max && currentValueNum > parseInt(String(max), 10)) { | 
					
						
							|  |  |  |                 return String(max); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 20:41:33 +03:00
										 |  |  |             onInput={onChange && (e => { | 
					
						
							| 
									
										
										
										
											2025-08-18 17:37:20 +03:00
										 |  |  |                 const target = e.currentTarget; | 
					
						
							| 
									
										
										
										
											2025-10-20 18:34:57 +03:00
										 |  |  |                 const currentValue = applyLimits(e.currentTarget.value); | 
					
						
							|  |  |  |                 onChange?.(currentValue, target.validity); | 
					
						
							| 
									
										
										
										
											2025-08-18 20:41:33 +03:00
										 |  |  |             })} | 
					
						
							| 
									
										
										
										
											2025-10-20 18:34:57 +03:00
										 |  |  |             onBlur={(e => { | 
					
						
							|  |  |  |                 const currentValue = applyLimits(e.currentTarget.value); | 
					
						
							|  |  |  |                 e.currentTarget.value = currentValue; | 
					
						
							|  |  |  |                 onBlur?.(currentValue); | 
					
						
							| 
									
										
										
										
											2025-08-18 20:41:33 +03:00
										 |  |  |             })} | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							| 
									
										
										
										
											2025-10-20 18:34:57 +03:00
										 |  |  |         </label> | 
					
						
							| 
									
										
										
										
											2025-08-14 21:31:09 +03:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2025-10-20 18:34:57 +03:00
										 |  |  | } |