feat(webui): use Shadcn Switch in toggle field and Rating component in search filter panel

This commit is contained in:
yiling
2026-06-19 15:49:54 +08:00
parent f645c8fefa
commit 7ae34a4bc0
3 changed files with 33 additions and 29 deletions
@@ -1,3 +1,4 @@
import { Switch } from '@/ui/components/ui/switch'
import type { FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import React from 'react'
@@ -9,32 +10,20 @@ interface FieldProps {
}
const ToggleField: React.FC<FieldProps> = ({ value, onSave, readOnly }) => {
// Toggle is instant edit, doesn't really have a separate "Edit mode" UI structure other than interaction.
const handleToggle = (e: React.MouseEvent) => {
e.stopPropagation()
const handleToggle = (checked: boolean) => {
if (!readOnly) {
const newValue = !value
onSave(newValue)
onSave(checked)
}
}
return (
<div
onClick={handleToggle}
className={`flex items-center h-[28px] w-full ${!readOnly ? 'cursor-pointer' : ''}`}
>
<div
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-primary' : 'bg-input'
} ${readOnly ? 'opacity-70' : ''}`}
>
<span
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'
}`}
style={{ transform: value ? 'translateX(18px)' : 'translateX(4px)' }}
/>
</div>
<div className="flex items-center h-[28px] w-full" onClick={(e) => e.stopPropagation()}>
<Switch
checked={value}
onCheckedChange={handleToggle}
disabled={readOnly}
className="cursor-pointer"
/>
</div>
)
}
@@ -6,11 +6,11 @@ import { Badge } from '@/ui/components/ui/badge'
import { Button } from '@/ui/components/ui/button'
import { Checkbox } from '@/ui/components/ui/checkbox'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/ui/components/ui/dropdown-menu'
import { EditableText } from '@/ui/components/ui/editable-text'
import { Skeleton } from '@/ui/components/ui/skeleton'
@@ -372,13 +372,13 @@ export function FileCard({
</DropdownMenu>
</div>
{fields && fields.length > 0 && (
<div className="space-y-2 border-t p-3 mx-2">
<div className="space-y-2 border-t p-1 pt-3 mx-2">
{fields.map((field) => {
const Icon = field.config?.type
? FIELD_TYPE_ICONS[field.config.type as FieldType]
: null
return (
<div key={field.id} className="space-y-1">
<div key={field.id} className="space-y-1 bg-muted/50 p-1 rounded border">
<label className="text-xs text-muted-foreground tracking-wide flex items-center gap-1">
{Icon && <Icon className="h-3.5 w-3.5 text-muted-foreground shrink-0" />}
<span>{field.config?.name}</span>
@@ -14,6 +14,7 @@ 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'
import RatingField from '../fields/rating-field'
interface FilterPanelProps {
fields: FieldInfo[]
@@ -46,6 +47,7 @@ export function FilterPanel({
label: f.config?.name || f.description || 'Unknown',
type: getFieldType(f),
options: f.config?.select?.options || f.config?.selectMulti?.options,
config: f.config,
})),
].filter((f) => !excludeFields?.includes(f.id))
@@ -189,7 +191,7 @@ function getFieldType(field: FieldInfo): string {
if (field.config?.type === 'selectMulti') return 'selectMulti'
if (field.config?.type === 'number') return 'number'
if (field.config?.type === 'date') return 'date'
if (field.config?.type === 'rating') return 'number'
if (field.config?.type === 'rating') return 'rating'
if (field.config?.type === 'toggle') return 'toggle'
return 'text'
}
@@ -199,6 +201,7 @@ function getDefaultOperator(type: string): SearchConditionOperator {
case 'selectMulti':
return 'hasAny' as SearchConditionOperator
case 'number':
case 'rating':
case 'date':
return 'eq'
default:
@@ -233,6 +236,7 @@ function getOperators(field: { id: string; label: string; type: string }): {
{ value: 'isNotEmpty' as SearchConditionOperator, label: 'is not empty' },
]
case 'number':
case 'rating':
return [
{ value: 'eq' as SearchConditionOperator, label: '=' },
{ value: 'neq' as SearchConditionOperator, label: '≠' },
@@ -291,6 +295,7 @@ interface FilterField {
label: string
type: string
options?: SelectOptionItem[]
config?: FieldInfo['config']
}
function isMultiSelectOperator(fieldType: string, operator: SearchConditionOperator): boolean {
@@ -478,6 +483,16 @@ function ConditionValueInput({
)
}
if (field.type === 'rating') {
return (
<RatingField
value={Number(value) || 0}
config={field.config || { name: field.label, type: 'rating' }}
onSave={onChange}
/>
)
}
if (field.type === 'toggle') {
return (
<Select