feat(fields): show field type icons in file views and align option colors

This commit is contained in:
yiling
2026-06-12 12:02:56 +08:00
parent 8fb3ba9533
commit eed812247d
6 changed files with 93 additions and 50 deletions
+49 -11
View File
@@ -25,15 +25,50 @@ import { Switch } from '@/ui/components/ui/switch'
import { arrayMove } from '@/ui/lib/dnd-utils'
import { PointerActivationConstraints } from '@dnd-kit/dom'
export const PREDEFINED_COLORS = [
'#ef4444', // Red
'#f97316', // Orange
'#eab308', // Yellow
'#22c55e', // Green
'#3b82f6', // Blue
'#a855f7', // Purple
'#808080', // Gray
]
export const PREDEFINED_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'gray']
export const COLOR_MAP: Record<string, { bg: string; text: string; hex: string }> = {
red: { bg: 'rgba(239, 68, 68, 0.15)', text: '#ef4444', hex: '#ef4444' },
orange: { bg: 'rgba(249, 115, 22, 0.15)', text: '#f97316', hex: '#f97316' },
yellow: { bg: 'rgba(234, 179, 8, 0.15)', text: '#ca8a04', hex: '#eab308' },
green: { bg: 'rgba(34, 197, 94, 0.15)', text: '#16a34a', hex: '#22c55e' },
blue: { bg: 'rgba(59, 130, 246, 0.15)', text: '#2563eb', hex: '#3b82f6' },
purple: { bg: 'rgba(168, 85, 247, 0.15)', text: '#9333ea', hex: '#a855f7' },
gray: { bg: 'rgba(128, 128, 128, 0.15)', text: '#4b5563', hex: '#808080' },
system: { bg: 'rgba(128, 128, 128, 0.1)', text: '#6b7280', hex: '#808080' },
}
export function getOptionStyle(color?: string) {
if (!color) {
return {
backgroundColor: 'rgba(128, 128, 128, 0.1)',
color: '#6b7280',
}
}
const normalized = color.toLowerCase()
const mapped = COLOR_MAP[normalized]
if (mapped) {
return {
backgroundColor: mapped.bg,
color: mapped.text,
}
}
// Support hex colors (e.g. for existing custom fields)
if (color.startsWith('#')) {
return {
backgroundColor: `${color}33`,
color: color,
}
}
// Fallback
return {
backgroundColor: 'rgba(128, 128, 128, 0.1)',
color: color,
}
}
import {
AlignLeft,
Calendar,
@@ -321,7 +356,10 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
<button
type="button"
className="w-6 h-6 rounded-full border shrink-0 cursor-pointer focus:outline-none hover:scale-105 transition-transform"
style={{ backgroundColor: opt.color || '#808080' }}
style={{
backgroundColor:
COLOR_MAP[opt.color || '']?.hex || opt.color || '#808080',
}}
/>
</PopoverTrigger>
<PopoverContent className="p-2 w-auto" align="end">
@@ -331,7 +369,7 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
key={color}
type="button"
className="w-5 h-5 rounded-full border cursor-pointer hover:scale-110 transition-transform"
style={{ backgroundColor: color }}
style={{ backgroundColor: COLOR_MAP[color]?.hex || color }}
onClick={() => {
const updated = [...newOptions]
updated[idx] = { ...opt, color }
@@ -3,6 +3,7 @@ import { ChevronDown, X } from 'lucide-react'
import React, { useState } from 'react'
import type { FieldProps } from './field-types'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
import { getOptionStyle } from '../fields-manager'
const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOnly }) => {
const selectConfig = config?.select
@@ -31,10 +32,7 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
return (
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={{
backgroundColor: `${option.color}33`,
color: option.color || undefined,
}} // 33 is approx 20% opacity hex
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
@@ -76,10 +74,7 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
>
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={{
backgroundColor: `${option.color}33`,
color: option.color || undefined,
}}
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
@@ -3,6 +3,7 @@ import React, { useLayoutEffect, useRef, useState } from 'react'
import type { FieldProps } from './field-types'
import type { SelectOption } from '@shumai/dtos'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
import { getOptionStyle } from '../fields-manager'
const SelectMultiField: React.FC<FieldProps<string[]>> = ({
value = [],
@@ -132,10 +133,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
<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-[22px]"
style={{
backgroundColor: `${option.color}33`,
color: option.color || undefined,
}}
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
@@ -167,10 +165,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
<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-[22px]"
style={{
backgroundColor: `${option.color}33`,
color: option.color || undefined,
}}
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
@@ -187,10 +182,7 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
>
<span
className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium"
style={{
backgroundColor: `${option.color}33`,
color: option.color || undefined,
}}
style={getOptionStyle(option.color)}
>
{option.displayName}
</span>
@@ -23,7 +23,8 @@ import type { DragState } from '../dnd-types'
import FieldRenderer from '../field-renderer'
import { FilePreview } from './file-preview'
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
import { type FieldInfo as MetadataFieldInfo, FieldType } from '@shumai/dtos'
import { FIELD_TYPE_ICONS } from '../fields-manager'
interface FileCardProps {
teamId?: string
@@ -372,21 +373,27 @@ export function FileCard({
</div>
{fields && fields.length > 0 && (
<div className="space-y-2 border-t p-3 mx-2">
{fields.map((field) => (
<div key={field.id} className="space-y-1">
<label className="text-xs text-muted-foreground tracking-wide">
{field.config?.name}
</label>
<div className="min-h-[28px]">
<FieldRenderer
config={field.config}
value={itemFieldValueMap[field.id!]?.value}
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
readOnly={field.readOnly || !canEdit}
/>
{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">
<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>
</label>
<div className="min-h-[28px]">
<FieldRenderer
config={field.config}
value={itemFieldValueMap[field.id!]?.value}
onSave={canEdit ? (val) => onSaveField(field.id!, val) : undefined}
readOnly={field.readOnly || !canEdit}
/>
</div>
</div>
</div>
))}
)
})}
</div>
)}
</div>
@@ -3,6 +3,8 @@
import type { AssetInfo } from '@shumai/dtos'
import type { FieldInfo } from '@shumai/dtos'
import type { SearchSort } from '@shumai/dtos'
import { FieldType } from '@shumai/dtos'
import { FIELD_TYPE_ICONS } from '../fields-manager'
import { useDroppable } from '@dnd-kit/react'
import { flexRender, getCoreRowModel, useReactTable, type ColumnDef } from '@tanstack/react-table'
import { useVirtualizer } from '@tanstack/react-virtual'
@@ -198,9 +200,15 @@ export function FileBrowserListView({
]
displayedFields.forEach((field) => {
const Icon = field.config?.type ? FIELD_TYPE_ICONS[field.config.type as FieldType] : null
cols.push({
id: field.id,
header: field.config?.name || field.id,
header: () => (
<div className="flex items-center gap-1.5 truncate">
{Icon && <Icon className="h-3.5 w-3.5 text-muted-foreground shrink-0" />}
<span className="truncate">{field.config?.name || field.id}</span>
</div>
),
size: 150,
minSize: 100,
})
@@ -25,7 +25,7 @@ import {
import { useMemo, useState, useEffect } from 'react'
import { toast } from 'sonner'
import { Popover, PopoverTrigger, PopoverContent } from '@/ui/components/ui/popover'
import { FIELD_TYPE_ICONS, PREDEFINED_COLORS } from './fields-manager'
import { FIELD_TYPE_ICONS, PREDEFINED_COLORS, COLOR_MAP } from './fields-manager'
import { Button } from '@/ui/components/ui/button'
import { Dialog, DialogContent, DialogTitle } from '@/ui/components/ui/dialog'
@@ -495,7 +495,10 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
<button
type="button"
className="w-6 h-6 rounded-full border shrink-0 cursor-pointer focus:outline-none hover:scale-105 transition-transform"
style={{ backgroundColor: opt.color || '#808080' }}
style={{
backgroundColor:
COLOR_MAP[opt.color || '']?.hex || opt.color || '#808080',
}}
disabled={isReadOnly}
/>
</PopoverTrigger>
@@ -507,7 +510,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
key={color}
type="button"
className="w-5 h-5 rounded-full border cursor-pointer hover:scale-110 transition-transform"
style={{ backgroundColor: color }}
style={{ backgroundColor: COLOR_MAP[color]?.hex || color }}
onClick={() => {
const newOpts = [
...((selectedField.config?.type === 'select'