Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 49x 4633x 649x 49x 4633x 4633x 4633x 4633x 4633x 4633x 167x 167x 4633x 270x 270x 4633x 272x 4633x 649x 48x 48x 48x 48x 4633x 224x 224x 224x 4633x 272x 270x 4633x 781x 304x 304x 4633x 1217x 20x 4633x 619x 48x 619x 4633x | import React, { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
import { PropertySelectionSummary } from "@/pages/flowsheet-page/flowsheet/PropertiesSidebar/components/PropertySelectionSummary";
import { Input, InputProps } from "./input";
interface DebouncedInputProps extends InputProps {
value: string | number;
onUpdate: (update: string | number) => void;
onDebouncedChange?: (update: string | number) => void;
isFixed?: boolean; // if not fixed, show a red border
autoSelect?: boolean;
unitType?: string;
debounceMs?: number;
validationState?: "error" | "warning" | "info" | "suggestion";
className?: string;
ariaLabel?: string;
}
const useUnmount = (fn: () => void) => {
useEffect(() => {
return fn;
}, []);
};
const DebouncedInput = ({
value,
onUpdate,
onDebouncedChange,
isFixed = true,
autoSelect = false,
debounceMs = 350,
validationState,
className,
ariaLabel,
...props
}: DebouncedInputProps) => {
const inputRef = useRef<HTMLInputElement | null>(null);
const currentValue = useRef(value);
const wasActive = useRef(false);
const wasDisabled = useRef(props.disabled);
const debounceRef = useRef<number | null>(null);
const handleEnterPressed = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (inputRef.current && e.key === "Enter") {
inputRef.current.blur();
}
};
const handleInput = (e: React.ChangeEvent<HTMLInputElement>) => {
currentValue.current = e.target.value; // This is used to avoid calling onBlur with stale data
Iif (onDebouncedChange) {
Iif (debounceRef.current !== null) {
window.clearTimeout(debounceRef.current);
}
debounceRef.current = window.setTimeout(() => {
onDebouncedChange(currentValue.current);
}, debounceMs);
}
};
const handleFocus = () => {
wasActive.current = true;
};
useEffect(() => {
if (autoSelect && inputRef.current) {
setTimeout(() => {
if (inputRef.current) {
inputRef.current.focus();
inputRef.current.select();
}
}, 1);
}
}, [autoSelect]);
const handleBlur = () => {
handleUpdate();
wasActive.current = false;
wasDisabled.current = false;
};
const handleUpdate = () => {
// only call onUpdate if the value has changed or if the input was previously disabled
if (currentValue.current !== value || wasDisabled.current || !isFixed) {
onUpdate(currentValue.current);
}
};
useEffect(() => {
if (
value !== undefined &&
value !== null &&
inputRef.current &&
inputRef.current.value !== value
) {
// handle external updates
currentValue.current = value;
inputRef.current.value = value.toString();
}
}, [value]);
useEffect(() => {
// we want to allow the input to be fixed after it becomes un-disabled
// if the input was previously disabled, we should update the value this one time
if (inputRef.current?.disabled) {
wasDisabled.current = true;
}
}, [inputRef.current?.disabled]);
useUnmount(() => {
if (wasActive.current) {
// handle unmounting while active
handleUpdate();
}
Iif (debounceRef.current !== null) {
window.clearTimeout(debounceRef.current);
}
});
return (
<Input
placeholder="Enter a value"
name="debounced-input"
ref={inputRef}
type="text"
onInput={handleInput}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyDown={handleEnterPressed}
className={cn(
!isFixed
? "border-rose-700"
: (props.disabled && value !== undefined) ||
(props.disabled && value === 0)
? "border-0"
: "",
validationState === "error" && "border-rose-600",
validationState === "warning" && "border-amber-500",
validationState === "info" && "border-blue-500",
validationState === "suggestion" && "border-emerald-500",
className,
)}
aria-label={ariaLabel}
{...props}
/>
);
};
export default DebouncedInput;
|