|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Dialog, |
| 5 | + DialogContent, |
| 6 | + DialogHeader, |
| 7 | + DialogTitle, |
| 8 | + DialogTrigger, |
| 9 | + Input, |
| 10 | + Badge |
| 11 | +} from '@object-ui/components'; |
| 12 | +import { Search, X } from 'lucide-react'; |
| 13 | +import { FieldWidgetProps } from './types'; |
| 14 | + |
| 15 | +interface LookupOption { |
| 16 | + value: string | number; |
| 17 | + label: string; |
| 18 | + [key: string]: any; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Lookup field for selecting related records |
| 23 | + * Supports single and multi-select with search |
| 24 | + */ |
| 25 | +export function LookupField({ value, onChange, field, readonly }: FieldWidgetProps<any>) { |
| 26 | + const [isOpen, setIsOpen] = useState(false); |
| 27 | + const [searchQuery, setSearchQuery] = useState(''); |
| 28 | + |
| 29 | + const lookupField = field as any; |
| 30 | + const options: LookupOption[] = lookupField.options || []; |
| 31 | + const multiple = lookupField.multiple || false; |
| 32 | + const displayField = lookupField.display_field || 'label'; |
| 33 | + |
| 34 | + // Filter options based on search |
| 35 | + const filteredOptions = options.filter(opt => |
| 36 | + opt.label.toLowerCase().includes(searchQuery.toLowerCase()) |
| 37 | + ); |
| 38 | + |
| 39 | + // Get selected option(s) |
| 40 | + const selectedOptions = multiple |
| 41 | + ? (Array.isArray(value) ? value : []).map(v => |
| 42 | + options.find(opt => opt.value === v) |
| 43 | + ).filter(Boolean) |
| 44 | + : value ? [options.find(opt => opt.value === value)].filter(Boolean) : []; |
| 45 | + |
| 46 | + const handleSelect = (option: LookupOption) => { |
| 47 | + if (multiple) { |
| 48 | + const currentValues = Array.isArray(value) ? value : []; |
| 49 | + const isSelected = currentValues.includes(option.value); |
| 50 | + |
| 51 | + if (isSelected) { |
| 52 | + onChange(currentValues.filter(v => v !== option.value)); |
| 53 | + } else { |
| 54 | + onChange([...currentValues, option.value]); |
| 55 | + } |
| 56 | + } else { |
| 57 | + onChange(option.value); |
| 58 | + setIsOpen(false); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const handleRemove = (optionValue: any) => { |
| 63 | + if (multiple) { |
| 64 | + const currentValues = Array.isArray(value) ? value : []; |
| 65 | + onChange(currentValues.filter(v => v !== optionValue)); |
| 66 | + } else { |
| 67 | + onChange(null); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + if (readonly) { |
| 72 | + if (!selectedOptions.length) { |
| 73 | + return <span className="text-sm">-</span>; |
| 74 | + } |
| 75 | + |
| 76 | + if (multiple) { |
| 77 | + return ( |
| 78 | + <div className="flex flex-wrap gap-1"> |
| 79 | + {selectedOptions.map((opt, idx) => ( |
| 80 | + <Badge key={idx} variant="outline"> |
| 81 | + {opt?.[displayField] || opt?.label} |
| 82 | + </Badge> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <span className="text-sm"> |
| 90 | + {selectedOptions[0]?.[displayField] || selectedOptions[0]?.label} |
| 91 | + </span> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className="space-y-2"> |
| 97 | + {/* Selected values display */} |
| 98 | + {selectedOptions.length > 0 && ( |
| 99 | + <div className="flex flex-wrap gap-1"> |
| 100 | + {selectedOptions.map((opt, idx) => ( |
| 101 | + <Badge |
| 102 | + key={idx} |
| 103 | + variant="outline" |
| 104 | + className="gap-1" |
| 105 | + > |
| 106 | + {opt?.[displayField] || opt?.label} |
| 107 | + <button |
| 108 | + onClick={() => handleRemove(opt?.value)} |
| 109 | + className="ml-1 hover:text-destructive" |
| 110 | + type="button" |
| 111 | + > |
| 112 | + <X className="size-3" /> |
| 113 | + </button> |
| 114 | + </Badge> |
| 115 | + ))} |
| 116 | + </div> |
| 117 | + )} |
| 118 | + |
| 119 | + {/* Lookup dialog trigger */} |
| 120 | + <Dialog open={isOpen} onOpenChange={setIsOpen}> |
| 121 | + <DialogTrigger asChild> |
| 122 | + <Button |
| 123 | + variant="outline" |
| 124 | + className="w-full justify-start text-left font-normal" |
| 125 | + type="button" |
| 126 | + > |
| 127 | + <Search className="mr-2 size-4" /> |
| 128 | + {selectedOptions.length === 0 |
| 129 | + ? field.placeholder || 'Select...' |
| 130 | + : multiple ? `${selectedOptions.length} selected` : 'Change selection' |
| 131 | + } |
| 132 | + </Button> |
| 133 | + </DialogTrigger> |
| 134 | + <DialogContent className="max-w-md"> |
| 135 | + <DialogHeader> |
| 136 | + <DialogTitle> |
| 137 | + {field.label || 'Select'} {multiple && '(multiple)'} |
| 138 | + </DialogTitle> |
| 139 | + </DialogHeader> |
| 140 | + |
| 141 | + {/* Search input */} |
| 142 | + <div className="space-y-4"> |
| 143 | + <Input |
| 144 | + placeholder="Search..." |
| 145 | + value={searchQuery} |
| 146 | + onChange={(e) => setSearchQuery(e.target.value)} |
| 147 | + className="w-full" |
| 148 | + /> |
| 149 | + |
| 150 | + {/* Options list */} |
| 151 | + <div className="max-h-64 overflow-y-auto space-y-1"> |
| 152 | + {filteredOptions.length === 0 ? ( |
| 153 | + <p className="text-sm text-gray-500 text-center py-4"> |
| 154 | + No options found |
| 155 | + </p> |
| 156 | + ) : ( |
| 157 | + filteredOptions.map((option) => { |
| 158 | + const isSelected = multiple |
| 159 | + ? (Array.isArray(value) ? value : []).includes(option.value) |
| 160 | + : value === option.value; |
| 161 | + |
| 162 | + return ( |
| 163 | + <button |
| 164 | + key={option.value} |
| 165 | + onClick={() => handleSelect(option)} |
| 166 | + className={`w-full text-left px-3 py-2 rounded-md text-sm hover:bg-gray-100 flex items-center justify-between ${ |
| 167 | + isSelected ? 'bg-blue-50 text-blue-700' : '' |
| 168 | + }`} |
| 169 | + type="button" |
| 170 | + > |
| 171 | + <span>{option.label}</span> |
| 172 | + {isSelected && ( |
| 173 | + <Badge variant="default" className="ml-2">Selected</Badge> |
| 174 | + )} |
| 175 | + </button> |
| 176 | + ); |
| 177 | + }) |
| 178 | + )} |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </DialogContent> |
| 182 | + </Dialog> |
| 183 | + </div> |
| 184 | + ); |
| 185 | +} |
0 commit comments