UNPKG

@brizy/ui

Version:
24 lines (23 loc) 1.34 kB
import React, { useMemo, useCallback, isValidElement } from "react"; import AntRadioGroup from "antd/lib/radio/group"; import { classNames } from "../classNamesFn"; export const RadioGroup = ({ value, children, type = "default", options, disabled, onChange, className, }) => { const optionType = type === "default" ? "default" : "button"; const buttonStyle = type === "default" ? undefined : type; //Here valueProp is made because of bug from antd that make the options to be without checked state const valueProp = value ? { value } : {}; const _className = classNames(className)("radio-group"); const renderChildren = useMemo(() => { return React.Children.map(children, child => { if (isValidElement(child) && type !== "default") { const props = { type: "button" }; return React.cloneElement(child, props); } return child; }); }, [children, type]); const _onChange = useCallback((e) => { onChange === null || onChange === void 0 ? void 0 : onChange(e.target.value); }, [onChange]); return (React.createElement(AntRadioGroup, Object.assign({ optionType: optionType, buttonStyle: buttonStyle, options: options, disabled: disabled, className: _className, onChange: _onChange }, valueProp), renderChildren)); };