-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add reusable tag selector for Add/Edit Task dialogs #390
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
Hell1213
wants to merge
4
commits into
CCExtractor:main
Choose a base branch
from
Hell1213:fix/issue-210-reusable-tags
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
4 commits
Select commit
Hold shift + click to select a range
1465795
feat: create TagMultiSelect component for reusable tag selection
Hell1213 eea25ec
feat: integrate TagMultiSelect with AddTaskDialog
Hell1213 49a0ea1
feat: integrate TagMultiSelect with TaskDialog for editing
Hell1213 3affb2a
test: add comprehensive tests for TagMultiSelect component
Hell1213 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
199 changes: 199 additions & 0 deletions
199
frontend/src/components/HomeComponents/Tasks/MultiSelect.tsx
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,199 @@ | ||
| import { useState, useRef, useEffect } from 'react'; | ||
| import { Badge } from '@/components/ui/badge'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Input } from '@/components/ui/input'; | ||
| import { MultiSelectProps } from '@/components/utils/types'; | ||
| import { ChevronDown, Plus, Check, X } from 'lucide-react'; | ||
| import { getFilteredItems, shouldShowCreateOption } from './multi-select-utils'; | ||
|
|
||
| export const MultiSelect = ({ | ||
| availableItems, | ||
| selectedItems, | ||
| onItemsChange, | ||
| placeholder = 'Select or create items', | ||
| disabled = false, | ||
| className = '', | ||
| showActions = false, | ||
| onSave, | ||
| onCancel, | ||
| }: MultiSelectProps) => { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [searchTerm, setSearchTerm] = useState(''); | ||
| const dropdownRef = useRef<HTMLDivElement>(null); | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| const handleClickOutside = (event: MouseEvent) => { | ||
| if ( | ||
| dropdownRef.current && | ||
| !dropdownRef.current.contains(event.target as Node) | ||
| ) { | ||
| setIsOpen(false); | ||
| setSearchTerm(''); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('mousedown', handleClickOutside); | ||
| return () => document.removeEventListener('mousedown', handleClickOutside); | ||
| }, []); | ||
|
|
||
| const filteredItems = getFilteredItems( | ||
| availableItems, | ||
| selectedItems, | ||
| searchTerm | ||
| ); | ||
|
|
||
| const handleItemSelect = (item: string) => { | ||
| if (!selectedItems.includes(item)) { | ||
| onItemsChange([...selectedItems, item]); | ||
| } | ||
| setSearchTerm(''); | ||
| }; | ||
|
|
||
| const handleItemRemove = (itemToRemove: string) => { | ||
| onItemsChange(selectedItems.filter((item) => item !== itemToRemove)); | ||
| }; | ||
|
|
||
| const handleNewItemCreate = () => { | ||
| const trimmedTerm = searchTerm.trim(); | ||
| if ( | ||
| trimmedTerm && | ||
| !selectedItems.includes(trimmedTerm) && | ||
| !availableItems.includes(trimmedTerm) | ||
| ) { | ||
| onItemsChange([...selectedItems, trimmedTerm]); | ||
| setSearchTerm(''); | ||
| } | ||
| }; | ||
|
|
||
| const handleKeyDown = (e: React.KeyboardEvent) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe extract this and other functions to the utils file |
||
| if (e.key === 'Enter') { | ||
| e.preventDefault(); | ||
| if (searchTerm.trim()) { | ||
| if (filteredItems.length > 0) { | ||
| handleItemSelect(filteredItems[0]); | ||
| } else { | ||
| handleNewItemCreate(); | ||
| } | ||
| } | ||
| } else if (e.key === 'Escape') { | ||
| setIsOpen(false); | ||
| setSearchTerm(''); | ||
| } | ||
| }; | ||
|
|
||
| const showCreate = shouldShowCreateOption( | ||
| searchTerm, | ||
| availableItems, | ||
| selectedItems | ||
| ); | ||
|
|
||
| return ( | ||
| <div className={`relative ${className}`} ref={dropdownRef}> | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| onClick={() => setIsOpen(!isOpen)} | ||
| disabled={disabled} | ||
| className="w-full justify-between text-left font-normal" | ||
| aria-label="Select items" | ||
| > | ||
| <span className="truncate"> | ||
| {selectedItems.length > 0 | ||
| ? `${selectedItems.length} item${selectedItems.length > 1 ? 's' : ''} selected` | ||
| : placeholder} | ||
| </span> | ||
| <ChevronDown className="h-4 w-4 opacity-50" /> | ||
| </Button> | ||
|
|
||
| {selectedItems.length > 0 && ( | ||
| <div className="flex flex-wrap items-center gap-2 mt-2"> | ||
| {selectedItems.map((item) => ( | ||
| <Badge key={item} className="flex items-center gap-1"> | ||
| <span>{item}</span> | ||
| <button | ||
| type="button" | ||
| onClick={() => handleItemRemove(item)} | ||
| className="ml-1 text-red-500 hover:text-red-700 text-xs" | ||
| disabled={disabled} | ||
| aria-label={`Remove ${item}`} | ||
| > | ||
| ✖ | ||
| </button> | ||
| </Badge> | ||
| ))} | ||
| {showActions && onSave && onCancel && ( | ||
| <div className="flex items-center gap-1 whitespace-nowrap"> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="icon" | ||
| onClick={onSave} | ||
| className="h-8 w-8" | ||
| aria-label="Save items" | ||
| > | ||
| <Check className="h-4 w-4 text-green-500" /> | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="icon" | ||
| onClick={onCancel} | ||
| className="h-8 w-8" | ||
| aria-label="Cancel" | ||
| > | ||
| <X className="h-4 w-4 text-red-500" /> | ||
| </Button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| )} | ||
|
|
||
| {isOpen && ( | ||
| <div className="absolute z-50 w-full mt-1 bg-background border border-border rounded-md shadow-lg max-h-60 overflow-hidden"> | ||
| <div className="p-2 border-b"> | ||
| <Input | ||
| ref={inputRef} | ||
| value={searchTerm} | ||
| onChange={(e) => setSearchTerm(e.target.value)} | ||
| onKeyDown={handleKeyDown} | ||
| placeholder="Search or create..." | ||
| className="h-8" | ||
| autoFocus | ||
| /> | ||
| </div> | ||
|
|
||
| <div className="max-h-40 overflow-y-auto"> | ||
| {filteredItems.map((item) => ( | ||
| <div | ||
| key={item} | ||
| className="px-3 py-2 cursor-pointer hover:bg-accent transition-colors" | ||
| onClick={() => handleItemSelect(item)} | ||
| > | ||
| <span className="text-sm">{item}</span> | ||
| </div> | ||
| ))} | ||
|
|
||
| {showCreate && ( | ||
| <div | ||
| className="flex items-center gap-2 px-3 py-2 cursor-pointer hover:bg-accent transition-colors border-t" | ||
| onClick={handleNewItemCreate} | ||
| > | ||
| <Plus className="h-4 w-4 text-muted-foreground" /> | ||
| <span className="flex-1 text-sm"> | ||
| Create "{searchTerm.trim()}" | ||
| </span> | ||
| </div> | ||
| )} | ||
|
|
||
| {filteredItems.length === 0 && !showCreate && ( | ||
| <div className="px-3 py-2 text-sm text-muted-foreground text-center"> | ||
| No items found | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.