Skip to content

Commit 0a896f4

Browse files
authored
ENG-1378 automatic source appending for evd incorrectly pre pends (#788)
* working * refactor * fix bug when changing input types * re-add isLocked as prop * make isLocked required
1 parent 1662562 commit 0a896f4

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

apps/roam/src/components/FuzzySelectInput.tsx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useState, useCallback, useMemo, useRef, useEffect } from "react";
1+
import React, {
2+
useState,
3+
useCallback,
4+
useMemo,
5+
useRef,
6+
useEffect,
7+
} from "react";
28
import {
39
Button,
410
TextArea,
@@ -14,27 +20,22 @@ import { Result } from "~/utils/types";
1420
type FuzzySelectInputProps<T extends Result = Result> = {
1521
value?: T;
1622
setValue: (q: T) => void;
17-
onLockedChange?: (isLocked: boolean) => void;
1823
mode: "create" | "edit";
19-
initialUid: string;
2024
options?: T[];
2125
placeholder?: string;
2226
autoFocus?: boolean;
23-
initialIsLocked?: boolean;
27+
isLocked: boolean;
2428
};
2529

2630
const FuzzySelectInput = <T extends Result = Result>({
2731
value,
2832
setValue,
29-
onLockedChange,
3033
mode,
31-
initialUid,
3234
options = [],
3335
placeholder = "Enter value",
3436
autoFocus,
35-
initialIsLocked,
37+
isLocked,
3638
}: FuzzySelectInputProps<T>) => {
37-
const [isLocked, setIsLocked] = useState(initialIsLocked || false);
3839
const [query, setQuery] = useState<string>(() => value?.text || "");
3940
const [isOpen, setIsOpen] = useState(false);
4041
const [activeIndex, setActiveIndex] = useState(0);
@@ -52,28 +53,31 @@ const FuzzySelectInput = <T extends Result = Result>({
5253

5354
const handleSelect = useCallback(
5455
(item: T) => {
55-
if (mode === "create" && item.uid && item.uid !== initialUid) {
56-
setIsLocked(true);
57-
setQuery(item.text);
58-
setValue(item);
59-
setIsOpen(false);
60-
onLockedChange?.(true);
61-
} else {
62-
setQuery(item.text);
63-
setValue(item);
64-
setIsOpen(false);
56+
setQuery(item.text);
57+
setValue(item);
58+
setIsOpen(false);
59+
60+
if (!(mode === "create" && item.uid)) {
6561
requestAnimationFrame(() => inputRef.current?.focus());
6662
}
6763
},
68-
[mode, initialUid, setValue, onLockedChange],
64+
[mode, setValue],
6965
);
7066

7167
const handleClear = useCallback(() => {
72-
setIsLocked(false);
7368
setQuery("");
7469
setValue({ ...value, text: "", uid: "" } as T);
75-
onLockedChange?.(false);
76-
}, [value, setValue, onLockedChange]);
70+
}, [value, setValue]);
71+
72+
const handleInputChange = useCallback(
73+
(text: string) => {
74+
setQuery(text);
75+
if (mode === "create" && !isLocked) {
76+
setValue({ text, uid: "" } as T);
77+
}
78+
},
79+
[mode, isLocked, setValue],
80+
);
7781

7882
const handleKeyDown = useCallback(
7983
(e: React.KeyboardEvent<HTMLInputElement>) => {
@@ -100,10 +104,9 @@ const FuzzySelectInput = <T extends Result = Result>({
100104
);
101105

102106
useEffect(() => {
103-
if (mode === "create" && !isLocked) {
104-
setValue({ text: query, uid: "" } as T);
105-
}
106-
}, [query, mode, isLocked, setValue]);
107+
if (isLocked) return;
108+
setQuery(value?.text ?? "");
109+
}, [isLocked, value?.text]);
107110

108111
useEffect(() => {
109112
if (isFocused && filteredItems.length > 0 && query) {
@@ -206,7 +209,7 @@ const FuzzySelectInput = <T extends Result = Result>({
206209
inputRef={inputRef}
207210
className="w-full"
208211
value={query}
209-
onChange={(e) => setQuery(e.target.value)}
212+
onChange={(e) => handleInputChange(e.target.value)}
210213
onKeyDown={handleKeyDown}
211214
autoFocus={autoFocus}
212215
placeholder={placeholder}
@@ -223,4 +226,4 @@ const FuzzySelectInput = <T extends Result = Result>({
223226
);
224227
};
225228

226-
export default FuzzySelectInput;
229+
export default FuzzySelectInput;

apps/roam/src/components/ModifyNodeDialog.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,8 @@ const ModifyNodeDialog = ({
8484
[content.uid, initialValue.uid, mode],
8585
);
8686
const isReferencedNodeLocked = useMemo(
87-
() =>
88-
Boolean(
89-
referencedNodeValue.uid &&
90-
referencedNodeValue.uid !== initialReferencedNode?.uid,
91-
),
92-
[referencedNodeValue.uid, initialReferencedNode?.uid],
87+
() => Boolean(referencedNodeValue.uid),
88+
[referencedNodeValue.uid],
9389
);
9490

9591
const [options, setOptions] = useState<{
@@ -537,7 +533,7 @@ const ModifyNodeDialog = ({
537533
: `Enter a ${selectedNodeType.text.toLowerCase()} ...`
538534
}
539535
mode={mode}
540-
initialUid={content.uid}
536+
isLocked={isContentLocked}
541537
autoFocus
542538
/>
543539
</div>
@@ -552,8 +548,7 @@ const ModifyNodeDialog = ({
552548
options={options.referencedNode}
553549
placeholder={loading ? "..." : "Select a referenced node"}
554550
mode={"create"}
555-
initialUid={referencedNodeValue.uid}
556-
initialIsLocked={isReferencedNodeLocked}
551+
isLocked={isReferencedNodeLocked}
557552
autoFocus={false}
558553
/>
559554
</div>

0 commit comments

Comments
 (0)