import React, { ChangeEvent, HTMLAttributes, useContext } from 'react' import classNames from 'classnames' import { RadioGroupContext } from './util' interface RadioProps extends Omit, 'onChange'> { value?: V checked?: boolean onChange?: (event: ChangeEvent) => void disabled?: boolean name?: string } function Radio({ value, checked, onChange, onClick, children, name, disabled, className, ...rest }: RadioProps) { const radioGroupContext = useContext(RadioGroupContext) const handleChange = (event: ChangeEvent) => { onChange && onChange(event) if (radioGroupContext.isInRadioGroup) { radioGroupContext.onChange(value!) } } let oName = name let oChecked = checked if (radioGroupContext.isInRadioGroup) { oName = radioGroupContext.name oChecked = radioGroupContext.value === value if (checked !== undefined || name !== undefined || onChange !== undefined) { console.warn('在 RadioGroup 下,不能提供 checked name onChange') } } return ( ) } export default Radio export type { RadioProps }