import * as React from 'react'; import cx from 'classnames'; import Radio from '../form-elements/radio/Radio'; import generateRandomString from '../../js/generateRandomString'; import {useCardRadioGroupContext} from './CardRadioGroupContext'; import type {StyleType} from './types'; export interface CardRadioPropsType extends Omit, 'onChange'> { /** * Required string. Value of the CardRadio input. */ value: string; /** * Optional boolean. Whether the Radio is required. * @default false */ required?: boolean; /** * Optional boolean. Whether the Radio is disabled. * @default false */ disabled?: boolean; /** * Optional boolean. Whether the Radio is invalid. * @default false */ invalid?: boolean; /** * Optional string. ID of the Radio. */ id?: string; /** * Optional string. Variant of the card. Default is 'outline'. */ variant?: 'solid' | 'outline'; /** * Optional string. Color of the card. Default is 'dark'. */ color?: 'light' | 'dark'; /** * Optional string. Additional class names. */ className?: string; /** * Optional React.ReactNode. Children of the card. This is the place where label should be used and connected to the card. * @example Card content */ children?: React.ReactNode; /** * Optional string. Width of the card. * @example */ width?: React.CSSProperties['width']; /** * Optional string. Height of the card. * @example */ height?: React.CSSProperties['height']; /** * Optional object. Inline styles. * @example */ style?: StyleType; /** * Function called whenever the state of the Radio changes. */ onChange?: (e: React.ChangeEvent) => void; /** * Optional string. ID of the element that labels the Radio. * @example **/ 'aria-labelledby'?: string; /** * Optional string. ID of the element that describes the Radio. * @example **/ 'aria-describedby'?: string; } type CardRadioContextType = { checked: boolean; disabled: boolean; }; export const CardRadioContext = React.createContext({ checked: false, disabled: false, }); const CardRadio = React.forwardRef( ( { variant = 'outline', color = 'dark', className, children, width, height, style, // radio related props id, disabled, required = false, invalid = false, value, onChange, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, ...props }: CardRadioPropsType, ref ) => { const context = useCardRadioGroupContext(); const cardId = React.useMemo(() => id || generateRandomString(), [id]); const isChecked = context.value === value; const isRequired = context.required || required; const isDisabled = context.disabled || disabled; const isInvalid = context.invalid || invalid; const cssVariables = { '--card-width': width, '--card-height': height, }; const handleInputChange = (e: React.ChangeEvent) => { if (context.onChange) { context.onChange(e.target.value); } if (onChange) { onChange(e); } }; return ( ); } ); export interface CardRadioIndicatorPropsType { slot?: | 'top-left' | 'center-left' | 'bottom-left' | 'top-right' | 'center-right' | 'bottom-right'; style?: React.CSSProperties; className?: string; } export function CardRadioIndicator({ slot = 'top-left', style, className, }: CardRadioIndicatorPropsType) { const {checked, disabled} = React.useContext(CardRadioContext); return (
); } CardRadioIndicator.displayName = 'CardRadioIndicator'; export {CardRadio};