import React, { useState, useMemo } from 'react'; import { IProps, HTMLButtonProps } from '@uiw/utils'; import Button from '@uiw/react-button'; /** * Constructs a type by picking all properties from `HTMLInputProps` and then removing `size`. * Omit: https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk */ export interface RadioButtonProps extends IProps, Omit { size?: 'large' | 'default' | 'small'; checked?: boolean; disabled?: boolean; onChange?: (even: any) => void; } export const RadioButton = React.forwardRef((props, ref) => { const { prefixCls = 'w-radio', type = 'button', disabled = false, value = '', className, style, children, size = 'small', checked: prChecked = false, onChange, ...other } = props; const [checked, setChecked] = useState(prChecked); const [prevChecked, setPrevChecked] = useState(); if (prChecked !== prevChecked) { setPrevChecked(prChecked); } useMemo(() => { if (prChecked !== prevChecked) { setChecked(prChecked); } }, [prevChecked]); const cls = [prefixCls, className, disabled ? 'disabled' : null, size ? `${prefixCls}-${size}` : null] .filter(Boolean) .join(' ') .trim(); useMemo(() => { if (checked !== props.checked) { setChecked(!!props.checked); } }, [props.checked]); console.log('props.checked', props.checked); function handleChange(e: React.MouseEvent & MouseEvent) { e.persist(); if (!checked) { setChecked(!checked); onChange && onChange(value); } } const label = children || value; return ( ); }); export default RadioButton;