import { useEffect, useRef, useState } from 'react'; import type { NumberFormField } from '@douglasneuroinformatics/libui-form-types'; import type { Simplify } from 'type-fest'; import { Label, RadioGroup } from '#components'; import { FieldGroup } from '../FieldGroup/FieldGroup.tsx'; import type { BaseFieldComponentProps } from '../types.ts'; export type NumberFieldRadioProps = Simplify< BaseFieldComponentProps & Extract >; export const NumberFieldRadio = ({ description, disableAutoPrefix, disabled, error, label, name, options, readOnly, setValue, value }: NumberFieldRadioProps) => { const radioGroupRef = useRef(null); const [isColumnLayout, setIsColumnLayout] = useState(false); const optionsCount = Object.keys(options).length; useEffect(() => { const observer = new ResizeObserver(([entry]) => { const { width: rootWidth } = entry!.target.getBoundingClientRect(); const children = Array.from(entry!.target.children); const totalChildWidth = children.reduce((sum, child) => sum + child.scrollWidth, 0); const isOverflowing = totalChildWidth > rootWidth - children.length * 24; // to additional provide spacing between items setIsColumnLayout(isOverflowing); }); if (radioGroupRef.current) { observer.observe(radioGroupRef.current); } return () => observer.disconnect(); }, []); return ( setValue(parseInt(value, 10))} > {Object.keys(options) .map((val) => parseInt(val)) .toSorted((a, b) => a - b) .map((val) => { return (
); })}
); };