feat(webui): redesign select metadata fields and improve long text field widget (#305)

* feat(webui): redesign select and multi-select metadata fields with search box and dynamic option creation

* fix(webui): stabilize select popover placement and preserve field visibility in store

* refactor(webui): enforce 2-row collapsed height and whitespace-pre-wrap on LongTextField
This commit is contained in:
Yiling-J
2026-08-13 22:46:40 +08:00
committed by GitHub
parent 9d50301a43
commit 7acf736300
21 changed files with 624 additions and 54 deletions
+131 -22
View File
@@ -1,15 +1,35 @@
import type { SelectOption } from '@shumai/dtos'
import { ChevronDown, X } from 'lucide-react'
import type { FieldInfo, SelectOption } from '@shumai/dtos'
import { ChevronDown, Plus, Search, X } from 'lucide-react'
import React, { useState } from 'react'
import { toast } from 'sonner'
import type { FieldProps } from './field-types'
import { client } from '@/ui/api/client'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
import { getOptionStyle } from '../fields-manager'
import { m } from '@/ui/paraglide/messages.js'
import { useFieldStore } from '@/ui/stores/fields'
import { getOptionStyle, getRandomUnusedColor } from '../fields-manager'
const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOnly }) => {
const SelectField: React.FC<FieldProps<string>> = ({
value,
config,
onSave,
readOnly,
fieldId,
}) => {
const selectConfig = config?.select
const options: SelectOption[] = selectConfig?.options || []
const [isEditing, setIsEditing] = useState(false)
const [searchQuery, setSearchQuery] = useState('')
const selectedOption = selectConfig?.options?.find((opt: SelectOption) => opt.id === value)
const selectedOption = options.find((opt: SelectOption) => opt.id === value)
const trimmedSearch = searchQuery.trim()
const filteredOptions = options.filter((opt: SelectOption) =>
opt.displayName.toLowerCase().includes(trimmedSearch.toLowerCase()),
)
const hasExactMatch = options.some(
(opt: SelectOption) => opt.displayName.trim().toLowerCase() === trimmedSearch.toLowerCase(),
)
const handleStartEdit = () => {
if (!readOnly) {
@@ -20,11 +40,60 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
const handleSelect = (optionId: string) => {
onSave(optionId)
setIsEditing(false)
setSearchQuery('')
}
const handleOpenChange = (open: boolean) => {
if (readOnly) return
setIsEditing(open)
if (!open) {
setSearchQuery('')
}
}
const handleAddOption = async () => {
if (!trimmedSearch) return
const newOption: SelectOption = {
id: trimmedSearch.toLowerCase().replace(/\s+/g, '-'),
displayName: trimmedSearch,
color: getRandomUnusedColor(options),
}
const newOptions = [...options, newOption]
const updatedConfig = {
...config,
select: {
...selectConfig,
options: newOptions,
},
}
if (fieldId) {
try {
const res = await client.api.fields[':fieldId'].$put({
param: { fieldId },
json: { config: updatedConfig },
})
if (!res.ok) {
toast.error(m.failed_update_field())
return
}
const updatedField = (await res.json()) as FieldInfo
const currentFields = useFieldStore.getState().fields
const newFields = currentFields.map((f) =>
f.id === updatedField.id
? { ...f, ...(updatedField as FieldInfo), visible: f.visible }
: f,
)
useFieldStore.getState().updateFields(newFields)
} catch {
toast.error(m.failed_update_field())
return
}
}
onSave(newOption.id)
setSearchQuery('')
setIsEditing(false)
}
const renderOptionBadge = (option: SelectOption | undefined) => {
@@ -57,29 +126,69 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
</PopoverTrigger>
<PopoverContent
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"
side="bottom"
align="start"
sideOffset={4}
className="p-1.5 w-64 max-h-72 flex flex-col bg-popover border border-border rounded-lg shadow-lg"
>
<div
onClick={() => handleSelect('')}
className="px-3 py-2 text-sm text-muted-foreground hover:bg-accent cursor-pointer flex items-center gap-2"
className="relative pb-1.5 border-b border-border mb-1"
onClick={(e) => e.stopPropagation()}
>
<X className="w-3 h-3" /> Clear
<Search className="w-3.5 h-3.5 absolute left-2 top-2 text-muted-foreground" />
<input
type="text"
className="w-full pl-7 pr-2 py-1 text-xs bg-muted/50 border border-input rounded-md focus:outline-none focus:ring-1 focus:ring-ring text-foreground"
placeholder={m.search_options_placeholder()}
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
autoFocus
/>
</div>
{selectConfig?.options?.map((option: SelectOption) => (
<div
key={option.id}
onClick={() => handleSelect(option.id)}
className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center"
>
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={getOptionStyle(option.color)}
<div className="overflow-y-auto max-h-48 min-h-[120px] space-y-0.5">
{searchQuery === '' && (
<div
onClick={() => handleSelect('')}
className="px-2.5 py-1.5 text-xs text-muted-foreground hover:bg-accent hover:text-accent-foreground rounded cursor-pointer flex items-center gap-2"
>
{option.displayName}
</span>
</div>
))}
<X className="w-3.5 h-3.5" /> Clear
</div>
)}
{filteredOptions.map((option: SelectOption) => (
<div
key={option.id}
onClick={() => handleSelect(option.id)}
className={`px-2.5 py-1.5 text-xs hover:bg-accent rounded cursor-pointer flex items-center justify-between ${
value === option.id ? 'bg-accent/50 font-medium' : ''
}`}
>
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
</div>
))}
{filteredOptions.length === 0 && !trimmedSearch && (
<div className="px-2.5 py-2 text-xs text-muted-foreground italic text-center">
{m.no_options_found()}
</div>
)}
{trimmedSearch !== '' && !hasExactMatch && (
<div
onClick={handleAddOption}
className="px-2.5 py-1.5 text-xs text-primary hover:bg-accent rounded cursor-pointer flex items-center gap-1.5 font-medium border-t border-border mt-1 pt-1.5"
>
<Plus className="w-3.5 h-3.5 shrink-0" />
<span className="truncate">{m.add_option_with_name({ name: trimmedSearch })}</span>
</div>
)}
</div>
</PopoverContent>
</Popover>
</div>