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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 6595x 6595x 783x 13190x 6595x 6595x 6595x 6595x 6595x 6595x 339x 228x 228x 228x 228x 339x 342x 6595x 57x 57x 57x 228x 113x 341x 341x 6595x 382x 382x 6595x 23x 6595x 6595x 341x 341x 342x 342x | import React, { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
import { PropertySelectionSummary } from "@/pages/flowsheet-page/flowsheet/PropertiesSidebar/components/PropertySelectionSummary";
import {
AccessControlledProps,
AccessControlledInput as Input,
} from "./accessControlled";
import { InputProps } from "./input";
interface DebouncedInputProps extends InputProps, AccessControlledProps {
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,
writeAccessOnly = true,
hideIfNoAccess = false,
onBlur: externalOnBlur,
onFocus: externalOnFocus,
...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 skipNextBlurUpdate = useRef(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);
}
};
const handleEnterPressed = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (inputRef.current && e.key === "Enter") {
e.preventDefault();
skipNextBlurUpdate.current = true;
handleUpdate();
inputRef.current.blur();
}
};
const handleInput = (e: React.ChangeEvent<HTMLInputElement>) => {
currentValue.current = e.target.value; // This is used to avoid calling onBlur with stale data
if (onDebouncedChange) {
if (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 = () => {
if (skipNextBlurUpdate.current) {
skipNextBlurUpdate.current = false;
} else {
handleUpdate();
}
wasActive.current = false;
wasDisabled.current = false;
};
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();
}
if (debounceRef.current !== null) {
window.clearTimeout(debounceRef.current);
}
});
return (
<Input
placeholder="Enter a value"
name="debounced-input"
ref={inputRef}
type="text"
onInput={handleInput}
onBlur={(event) => {
handleBlur();
externalOnBlur?.(event);
}}
onFocus={(event) => {
handleFocus();
externalOnFocus?.(event);
}}
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}
writeAccessOnly={writeAccessOnly}
hideIfNoAccess={hideIfNoAccess}
{...props}
/>
);
};
export default DebouncedInput;
|