import React, { useContext } from "react"; import classNames from "classnames"; import { Omit, StyledProps } from "../_type"; import { CheckProps, CheckContext } from "../check"; import { Text } from "../text"; import { useDefaultValue } from "../form/controlled"; import { callBoth } from "../_util/call-both"; import { Tooltip } from "../tooltip"; import { useConfig } from "../_util/config-context"; import { noop } from "../_util/noop"; /** * Switch 组件所接收的参数 */ export interface SwitchProps extends Omit, StyledProps { /** * 是否处于加载中状态 */ loading?: boolean; } export const Switch = React.forwardRef(function Switch( props: SwitchProps, ref: React.Ref ) { const { classPrefix } = useConfig(); // 支持从 Context 注入 const context = useContext(CheckContext); if (context) { const checkProps = context.inject({ type: "checkbox", ...props }); delete checkProps.type; // eslint-disable-next-line no-param-reassign props = checkProps; } const { name, value, disabled, onChange = noop, onClick = noop, loading, tooltip, children, className, ...extraProps } = useDefaultValue(props, false); let component = ( ); // 默认是不需要 children 的,如果提供了,需要包装一层 if (children) { component = ( ); } if (tooltip) { component = {component}; } return component; }); Switch.displayName = "Switch";