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.

12.9 KB
Raw Blame
import './LemonInput.scss'

import { useMergeRefs } from '@floating-ui/react'
import clsx from 'clsx'
import React, { useRef, useState } from 'react'

import { IconEye, IconSearch, IconX } from '@posthog/icons'
import { Tooltip } from '@posthog/lemon-ui'

import { IconEyeHidden } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonTag } from 'lib/lemon-ui/LemonTag'

import { RawInputAutosize } from './RawInputAutosize'

interface LemonInputPropsBase extends Pick<
    // NOTE: We explicitly pick rather than omit to ensure these components aren't used incorrectly
    React.InputHTMLAttributes<HTMLInputElement>,
    | 'className'
    | 'onClick'
    | 'onFocus'
    | 'onBlur'
    | 'autoFocus'
    | 'maxLength'
    | 'onKeyDown'
    | 'onKeyUp'
    | 'onKeyPress'
    | 'onPaste'
    | 'autoComplete'
    | 'autoCorrect'
    | 'autoCapitalize'
    | 'spellCheck'
    | 'inputMode'
    | 'pattern'
> {
    inputRef?: React.Ref<HTMLInputElement>
    inputComponent?: React.JSXElementConstructor<
        React.InputHTMLAttributes<HTMLInputElement> & React.RefAttributes<HTMLInputElement>
    >
    id?: string
    placeholder?: string
    /** Use the danger status for invalid input. */
    status?: 'default' | 'danger'
    /** Whether there should be a clear icon to the right allowing you to reset the input. The `suffix` prop will be ignored if clearing is allowed. */
    allowClear?: boolean
    /** Element to prefix input field */
    prefix?: React.ReactElement | null
    /** Element to suffix input field */
    suffix?: React.ReactElement | null
    suffixAfterClear?: boolean
    /** @deprecated Use `disabledReason` instead and provide a reason. */
    disabled?: boolean
    /** Like plain `disabled`, except we enforce a reason to be shown in the tooltip. */
    disabledReason?: React.ReactNode | null | false
    /** Whether the disabled reason tooltip is interactive (e.g., contains a link) */
    disabledReasonInteractive?: boolean
    /** Whether input field is full width. Cannot be used in conjuction with `autoWidth`. */
    fullWidth?: boolean
    /** Whether input field should be as wide as its content. Cannot be used in conjuction with `fullWidth`. */
    autoWidth?: boolean
    /** Special case - show a transparent background rather than white */
    transparentBackground?: boolean
    /** Size of the element. Default: `'medium'`. */
    size?: 'xsmall' | 'small' | 'medium' | 'large'
    onPressEnter?: (event: React.KeyboardEvent<HTMLInputElement>) => void
    'data-attr'?: string
    'aria-label'?: string
    /** Whether to stop propagation of events from the input */
    stopPropagation?: boolean
    /** Small label shown above the top-right corner, e.g. "last used" */
    badgeText?: string
}

export interface LemonInputPropsText extends LemonInputPropsBase {
    type?: 'text' | 'email' | 'search' | 'url' | 'password' | 'time'
    value?: string
    defaultValue?: string
    onChange?: (newValue: string) => void
    /** Seconds between valid values; mainly for `type="time"` (passed to native `<input>`). */
    step?: number
}

export interface LemonInputPropsNumber
    extends LemonInputPropsBase, Pick<React.InputHTMLAttributes<HTMLInputElement>, 'step' | 'min' | 'max'> {
    type: 'number'
    value?: number
    defaultValue?: number
    onChange?: (newValue: number | undefined) => void
}

export type LemonInputProps = LemonInputPropsText | LemonInputPropsNumber

// Delay for interactive tooltips to close after mouse leave.
// This allows some grace period in case the user moves the
// cursor out of the tooltip briefly while intending to
// interact with it.
export const INTERACTIVE_CLOSE_DELAY_MS = 750

export const LemonInput = React.forwardRef<HTMLDivElement, LemonInputProps>(function LemonInput(
    {
        className,
        onChange,
        onFocus,
        onBlur,
        onPressEnter,
        status = 'default',
        allowClear, // Default handled inside the component
        fullWidth,
        autoWidth,
        prefix,
        suffix,
        suffixAfterClear = false,
        type,
        value,
        transparentBackground = false,
        size = 'medium',
        stopPropagation = false,
        inputRef,
        inputComponent,
        disabled,
        disabledReason,
        disabledReasonInteractive,
        badgeText,
        ...props
    },
    ref
): JSX.Element {
    const internalInputRef = useRef<HTMLInputElement>(null)
    const mergedInputRef = useMergeRefs([inputRef, internalInputRef])

    const [focused, setFocused] = useState<boolean>(Boolean(props.autoFocus))
    const [passwordVisible, setPasswordVisible] = useState<boolean>(false)
    // A cleared number field reports NaN, which consumers routinely collapse back into a default and
    // echo into the input. While the user is still editing, their empty text wins over that default.
    const [numberDraftEmpty, setNumberDraftEmpty] = useState<boolean>(false)

    if (autoWidth && fullWidth) {
        throw new Error('Cannot use `autoWidth` and `fullWidth` props together')
    }

    const focus = (): void => {
        internalInputRef.current?.focus()
        setFocused(true)
    }

    if (type === 'search') {
        allowClear = allowClear ?? true
        prefix = prefix ?? <IconSearch />
    } else if (type === 'password') {
        const showPasswordButton = (
            <LemonButton
                size="small"
                noPadding
                icon={passwordVisible ? <IconEyeHidden /> : <IconEye />}
                tooltip={passwordVisible ? 'Hide password' : 'Show password'}
                onClick={(e) => {
                    e.stopPropagation()
                    focus()
                    setPasswordVisible(!passwordVisible)
                }}
            />
        )
        if (suffix) {
            suffix = (
                <>
                    {showPasswordButton}
                    {suffix}
                </>
            )
        } else {
            suffix = showPasswordButton
        }
    }
    // when allowClear is set with a value, render a clear button alongside any
    // existing suffix so consumers (e.g. TaxonomicFilter's category dropdown)
    // remain reachable while the user is typing
    if (allowClear && value) {
        const clearButton = (
            <LemonButton
                size="small"
                noPadding
                icon={<IconX />}
                tooltip="Clear input"
                onClick={(e) => {
                    if (stopPropagation) {
                        e.stopPropagation()
                    }
                    if (onChange) {
                        if (type === 'number') {
                            setNumberDraftEmpty(false)
                            // @ts-expect-error - onChange is typed as never, force it to match the right one
                            onChange(0)
                        } else {
                            // @ts-expect-error - onChange is typed as never, force it to match the right one
                            onChange('')
                        }
                    }

                    focus()
                }}
            />
        )
        suffix = suffix ? (
            <>
                {suffixAfterClear ? clearButton : suffix}
                {suffixAfterClear ? suffix : clearButton}
            </>
        ) : (
            clearButton
        )
    }

    const InputComponent = autoWidth ? RawInputAutosize : (inputComponent ?? 'input')
    // A cleared controlled number input holds NaN; show '' so it stays controlled instead of feeding
    // NaN to the DOM. While the field is focused and the user has emptied it, their empty text also
    // wins over the fallback a consumer echoes back. Both branches require a controlled input —
    // `undefined` means the consumer passed only `defaultValue`, and swapping that for '' mid-edit
    // would flip the input uncontrolled -> controlled and back on the next keystroke.
    const displayValue =
        type === 'number' &&
        value !== undefined &&
        ((focused && numberDraftEmpty) || (typeof value === 'number' && Number.isNaN(value)))
            ? ''
            : value
    return (
        <Tooltip
            title={disabledReason ?? undefined}
            interactive={disabledReasonInteractive}
            closeDelayMs={disabledReasonInteractive ? INTERACTIVE_CLOSE_DELAY_MS : undefined}
        >
            <span
                className={clsx(
                    'LemonInput',
                    'input-like',
                    status !== 'default' && `LemonInput--status-${status}`,
                    type && `LemonInput--type-${type}`,
                    size && `LemonInput--${size}`,
                    fullWidth && 'LemonInput--full-width',
                    displayValue && 'LemonInput--has-content',
                    !disabled && !disabledReason && focused && 'LemonInput--focused',
                    transparentBackground && 'LemonInput--transparent-background',
                    badgeText && 'relative',
                    className
                )}
                aria-disabled={disabled || !!disabledReason}
                onClick={(event) => {
                    // Native segmented inputs (notably `type="time"` in Safari) reset to their
                    // first segment when focused again. The input already handles its own clicks;
                    // only focus it when the surrounding input chrome was clicked.
                    if (event.target !== internalInputRef.current) {
                        focus()
                    }
                }}
                ref={ref}
            >
                {prefix}
                <InputComponent
                    className="LemonInput__input"
                    ref={mergedInputRef}
                    type={(type === 'password' && passwordVisible ? 'text' : type) || 'text'}
                    value={displayValue}
                    disabled={disabled || !!disabledReason}
                    onChange={(event) => {
                        if (stopPropagation) {
                            event.stopPropagation()
                        }

                        if (type === 'number') {
                            // Value sanitization empties `value` for anything that isn't a valid float,
                            // so intermediate states like '-' and '0.' count as draft-empty too. That is
                            // what we want: the browser keeps showing the half-typed text, and marking it
                            // empty stops React's `value === 0 && node.value === ''` case from replacing
                            // it with a fallback of 0 before the user can finish the number.
                            setNumberDraftEmpty(event.currentTarget.value === '')
                        }

                        if (onChange) {
                            if (type === 'number') {
                                // @ts-expect-error - onChange is typed as never, force it to match the right one
                                onChange(event.currentTarget.valueAsNumber)
                            } else {
                                // @ts-expect-error - onChange is typed as never, force it to match the right one
                                onChange(event.currentTarget.value ?? '')
                            }
                        }
                    }}
                    onFocus={(event) => {
                        if (stopPropagation) {
                            event.stopPropagation()
                        }
                        setFocused(true)
                        onFocus?.(event)
                    }}
                    onBlur={(event) => {
                        if (stopPropagation) {
                            event.stopPropagation()
                        }
                        setFocused(false)
                        setNumberDraftEmpty(false)
                        onBlur?.(event)
                    }}
                    onKeyDown={(event) => {
                        if (stopPropagation) {
                            event.stopPropagation()
                        }
                        if (event.key === 'Enter' && !event.nativeEvent.isComposing) {
                            // Enter commits without blurring — through onPressEnter, and through a
                            // surrounding form's implicit submission even when there is no handler
                            // here. End the draft either way, so the field stops showing empty while
                            // the consumer's value is what actually got submitted.
                            setNumberDraftEmpty(false)
                            onPressEnter?.(event)
                        }
                    }}
                    {...props}
                />
                {suffix}
                {badgeText && (
                    <LemonTag className="absolute -top-3 -right-2 pointer-events-none" size="small" type="muted">
                        {badgeText}
                    </LemonTag>
                )}
            </span>
        </Tooltip>
    )
})