mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-26 02:21:06 -04:00
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import {ReactNode, useState} from 'react'
|
|
|
|
import {ExpandingInput} from './expanding-input'
|
|
|
|
export const EditInPlaceInput = (props: {
|
|
className?: string
|
|
disabled?: boolean
|
|
initialValue?: string
|
|
onSave: (value: string) => void
|
|
/** The text to show when input is not focused. Required. */
|
|
children: (value: string) => ReactNode
|
|
}) => {
|
|
const {className, disabled, initialValue = '', onSave, children} = props
|
|
const [value, setValue] = useState(initialValue)
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
const save = () => {
|
|
onSave(value)
|
|
setEditing(false)
|
|
}
|
|
|
|
return editing ? (
|
|
<ExpandingInput
|
|
className={className}
|
|
value={value}
|
|
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setValue(e.target.value)}
|
|
onBlur={save}
|
|
onKeyDown={(e) => e.key === 'Enter' && save()}
|
|
autoFocus
|
|
onFocus={(e) => {
|
|
// move cursor to end
|
|
e.target.value = ' '
|
|
e.target.value = value
|
|
}}
|
|
/>
|
|
) : (
|
|
<div onClick={() => !disabled && setEditing(true)}>{children(value)}</div>
|
|
)
|
|
}
|