diff --git a/src/components/Form/BaseRadioField.tsx b/src/components/Form/BaseRadioField.tsx index c6deb5f..8633957 100644 --- a/src/components/Form/BaseRadioField.tsx +++ b/src/components/Form/BaseRadioField.tsx @@ -1,4 +1,3 @@ -import { cva } from 'class-variance-authority'; import type { Simplify } from 'type-fest'; import { Label } from '../Label/Label.tsx'; @@ -7,25 +6,12 @@ import { FieldGroup } from './FieldGroup/FieldGroup.tsx'; import type { BaseFieldComponentProps } from './types.ts'; -const baseRadioFieldVariants = cva('flex', { - defaultVariants: { - orientation: 'vertical' - }, - variants: { - orientation: { - horizontal: 'flex-col @3xl:flex-row @3xl:items-center @3xl:justify-between', - vertical: 'flex-col' - } - } -}); - export type BaseRadioFieldProps = Simplify< BaseFieldComponentProps & { description?: string; disabled?: boolean; label: string; options: { [K in T]: string }; - orientation?: 'horizontal' | 'vertical'; } >; @@ -36,24 +22,17 @@ export const BaseRadioField = ({ label, name, options, - orientation = 'vertical', readOnly, setValue, value }: BaseRadioFieldProps) => { - const optionsCount = Object.keys(options).length; return ( - 5 ? 'vertical' : orientation })} - name={name} - value={value ?? ''} - onValueChange={(value) => setValue(value as T)} - > + setValue(value as T)}> {Object.keys(options).map((option) => (
diff --git a/src/components/Form/NumberField/NumberFieldRadio.tsx b/src/components/Form/NumberField/NumberFieldRadio.tsx index 23da611..ddba5dc 100644 --- a/src/components/Form/NumberField/NumberFieldRadio.tsx +++ b/src/components/Form/NumberField/NumberFieldRadio.tsx @@ -1,8 +1,9 @@ +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 { cn } from '#utils'; import { FieldGroup } from '../FieldGroup/FieldGroup.tsx'; @@ -24,8 +25,25 @@ export const NumberFieldRadio = ({ 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 ( @@ -33,28 +51,29 @@ export const NumberFieldRadio = ({ 5 ? 'flex-col' : 'flex-col @3xl:flex-row @3xl:items-center @3xl:justify-between' - )} + className="grid justify-between" name={name} + ref={radioGroupRef} + style={{ + gridTemplateColumns: isColumnLayout ? 'repeat(1, 1fr)' : `repeat(${optionsCount}, auto)` + }} value={value?.toString() ?? ''} - onValueChange={(value) => setValue(parseInt(value))} + onValueChange={(value) => setValue(parseInt(value, 10))} > {Object.keys(options) .map((val) => parseInt(val)) .toSorted((a, b) => a - b) .map((val) => { - const text = (disableAutoPrefix ? '' : `${val} - `) + options[val]; return ( -
+
); diff --git a/src/components/RadioGroup/RadioGroup.tsx b/src/components/RadioGroup/RadioGroup.tsx index 5d48a76..137d0ed 100644 --- a/src/components/RadioGroup/RadioGroup.tsx +++ b/src/components/RadioGroup/RadioGroup.tsx @@ -1,21 +1,16 @@ import * as React from 'react'; -import * as RadioGroupPrimitive from '@radix-ui/react-radio-group'; +import { Root } from '@radix-ui/react-radio-group'; import { cn } from '#utils'; import { RadioGroupItem } from './RadioGroupItem.tsx'; -type RadioGroupProps = React.ComponentPropsWithoutRef; +type RadioGroupProps = React.ComponentProps; -const RadioGroupRoot = React.forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(function RadioGroup({ className, ...props }, ref) { - return ( - - ); -}); +const RadioGroupRoot: React.FC = ({ className, ...props }) => { + return ; +}; export const RadioGroup = Object.assign(RadioGroupRoot, { Item: RadioGroupItem diff --git a/src/components/RadioGroup/RadioGroupItem.tsx b/src/components/RadioGroup/RadioGroupItem.tsx index e57a6c5..e2285a0 100644 --- a/src/components/RadioGroup/RadioGroupItem.tsx +++ b/src/components/RadioGroup/RadioGroupItem.tsx @@ -1,29 +1,23 @@ -import { forwardRef } from 'react'; - -import * as RadioGroupPrimitive from '@radix-ui/react-radio-group'; +import { Indicator, Item } from '@radix-ui/react-radio-group'; import { CircleIcon } from 'lucide-react'; import { cn } from '#utils'; -export const RadioGroupItem = forwardRef< - React.ElementRef, - React.ComponentPropsWithoutRef ->(function RadioGroupItem({ className, ...props }, ref) { +export const RadioGroupItem: React.FC> = ({ className, ...props }) => { return ( - - + - - + + ); -}); +};