From 61a61abc7c4ec8a1db3e7b05d8f74ee3c3e89423 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Fri, 14 Aug 2026 14:35:17 +0800 Subject: [PATCH] fix(webui): fix select field height and multi-select overflow/interaction issues (#307) * fix(webui): standardize single-select custom field height * fix(webui): fix multi-select badge overflow calculation and simplify direct edit --- .../components/fields/select-field.test.tsx | 17 +- .../webui/components/fields/select-field.tsx | 7 +- .../fields/select-multi-field.test.tsx | 11 + .../components/fields/select-multi-field.tsx | 191 +++++++----------- .../components/fields/user-multi-field.tsx | 126 +++++------- 5 files changed, 151 insertions(+), 201 deletions(-) diff --git a/packages/webui/components/fields/select-field.test.tsx b/packages/webui/components/fields/select-field.test.tsx index 13a46e9c..f3933ee2 100644 --- a/packages/webui/components/fields/select-field.test.tsx +++ b/packages/webui/components/fields/select-field.test.tsx @@ -45,8 +45,21 @@ describe('SelectField Component', () => { } it('renders selected option badge correctly', () => { - render() - expect(screen.getByText('Foo')).toBeTruthy() + const { container } = render() + const badge = screen.getByText('Foo') + expect(badge).toBeTruthy() + expect(badge.className).toContain('h-[22px]') + const trigger = container.querySelector('div[class*="h-[28px]"]') + expect(trigger).toBeTruthy() + }) + + it('renders placeholder with text-sm styling', () => { + const { container } = render() + const placeholder = screen.getByText('Select an option') + expect(placeholder).toBeTruthy() + expect(placeholder.className).toContain('text-sm') + const trigger = container.querySelector('div[class*="h-[28px]"]') + expect(trigger).toBeTruthy() }) it('filters options based on search query', () => { diff --git a/packages/webui/components/fields/select-field.tsx b/packages/webui/components/fields/select-field.tsx index 606030e3..88692646 100644 --- a/packages/webui/components/fields/select-field.tsx +++ b/packages/webui/components/fields/select-field.tsx @@ -99,10 +99,11 @@ const SelectField: React.FC> = ({ } const renderOptionBadge = (option: SelectOption | undefined) => { - if (!option) return Select an option + if (!option) + return Select an option return ( {option.displayName} @@ -116,7 +117,7 @@ const SelectField: React.FC> = ({
diff --git a/packages/webui/components/fields/select-multi-field.test.tsx b/packages/webui/components/fields/select-multi-field.test.tsx index 3ef1d376..1c9320cd 100644 --- a/packages/webui/components/fields/select-multi-field.test.tsx +++ b/packages/webui/components/fields/select-multi-field.test.tsx @@ -121,4 +121,15 @@ describe('SelectMultiField Component', () => { expect(onSave).toHaveBeenCalledWith(['foo', 'new-multi-option']) }) }) + + it('opens search input directly on first click even when multiple items are selected', () => { + render() + + const trigger = screen.getByText('Foo') + fireEvent.click(trigger) + + const searchInput = screen.getByPlaceholderText('Search options...') + expect(searchInput).toBeTruthy() + expect(screen.getByText('abc')).toBeTruthy() + }) }) diff --git a/packages/webui/components/fields/select-multi-field.tsx b/packages/webui/components/fields/select-multi-field.tsx index 11c9d797..e47f8cc8 100644 --- a/packages/webui/components/fields/select-multi-field.tsx +++ b/packages/webui/components/fields/select-multi-field.tsx @@ -20,7 +20,6 @@ const SelectMultiField: React.FC> = ({ const options: SelectOption[] = selectConfig?.options || [] const containerRef = useRef(null) const [isEditing, setIsEditing] = useState(false) - const [expanded, setExpanded] = useState(false) const [visibleCount, setVisibleCount] = useState(value.length) const [searchQuery, setSearchQuery] = useState('') @@ -39,44 +38,44 @@ const SelectMultiField: React.FC> = ({ // Dynamic calculation for how many items fit in one line useLayoutEffect(() => { - if (isEditing || expanded) { + if (isEditing) { setVisibleCount(selectedOptions.length) return } if (containerRef.current) { - const containerWidth = containerRef.current.offsetWidth + const innerWidth = containerRef.current.clientWidth - 8 // account for px-1 (8px padding) const canvas = document.createElement('canvas') const context = canvas.getContext('2d') - if (context) { - context.font = '12px ui-sans-serif, system-ui, sans-serif' + if (context && innerWidth > 0) { + context.font = '500 12px ui-sans-serif, system-ui, sans-serif' + const gap = 4 + const badgeWidths = selectedOptions.map( + (opt) => Math.ceil(context.measureText(opt.displayName).width) + 18, + ) + const totalAllWidth = + badgeWidths.reduce((a, b) => a + b, 0) + Math.max(0, badgeWidths.length - 1) * gap + if (totalAllWidth <= innerWidth) { + setVisibleCount(selectedOptions.length) + return + } + + const moreBadgeWidth = 32 + const maxAvailableWidth = innerWidth - moreBadgeWidth - gap let currentWidth = 0 let count = 0 - const moreBadgeWidth = 28 - 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 - - if (currentWidth + badgeWidth > maxAvailableWidth && i < selectedOptions.length - 1) { - break - } - if (i === selectedOptions.length - 1) { - if (currentWidth + badgeWidth <= containerWidth) { - count++ - } - break - } - - currentWidth += badgeWidth + for (let i = 0; i < badgeWidths.length; i++) { + const widthWithGap = count > 0 ? currentWidth + gap + badgeWidths[i] : badgeWidths[i] + if (widthWithGap > maxAvailableWidth) break + currentWidth = widthWithGap count++ } setVisibleCount(count) } } - }, [selectedOptions, isEditing, expanded, value]) + }, [selectedOptions, isEditing, value]) const toggleOption = (optionId: string) => { const currentVal = value || [] @@ -134,20 +133,6 @@ const SelectMultiField: React.FC> = ({ } const handlePlaceholderClick = (e: React.MouseEvent) => { - e.stopPropagation() - if (isEditing) return - - const hiddenCount = selectedOptions.length - visibleCount - if (!expanded && hiddenCount > 0) { - setExpanded(true) - } else { - if (!readOnly) { - setIsEditing(true) - } - } - } - - const handleOverlayClick = (e: React.MouseEvent) => { e.stopPropagation() if (!readOnly) { setIsEditing(true) @@ -155,20 +140,18 @@ const SelectMultiField: React.FC> = ({ } const hiddenCount = selectedOptions.length - visibleCount - const isOpen = (expanded && !isEditing) || isEditing const handleOpenChange = (open: boolean) => { if (readOnly) return + setIsEditing(open) if (!open) { - setExpanded(false) - setIsEditing(false) setSearchQuery('') } } return (
e.stopPropagation()}> - + {/* Placeholder Display Mode */}
> = ({ 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 overflow-auto' - : 'w-64 border-border' - }`} - onClick={expanded && !isEditing ? handleOverlayClick : undefined} + className="p-1.5 w-64 max-h-72 flex flex-col bg-popover border border-border rounded-lg shadow-lg" > - {expanded && !isEditing ? ( - <> - {selectedOptions.length === 0 && ( - Empty - )} - {selectedOptions.map((option: SelectOption) => ( - e.stopPropagation()} + > + + setSearchQuery(e.target.value)} + autoFocus + /> +
+ +
+ {filteredOptions.map((option: SelectOption) => { + const isSelected = (value || []).includes(option.id) + return ( +
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' : '' + }`} > - {option.displayName} - - ))} - - ) : ( - <> -
e.stopPropagation()} - > - - setSearchQuery(e.target.value)} - autoFocus - /> -
- -
- {filteredOptions.map((option: SelectOption) => { - const isSelected = (value || []).includes(option.id) - return ( -
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' : '' - }`} - > - - {option.displayName} - - {isSelected && } -
- ) - })} - - {filteredOptions.length === 0 && !trimmedSearch && ( -
- {m.no_options_found()} -
- )} - - {trimmedSearch !== '' && !hasExactMatch && ( -
- - - {m.add_option_with_name({ name: trimmedSearch })} - -
- )} + {option.displayName} + + {isSelected && } +
+ ) + })} + + {filteredOptions.length === 0 && !trimmedSearch && ( +
+ {m.no_options_found()}
- - )} + )} + + {trimmedSearch !== '' && !hasExactMatch && ( +
+ + {m.add_option_with_name({ name: trimmedSearch })} +
+ )} +
diff --git a/packages/webui/components/fields/user-multi-field.tsx b/packages/webui/components/fields/user-multi-field.tsx index 84913a50..e70a4277 100644 --- a/packages/webui/components/fields/user-multi-field.tsx +++ b/packages/webui/components/fields/user-multi-field.tsx @@ -12,7 +12,6 @@ const UserMultiField: React.FC> = ({ value = [], onSave, re const { members, fetchMembers } = useMemberStore() const containerRef = useRef(null) const [isEditing, setIsEditing] = useState(false) - const [expanded, setExpanded] = useState(false) const [visibleCount, setVisibleCount] = useState(value.length) useEffect(() => { @@ -34,45 +33,46 @@ const UserMultiField: React.FC> = ({ value = [], onSave, re // Dynamic calculation for how many items fit in one line useLayoutEffect(() => { - if (isEditing || expanded) { + if (isEditing) { setVisibleCount(selectedUsers.length) return } if (containerRef.current) { - const containerWidth = containerRef.current.offsetWidth + const innerWidth = containerRef.current.clientWidth - 8 // account for px-1 (8px padding) const canvas = document.createElement('canvas') const context = canvas.getContext('2d') - if (context) { - context.font = '12px ui-sans-serif, system-ui, sans-serif' + if (context && innerWidth > 0) { + context.font = '500 12px ui-sans-serif, system-ui, sans-serif' + const gap = 4 + const badgeWidths = selectedUsers.map((user) => { + const textWidth = Math.min(100, Math.ceil(context.measureText(user.name).width)) + // 16px padding + 2px border + 16px avatar + 6px gap = 40px + return textWidth + 40 + }) + const totalAllWidth = + badgeWidths.reduce((a, b) => a + b, 0) + Math.max(0, badgeWidths.length - 1) * gap + if (totalAllWidth <= innerWidth) { + setVisibleCount(selectedUsers.length) + return + } + + const moreBadgeWidth = 32 + const maxAvailableWidth = innerWidth - moreBadgeWidth - gap let currentWidth = 0 let count = 0 - const moreBadgeWidth = 28 - const maxAvailableWidth = containerWidth - moreBadgeWidth - for (let i = 0; i < selectedUsers.length; i++) { - const textWidth = context.measureText(selectedUsers[i].name).width - // 16px padding + 16px avatar + 6px gap + 4px extra gap - const badgeWidth = textWidth + 42 - - if (currentWidth + badgeWidth > maxAvailableWidth && i < selectedUsers.length - 1) { - break - } - if (i === selectedUsers.length - 1) { - if (currentWidth + badgeWidth <= containerWidth) { - count++ - } - break - } - - currentWidth += badgeWidth + for (let i = 0; i < badgeWidths.length; i++) { + const widthWithGap = count > 0 ? currentWidth + gap + badgeWidths[i] : badgeWidths[i] + if (widthWithGap > maxAvailableWidth) break + currentWidth = widthWithGap count++ } setVisibleCount(count) } } - }, [selectedUsers, isEditing, expanded, value]) + }, [selectedUsers, isEditing, value]) const toggleUser = (userId: string) => { const currentVal = value || [] @@ -86,20 +86,6 @@ const UserMultiField: React.FC> = ({ value = [], onSave, re } const handlePlaceholderClick = (e: React.MouseEvent) => { - e.stopPropagation() - if (isEditing) return - - const hiddenCount = selectedUsers.length - visibleCount - if (!expanded && hiddenCount > 0) { - setExpanded(true) - } else { - if (!readOnly) { - setIsEditing(true) - } - } - } - - const handleOverlayClick = (e: React.MouseEvent) => { e.stopPropagation() if (!readOnly) { setIsEditing(true) @@ -107,14 +93,10 @@ const UserMultiField: React.FC> = ({ value = [], onSave, re } const hiddenCount = selectedUsers.length - visibleCount - const isOpen = (expanded && !isEditing) || isEditing const handleOpenChange = (open: boolean) => { if (readOnly) return - if (!open) { - setExpanded(false) - setIsEditing(false) - } + setIsEditing(open) } const renderUserBadge = (user: UserInfo) => { @@ -136,7 +118,7 @@ const UserMultiField: React.FC> = ({ value = [], onSave, re return (
e.stopPropagation()}> - +
> = ({ value = [], onSave, re - {expanded && !isEditing ? ( - <> - {selectedUsers.length === 0 && ( - Empty - )} - {selectedUsers.map(renderUserBadge)} - - ) : ( - members.map((user) => { - const isSelected = (value || []).includes(user.id) - return ( -
toggleUser(user.id)} - className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center justify-between rounded-sm" - > -
- - - - {getInitials(user.name)} - - - {user.name} -
- {isSelected && } + {members.map((user) => { + const isSelected = (value || []).includes(user.id) + return ( +
toggleUser(user.id)} + className="px-3 py-2 hover:bg-accent cursor-pointer flex items-center justify-between rounded-sm" + > +
+ + + + {getInitials(user.name)} + + + {user.name}
- ) - }) - )} + {isSelected && } +
+ ) + })}