: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import clsx from 'clsx'
import { Field as KeaField, FieldProps as KeaFieldProps } from 'kea-forms/lib/components'
import { cloneElement, isValidElement, useId } from 'react'
import { IconErrorOutline } from 'lib/lemon-ui/icons'
import { LemonLabel } from 'lib/lemon-ui/LemonLabel/LemonLabel'
import { cn } from 'lib/utils/css-classes'
export type LemonPureFieldProps = {
/** The label name to be displayed */
label?: React.ReactNode
/** Will show a muted (optional) next to the label */
showOptional?: boolean
/** Will show a clickable (what is this?) next to the label, useful if we want to toggle explanation modals on click */
onExplanationClick?: () => void
/** Info tooltip to be displayed next to the label */
info?: React.ReactNode
/** Help text to be shown directly beneath the input */
help?: React.ReactNode
/** Error message to be displayed */
error?: React.ReactNode
renderError?: (error: string) => React.ReactNode
className?: string
children?: React.ReactNode
onClick?: () => void
/** Flex the field as a row rather than columns */
inline?: boolean
/** The id of the input this field is for */
htmlFor?: string
/** The class name override for the label */
labelClassName?: string
}
const LemonFieldError = ({ error }: { error: string }): JSX.Element => {
return (
<div className="text-danger flex items-center gap-1 text-sm">
<IconErrorOutline className="text-xl shrink-0" /> {error}
</div>
)
}
const LemonPureField = ({
label,
info,
error,
help,
htmlFor,
showOptional,
onExplanationClick,
className,
children,
inline,
onClick,
renderError,
labelClassName,
}: LemonPureFieldProps): JSX.Element => {
return (
<div
onClick={onClick}
className={cn(
'Field flex',
{ 'gap-2': className ? className.indexOf('gap-') === -1 : true },
className,
error && 'Field--error',
inline ? 'flex-row' : 'flex-col'
)}
>
{label ? (
<LemonLabel
info={info}
showOptional={showOptional}
onExplanationClick={onExplanationClick}
className={clsx(labelClassName, {
'cursor-pointer': !!onClick,
})}
htmlFor={htmlFor}
>
{label}
</LemonLabel>
) : null}
{children}
{help ? <div className="text-secondary text-xs">{help}</div> : null}
{typeof error === 'string' ? renderError ? renderError(error) : <LemonFieldError error={error} /> : null}
</div>
)
}
export type LemonFieldProps = Omit<LemonPureFieldProps, 'children' | 'error'> & Pick<KeaFieldProps, 'children' | 'name'>
/** A field for use within a Kea form. Outside a form use `LemonField.Pure`. */
export const LemonField = ({
name,
help,
className,
showOptional,
inline,
info,
renderError,
labelClassName,
htmlFor,
...keaFieldProps
}: LemonFieldProps): JSX.Element => {
// Stable fallback id so clicking the label focuses the wrapped input. Used when neither the
// caller nor kea-forms (function-as-child case) put an id on the rendered input.
const generatedId = useId()
const template: KeaFieldProps['template'] = ({ label, kids, error }) => {
const kidsElement = isValidElement(kids) ? (kids as React.ReactElement<{ id?: string }>) : null
const existingId = kidsElement?.props.id
const inputId = htmlFor ?? existingId ?? generatedId
const renderedKids = kidsElement && existingId !== inputId ? cloneElement(kidsElement, { id: inputId }) : kids
return (
<LemonPureField
label={label}
error={error}
help={help}
className={className}
showOptional={showOptional}
inline={inline}
info={info}
renderError={renderError}
labelClassName={labelClassName}
htmlFor={inputId}
>
{renderedKids as React.ReactNode}
</LemonPureField>
)
}
return <KeaField {...keaFieldProps} name={name} template={template} noStyle />
}
/** A field without Kea form functionality. Within a form use `LemonField`. */
LemonField.Pure = LemonPureField
LemonField.Error = LemonFieldError