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.

3.5 KB
Raw Blame
import clsx from 'clsx'
import { useId } from 'react'

import { Tooltip } from 'lib/lemon-ui/Tooltip'

export interface LemonRadioOption<T extends React.Key> {
    label: string | JSX.Element
    description?: string | JSX.Element
    value: T
    disabledReason?: string
    'data-attr'?: string
    'aria-label'?: string
}

export interface LemonRadioProps<T extends React.Key> {
    value?: T
    onChange: (newValue: T) => void
    options: LemonRadioOption<T>[]
    className?: string
    radioPosition?: 'center' | 'top'
    orientation?: 'vertical' | 'horizontal'
    /** Accessible name for the radio group, announced by screen readers. */
    'aria-label'?: string
    'data-attr'?: string
}

/** Single choice radio. */
export function LemonRadio<T extends React.Key>({
    value: selectedValue,
    onChange,
    options,
    className,
    radioPosition,
    orientation = 'vertical',
    'aria-label': ariaLabel,
    'data-attr': dataAttr,
}: LemonRadioProps<T>): JSX.Element {
    // A shared name makes the inputs one native radio group, so arrow keys move between them.
    const groupName = useId()
    return (
        <div
            role="radiogroup"
            aria-label={ariaLabel}
            data-attr={dataAttr}
            className={clsx(
                'flex font-medium',
                orientation === 'vertical' ? 'flex-col gap-2' : 'flex-row gap-4',
                className
            )}
        >
            {options.map(({ value, label, disabledReason, description, ...optionProps }) => {
                const content = (
                    <label
                        key={value}
                        className={clsx(
                            'grid gap-x-2 grid-cols-[min-content_auto] text-sm',
                            disabledReason ? 'text-secondary cursor-not-allowed' : 'cursor-pointer',
                            {
                                // baseline tracks the label's first text line even when the
                                // label JSX uses its own font size or line-height
                                'items-baseline': radioPosition === 'top',
                                'items-center': radioPosition === 'center' || !radioPosition,
                            }
                        )}
                    >
                        <input
                            type="radio"
                            className="cursor-pointer"
                            name={groupName}
                            checked={value === selectedValue}
                            value={String(value)}
                            onChange={() => {
                                if (!disabledReason) {
                                    onChange(value)
                                }
                            }}
                            disabled={!!disabledReason}
                            {...optionProps}
                        />
                        <span>{label}</span>
                        {description && (
                            <div className="text-secondary font-normal row-start-2 col-start-2 text-pretty text-xs">
                                {description}
                            </div>
                        )}
                    </label>
                )

                if (disabledReason) {
                    return (
                        <Tooltip key={value} title={disabledReason}>
                            {content}
                        </Tooltip>
                    )
                }
                return content
            })}
        </div>
    )
}