mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-01 06:49:32 +08:00
10427f7c3d
* feat: basic convert to cssinjs * feat: update * fix: lint issue * fix: remove important * chore: update * chore: update * refactor: add optionType for Radio instead of using prefixCls * fix: use colorPrimaryOutline for primary-1 * chore: add some comments * fix: conflict code cleanup * feat: use motionDurationSlow instead of radioDuration * chore: update * chore: update * fix: use isnert-inline for position left/right * fix: use css logical properties and values * fix: padding
80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
import * as React from 'react';
|
|
import RcCheckbox from 'rc-checkbox';
|
|
import classNames from 'classnames';
|
|
import { composeRef } from 'rc-util/lib/ref';
|
|
import { useContext } from 'react';
|
|
import { FormItemInputContext } from '../form/context';
|
|
import { RadioProps, RadioChangeEvent } from './interface';
|
|
import { ConfigContext } from '../config-provider';
|
|
import RadioGroupContext, { RadioOptionTypeContext } from './context';
|
|
import devWarning from '../_util/devWarning';
|
|
|
|
import useStyle from './style';
|
|
|
|
const InternalRadio: React.ForwardRefRenderFunction<HTMLElement, RadioProps> = (props, ref) => {
|
|
const groupContext = React.useContext(RadioGroupContext);
|
|
const radioOptionTypeContext = React.useContext(RadioOptionTypeContext);
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
const innerRef = React.useRef<HTMLElement>();
|
|
const mergedRef = composeRef(ref, innerRef);
|
|
const { isFormItemInput } = useContext(FormItemInputContext);
|
|
|
|
React.useEffect(() => {
|
|
devWarning(!('optionType' in props), 'Radio', '`optionType` is only support in Radio.Group.');
|
|
}, []);
|
|
|
|
const onChange = (e: RadioChangeEvent) => {
|
|
props.onChange?.(e);
|
|
groupContext?.onChange?.(e);
|
|
};
|
|
|
|
const { prefixCls: customizePrefixCls, className, children, style, ...restProps } = props;
|
|
const radioPrefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
const prefixCls =
|
|
(groupContext?.optionType || radioOptionTypeContext) === 'button'
|
|
? `${radioPrefixCls}-button`
|
|
: radioPrefixCls;
|
|
|
|
// Style
|
|
const [wrapSSR, hashId] = useStyle(radioPrefixCls, getPrefixCls());
|
|
|
|
const radioProps: RadioProps = { ...restProps };
|
|
if (groupContext) {
|
|
radioProps.name = groupContext.name;
|
|
radioProps.onChange = onChange;
|
|
radioProps.checked = props.value === groupContext.value;
|
|
radioProps.disabled = props.disabled || groupContext.disabled;
|
|
}
|
|
const wrapperClassString = classNames(
|
|
`${prefixCls}-wrapper`,
|
|
{
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
|
[`${prefixCls}-wrapper-rtl`]: direction === 'rtl',
|
|
[`${prefixCls}-wrapper-in-form-item`]: isFormItemInput,
|
|
},
|
|
className,
|
|
hashId,
|
|
);
|
|
|
|
return wrapSSR(
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
|
<label
|
|
className={wrapperClassString}
|
|
style={style}
|
|
onMouseEnter={props.onMouseEnter}
|
|
onMouseLeave={props.onMouseLeave}
|
|
>
|
|
<RcCheckbox {...radioProps} type="radio" prefixCls={prefixCls} ref={mergedRef} />
|
|
{children !== undefined ? <span>{children}</span> : null}
|
|
</label>,
|
|
);
|
|
};
|
|
|
|
const Radio = React.forwardRef<unknown, RadioProps>(InternalRadio);
|
|
|
|
Radio.displayName = 'Radio';
|
|
|
|
export default Radio;
|