refactor: modals refactored

This commit is contained in:
2025-09-05 14:25:36 +04:00
parent 7694b4ae03
commit d0c734d481
24 changed files with 292 additions and 97 deletions

View File

@ -3,22 +3,22 @@ import { TextInput } from "@mantine/core";
import { Styles } from "@mantine/core/lib/core/styles-api/styles-api.types";
type Props = {
defaultValue?: string;
onComplete: (value: string) => void;
value?: string;
onChange: (value: string) => void;
placeholder?: string;
getChildren: (startEditing: () => void) => ReactNode;
inputStyles?: Styles<any>;
};
const InPlaceInputDesktop: FC<Props> = ({
onComplete,
onChange,
placeholder,
inputStyles,
getChildren,
defaultValue = "",
value = "",
}) => {
const [isWriting, setIsWriting] = useState<boolean>(false);
const [value, setValue] = useState<string>(defaultValue);
const [localValue, setLocalValue] = useState<string>(value);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
@ -41,22 +41,22 @@ const InPlaceInputDesktop: FC<Props> = ({
document.addEventListener("mousedown", handleClickOutside);
return () =>
document.removeEventListener("mousedown", handleClickOutside);
}, [isWriting, value]);
}, [isWriting, localValue]);
const onStartCreating = () => {
setValue(defaultValue);
setLocalValue(localValue);
setIsWriting(true);
};
const onCancelCreating = () => {
setValue(defaultValue);
setLocalValue(localValue);
setIsWriting(false);
};
const onCompleteCreating = () => {
const localValue = value.trim();
if (localValue) {
onComplete(localValue);
const val = localValue.trim();
if (val) {
onChange(val);
}
setIsWriting(false);
};
@ -67,8 +67,8 @@ const InPlaceInputDesktop: FC<Props> = ({
ref={inputRef}
placeholder={placeholder}
variant={"unstyled"}
value={value}
onChange={e => setValue(e.target.value)}
value={localValue}
onChange={e => setLocalValue(e.target.value)}
onKeyDown={e => {
e.stopPropagation();
if (e.key === "Enter") onCompleteCreating();