mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-14 13:51:41 +08:00

Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
* feat: use cssVar by default * chore: update deps * chore: update snapshot * chore: update snapshot * chore: update snapshot * chore: test * chore: update * chore: update scripts * chore: UPDATE TEST * feat: add root * chore: update snapshot * chore: fix test case * chore: fix cycle deps * feat: rm legacy css variables configuration * chore: update test case * chore: update * chore: fix test * chore: update overrides * chore: bump cssinjs * chore: add test case
160 lines
4.1 KiB
TypeScript
Executable File
160 lines
4.1 KiB
TypeScript
Executable File
import * as React from 'react';
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
import RcSwitch from '@rc-component/switch';
|
|
import type { SwitchChangeEventHandler, SwitchClickEventHandler } from '@rc-component/switch';
|
|
import useMergedState from '@rc-component/util/lib/hooks/useMergedState';
|
|
import classNames from 'classnames';
|
|
|
|
import Wave from '../_util/wave';
|
|
import { useComponentConfig } from '../config-provider/context';
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
import useSize from '../config-provider/hooks/useSize';
|
|
import useStyle from './style';
|
|
|
|
export type SwitchSize = 'small' | 'default';
|
|
export type { SwitchChangeEventHandler, SwitchClickEventHandler };
|
|
|
|
type SemanticName = 'root' | 'content';
|
|
export interface SwitchProps {
|
|
prefixCls?: string;
|
|
size?: SwitchSize;
|
|
className?: string;
|
|
rootClassName?: string;
|
|
checked?: boolean;
|
|
defaultChecked?: boolean;
|
|
/**
|
|
* Alias for `checked`.
|
|
* @since 5.12.0
|
|
*/
|
|
value?: boolean;
|
|
/**
|
|
* Alias for `defaultChecked`.
|
|
* @since 5.12.0
|
|
*/
|
|
defaultValue?: boolean;
|
|
onChange?: SwitchChangeEventHandler;
|
|
onClick?: SwitchClickEventHandler;
|
|
checkedChildren?: React.ReactNode;
|
|
unCheckedChildren?: React.ReactNode;
|
|
disabled?: boolean;
|
|
loading?: boolean;
|
|
autoFocus?: boolean;
|
|
style?: React.CSSProperties;
|
|
title?: string;
|
|
tabIndex?: number;
|
|
id?: string;
|
|
classNames?: Partial<Record<SemanticName, string>>;
|
|
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
|
|
}
|
|
|
|
const InternalSwitch = React.forwardRef<HTMLButtonElement, SwitchProps>((props, ref) => {
|
|
const {
|
|
prefixCls: customizePrefixCls,
|
|
size: customizeSize,
|
|
disabled: customDisabled,
|
|
loading,
|
|
className,
|
|
rootClassName,
|
|
style,
|
|
checked: checkedProp,
|
|
value,
|
|
defaultChecked: defaultCheckedProp,
|
|
defaultValue,
|
|
onChange,
|
|
styles,
|
|
classNames: switchClassNames,
|
|
...restProps
|
|
} = props;
|
|
|
|
const [checked, setChecked] = useMergedState<boolean>(false, {
|
|
value: checkedProp ?? value,
|
|
defaultValue: defaultCheckedProp ?? defaultValue,
|
|
});
|
|
|
|
const {
|
|
getPrefixCls,
|
|
direction,
|
|
className: contextClassName,
|
|
style: contextStyle,
|
|
classNames: contextClassNames,
|
|
styles: contextStyles,
|
|
} = useComponentConfig('switch');
|
|
|
|
// ===================== Disabled =====================
|
|
const disabled = React.useContext(DisabledContext);
|
|
const mergedDisabled = (customDisabled ?? disabled) || loading;
|
|
|
|
const prefixCls = getPrefixCls('switch', customizePrefixCls);
|
|
|
|
const loadingIcon = (
|
|
<div className={`${prefixCls}-handle`}>
|
|
{loading && <LoadingOutlined className={`${prefixCls}-loading-icon`} />}
|
|
</div>
|
|
);
|
|
|
|
// Style
|
|
const [hashId, cssVarCls] = useStyle(prefixCls);
|
|
|
|
const mergedSize = useSize(customizeSize);
|
|
|
|
const classes = classNames(
|
|
contextClassName,
|
|
{
|
|
[`${prefixCls}-small`]: mergedSize === 'small',
|
|
[`${prefixCls}-loading`]: loading,
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
},
|
|
className,
|
|
rootClassName,
|
|
switchClassNames?.root,
|
|
contextClassNames.root,
|
|
hashId,
|
|
cssVarCls,
|
|
);
|
|
|
|
const mergedStyle: React.CSSProperties = {
|
|
...contextStyles.root,
|
|
...styles?.root,
|
|
...contextStyle,
|
|
...style,
|
|
};
|
|
|
|
const changeHandler: SwitchChangeEventHandler = (...args) => {
|
|
setChecked(args[0]);
|
|
onChange?.(...args);
|
|
};
|
|
|
|
return (
|
|
<Wave component="Switch">
|
|
<RcSwitch
|
|
{...restProps}
|
|
classNames={{ content: classNames(contextClassNames.content, switchClassNames?.content) }}
|
|
styles={{ content: { ...contextStyles.content, ...styles?.content } }}
|
|
checked={checked}
|
|
onChange={changeHandler}
|
|
prefixCls={prefixCls}
|
|
className={classes}
|
|
style={mergedStyle}
|
|
disabled={mergedDisabled}
|
|
ref={ref}
|
|
loadingIcon={loadingIcon}
|
|
/>
|
|
</Wave>
|
|
);
|
|
});
|
|
|
|
type CompoundedComponent = typeof InternalSwitch & {
|
|
/** @internal */
|
|
__ANT_SWITCH: boolean;
|
|
};
|
|
|
|
const Switch = InternalSwitch as CompoundedComponent;
|
|
|
|
Switch.__ANT_SWITCH = true;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
Switch.displayName = 'Switch';
|
|
}
|
|
|
|
export default Switch;
|