All checks were successful
Build & Publish / publish (push) Successful in 1m2s
Add onKeyDown (Enter/Space) to the CommandPalette overlay backdrop div and result item divs to satisfy SonarQube S1082. RouteFlow and ProcessorTimeline already had the fixes in place. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import { useState, useRef, useEffect } from 'react'
|
|
import styles from './InlineEdit.module.css'
|
|
|
|
export interface InlineEditProps {
|
|
value: string
|
|
onSave: (value: string) => void
|
|
placeholder?: string
|
|
disabled?: boolean
|
|
className?: string
|
|
}
|
|
|
|
export function InlineEdit({ value, onSave, placeholder, disabled, className }: InlineEditProps) {
|
|
const [editing, setEditing] = useState(false)
|
|
const [draft, setDraft] = useState(value)
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
|
|
useEffect(() => {
|
|
if (editing) {
|
|
inputRef.current?.focus()
|
|
inputRef.current?.select()
|
|
}
|
|
}, [editing])
|
|
|
|
function startEdit() {
|
|
if (disabled) return
|
|
setDraft(value)
|
|
setEditing(true)
|
|
}
|
|
|
|
function handleKeyDown(e: React.KeyboardEvent) {
|
|
if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
setEditing(false)
|
|
onSave(draft)
|
|
} else if (e.key === 'Escape') {
|
|
setEditing(false)
|
|
}
|
|
}
|
|
|
|
function handleBlur() {
|
|
setEditing(false)
|
|
}
|
|
|
|
if (editing) {
|
|
return (
|
|
<input
|
|
ref={inputRef}
|
|
className={`${styles.input} ${className ?? ''}`}
|
|
value={draft}
|
|
onChange={(e) => setDraft(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
onBlur={handleBlur}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const isEmpty = !value
|
|
return (
|
|
<span className={`${styles.display} ${disabled ? styles.disabled : ''} ${className ?? ''}`}>
|
|
<span
|
|
className={isEmpty ? styles.placeholder : styles.value}
|
|
role="button"
|
|
tabIndex={disabled ? undefined : 0}
|
|
onClick={startEdit}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
e.preventDefault()
|
|
startEdit()
|
|
}
|
|
}}
|
|
>
|
|
{isEmpty ? placeholder : value}
|
|
</span>
|
|
{!disabled && (
|
|
<button
|
|
className={styles.editBtn}
|
|
onClick={startEdit}
|
|
aria-label="Edit"
|
|
type="button"
|
|
>
|
|
✎
|
|
</button>
|
|
)}
|
|
</span>
|
|
)
|
|
}
|