-
Notifications
You must be signed in to change notification settings - Fork 3
[ENG-979] Consolidate create node flow #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trangdoan982
wants to merge
17
commits into
main
Choose a base branch
from
eng-979-consolidate-create-node-flow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
898e3f5
current prog
trangdoan982 1ace767
cur progress
trangdoan982 c82a831
curr progress
trangdoan982 0e15020
replaced LabelDialog successfully
trangdoan982 faada12
curr progress
trangdoan982 11c9930
new component wrote
trangdoan982 f723ca2
finally work!!!
trangdoan982 192a79d
get the command line works as well
trangdoan982 1b161e6
fix for canvas
trangdoan982 736406d
cleanup
trangdoan982 e3167d8
tried out autoSelectFirstOption but didn't work
trangdoan982 a050f77
clean up
trangdoan982 66be474
address PR comments
trangdoan982 fc1025c
del excess file
trangdoan982 a38b624
fix command
trangdoan982 581295a
fix some stylings
trangdoan982 4c5fcd3
fix the focus issue + edit mode
trangdoan982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| import React, { useState, useCallback, useMemo, useRef, useEffect } from "react"; | ||
| import { | ||
| Button, | ||
| Icon, | ||
| TextArea, | ||
| InputGroup, | ||
| Menu, | ||
| MenuItem, | ||
| Popover, | ||
| PopoverPosition, | ||
| } from "@blueprintjs/core"; | ||
| import fuzzy from "fuzzy"; | ||
| import { Result } from "~/utils/types"; | ||
|
|
||
| type FuzzySelectInputProps<T extends Result = Result> = { | ||
| value?: T; | ||
| setValue: (q: T) => void; | ||
| onLockedChange?: (isLocked: boolean) => void; | ||
| mode: "create" | "edit"; | ||
| initialUid: string; | ||
| options?: T[]; | ||
| placeholder?: string; | ||
| autoFocus?: boolean; | ||
| disabled?: boolean; | ||
| }; | ||
|
|
||
| const FuzzySelectInput = <T extends Result = Result>({ | ||
| value, | ||
| setValue, | ||
| onLockedChange, | ||
| mode, | ||
| initialUid, | ||
| options = [], | ||
| placeholder = "Enter value", | ||
| autoFocus, | ||
| disabled, | ||
| }: FuzzySelectInputProps<T>) => { | ||
| const [isLocked, setIsLocked] = useState(false); | ||
| const [lockedValue, setLockedValue] = useState<T | undefined>(undefined); | ||
| const [query, setQuery] = useState<string>(() => value?.text || ""); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [activeIndex, setActiveIndex] = useState(0); | ||
| const [isFocused, setIsFocused] = useState(false); | ||
|
|
||
| const menuRef = useRef<HTMLUListElement>(null); | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| // Fuzzy filter options | ||
| const filteredItems = useMemo(() => { | ||
| if (!query) return options; | ||
| return fuzzy | ||
| .filter(query, options, { extract: (item) => item.text }) | ||
| .map((result) => result.original); | ||
| }, [query, options]); | ||
|
|
||
| // Handle option selection | ||
| const handleSelect = useCallback( | ||
| (item: T) => { | ||
| if (mode === "create" && item.uid && item.uid !== initialUid) { | ||
| // Lock the value | ||
| setLockedValue(item); | ||
| setIsLocked(true); | ||
| setQuery(item.text); | ||
| setValue(item); | ||
| setIsOpen(false); | ||
| onLockedChange?.(true); | ||
| } else { | ||
| // Just update the value | ||
| setQuery(item.text); | ||
| setValue(item); | ||
| setIsOpen(false); | ||
| } | ||
| }, | ||
| [mode, initialUid, setValue, onLockedChange], | ||
| ); | ||
|
|
||
| // Handle clear locked value | ||
| const handleClear = useCallback(() => { | ||
| setIsLocked(false); | ||
| setLockedValue(undefined); | ||
| setQuery(""); | ||
| setValue({ text: "", uid: "" } as T); | ||
| onLockedChange?.(false); | ||
| }, [setValue, onLockedChange]); | ||
|
|
||
| // Handle keyboard navigation | ||
| const handleKeyDown = useCallback( | ||
| (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
| if (e.key === "ArrowDown") { | ||
| e.preventDefault(); | ||
| setActiveIndex((prev) => | ||
| prev < filteredItems.length - 1 ? prev + 1 : prev, | ||
| ); | ||
| } else if (e.key === "ArrowUp") { | ||
| e.preventDefault(); | ||
| setActiveIndex((prev) => (prev > 0 ? prev - 1 : 0)); | ||
| } else if (e.key === "Enter") { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| if (isOpen && filteredItems[activeIndex]) { | ||
| handleSelect(filteredItems[activeIndex]); | ||
| } | ||
| } else if (e.key === "Escape") { | ||
| e.preventDefault(); | ||
| setIsOpen(false); | ||
| } | ||
| }, | ||
| [filteredItems, activeIndex, isOpen, handleSelect], | ||
| ); | ||
|
|
||
| // Update value as user types | ||
| useEffect(() => { | ||
| if (mode === "create" && !isLocked) { | ||
| setValue({ text: query, uid: "" } as T); | ||
| } | ||
| }, [query, mode, isLocked, setValue]); | ||
|
|
||
| // Open/close dropdown based on filtered items | ||
| // Only show dropdown if input is focused | ||
| useEffect(() => { | ||
| if (isFocused && filteredItems.length > 0 && query) { | ||
| setIsOpen(true); | ||
| } else { | ||
| setIsOpen(false); | ||
| } | ||
| }, [filteredItems.length, query, isFocused]); | ||
|
|
||
| // Reset active index when filtered items change | ||
| useEffect(() => { | ||
| setActiveIndex(0); | ||
| }, [filteredItems]); | ||
|
|
||
| // Scroll active item into view | ||
| useEffect(() => { | ||
| if (menuRef.current && isOpen) { | ||
| const activeElement = menuRef.current.children[ | ||
| activeIndex | ||
| ] as HTMLElement; | ||
| if (activeElement) { | ||
| activeElement.scrollIntoView({ | ||
| block: "nearest", | ||
| behavior: "smooth", | ||
| }); | ||
| } | ||
| } | ||
| }, [activeIndex, isOpen]); | ||
|
|
||
| // Edit mode: simple TextArea | ||
| if (mode === "edit") { | ||
| return ( | ||
| <TextArea | ||
| value={value?.text || ""} | ||
| onChange={(e) => { | ||
| setValue({ text: e.target.value, uid: value?.uid || "" } as T); | ||
| }} | ||
| fill | ||
| growVertically | ||
| placeholder={placeholder} | ||
| autoFocus={autoFocus} | ||
| disabled={disabled} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| // Create mode: locked value display | ||
| if (isLocked && lockedValue) { | ||
| return ( | ||
| <div className="flex w-full items-center gap-2"> | ||
| <div className="flex flex-1 items-center gap-2 rounded border border-gray-300 bg-gray-100 px-3 py-2 dark:border-gray-600 dark:bg-gray-800"> | ||
| <Icon | ||
| icon="lock" | ||
| iconSize={14} | ||
| className="text-gray-600 dark:text-gray-400" | ||
| /> | ||
| <span className="flex-1 text-gray-900 dark:text-gray-100"> | ||
| {lockedValue.text} | ||
| </span> | ||
| <Button | ||
| icon="cross" | ||
| minimal | ||
| small | ||
| onClick={handleClear} | ||
| className="flex-shrink-0" | ||
| aria-label="Clear selection" | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // Create mode: fuzzy search input | ||
| return ( | ||
| <Popover | ||
| isOpen={isOpen} | ||
| minimal | ||
| autoFocus={false} | ||
| enforceFocus={false} | ||
| position={PopoverPosition.BOTTOM_LEFT} | ||
| modifiers={{ | ||
| flip: { enabled: false }, | ||
| preventOverflow: { enabled: false }, | ||
| }} | ||
| className="fuzzy-select-input-popover w-full" | ||
| content={ | ||
| <Menu className="max-h-64 max-w-md overflow-auto" ulRef={menuRef}> | ||
| {filteredItems.map((item, index) => ( | ||
| <MenuItem | ||
| key={item.uid || index} | ||
| text={item.text} | ||
| active={activeIndex === index} | ||
| onClick={() => handleSelect(item)} | ||
| multiline | ||
| /> | ||
| ))} | ||
| </Menu> | ||
| } | ||
| target={ | ||
| <InputGroup | ||
| fill | ||
| className="w-full" | ||
| disabled={disabled} | ||
| value={query} | ||
| onChange={(e) => setQuery(e.target.value)} | ||
| onKeyDown={handleKeyDown} | ||
| autoFocus={autoFocus} | ||
| placeholder={placeholder} | ||
| inputRef={inputRef} | ||
| onFocus={() => { | ||
| setIsFocused(true); | ||
| }} | ||
| onBlur={() => { | ||
| setIsFocused(false); | ||
| setTimeout(() => setIsOpen(false), 200); | ||
| }} | ||
| /> | ||
| } | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default FuzzySelectInput; | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i left these changes here after trying out the
autoSelectFirstOptionprops because I think we should upgrade roamjs-compoenents anyway