feat(fields): improve fields popup options, group filter, and dropdown clipping

This commit is contained in:
yiling
2026-06-12 11:11:00 +08:00
parent 1b57c3f877
commit 66f2efd77f
5 changed files with 274 additions and 172 deletions
@@ -1,28 +1,15 @@
import type { SelectOption } from '@shumai/dtos'
import { ChevronDown, X } from 'lucide-react'
import React, { useEffect, useRef, useState } from 'react'
import React, { useState } from 'react'
import type { FieldProps } from './field-types'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, readOnly }) => {
const selectConfig = config?.select
const [isEditing, setIsEditing] = useState(false)
const containerRef = useRef<HTMLDivElement>(null)
const selectedOption = selectConfig?.options?.find((opt: SelectOption) => opt.id === value)
// Click outside handler
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
if (isEditing) {
setIsEditing(false)
}
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [isEditing])
const handleStartEdit = () => {
if (!readOnly) {
setIsEditing(true)
@@ -50,46 +37,53 @@ const SelectField: React.FC<FieldProps<string>> = ({ value, config, onSave, read
}
return (
<div className="relative w-full" ref={containerRef}>
<div
onClick={handleStartEdit}
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' : ''
}`}
>
{renderOptionBadge(selectedOption)}
{!readOnly && (
<ChevronDown className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100" />
)}
</div>
{isEditing && (
<div className="absolute z-50 top-full left-0 mt-1 w-full min-w-[150px] bg-white border border-gray-200 rounded-lg shadow-lg py-1 max-h-60 overflow-auto">
<div className="relative w-full">
<Popover open={isEditing} onOpenChange={setIsEditing}>
<PopoverTrigger asChild>
<div
onClick={() => handleSelect('')}
className="px-3 py-2 text-sm text-gray-500 hover:bg-gray-50 cursor-pointer flex items-center gap-2"
onClick={handleStartEdit}
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' : ''
}`}
>
<X className="w-3 h-3" /> Clear
{renderOptionBadge(selectedOption)}
{!readOnly && (
<ChevronDown className="w-4 h-4 text-gray-400 opacity-0 group-hover:opacity-100" />
)}
</div>
{selectConfig?.options?.map((option: SelectOption) => (
</PopoverTrigger>
{isEditing && (
<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"
align="start"
>
<div
key={option.id}
onClick={() => handleSelect(option.id)}
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center"
onClick={() => handleSelect('')}
className="px-3 py-2 text-sm text-gray-500 hover:bg-gray-50 cursor-pointer flex items-center gap-2"
>
<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,
}}
>
{option.displayName}
</span>
<X className="w-3 h-3" /> Clear
</div>
))}
</div>
)}
{selectConfig?.options?.map((option: SelectOption) => (
<div
key={option.id}
onClick={() => handleSelect(option.id)}
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center"
>
<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,
}}
>
{option.displayName}
</span>
</div>
))}
</PopoverContent>
)}
</Popover>
</div>
)
}
@@ -1,7 +1,8 @@
import { Check } from 'lucide-react'
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
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'
const SelectMultiField: React.FC<FieldProps<string[]>> = ({
value = [],
@@ -11,7 +12,6 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
}) => {
const selectConfig = config?.selectMulti
const containerRef = useRef<HTMLDivElement>(null)
const dropdownRef = useRef<HTMLDivElement>(null)
const [isEditing, setIsEditing] = useState(false)
const [expanded, setExpanded] = useState(false)
const [visibleCount, setVisibleCount] = useState(value.length)
@@ -63,28 +63,6 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
}
}, [selectedOptions, isEditing, expanded, value])
// Click outside to close dropdown or expanded view
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
// Check if click is outside both the main container and the dropdown
const isOutsideDropdown =
!dropdownRef.current || !dropdownRef.current.contains(event.target as Node)
const isOutsideContainer =
!containerRef.current || !containerRef.current.contains(event.target as Node)
if (isOutsideDropdown && isOutsideContainer) {
if (isEditing) {
setIsEditing(false)
}
if (expanded) {
setExpanded(false)
}
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [isEditing, expanded])
const toggleOption = (optionId: string) => {
const currentVal = value || []
let newVal
@@ -121,99 +99,108 @@ const SelectMultiField: React.FC<FieldProps<string[]>> = ({
}
const hiddenCount = selectedOptions.length - visibleCount
const isOpen = (expanded && !isEditing) || isEditing
const handleOpenChange = (open: boolean) => {
if (!open) {
setExpanded(false)
setIsEditing(false)
}
}
return (
<div className="relative w-full">
{/* Placeholder Display Mode */}
<div
ref={containerRef}
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 ${
!readOnly || hiddenCount > 0
? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200'
: ''
}`}
>
{selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
)}
{/* Render visible items */}
{selectedOptions.slice(0, visibleCount).map((option: SelectOption) => (
<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,
}}
<Popover open={isOpen} onOpenChange={handleOpenChange}>
<PopoverTrigger asChild>
{/* Placeholder Display Mode */}
<div
ref={containerRef}
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 ${
!readOnly || hiddenCount > 0
? 'cursor-pointer hover:bg-gray-100 hover:border-gray-200'
: ''
}`}
>
{option.displayName}
</span>
))}
{selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
)}
{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]">
+{hiddenCount}
</span>
)}
</div>
{/* Expanded Overlay Mode */}
{expanded && !isEditing && (
<div
onClick={handleOverlayClick}
className={`absolute top-0 left-0 w-full px-1 py-1 flex flex-wrap gap-1 bg-white rounded border border-blue-500 shadow-lg z-50 min-h-[32px] h-auto ${
!readOnly ? 'cursor-pointer' : ''
}`}
>
{selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
)}
{selectedOptions.map((option: SelectOption) => (
<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,
}}
>
{option.displayName}
</span>
))}
</div>
)}
{/* Edit Mode Dropdown */}
{isEditing && (
<div
ref={dropdownRef}
className="absolute z-50 top-full left-0 mt-1 w-64 bg-white border border-gray-200 rounded-lg shadow-xl max-h-60 overflow-auto"
>
{selectConfig?.options?.map((option: SelectOption) => {
const isSelected = (value || []).includes(option.id)
return (
<div
{/* Render visible items */}
{selectedOptions.slice(0, visibleCount).map((option: SelectOption) => (
<span
key={option.id}
onClick={() => toggleOption(option.id)}
className="px-3 py-2 hover:bg-gray-50 cursor-pointer flex items-center justify-between"
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,
}}
>
<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,
}}
{option.displayName}
</span>
))}
{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]">
+{hiddenCount}
</span>
)}
</div>
</PopoverTrigger>
{expanded && !isEditing && (
<PopoverContent
onClick={handleOverlayClick}
className="p-1 w-[--radix-popover-trigger-width] min-w-[200px] flex flex-wrap gap-1 bg-white rounded border border-blue-500 shadow-lg min-h-[32px] h-auto cursor-pointer"
align="start"
>
{selectedOptions.length === 0 && (
<span className="text-gray-400 text-sm italic px-1 pt-0.5">Empty</span>
)}
{selectedOptions.map((option: SelectOption) => (
<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,
}}
>
{option.displayName}
</span>
))}
</PopoverContent>
)}
{isEditing && (
<PopoverContent
className="p-1 w-64 bg-white border border-gray-200 rounded-lg shadow-xl max-h-60 overflow-auto"
align="start"
>
{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-gray-50 cursor-pointer flex items-center justify-between"
>
{option.displayName}
</span>
{isSelected && <Check className="w-4 h-4 text-blue-500" />}
</div>
)
})}
</div>
)}
<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,
}}
>
{option.displayName}
</span>
{isSelected && <Check className="w-4 h-4 text-blue-500" />}
</div>
)
})}
</PopoverContent>
)}
</Popover>
</div>
)
}