feat(webui): truncate long file names with 100ms hover tooltip (#355)

This commit is contained in:
Yiling-J
2026-08-27 10:54:35 +08:00
committed by GitHub
parent 5ec4cd7fcd
commit d9a8b6a191
7 changed files with 324 additions and 24 deletions
@@ -0,0 +1,100 @@
// @vitest-environment happy-dom
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { EditableText } from './editable-text'
describe('EditableText', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
act(() => {
vi.runOnlyPendingTimers()
})
vi.useRealTimers()
cleanup()
})
it('renders a span in read-only (disabled) mode with the value', () => {
render(<EditableText value="MyLongFileName.mp4" disabled={true} />)
expect(screen.queryByRole('textbox')).toBeNull()
const span = screen.getByText('MyLongFileName.mp4')
expect(span).toBeTruthy()
expect(span.tagName.toLowerCase()).toBe('span')
expect(span.className).toContain('truncate')
})
it('renders an input in editable (!disabled) mode', () => {
const handleChange = vi.fn()
render(<EditableText value="MyFile.txt" disabled={false} onChange={handleChange} />)
const input = screen.getByRole('textbox') as HTMLInputElement
expect(input).toBeTruthy()
expect(input.value).toBe('MyFile.txt')
})
it('does not show tooltip on hover when text is not truncated', () => {
render(<EditableText value="Short.png" disabled={true} />)
const span = screen.getByText('Short.png')
Object.defineProperty(span, 'scrollWidth', { value: 80, configurable: true })
Object.defineProperty(span, 'clientWidth', { value: 100, configurable: true })
fireEvent.pointerMove(span, { pointerType: 'mouse' })
act(() => {
vi.advanceTimersByTime(200)
})
// Tooltip should not be displayed
expect(screen.queryByRole('tooltip')).toBeNull()
})
it('shows full name in tooltip after 100ms when text is truncated', () => {
render(
<EditableText
value="A_very_long_file_name_that_will_overflow_the_column_width.mov"
disabled={true}
/>,
)
const span = screen.getByText('A_very_long_file_name_that_will_overflow_the_column_width.mov')
Object.defineProperty(span, 'scrollWidth', { value: 300, configurable: true })
Object.defineProperty(span, 'clientWidth', { value: 120, configurable: true })
fireEvent.pointerMove(span, { pointerType: 'mouse' })
act(() => {
vi.advanceTimersByTime(200)
})
const tooltip = screen.getByRole('tooltip')
expect(tooltip).toBeTruthy()
expect(tooltip.textContent).toContain(
'A_very_long_file_name_that_will_overflow_the_column_width.mov',
)
})
it('handles input events and keydown when editing', () => {
const handleKeyDown = vi.fn()
const handleBlur = vi.fn()
render(
<EditableText
value="Editable.txt"
disabled={false}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
/>,
)
const input = screen.getByRole('textbox')
fireEvent.keyDown(input, { key: 'Enter' })
expect(handleKeyDown).toHaveBeenCalledTimes(1)
fireEvent.blur(input)
expect(handleBlur).toHaveBeenCalledTimes(1)
})
})
+78 -15
View File
@@ -1,33 +1,96 @@
import { Input } from '@/ui/components/ui/input'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/ui/components/ui/tooltip'
import { cn } from '@/ui/lib/utils'
import React from 'react'
import React, { useRef, useState } from 'react'
export type EditableTextProps = React.ComponentPropsWithoutRef<'input'>
export type EditableTextProps = React.ComponentPropsWithoutRef<'input'> & {
tooltipSide?: 'top' | 'right' | 'bottom' | 'left'
tooltipDelayDuration?: number
}
const EditableText = React.forwardRef<HTMLInputElement, EditableTextProps>(
({ className, onClick, onMouseDown, disabled, ...props }, ref) => {
(
{
className,
onClick,
onMouseDown,
disabled,
value,
title: _title,
tooltipSide = 'top',
tooltipDelayDuration = 100,
...props
},
ref,
) => {
const [isOpen, setIsOpen] = useState(false)
const spanRef = useRef<HTMLSpanElement>(null)
const textContent = typeof value === 'string' ? value : (value?.toString() ?? '')
if (disabled) {
return (
<TooltipProvider delayDuration={tooltipDelayDuration}>
<Tooltip
open={isOpen}
onOpenChange={(nextOpen) => {
if (nextOpen) {
if (spanRef.current && spanRef.current.scrollWidth > spanRef.current.clientWidth) {
setIsOpen(true)
} else {
setIsOpen(false)
}
} else {
setIsOpen(false)
}
}}
>
<TooltipTrigger asChild>
<span
ref={spanRef}
className={cn(
'box-border block min-w-0 w-full rounded-sm border border-transparent px-1 py-0.5 text-base shadow-none transition-all outline-none cursor-default opacity-100 select-none truncate',
className,
)}
onClick={onClick}
onMouseDown={onMouseDown}
>
{textContent}
</span>
</TooltipTrigger>
{isOpen && (
<TooltipContent
side={tooltipSide}
className="max-w-md break-all text-wrap [text-wrap:normal] text-left"
>
{textContent}
</TooltipContent>
)}
</Tooltip>
</TooltipProvider>
)
}
return (
<Input
ref={ref}
readOnly={disabled}
tabIndex={disabled ? -1 : undefined}
value={value}
tabIndex={0}
className={cn(
'h-auto min-h-0 w-full rounded-sm border px-1 py-0.5 text-base shadow-none transition-all outline-none focus-visible:ring-0',
!disabled
? 'border-blue-500 !bg-muted/10 ring-1 ring-blue-500'
: 'border-transparent !bg-transparent cursor-default opacity-100 focus-visible:border-transparent focus-visible:ring-0 caret-transparent select-none',
'box-border block min-w-0 h-auto w-full rounded-sm border border-blue-500 !bg-muted/10 ring-1 ring-blue-500 px-1 py-0.5 text-base shadow-none transition-all outline-none focus-visible:ring-0',
className,
)}
onClick={(e) => {
if (!disabled) {
e.stopPropagation()
}
e.stopPropagation()
onClick?.(e)
}}
onMouseDown={(e) => {
if (!disabled) {
e.stopPropagation()
}
e.stopPropagation()
onMouseDown?.(e)
}}
{...props}