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.

1.5 KB
Raw Blame
import { useId } from 'react'

import { Field, ToggleGroup, ToggleGroupItem } from '@posthog/quill'

import type { ChartOptionToggleGroupProps } from './ChartOptionToggleGroup'
import { QuillChartOptionLabel } from './QuillChartOptionLabel'

export function QuillChartOptionToggleGroup<T extends string | number>({
    label,
    info,
    value,
    options,
    onChange,
    disabledReason,
    'data-attr': dataAttr,
}: ChartOptionToggleGroupProps<T>): JSX.Element {
    const id = useId()

    return (
        <Field data-attr={dataAttr}>
            {label && <QuillChartOptionLabel htmlFor={id} label={label} info={info} />}
            <ToggleGroup
                id={id}
                className="w-full"
                value={[String(value)]}
                onValueChange={(next: string[]) => {
                    const picked = options.find((option) => String(option.value) === next[0])
                    if (picked) {
                        onChange(picked.value)
                    }
                }}
            >
                {options.map((option) => (
                    <ToggleGroupItem
                        key={String(option.value)}
                        value={String(option.value)}
                        className="flex-1"
                        disabledReason={disabledReason || undefined}
                    >
                        {option.label}
                    </ToggleGroupItem>
                ))}
            </ToggleGroup>
        </Field>
    )
}