UNPKG

@yamada-ui/radio

Version:

Yamada UI radio component

1 lines 7.83 kB
{"version":3,"sources":["../src/radio-card.tsx"],"sourcesContent":["import type { ComponentArgs, HTMLUIProps, ThemeProps } from \"@yamada-ui/core\"\nimport type {\n ForwardedRef,\n InputHTMLAttributes,\n ReactElement,\n ReactNode,\n RefAttributes,\n} from \"react\"\nimport type { RadioCardAddonProps } from \"./radio-card-addon\"\nimport type { RadioCardDescriptionProps } from \"./radio-card-description\"\nimport type { RadioCardLabelProps } from \"./radio-card-label\"\nimport type { UseRadioProps } from \"./use-radio\"\nimport { omitThemeProps, ui, useComponentMultiStyle } from \"@yamada-ui/core\"\nimport { useFormControl } from \"@yamada-ui/form-control\"\nimport {\n cx,\n findChild,\n funcAll,\n getValidChildren,\n isEmpty,\n omitChildren,\n} from \"@yamada-ui/utils\"\nimport { forwardRef } from \"react\"\nimport { RadioCardAddon } from \"./radio-card-addon\"\nimport { RadioCardDescription } from \"./radio-card-description\"\nimport { RadioCardLabel } from \"./radio-card-label\"\nimport { RadioCardProvider, useRadioCardGroupContext } from \"./radio-context\"\nimport { useRadio } from \"./use-radio\"\n\ninterface RadioCardOptions {\n /**\n * The addon of the radio card.\n */\n addon?: ReactNode\n /**\n * The body of the radio card.\n */\n description?: ReactNode\n /**\n * The label of the radio card.\n */\n label?: ReactNode\n /**\n * If `true`, the icon will be displayed.\n *\n * @default true\n */\n withIcon?: boolean\n /**\n * Props for the footer of the radio card.\n */\n addonProps?: RadioCardAddonProps\n /**\n * Props for the description of the radio card.\n */\n descriptionProps?: RadioCardDescriptionProps\n /**\n * Props for the icon of the radio card.\n */\n iconProps?: HTMLUIProps\n /**\n * Props for input element.\n */\n inputProps?: InputHTMLAttributes<HTMLInputElement>\n /**\n * Props for the label of the radio card.\n */\n labelProps?: RadioCardLabelProps\n}\n\nexport interface RadioCardProps<Y extends number | string = string>\n extends Omit<HTMLUIProps<\"label\">, keyof UseRadioProps>,\n ThemeProps<\"RadioCard\">,\n UseRadioProps<Y>,\n RadioCardOptions {}\n\n/**\n * `RadioCard` is a component used for allowing users to select one option from multiple choices.\n *\n * @see Docs https://yamada-ui.com/components/forms/radio-card\n */\nexport const RadioCard = forwardRef(\n <Y extends number | string = string>(\n props: RadioCardProps<Y>,\n ref: ForwardedRef<HTMLInputElement>,\n ) => {\n const group = useRadioCardGroupContext()\n const { value: groupValue, ...groupProps } = { ...group }\n const control = useFormControl(props)\n const [styles, mergedProps] = useComponentMultiStyle(\"RadioCard\", {\n ...groupProps,\n ...props,\n })\n const {\n className,\n addon,\n children,\n description,\n disabled = groupProps.disabled ?? control.disabled,\n invalid = groupProps.invalid ?? control.invalid,\n label,\n readOnly = groupProps.readOnly ?? control.readOnly,\n required = groupProps.required ?? control.required,\n withIcon = true,\n addonProps,\n descriptionProps,\n iconProps,\n inputProps,\n labelProps,\n ...computedProps\n } = omitThemeProps(mergedProps)\n\n computedProps.checked ??= computedProps.isChecked\n\n const checkedProp =\n groupValue && computedProps.value\n ? groupValue === computedProps.value\n : computedProps.checked\n\n const onChange =\n groupProps.onChange && computedProps.value\n ? funcAll(groupProps.onChange, computedProps.onChange)\n : computedProps.onChange\n\n const {\n checked,\n props: rest,\n getContainerProps,\n getIconProps,\n getInputProps,\n } = useRadio({\n ...computedProps,\n checked: checkedProp,\n disabled,\n invalid,\n readOnly,\n required,\n onChange,\n })\n\n const tabIndex = !groupValue ? 0 : checked ? 0 : -1\n\n const validChildren = getValidChildren(children)\n const customLabel = findChild(validChildren, RadioCardLabel)\n const customDescription = findChild(validChildren, RadioCardDescription)\n const customAddon = findChild(validChildren, RadioCardAddon)\n\n const computedChildren = !isEmpty(validChildren)\n ? omitChildren(\n validChildren,\n RadioCardLabel,\n RadioCardDescription,\n RadioCardAddon,\n )\n : children\n\n return (\n <RadioCardProvider value={{ styles, withIcon, getIconProps, iconProps }}>\n <ui.label\n className={cx(\"ui-radio-card\", className)}\n {...getContainerProps(rest)}\n __css={{ ...styles.container }}\n >\n <ui.input\n className=\"ui-radio-card__input\"\n {...getInputProps(\n {\n ...inputProps,\n tabIndex,\n },\n ref,\n )}\n />\n\n {customLabel ??\n (label ? (\n <RadioCardLabel {...labelProps}>{label}</RadioCardLabel>\n ) : null)}\n\n {customDescription ??\n (description ? (\n <RadioCardDescription {...descriptionProps}>\n {description}\n </RadioCardDescription>\n ) : null)}\n\n {customAddon ??\n (addon ? (\n <RadioCardAddon {...addonProps}>{addon}</RadioCardAddon>\n ) : null)}\n\n {computedChildren}\n </ui.label>\n </RadioCardProvider>\n )\n },\n) as {\n <Y extends number | string = string>(\n props: RadioCardProps<Y> & RefAttributes<HTMLInputElement>,\n ): ReactElement\n} & ComponentArgs\n\nRadioCard.displayName = \"RadioCard\"\nRadioCard.__ui__ = \"RadioCard\"\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAYA,SAAS,gBAAgB,IAAI,8BAA8B;AAC3D,SAAS,sBAAsB;AAC/B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,kBAAkB;AAwInB,SAKE,KALF;AA7ED,IAAM,YAAY;AAAA,EACvB,CACE,OACA,QACG;AArFP;AAsFI,UAAM,QAAQ,yBAAyB;AACvC,UAAM,EAAE,OAAO,YAAY,GAAG,WAAW,IAAI,EAAE,GAAG,MAAM;AACxD,UAAM,UAAU,eAAe,KAAK;AACpC,UAAM,CAAC,QAAQ,WAAW,IAAI,uBAAuB,aAAa;AAAA,MAChE,GAAG;AAAA,MACH,GAAG;AAAA,IACL,CAAC;AACD,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,YAAW,gBAAW,aAAX,YAAuB,QAAQ;AAAA,MAC1C,WAAU,gBAAW,YAAX,YAAsB,QAAQ;AAAA,MACxC;AAAA,MACA,YAAW,gBAAW,aAAX,YAAuB,QAAQ;AAAA,MAC1C,YAAW,gBAAW,aAAX,YAAuB,QAAQ;AAAA,MAC1C,WAAW;AAAA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