mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 02:27:38 +00:00
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:
@@ -12,6 +12,7 @@ import UserField from './fields/user-field'
|
||||
import UserMultiField from './fields/user-multi-field'
|
||||
|
||||
interface FieldRendererProps {
|
||||
fieldId?: string
|
||||
config: FieldInfo['config']
|
||||
value: unknown
|
||||
onSave?: (value: unknown) => void
|
||||
|
||||
@@ -7,4 +7,5 @@ export interface FieldProps<T = unknown> {
|
||||
config: FieldConfig
|
||||
onSave: (value: T) => void
|
||||
readOnly?: boolean
|
||||
fieldId?: string
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import LongTextField from './long-text-field'
|
||||
|
||||
describe('LongTextField Component', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const dummyConfig = {
|
||||
name: 'Description',
|
||||
type: 'longText' as const,
|
||||
}
|
||||
|
||||
it('renders collapsed placeholder with fixed 2-row height and whitespace-pre-wrap', () => {
|
||||
render(<LongTextField value="Line 1\nLine 2\nLine 3" config={dummyConfig} onSave={vi.fn()} />)
|
||||
const collapsed = screen.getByTitle('Click to expand')
|
||||
expect(collapsed).toBeTruthy()
|
||||
expect(collapsed.className).toContain('h-[48px]')
|
||||
expect(collapsed.className).toContain('line-clamp-2')
|
||||
expect(collapsed.className).toContain('whitespace-pre-wrap')
|
||||
})
|
||||
|
||||
it('renders Empty state inside 2-row container when value is empty', () => {
|
||||
render(<LongTextField value="" config={dummyConfig} onSave={vi.fn()} />)
|
||||
const collapsed = screen.getByTitle('Click to expand')
|
||||
expect(collapsed.className).toContain('h-[48px]')
|
||||
expect(screen.getByText('Empty')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('expands on click to show full content overlay', () => {
|
||||
render(<LongTextField value="Line 1\nLine 2\nLine 3" config={dummyConfig} onSave={vi.fn()} />)
|
||||
const collapsed = screen.getByTitle('Click to expand')
|
||||
fireEvent.click(collapsed)
|
||||
|
||||
const elements = screen.getAllByText(/Line 1/)
|
||||
expect(elements.length).toBe(2)
|
||||
})
|
||||
})
|
||||
@@ -73,7 +73,7 @@ const LongTextField: React.FC<FieldProps<string>> = ({ value, onSave, readOnly }
|
||||
{/* Placeholder */}
|
||||
<div
|
||||
onClick={handlePlaceholderClick}
|
||||
className={`w-full text-foreground 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 h-[48px] line-clamp-2 whitespace-pre-wrap overflow-hidden ${
|
||||
!readOnly ? 'cursor-pointer hover:bg-accent hover:border-border' : 'cursor-pointer'
|
||||
}`}
|
||||
title="Click to expand"
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import SelectField from './select-field'
|
||||
|
||||
// Mock client and stores
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
fields: {
|
||||
':fieldId': {
|
||||
$put: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/stores/fields', () => ({
|
||||
useFieldStore: {
|
||||
getState: () => ({
|
||||
fields: [],
|
||||
updateFields: vi.fn(),
|
||||
}),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('SelectField Component', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const sampleConfig = {
|
||||
name: 'Status',
|
||||
type: 'select' as const,
|
||||
select: {
|
||||
options: [
|
||||
{ id: 'foo', displayName: 'Foo', color: '#ff0000' },
|
||||
{ id: 'bar', displayName: 'Bar', color: '#00ff00' },
|
||||
{ id: 'abc', displayName: 'abc', color: '#0000ff' },
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
it('renders selected option badge correctly', () => {
|
||||
render(<SelectField value="foo" config={sampleConfig} onSave={vi.fn()} />)
|
||||
expect(screen.getByText('Foo')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('filters options based on search query', () => {
|
||||
render(<SelectField value="" config={sampleConfig} onSave={vi.fn()} />)
|
||||
|
||||
// Click trigger to open popover
|
||||
const trigger = screen.getByText('Select an option')
|
||||
fireEvent.click(trigger)
|
||||
|
||||
// Search box should be visible
|
||||
const searchInput = screen.getByPlaceholderText('Search options...')
|
||||
expect(searchInput).toBeTruthy()
|
||||
|
||||
// Type "bc" -> should only show "abc" and "Add options: bc"
|
||||
fireEvent.change(searchInput, { target: { value: 'bc' } })
|
||||
expect(screen.getByText('abc')).toBeTruthy()
|
||||
expect(screen.queryByText('Foo')).toBeNull()
|
||||
expect(screen.queryByText('Bar')).toBeNull()
|
||||
|
||||
// Add option action should be present for "bc"
|
||||
expect(screen.getByText('Add options: bc')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('hides add option button when user input matches an option exactly', () => {
|
||||
render(<SelectField value="" config={sampleConfig} onSave={vi.fn()} />)
|
||||
|
||||
const trigger = screen.getByText('Select an option')
|
||||
fireEvent.click(trigger)
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search options...')
|
||||
|
||||
// Type "abc" -> exact match with option "abc"
|
||||
fireEvent.change(searchInput, { target: { value: 'abc' } })
|
||||
expect(screen.getByText('abc')).toBeTruthy()
|
||||
// "Add options: abc" button should be hidden
|
||||
expect(screen.queryByText('Add options: abc')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import SelectMultiField from './select-multi-field'
|
||||
|
||||
// Mock client and stores
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
fields: {
|
||||
':fieldId': {
|
||||
$put: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/stores/fields', () => ({
|
||||
useFieldStore: {
|
||||
getState: () => ({
|
||||
fields: [],
|
||||
updateFields: vi.fn(),
|
||||
}),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('SelectMultiField Component', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const sampleConfig = {
|
||||
name: 'Tags',
|
||||
type: 'selectMulti' as const,
|
||||
selectMulti: {
|
||||
options: [
|
||||
{ id: 'foo', displayName: 'Foo', color: '#ff0000' },
|
||||
{ id: 'bar', displayName: 'Bar', color: '#00ff00' },
|
||||
{ id: 'abc', displayName: 'abc', color: '#0000ff' },
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
it('renders Empty placeholder when no values selected', () => {
|
||||
render(<SelectMultiField value={[]} config={sampleConfig} onSave={vi.fn()} />)
|
||||
expect(screen.getByText('Empty')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('filters options based on search query in MultiSelect', () => {
|
||||
render(<SelectMultiField value={[]} config={sampleConfig} onSave={vi.fn()} />)
|
||||
|
||||
// Click trigger to open popover
|
||||
const trigger = screen.getByText('Empty')
|
||||
fireEvent.click(trigger)
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search options...')
|
||||
expect(searchInput).toBeTruthy()
|
||||
|
||||
// Type "bc" -> should show "abc" and "Add options: bc"
|
||||
fireEvent.change(searchInput, { target: { value: 'bc' } })
|
||||
expect(screen.getByText('abc')).toBeTruthy()
|
||||
expect(screen.queryByText('Foo')).toBeNull()
|
||||
|
||||
expect(screen.getByText('Add options: bc')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('hides add option button when exact match exists in MultiSelect', () => {
|
||||
render(<SelectMultiField value={[]} config={sampleConfig} onSave={vi.fn()} />)
|
||||
|
||||
const trigger = screen.getByText('Empty')
|
||||
fireEvent.click(trigger)
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search options...')
|
||||
|
||||
// Type "abc" -> exact match
|
||||
fireEvent.change(searchInput, { target: { value: 'abc' } })
|
||||
expect(screen.getByText('abc')).toBeTruthy()
|
||||
expect(screen.queryByText('Add options: abc')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -1,30 +1,43 @@
|
||||
import { Check } from 'lucide-react'
|
||||
import type { FieldInfo, SelectOption } from '@shumai/dtos'
|
||||
import { Check, Plus, Search } from 'lucide-react'
|
||||
import React, { useLayoutEffect, useRef, useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import type { FieldProps } from './field-types'
|
||||
import type { SelectOption } from '@shumai/dtos'
|
||||
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 SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
value = [],
|
||||
config,
|
||||
onSave,
|
||||
readOnly,
|
||||
fieldId,
|
||||
}) => {
|
||||
const selectConfig = config?.selectMulti
|
||||
const options: SelectOption[] = selectConfig?.options || []
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const [expanded, setExpanded] = useState(false)
|
||||
const [visibleCount, setVisibleCount] = useState(value.length)
|
||||
const [searchQuery, setSearchQuery] = useState('')
|
||||
|
||||
const selectedOptions =
|
||||
selectConfig?.options?.filter((opt: SelectOption) => (value || []).includes(opt.id)) || []
|
||||
options.filter((opt: SelectOption) => (value || []).includes(opt.id)) || []
|
||||
|
||||
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(),
|
||||
)
|
||||
|
||||
// Dynamic calculation for how many items fit in one line
|
||||
useLayoutEffect(() => {
|
||||
if (isEditing || expanded) {
|
||||
// When editing or expanded, we technically don't use visibleCount for rendering
|
||||
// (we render all), but keeping state clean is good.
|
||||
setVisibleCount(selectedOptions.length)
|
||||
return
|
||||
}
|
||||
@@ -39,12 +52,11 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
let currentWidth = 0
|
||||
let count = 0
|
||||
const moreBadgeWidth = 28
|
||||
// We use a small buffer to be safe
|
||||
const maxAvailableWidth = containerWidth - moreBadgeWidth
|
||||
|
||||
for (let i = 0; i < selectedOptions.length; i++) {
|
||||
const textWidth = context.measureText(selectedOptions[i].displayName).width
|
||||
const badgeWidth = textWidth + 16 + 4 // 16px padding, 4px gap
|
||||
const badgeWidth = textWidth + 16 + 4
|
||||
|
||||
if (currentWidth + badgeWidth > maxAvailableWidth && i < selectedOptions.length - 1) {
|
||||
break
|
||||
@@ -75,16 +87,58 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
onSave(newVal)
|
||||
}
|
||||
|
||||
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,
|
||||
selectMulti: {
|
||||
...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([...(value || []), newOption.id])
|
||||
setSearchQuery('')
|
||||
}
|
||||
|
||||
const handlePlaceholderClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
if (isEditing) return
|
||||
|
||||
// "if there is hidden items, first click will expand"
|
||||
const hiddenCount = selectedOptions.length - visibleCount
|
||||
if (!expanded && hiddenCount > 0) {
|
||||
setExpanded(true)
|
||||
} else {
|
||||
// "if there is no hidden items, will change to edit mode"
|
||||
if (!readOnly) {
|
||||
setIsEditing(true)
|
||||
}
|
||||
@@ -93,7 +147,6 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
|
||||
const handleOverlayClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
// "second click (on expanded) will change to edit mode"
|
||||
if (!readOnly) {
|
||||
setIsEditing(true)
|
||||
}
|
||||
@@ -107,6 +160,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
if (!open) {
|
||||
setExpanded(false)
|
||||
setIsEditing(false)
|
||||
setSearchQuery('')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,12 +202,14 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
</PopoverTrigger>
|
||||
|
||||
<PopoverContent
|
||||
className={`p-1 bg-popover border rounded-lg shadow-xl max-h-60 overflow-auto ${
|
||||
side="bottom"
|
||||
align="start"
|
||||
sideOffset={4}
|
||||
className={`p-1.5 bg-popover border rounded-lg shadow-xl max-h-72 flex flex-col ${
|
||||
expanded && !isEditing
|
||||
? 'w-[--radix-popover-trigger-width] min-w-[200px] flex flex-wrap gap-1 border-ring 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 overflow-auto'
|
||||
: 'w-64 border-border'
|
||||
}`}
|
||||
align="start"
|
||||
onClick={expanded && !isEditing ? handleOverlayClick : undefined}
|
||||
>
|
||||
{expanded && !isEditing ? (
|
||||
@@ -172,24 +228,63 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
|
||||
))}
|
||||
</>
|
||||
) : (
|
||||
selectConfig?.options?.map((option: SelectOption) => {
|
||||
const isSelected = (value || []).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"
|
||||
>
|
||||
<span
|
||||
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
|
||||
style={getOptionStyle(option.color)}
|
||||
<>
|
||||
<div
|
||||
className="relative pb-1.5 border-b border-border mb-1"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<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>
|
||||
|
||||
<div className="overflow-y-auto max-h-48 min-h-[120px] space-y-0.5">
|
||||
{filteredOptions.map((option: SelectOption) => {
|
||||
const isSelected = (value || []).includes(option.id)
|
||||
return (
|
||||
<div
|
||||
key={option.id}
|
||||
onClick={() => toggleOption(option.id)}
|
||||
className={`px-2.5 py-1.5 hover:bg-accent rounded cursor-pointer flex items-center justify-between text-xs ${
|
||||
isSelected ? 'bg-accent/30 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>
|
||||
{isSelected && <Check className="w-3.5 h-3.5 text-primary" />}
|
||||
</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"
|
||||
>
|
||||
{option.displayName}
|
||||
</span>
|
||||
{isSelected && <Check className="w-4 h-4 text-primary" />}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
<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>
|
||||
|
||||
@@ -419,6 +419,7 @@ export function FileCard({
|
||||
</label>
|
||||
<div className="min-h-[28px]">
|
||||
<FieldRenderer
|
||||
fieldId={field.id}
|
||||
config={field.config}
|
||||
value={itemFieldValueMap[field.id!]?.value}
|
||||
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
|
||||
|
||||
@@ -303,6 +303,7 @@ export function FileListItem({
|
||||
}}
|
||||
>
|
||||
<FieldRenderer
|
||||
fieldId={field.id}
|
||||
config={field.config}
|
||||
value={itemFieldValueMap[field.id!]?.value}
|
||||
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
|
||||
|
||||
@@ -449,6 +449,7 @@ export function FileViewerRightSidebar({
|
||||
</label>
|
||||
<div className="min-h-[28px]">
|
||||
<FieldRenderer
|
||||
fieldId={field.id}
|
||||
config={field.config}
|
||||
value={itemFieldValueMap[field.id!]?.value}
|
||||
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
|
||||
|
||||
@@ -731,6 +731,9 @@
|
||||
"visible_count": "({count} visible)",
|
||||
"option_name_placeholder": "Option name",
|
||||
"unknown_field_type": "Unknown Field Type",
|
||||
"search_options_placeholder": "Search options...",
|
||||
"add_option_with_name": "Add options: {name}",
|
||||
"no_options_found": "No options found",
|
||||
|
||||
"settings_updated": "Settings updated",
|
||||
"failed_update_settings": "Failed to update settings",
|
||||
|
||||
@@ -731,6 +731,9 @@
|
||||
"visible_count": "({count} 个可见)",
|
||||
"option_name_placeholder": "选项名称",
|
||||
"unknown_field_type": "未知字段类型",
|
||||
"search_options_placeholder": "搜索选项...",
|
||||
"add_option_with_name": "添加选项:{name}",
|
||||
"no_options_found": "未找到选项",
|
||||
|
||||
"settings_updated": "设置已更新",
|
||||
"failed_update_settings": "更新设置失败",
|
||||
|
||||
+3
@@ -692,6 +692,9 @@ export * from "./search_fields_placeholder.js";
|
||||
export * from "./visible_count.js";
|
||||
export * from "./option_name_placeholder.js";
|
||||
export * from "./unknown_field_type.js";
|
||||
export * from "./search_options_placeholder.js";
|
||||
export * from "./add_option_with_name.js";
|
||||
export * from "./no_options_found.js";
|
||||
export * from "./settings_updated.js";
|
||||
export * from "./failed_update_settings.js";
|
||||
export * from "./unknown_user.js";
|
||||
|
||||
+3
@@ -694,6 +694,9 @@ export * from './search_fields_placeholder.js'
|
||||
export * from './visible_count.js'
|
||||
export * from './option_name_placeholder.js'
|
||||
export * from './unknown_field_type.js'
|
||||
export * from './search_options_placeholder.js'
|
||||
export * from './add_option_with_name.js'
|
||||
export * from './no_options_found.js'
|
||||
export * from './settings_updated.js'
|
||||
export * from './failed_update_settings.js'
|
||||
export * from './unknown_user.js'
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Add options: {name}" |
|
||||
*
|
||||
* @param {Add_Option_With_NameInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const add_option_with_name: ((inputs: Add_Option_With_NameInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Add_Option_With_NameInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type Add_Option_With_NameInputs = {
|
||||
name: NonNullable<unknown>;
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{ name: NonNullable<unknown> }} Add_Option_With_NameInputs */
|
||||
|
||||
const en_add_option_with_name = /** @type {(inputs: Add_Option_With_NameInputs) => LocalizedString} */ (i) => {
|
||||
return /** @type {LocalizedString} */ (`Add options: ${i?.name}`)
|
||||
};
|
||||
|
||||
const zh_add_option_with_name = /** @type {(inputs: Add_Option_With_NameInputs) => LocalizedString} */ (i) => {
|
||||
return /** @type {LocalizedString} */ (`添加选项:${i?.name}`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Add options: {name}" |
|
||||
*
|
||||
* @param {Add_Option_With_NameInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const add_option_with_name = /** @type {((inputs: Add_Option_With_NameInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Add_Option_With_NameInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_add_option_with_name(inputs)
|
||||
return zh_add_option_with_name(inputs)
|
||||
});
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "No options found" |
|
||||
*
|
||||
* @param {No_Options_FoundInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const no_options_found: ((inputs?: No_Options_FoundInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<No_Options_FoundInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type No_Options_FoundInputs = {};
|
||||
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{}} No_Options_FoundInputs */
|
||||
|
||||
const en_no_options_found = /** @type {(inputs: No_Options_FoundInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`No options found`)
|
||||
};
|
||||
|
||||
const zh_no_options_found = /** @type {(inputs: No_Options_FoundInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`未找到选项`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "No options found" |
|
||||
*
|
||||
* @param {No_Options_FoundInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const no_options_found = /** @type {((inputs?: No_Options_FoundInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<No_Options_FoundInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_no_options_found(inputs)
|
||||
return zh_no_options_found(inputs)
|
||||
});
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Search options..." |
|
||||
*
|
||||
* @param {Search_Options_PlaceholderInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const search_options_placeholder: ((inputs?: Search_Options_PlaceholderInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Search_Options_PlaceholderInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type Search_Options_PlaceholderInputs = {};
|
||||
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{}} Search_Options_PlaceholderInputs */
|
||||
|
||||
const en_search_options_placeholder = /** @type {(inputs: Search_Options_PlaceholderInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`Search options...`)
|
||||
};
|
||||
|
||||
const zh_search_options_placeholder = /** @type {(inputs: Search_Options_PlaceholderInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`搜索选项...`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Search options..." |
|
||||
*
|
||||
* @param {Search_Options_PlaceholderInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const search_options_placeholder = /** @type {((inputs?: Search_Options_PlaceholderInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Search_Options_PlaceholderInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_search_options_placeholder(inputs)
|
||||
return zh_search_options_placeholder(inputs)
|
||||
});
|
||||
Reference in New Issue
Block a user