Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

14.0 KB
Raw Blame
import './EditableField.scss'

import clsx from 'clsx'
import { useValues } from 'kea'
import React, { useEffect, useRef, useState } from 'react'
import TextareaAutosize from 'react-textarea-autosize'

import { IconMarkdownFilled, IconPencil } from '@posthog/icons'

import { useResizeObserver } from 'lib/hooks/useResizeObserver'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { RawInputAutosize } from 'lib/lemon-ui/LemonInput/RawInputAutosize'
import { LemonMarkdown } from 'lib/lemon-ui/LemonMarkdown'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { pluralize } from 'lib/utils/strings'

import { AvailableFeature } from '~/types'

import { upgradeModalLogic } from '../UpgradeModal/upgradeModalLogic'

export interface EditableFieldProps {
    /** What this field stands for. */
    name: string
    value: string
    onChange?: (value: string) => void
    onSave?: (value: string) => void
    saveOnBlur?: boolean
    placeholder?: string
    minLength?: number
    maxLength?: number
    autoFocus?: boolean
    multiline?: boolean
    /** Whether to render the content as Markdown in view mode. */
    markdown?: boolean
    compactButtons?: boolean | 'xsmall' // The 'xsmall' is somewhat hacky, but necessary for 3000 breadcrumbs
    /** Whether this field should be gated behind a "paywall". */
    paywallFeature?: AvailableFeature
    /** Controlled mode. */
    mode?: 'view' | 'edit'
    onModeToggle?: (newMode: 'view' | 'edit') => void
    /** @default 'outlined' */
    editingIndication?: 'outlined' | 'underlined'
    className?: string
    style?: React.CSSProperties
    'data-attr'?: string
    saveButtonText?: string
    /** Extra information shown next to the field. */
    notice?: {
        icon: React.ReactElement
        tooltip: string
    }
    /** If true, single click on text enters edit mode (default is double-click). */
    clickToEdit?: boolean
    /** If true, uses a smaller pencil icon. */
    compactIcon?: boolean
    /** If true, hides the edit icon until hover. */
    showEditIconOnHover?: boolean
}

export function EditableField({
    name,
    value,
    onChange,
    onSave,
    saveOnBlur = false,
    placeholder,
    minLength,
    maxLength,
    autoFocus = false,
    multiline = false,
    markdown = false,
    compactButtons = false,
    paywallFeature,
    mode,
    onModeToggle,
    editingIndication = 'outlined',
    className,
    style,
    'data-attr': dataAttr,
    saveButtonText = 'Save',
    notice,
    clickToEdit = false,
    compactIcon = false,
    showEditIconOnHover = false,
}: EditableFieldProps): JSX.Element {
    const { guardAvailableFeature } = useValues(upgradeModalLogic)
    const [localIsEditing, setLocalIsEditing] = useState(mode === 'edit')
    const [localTentativeValue, setLocalTentativeValue] = useState(value)
    const [isDisplayTooltipNeeded, setIsDisplayTooltipNeeded] = useState(false)
    const containerRef = useRef<HTMLDivElement>(null)
    const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
    const displayRef = useRef<HTMLSpanElement>(null)
    const previousIsEditing = useRef<boolean>()

    useEffect(() => {
        setLocalTentativeValue(value)
    }, [value])

    // Save pending changes on unmount (e.g. when a parent popover closes while editing)
    const saveOnUnmountRef = useRef({ localTentativeValue, value, localIsEditing, onSave, saveOnBlur })
    saveOnUnmountRef.current = { localTentativeValue, value, localIsEditing, onSave, saveOnBlur }
    useEffect(() => {
        return () => {
            const {
                localTentativeValue: v,
                value: orig,
                localIsEditing: editing,
                onSave: save,
                saveOnBlur: sob,
            } = saveOnUnmountRef.current
            if (sob && editing && v !== orig) {
                save?.(v)
            }
        }
    }, [])

    useEffect(() => {
        setLocalIsEditing(mode === 'edit')
    }, [mode])

    useEffect(() => {
        // We always want to focus when switching to edit mode, but can't use autoFocus, because we don't want this to
        // happen when the component is _initially_ rendered in edit mode. The `previousIsEditing.current === false`
        // check is important for this, because only `false` means that the component was previously rendered in view
        // mode. `undefined` means that the component was never rendered before.
        if (inputRef.current && previousIsEditing.current === false && localIsEditing) {
            const endOfInput = inputRef.current.value.length
            inputRef.current.setSelectionRange(endOfInput, endOfInput)
            inputRef.current.focus()
        }
        previousIsEditing.current = localIsEditing
    }, [localIsEditing])

    useResizeObserver({
        ref: containerRef,
        onResize: () => {
            if (displayRef.current) {
                setIsDisplayTooltipNeeded(displayRef.current.scrollWidth > displayRef.current.clientWidth)
            }
        },
    })
    const isSaveable = !minLength || localTentativeValue.length >= minLength

    const mouseDownOnCancelButton = (e: React.MouseEvent): void => {
        // if saveOnBlur is set the onBlur handler of the input fires before the onClick event of the button
        // this onMouseDown handler fires before the input can see the click and fire onBlur
        e.preventDefault()
    }

    const cancel = (): void => {
        setLocalIsEditing(false)
        setLocalTentativeValue(value)
        onModeToggle?.('view')
    }

    const save = (): void => {
        onSave?.(localTentativeValue)
        setLocalIsEditing(false)
        onModeToggle?.('view')
    }

    const isEditing = mode === 'edit' || localIsEditing

    const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>): void => {
        if (isEditing) {
            // Cmd/Ctrl are required in addition to Enter if newlines are permitted
            if (isSaveable && e.key === 'Enter' && (!multiline || e.metaKey || e.ctrlKey)) {
                save() // Save on Enter press
                e.stopPropagation()
                e.preventDefault()
            } else if (e.key === 'Escape') {
                cancel()
                e.stopPropagation()
                e.preventDefault()
            }
        }
    }

    const enterEditMode = (): void => {
        if (!isEditing) {
            guardAvailableFeature(paywallFeature, () => {
                setLocalIsEditing(true)
                onModeToggle?.('edit')
            })
        }
    }

    const handleDoubleClick = (): void => {
        if (!clickToEdit) {
            enterEditMode()
        }
    }

    const handleClick = (): void => {
        if (clickToEdit) {
            enterEditMode()
        }
    }

    return (
        <div
            className={clsx(
                'EditableField',
                multiline && 'EditableField--multiline',
                isEditing && 'EditableField--editing',
                editingIndication === 'underlined' && 'EditableField--underlined',
                showEditIconOnHover && 'group/editable',
                className
            )}
            data-attr={dataAttr}
            // eslint-disable-next-line react/forbid-dom-props
            style={style}
            ref={containerRef}
            onDoubleClick={handleDoubleClick}
        >
            <div className="EditableField__highlight">
                {isEditing ? (
                    <>
                        {multiline ? (
                            <TextareaAutosize
                                name={name}
                                value={localTentativeValue}
                                onChange={(e) => {
                                    onChange?.(e.target.value)
                                    setLocalTentativeValue(e.target.value)
                                }}
                                onBlur={saveOnBlur ? (localTentativeValue !== value ? save : cancel) : undefined}
                                onKeyDown={handleKeyDown}
                                placeholder={placeholder}
                                minLength={minLength}
                                maxLength={maxLength}
                                autoFocus={autoFocus}
                                ref={inputRef as React.RefObject<HTMLTextAreaElement>}
                            />
                        ) : (
                            <RawInputAutosize
                                name={name}
                                value={localTentativeValue}
                                onChange={(e) => {
                                    guardAvailableFeature(paywallFeature, () => {
                                        onChange?.(e.currentTarget.value)
                                        setLocalTentativeValue(e.currentTarget.value)
                                    })
                                }}
                                onBlur={saveOnBlur ? (localTentativeValue !== value ? save : cancel) : undefined}
                                onKeyDown={handleKeyDown}
                                placeholder={placeholder}
                                minLength={minLength}
                                maxLength={maxLength}
                                autoFocus={autoFocus}
                                ref={inputRef as React.RefObject<HTMLInputElement>}
                                wrapperClassName="self-center py-px"
                            />
                        )}
                        {(!mode || !!onModeToggle) && !saveOnBlur && (
                            <div className="EditableField__actions">
                                {markdown && (
                                    <Tooltip title="Markdown formatting support">
                                        <span className="flex items-center">
                                            <IconMarkdownFilled className="text-secondary text-2xl" />
                                        </span>
                                    </Tooltip>
                                )}
                                <LemonButton
                                    title="Cancel editing"
                                    size={typeof compactButtons === 'string' ? compactButtons : 'small'}
                                    onClick={cancel}
                                    type="secondary"
                                    onMouseDown={mouseDownOnCancelButton}
                                >
                                    Cancel
                                </LemonButton>
                                <LemonButton
                                    title={
                                        !minLength
                                            ? 'Save'
                                            : `Save (at least ${pluralize(
                                                  minLength,
                                                  'character',
                                                  'characters'
                                              )} required)`
                                    }
                                    size={typeof compactButtons === 'string' ? compactButtons : 'small'}
                                    disabled={!isSaveable}
                                    onClick={save}
                                    type="primary"
                                >
                                    {saveButtonText}
                                </LemonButton>
                            </div>
                        )}
                    </>
                ) : (
                    <>
                        {localTentativeValue && markdown ? (
                            <div className={clsx(clickToEdit && 'cursor-text')} onClick={handleClick}>
                                <LemonMarkdown lowKeyHeadings>{localTentativeValue}</LemonMarkdown>
                            </div>
                        ) : (
                            <Tooltip
                                title={isDisplayTooltipNeeded ? localTentativeValue : undefined}
                                placement="bottom-start"
                                delayMs={0}
                            >
                                <span
                                    className={clsx('EditableField__display', clickToEdit && 'cursor-text')}
                                    ref={displayRef}
                                    onClick={handleClick}
                                >
                                    {localTentativeValue || <i>{placeholder}</i>}
                                </span>
                            </Tooltip>
                        )}
                        {(!mode || !!onModeToggle) && (
                            <div
                                className={clsx(
                                    'EditableField__actions',
                                    showEditIconOnHover &&
                                        'opacity-0 group-hover/editable:opacity-100 transition-opacity'
                                )}
                            >
                                <LemonButton
                                    title="Edit"
                                    icon={<IconPencil className={compactIcon ? 'w-3 h-3' : undefined} />}
                                    size={compactButtons ? 'small' : undefined}
                                    onClick={enterEditMode}
                                    data-attr={`edit-prop-${name}`}
                                    noPadding
                                />
                            </div>
                        )}
                    </>
                )}
            </div>
            {!isEditing && notice && (
                <Tooltip title={notice.tooltip} placement="right">
                    <span className="flex items-center">
                        {React.cloneElement(notice.icon, {
                            ...notice.icon.props,
                            className: clsx(notice.icon.props.className, 'EditableField__notice'),
                        })}
                    </span>
                </Tooltip>
            )}
        </div>
    )
}