import React from 'react' import { Text } from '../Text' import { Touchable } from '../Touchable' import { TypeGuards } from '@codeleap/types' import { View } from '../View' import { InputBase, selectInputBaseProps } from '../InputBase' import { RadioGroupProps, RadioOptionProps } from './types' import { AnyRecord, IJSX, StyledComponentProps } from '@codeleap/styles' import { MobileStyleRegistry } from '../../Registry' import { useStylesFor } from '../../hooks' import { useInputBasePartialStyles } from '../InputBase/useInputBasePartialStyles' import { useInputBase } from '../InputBase/useInputBase' import { SelectableField, fields } from '@codeleap/form' export * from './styles' export * from './types' const Option = (props: RadioOptionProps) => { const { debugName, item, disabled, styles, selected, onSelect, separator = false, reverseOrder, } = props const isDisabled = disabled || item.disabled const partialStyles = useInputBasePartialStyles( styles, ['optionLabel', 'optionWrapper', 'optionIndicator', 'optionIndicatorInner'], { selectedDisabled: isDisabled && selected, disabled: isDisabled, selected, } ) const label = TypeGuards.isString(item.label) ? : item.label return <> {reverseOrder ? ( <> {label} ) : ( <> {label} )} {separator ? : null} } export const RadioGroup = (props: RadioGroupProps) => { const { inputBaseProps, others, } = selectInputBaseProps({ ...RadioGroup.defaultProps, ...props, }) const { disabled, debugName, radioOnRight, style, field, options, value, onValueChange, } = others const styles = useStylesFor(RadioGroup.styleRegistryName, style) const { wrapperRef, inputValue, onInputValueChange, } = useInputBase( field as SelectableField, fields.selectable as () => SelectableField, { value, onValueChange } ) /** `radioOnRight` can be baked into the style variant via `__props` so themes can set the default layout without requiring a prop on every usage site. */ // @ts-expect-error icss type const _radioOnRight = radioOnRight ?? styles?.__props?.radioOnRight const hasValue = !TypeGuards.isNil(inputValue) return {options?.map((item, idx) => ( } RadioGroup.styleRegistryName = 'RadioGroup' RadioGroup.elements = [...InputBase.elements, 'option', '__props'] RadioGroup.rootElement = 'wrapper' RadioGroup.withVariantTypes = (styles: S) => { return RadioGroup as ((props: StyledComponentProps, typeof styles>) => IJSX) } RadioGroup.defaultProps = {} as Partial> MobileStyleRegistry.registerComponent(RadioGroup)