Merge pull request #130 from shumaiOne/fix/field-colors-and-filter-selects

fix(webui): use system colors for fields and improve search filter select options
This commit is contained in:
Yiling-J
2026-06-18 23:27:48 +08:00
committed by GitHub
9 changed files with 202 additions and 96 deletions
+22 -16
View File
@@ -119,31 +119,35 @@ const DateField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOn
if (isEditing) { if (isEditing) {
return ( return (
<div className="relative w-full" ref={containerRef}> <div className="relative w-full" ref={containerRef}>
<div className="w-full px-2 py-1 border border-blue-500 rounded bg-white text-sm shadow-sm min-h-[28px] flex items-center"> <div className="w-full px-2 py-1 border border-ring rounded bg-background text-sm shadow-sm min-h-[28px] flex items-center">
{value ? getDisplayValue(value) : <span className="text-gray-400">Select date...</span>} {value ? (
getDisplayValue(value)
) : (
<span className="text-muted-foreground">Select date...</span>
)}
</div> </div>
<div className="absolute z-50 top-full left-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-xl flex flex-col md:flex-row overflow-hidden min-w-[280px]"> <div className="absolute z-50 top-full left-0 mt-1 bg-popover border border-border rounded-lg shadow-xl flex flex-col md:flex-row overflow-hidden min-w-[280px]">
{/* Date Picker */} {/* Date Picker */}
<div className="p-4 w-64"> <div className="p-4 w-64">
<div className="flex justify-between items-center mb-4"> <div className="flex justify-between items-center mb-4">
{/* Replaced subMonths with addMonths(-1) */} {/* Replaced subMonths with addMonths(-1) */}
<button <button
onClick={() => setViewDate(addMonths(viewDate, -1))} onClick={() => setViewDate(addMonths(viewDate, -1))}
className="p-1 hover:bg-gray-100 rounded" className="p-1 hover:bg-accent rounded"
> >
<ChevronLeft className="w-4 h-4" /> <ChevronLeft className="w-4 h-4" />
</button> </button>
<span className="font-semibold text-sm">{format(viewDate, 'MMMM yyyy')}</span> <span className="font-semibold text-sm">{format(viewDate, 'MMMM yyyy')}</span>
<button <button
onClick={() => setViewDate(addMonths(viewDate, 1))} onClick={() => setViewDate(addMonths(viewDate, 1))}
className="p-1 hover:bg-gray-100 rounded" className="p-1 hover:bg-accent rounded"
> >
<ChevronRight className="w-4 h-4" /> <ChevronRight className="w-4 h-4" />
</button> </button>
</div> </div>
<div className="grid grid-cols-7 gap-1 text-center text-xs mb-2 text-gray-500"> <div className="grid grid-cols-7 gap-1 text-center text-xs mb-2 text-muted-foreground">
{['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'].map((d) => ( {['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'].map((d) => (
<div key={d}>{d}</div> <div key={d}>{d}</div>
))} ))}
@@ -166,8 +170,10 @@ const DateField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOn
<button <button
key={date.toString()} key={date.toString()}
onClick={() => handleDateClick(date)} onClick={() => handleDateClick(date)}
className={`w-8 h-8 rounded-full flex items-center justify-center hover:bg-blue-50 transition-colors ${ className={`w-8 h-8 rounded-full flex items-center justify-center hover:bg-accent transition-colors ${
isSelected ? 'bg-blue-50 text-white hover:bg-blue-600' : 'text-gray-700' isSelected
? 'bg-primary text-primary-foreground hover:bg-primary/90'
: 'text-foreground'
}`} }`}
> >
{getDate(date)} {getDate(date)}
@@ -179,17 +185,17 @@ const DateField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOn
{/* Time Picker */} {/* Time Picker */}
{dateConfig?.includeTime && ( {dateConfig?.includeTime && (
<div className="border-l border-gray-200 bg-gray-50 w-32 flex flex-col"> <div className="border-l border-border bg-muted w-32 flex flex-col">
<div className="p-2 border-b border-gray-200 text-xs font-semibold text-gray-500 flex items-center justify-center gap-1"> <div className="p-2 border-b border-border text-xs font-semibold text-muted-foreground flex items-center justify-center gap-1">
<Clock className="w-3 h-3" /> Time <Clock className="w-3 h-3" /> Time
</div> </div>
<div className="flex flex-1 h-64"> <div className="flex flex-1 h-64">
<div className="flex-1 overflow-y-auto scrollbar-hide border-r border-gray-200"> <div className="flex-1 overflow-y-auto scrollbar-hide border-r border-border">
{hours.map((h) => ( {hours.map((h) => (
<div <div
key={h} key={h}
onClick={() => handleTimeChange('hour', h)} onClick={() => handleTimeChange('hour', h)}
className={`px-2 py-1 text-center text-sm cursor-pointer hover:bg-gray-200 ${getHours(safeDate) === h ? 'bg-blue-100 font-medium text-blue-700' : ''}`} className={`px-2 py-1 text-center text-sm cursor-pointer hover:bg-accent ${getHours(safeDate) === h ? 'bg-primary/10 font-medium text-primary' : ''}`}
> >
{h.toString().padStart(2, '0')} {h.toString().padStart(2, '0')}
</div> </div>
@@ -200,7 +206,7 @@ const DateField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOn
<div <div
key={m} key={m}
onClick={() => handleTimeChange('minute', m)} onClick={() => handleTimeChange('minute', m)}
className={`px-2 py-1 text-center text-sm cursor-pointer hover:bg-gray-200 ${getMinutes(safeDate) === m ? 'bg-blue-100 font-medium text-blue-700' : ''}`} className={`px-2 py-1 text-center text-sm cursor-pointer hover:bg-accent ${getMinutes(safeDate) === m ? 'bg-primary/10 font-medium text-primary' : ''}`}
> >
{m.toString().padStart(2, '0')} {m.toString().padStart(2, '0')}
</div> </div>
@@ -217,11 +223,11 @@ const DateField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOn
return ( return (
<div <div
onClick={handleStartEdit} onClick={handleStartEdit}
className={`w-full min-h-[28px] px-2 py-1 text-sm text-gray-800 rounded border border-transparent transition-colors ${ className={`w-full min-h-[28px] px-2 py-1 text-sm text-foreground rounded border border-transparent transition-colors ${
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200' : '' !readOnly ? 'cursor-pointer hover:bg-accent hover:border-border' : ''
}`} }`}
> >
{value ? getDisplayValue(value) : <span className="text-gray-400 italic">Empty</span>} {value ? getDisplayValue(value) : <span className="text-muted-foreground italic">Empty</span>}
</div> </div>
) )
} }
@@ -63,7 +63,7 @@ const LongTextField: React.FC<FieldProps<string>> = ({ value, onSave, readOnly }
value={localValue} value={localValue}
onChange={(e) => setLocalValue(e.target.value)} onChange={(e) => setLocalValue(e.target.value)}
onBlur={handleBlur} onBlur={handleBlur}
className="w-full min-h-[80px] p-2 border border-blue-500 rounded bg-white text-sm outline-none shadow-sm resize-none" className="w-full min-h-[80px] p-2 border border-ring rounded bg-background text-sm outline-none shadow-sm resize-none"
/> />
) )
} }
@@ -73,23 +73,23 @@ const LongTextField: React.FC<FieldProps<string>> = ({ value, onSave, readOnly }
{/* Placeholder */} {/* Placeholder */}
<div <div
onClick={handlePlaceholderClick} onClick={handlePlaceholderClick}
className={`w-full text-gray-800 rounded border border-transparent transition-all px-2 py-1 line-clamp-2 max-h-[3rem] overflow-hidden ${ className={`w-full text-foreground rounded border border-transparent transition-all px-2 py-1 line-clamp-2 max-h-[3rem] overflow-hidden ${
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200' : 'cursor-pointer' !readOnly ? 'cursor-pointer hover:bg-accent hover:border-border' : 'cursor-pointer'
}`} }`}
title="Click to expand" title="Click to expand"
> >
{value ? value : <span className="text-gray-400 italic">Empty</span>} {value ? value : <span className="text-muted-foreground italic">Empty</span>}
</div> </div>
{/* Expanded Overlay */} {/* Expanded Overlay */}
{expanded && ( {expanded && (
<div <div
onClick={handleOverlayClick} onClick={handleOverlayClick}
className={`absolute top-0 left-0 w-full min-h-full h-auto z-50 bg-white border border-blue-500 rounded shadow-lg px-2 py-1 whitespace-pre-wrap ${ className={`absolute top-0 left-0 w-full min-h-full h-auto z-50 bg-background border border-ring rounded shadow-lg px-2 py-1 whitespace-pre-wrap ${
!readOnly ? 'cursor-text' : '' !readOnly ? 'cursor-text' : ''
}`} }`}
> >
{value ? value : <span className="text-gray-400 italic">Empty</span>} {value ? value : <span className="text-muted-foreground italic">Empty</span>}
</div> </div>
)} )}
</div> </div>
@@ -62,7 +62,7 @@ const NumberField: React.FC<FieldProps<number | undefined>> = ({
onBlur={handleBlur} onBlur={handleBlur}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onDoubleClick={(e) => e.stopPropagation()} onDoubleClick={(e) => e.stopPropagation()}
className="w-full px-2 py-1 border border-blue-500 rounded bg-white text-sm outline-none shadow-sm text-left font-mono" className="w-full px-2 py-1 border border-ring rounded bg-background text-sm outline-none shadow-sm text-left font-mono"
/> />
) )
} }
@@ -71,14 +71,14 @@ const NumberField: React.FC<FieldProps<number | undefined>> = ({
<div <div
onClick={handleStartEdit} onClick={handleStartEdit}
onDoubleClick={(e) => e.stopPropagation()} onDoubleClick={(e) => e.stopPropagation()}
className={`w-full min-h-[28px] px-2 py-1 text-sm text-gray-800 rounded border border-transparent transition-colors text-left font-mono ${ className={`w-full min-h-[28px] px-2 py-1 text-sm text-foreground rounded border border-transparent transition-colors text-left font-mono ${
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200' : '' !readOnly ? 'cursor-pointer hover:bg-accent hover:border-border' : ''
}`} }`}
> >
{value !== undefined && value !== null ? ( {value !== undefined && value !== null ? (
formatNumber(value) formatNumber(value)
) : ( ) : (
<span className="text-gray-400 italic font-sans text-left block">Empty</span> <span className="text-muted-foreground italic font-sans text-left block">Empty</span>
)} )}
</div> </div>
) )
@@ -43,7 +43,7 @@ const RatingField: React.FC<FieldProps<number>> = ({ value, config, onSave, read
> >
<Star <Star
className={`w-5 h-5 transition-colors ${ className={`w-5 h-5 transition-colors ${
isFilled ? 'fill-yellow-400 text-yellow-400' : 'fill-gray-100 text-gray-300' isFilled ? 'fill-yellow-400 text-yellow-400' : 'fill-muted text-muted-foreground/30'
}`} }`}
/> />
</button> </button>
@@ -28,7 +28,7 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
} }
const renderOptionBadge = (option: SelectOption | undefined) => { const renderOptionBadge = (option: SelectOption | undefined) => {
if (!option) return <span className="text-gray-400 italic">Select an option</span> if (!option) return <span className="text-muted-foreground italic">Select an option</span>
return ( return (
<span <span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium" className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
@@ -46,23 +46,23 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
<div <div
onClick={handleStartEdit} onClick={handleStartEdit}
className={`flex items-center justify-between w-full min-h-[28px] px-2 py-1 rounded border border-transparent transition-colors ${ className={`flex items-center justify-between w-full min-h-[28px] px-2 py-1 rounded border border-transparent transition-colors ${
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200 group' : '' !readOnly ? 'cursor-pointer hover:bg-accent hover:border-border group' : ''
}`} }`}
> >
{renderOptionBadge(selectedOption)} {renderOptionBadge(selectedOption)}
{!readOnly && ( {!readOnly && (
<ChevronDown className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100" /> <ChevronDown className="w-4 h-4 text-muted-foreground opacity-0 group-hover:opacity-100" />
)} )}
</div> </div>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent <PopoverContent
className="p-1 w-[--radix-popover-trigger-width] min-w-[150px] max-h-60 overflow-auto bg-white border border-gray-200 rounded-lg shadow-lg" className="p-1 w-[--radix-popover-trigger-width] min-w-[150px] max-h-60 overflow-auto bg-popover border border-border rounded-lg shadow-lg"
align="start" align="start"
> >
<div <div
onClick={() => handleSelect('')} onClick={() => handleSelect('')}
className="px-3 py-2 text-sm text-gray-500 hover:bg-gray-50 cursor-pointer flex items-center gap-2" className="px-3 py-2 text-sm text-muted-foreground hover:bg-accent cursor-pointer flex items-center gap-2"
> >
<X className="w-3 h-3" /> Clear <X className="w-3 h-3" /> Clear
</div> </div>
@@ -70,7 +70,7 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
<div <div
key={option.id} key={option.id}
onClick={() => handleSelect(option.id)} onClick={() => handleSelect(option.id)}
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center" className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center"
> >
<span <span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium" className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
@@ -120,12 +120,12 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
onClick={handlePlaceholderClick} onClick={handlePlaceholderClick}
className={`w-full px-1 py-1 flex flex-wrap gap-1 rounded border border-transparent transition-all box-border h-[32px] overflow-hidden ${ className={`w-full px-1 py-1 flex flex-wrap gap-1 rounded border border-transparent transition-all box-border h-[32px] overflow-hidden ${
!readOnly || hiddenCount > 0 !readOnly || hiddenCount > 0
? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200' ? 'cursor-pointer hover:bg-accent hover:border-border'
: '' : ''
}`} }`}
> >
{selectedOptions.length === 0 && ( {selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span> <span className="text-muted-foreground text-sm italic px-1 pt-0.5">Empty</span>
)} )}
{/* Render visible items */} {/* Render visible items */}
@@ -140,7 +140,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
))} ))}
{hiddenCount > 0 && ( {hiddenCount > 0 && (
<span className="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-gray-200 text-gray-600 h-[22px]"> <span className="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-muted text-muted-foreground h-[22px]">
+{hiddenCount} +{hiddenCount}
</span> </span>
)} )}
@@ -148,10 +148,10 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
</PopoverTrigger> </PopoverTrigger>
<PopoverContent <PopoverContent
className={`p-1 bg-white border rounded-lg shadow-xl max-h-60 overflow-auto ${ className={`p-1 bg-popover border rounded-lg shadow-xl max-h-60 overflow-auto ${
expanded && !isEditing expanded && !isEditing
? 'w-[--radix-popover-trigger-width] min-w-[200px] flex flex-wrap gap-1 border-blue-500 min-h-[32px] h-auto cursor-pointer' ? 'w-[--radix-popover-trigger-width] min-w-[200px] flex flex-wrap gap-1 border-ring min-h-[32px] h-auto cursor-pointer'
: 'w-64 border-gray-200' : 'w-64 border-border'
}`} }`}
align="start" align="start"
onClick={expanded && !isEditing ? handleOverlayClick : undefined} onClick={expanded && !isEditing ? handleOverlayClick : undefined}
@@ -159,7 +159,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
{expanded && !isEditing ? ( {expanded && !isEditing ? (
<> <>
{selectedOptions.length === 0 && ( {selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span> <span className="text-muted-foreground text-sm italic px-1 pt-0.5">Empty</span>
)} )}
{selectedOptions.map((option: SelectOption) => ( {selectedOptions.map((option: SelectOption) => (
<span <span
@@ -178,7 +178,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
<div <div
key={option.id} key={option.id}
onClick={() => toggleOption(option.id)} onClick={() => toggleOption(option.id)}
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center justify-between" className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center justify-between"
> >
<span <span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium" className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
@@ -186,7 +186,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
> >
{option.displayName} {option.displayName}
</span> </span>
{isSelected && <Check className="w-4 h-4 text-blue-500" />} {isSelected && <Check className="w-4 h-4 text-primary" />}
</div> </div>
) )
}) })
@@ -43,7 +43,7 @@ const TextField: React.FC<FieldProps<string>> = ({ value, onSave, readOnly }) =>
onChange={(e) => setLocalValue(e.target.value)} onChange={(e) => setLocalValue(e.target.value)}
onBlur={handleBlur} onBlur={handleBlur}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
className="w-full px-2 py-1 border border-blue-500 rounded bg-white text-sm outline-none shadow-sm" className="w-full px-2 py-1 border border-ring rounded bg-background text-sm outline-none shadow-sm"
/> />
) )
} }
@@ -51,11 +51,11 @@ const TextField: React.FC<FieldProps<string>> = ({ value, onSave, readOnly }) =>
return ( return (
<div <div
onClick={handleStartEdit} onClick={handleStartEdit}
className={`w-full min-h-[28px] px-2 py-1 text-sm text-gray-800 rounded truncate border border-transparent transition-colors ${ className={`w-full min-h-[28px] px-2 py-1 text-sm text-foreground rounded truncate border border-transparent transition-colors ${
!readOnly ? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200' : '' !readOnly ? 'cursor-pointer hover:bg-accent hover:border-border' : ''
}`} }`}
> >
{value || <span className="text-gray-400 italic">Empty</span>} {value || <span className="text-muted-foreground italic">Empty</span>}
</div> </div>
) )
} }
@@ -24,12 +24,12 @@ const ToggleField: React.FC<FieldProps> = ({ value, onSave, readOnly }) => {
className={`flex items-center h-[28px] w-full ${!readOnly ? 'cursor-pointer' : ''}`} className={`flex items-center h-[28px] w-full ${!readOnly ? 'cursor-pointer' : ''}`}
> >
<div <div
className={`relative inline-flex h-5 w-9 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 ${ className={`relative inline-flex h-5 w-9 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 ${
value ? 'bg-green-500' : 'bg-gray-300' value ? 'bg-primary' : 'bg-input'
} ${readOnly ? 'opacity-70' : ''}`} } ${readOnly ? 'opacity-70' : ''}`}
> >
<span <span
className={`inline-block h-3.5 w-3.5 transform rounded-full bg-white transition duration-200 ease-in-out ${ className={`inline-block h-3.5 w-3.5 transform rounded-full bg-background transition duration-200 ease-in-out ${
value ? 'translate-x-4.5' : 'translate-x-1' value ? 'translate-x-4.5' : 'translate-x-1'
}`} }`}
style={{ transform: value ? 'translateX(18px)' : 'translateX(4px)' }} style={{ transform: value ? 'translateX(18px)' : 'translateX(4px)' }}
+147 -47
View File
@@ -1,8 +1,7 @@
import type { SearchCondition, SearchConditionOperator } from '@shumai/dtos'
import { type FieldInfo } from '@shumai/dtos'
import { Button } from '@/ui/components/ui/button' import { Button } from '@/ui/components/ui/button'
import { Input } from '@/ui/components/ui/input'
import { DebouncedInput } from '@/ui/components/ui/debounced-input' import { DebouncedInput } from '@/ui/components/ui/debounced-input'
import { Input } from '@/ui/components/ui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
import { import {
Select, Select,
SelectContent, SelectContent,
@@ -11,7 +10,10 @@ import {
SelectValue, SelectValue,
} from '@/ui/components/ui/select' } from '@/ui/components/ui/select'
import { cn } from '@/ui/lib/utils' import { cn } from '@/ui/lib/utils'
import { Trash2 } from 'lucide-react' import type { SearchCondition, SearchConditionOperator } from '@shumai/dtos'
import { type FieldInfo } from '@shumai/dtos'
import { Check, ChevronDown, Trash2 } from 'lucide-react'
import { getOptionStyle } from '../fields-manager'
interface FilterPanelProps { interface FilterPanelProps {
fields: FieldInfo[] fields: FieldInfo[]
@@ -43,7 +45,7 @@ export function FilterPanel({
id: f.id!, id: f.id!,
label: f.config?.name || f.description || 'Unknown', label: f.config?.name || f.description || 'Unknown',
type: getFieldType(f), type: getFieldType(f),
options: f.config?.select?.options, options: f.config?.select?.options || f.config?.selectMulti?.options,
})), })),
].filter((f) => !excludeFields?.includes(f.id)) ].filter((f) => !excludeFields?.includes(f.id))
@@ -77,6 +79,29 @@ export function FilterPanel({
} }
} }
// Convert value format when switching between single/multi-select operators
if (key === 'operator') {
const field = allFields.find((f) => f.id === newConditions[index].field)
if (field && (field.type === 'select' || field.type === 'selectMulti')) {
const oldOperator = conditions[index].operator
const newOperator = value as SearchConditionOperator
const wasMulti = isMultiSelectOperator(field.type, oldOperator)
const isMulti = isMultiSelectOperator(field.type, newOperator)
if (wasMulti && !isMulti) {
// Multi -> Single: take first item or empty string
const arr = Array.isArray(newConditions[index].value)
? (newConditions[index].value as string[])
: []
newConditions[index].value = arr[0] || ''
} else if (!wasMulti && isMulti) {
// Single -> Multi: wrap in array if non-empty
const str = newConditions[index].value as string
newConditions[index].value = str ? [str] : []
}
}
}
onChange(newConditions) onChange(newConditions)
} }
@@ -132,6 +157,7 @@ export function FilterPanel({
{condition.operator !== 'isEmpty' && condition.operator !== 'isNotEmpty' && ( {condition.operator !== 'isEmpty' && condition.operator !== 'isNotEmpty' && (
<ConditionValueInput <ConditionValueInput
field={allFields.find((f) => f.id === condition.field)} field={allFields.find((f) => f.id === condition.field)}
operator={condition.operator}
value={condition.value} value={condition.value}
onChange={(val) => handleConditionChange(index, 'value', val)} onChange={(val) => handleConditionChange(index, 'value', val)}
/> />
@@ -254,67 +280,141 @@ function getOperators(field: { id: string; label: string; type: string }): {
} }
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any interface SelectOptionItem {
type FieldType = any id: string
displayName: string
color?: string
}
interface FilterField {
id: string
label: string
type: string
options?: SelectOptionItem[]
}
function isMultiSelectOperator(fieldType: string, operator: SearchConditionOperator): boolean {
if (fieldType === 'select') {
return operator === 'in' || operator === 'notIn'
}
if (fieldType === 'selectMulti') {
return operator !== 'eq'
}
return false
}
function MultiSelectValueInput({
options,
selected,
onChange,
}: {
options: SelectOptionItem[]
selected: string[]
onChange: (val: string[]) => void
}) {
const selectedOptions = options.filter((opt) => selected.includes(opt.id))
const toggleOption = (optionId: string) => {
let newVal
if (selected.includes(optionId)) {
newVal = selected.filter((id) => id !== optionId)
} else {
newVal = [...selected, optionId]
}
onChange(newVal)
}
return (
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className="flex min-h-9 w-full items-center justify-between rounded-md border border-input bg-transparent px-3 py-1.5 text-sm shadow-xs hover:bg-accent/50 focus:outline-hidden focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 text-left"
>
<div className="flex flex-wrap gap-1 items-center max-w-[90%]">
{selectedOptions.length === 0 ? (
<span className="text-muted-foreground">Select options...</span>
) : (
selectedOptions.map((option) => (
<span
key={option.id}
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium border border-transparent whitespace-nowrap h-[20px]"
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
))
)}
</div>
<ChevronDown className="h-4 w-4 shrink-0 opacity-50 ml-2" />
</button>
</PopoverTrigger>
<PopoverContent
className="w-64 p-1 bg-popover border border-border rounded-lg shadow-xl max-h-60 overflow-auto"
align="start"
>
{options.map((option) => {
const isSelected = selected.includes(option.id)
return (
<div
key={option.id}
onClick={() => toggleOption(option.id)}
className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center justify-between rounded-sm"
>
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
{isSelected && <Check className="w-4 h-4 text-primary" />}
</div>
)
})}
</PopoverContent>
</Popover>
)
}
function ConditionValueInput({ function ConditionValueInput({
field, field,
operator,
value, value,
onChange, onChange,
}: { }: {
field: FieldType field: FilterField | undefined
operator: SearchConditionOperator
value: unknown value: unknown
onChange: (val: unknown) => void onChange: (val: unknown) => void
}) { }) {
if (!field) return <Input disabled /> if (!field) return <Input disabled />
if (field.type === 'select' && field.options) { if ((field.type === 'select' || field.type === 'selectMulti') && field.options) {
if (isMultiSelectOperator(field.type, operator)) {
const selected = Array.isArray(value) ? (value as string[]) : []
return (
<MultiSelectValueInput
options={field.options}
selected={selected}
onChange={(val) => onChange(val)}
/>
)
}
// Single-select
return ( return (
<Select value={value as string} onValueChange={onChange}> <Select value={value as string} onValueChange={onChange}>
<SelectTrigger> <SelectTrigger>
<SelectValue placeholder="Select option" /> <SelectValue placeholder="Select option" />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
{field.options.map((opt: FieldType) => ( {field.options.map((opt) => (
<SelectItem key={opt.id} value={opt.id}> <SelectItem key={opt.id} value={opt.id}>
{opt.displayName} <span
</SelectItem> className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
))} style={getOptionStyle(opt.color)}
</SelectContent> >
</Select>
)
}
if (field.type === 'selectMulti' && field.options) {
const selected = Array.isArray(value) ? value : []
return (
<Select
value={selected[0] || ''}
onValueChange={(val) => {
if (selected.includes(val)) {
onChange(selected.filter((v) => v !== val))
} else {
onChange([...selected, val])
}
}}
>
<SelectTrigger>
<SelectValue
placeholder={selected.length > 0 ? `${selected.length} selected` : 'Select options'}
/>
</SelectTrigger>
<SelectContent>
{field.options.map((opt: FieldType) => (
<SelectItem key={opt.id} value={opt.id}>
<div className="flex items-center gap-2">
<input
type="checkbox"
checked={selected.includes(opt.id)}
readOnly
className="pointer-events-none"
/>
{opt.displayName} {opt.displayName}
</div> </span>
</SelectItem> </SelectItem>
))} ))}
</SelectContent> </SelectContent>