## ➡️ #17224 - Replace the [solution merged here](https://github.com/fleetdm/fleet/pull/17086) with a simpler and more easily extendable one - Apply same approach to the Edit policies form. Edit query demo: https://www.loom.com/share/589d3bfedc754f62b31c39bc517c2382?sid=ce65e5a5-e13f-4446-a37c-991a09cc8960 Edit query: <img width="1791" alt="Screenshot 2024-03-04 at 5 34 35 PM" src="https://github.com/fleetdm/fleet/assets/61553566/6727bd9e-ad05-45b0-a65c-e01487d7d923"> Edit policy: <img width="2550" alt="Screenshot 2024-03-05 at 11 47 34 AM" src="https://github.com/fleetdm/fleet/assets/61553566/fdd8e455-7f54-4177-be3e-4a82a879a176"> <img width="2550" alt="Screenshot 2024-03-05 at 11 47 27 AM" src="https://github.com/fleetdm/fleet/assets/61553566/3b33b097-f652-4e99-b944-2d29ffe3f311"> - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
106 lines
2.4 KiB
TypeScript
106 lines
2.4 KiB
TypeScript
import React, {
|
|
ChangeEvent,
|
|
KeyboardEvent,
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
} from "react";
|
|
import classnames from "classnames";
|
|
|
|
interface IAutoSizeInputFieldProps {
|
|
name: string;
|
|
placeholder: string;
|
|
value: string;
|
|
inputClassName?: string;
|
|
maxLength: number;
|
|
hasError?: boolean;
|
|
isDisabled?: boolean;
|
|
isFocused?: boolean;
|
|
onFocus?: () => void;
|
|
onBlur?: () => void;
|
|
onChange: (newSelectedValue: string) => void;
|
|
onKeyPress: (event: KeyboardEvent<HTMLTextAreaElement>) => void;
|
|
}
|
|
|
|
const baseClass = "component__auto-size-input-field";
|
|
|
|
const AutoSizeInputField = ({
|
|
name,
|
|
placeholder,
|
|
value,
|
|
inputClassName,
|
|
maxLength,
|
|
hasError,
|
|
isDisabled,
|
|
isFocused,
|
|
onFocus = () => null,
|
|
onBlur = () => null,
|
|
onChange,
|
|
onKeyPress,
|
|
}: IAutoSizeInputFieldProps): JSX.Element => {
|
|
const [inputValue, setInputValue] = useState(value);
|
|
|
|
const inputClasses = classnames(baseClass, inputClassName, "no-hover", {
|
|
[`${baseClass}--disabled`]: isDisabled,
|
|
[`${baseClass}--error`]: hasError,
|
|
[`${baseClass}__textarea`]: true,
|
|
});
|
|
|
|
const inputElement = useRef<any>(null);
|
|
|
|
useEffect(() => {
|
|
onChange(inputValue);
|
|
}, [inputValue]);
|
|
|
|
useEffect(() => {
|
|
if (isFocused && inputElement.current) {
|
|
inputElement.current.focus();
|
|
inputElement.current.selectionStart = inputValue.length;
|
|
inputElement.current.selectionEnd = inputValue.length;
|
|
}
|
|
}, [isFocused]);
|
|
|
|
const onInputChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
|
|
setInputValue(event.currentTarget.value);
|
|
};
|
|
|
|
const onInputFocus = () => {
|
|
isFocused = true;
|
|
onFocus();
|
|
};
|
|
|
|
const onInputBlur = () => {
|
|
isFocused = false;
|
|
onBlur();
|
|
};
|
|
|
|
const onInputKeyPress = (event: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
onKeyPress(event);
|
|
};
|
|
|
|
return (
|
|
<div className={baseClass}>
|
|
<label className="input-sizer" data-value={inputValue} htmlFor={name}>
|
|
<textarea
|
|
name={name}
|
|
id={name}
|
|
onChange={onInputChange}
|
|
placeholder={placeholder}
|
|
value={inputValue}
|
|
maxLength={maxLength}
|
|
className={inputClasses}
|
|
cols={1}
|
|
rows={1}
|
|
tabIndex={0}
|
|
onFocus={onInputFocus}
|
|
onBlur={onInputBlur}
|
|
onKeyPress={onInputKeyPress}
|
|
ref={inputElement}
|
|
/>
|
|
</label>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AutoSizeInputField;
|