Browse Source

refactor(switch): rewrite with hook (#23552)

* refactor(switch): rewrite with hook

* fix: size

* fix

* fix test

* fix

* fix: size
pull/23644/head
xrkffgg 5 years ago
committed by GitHub
parent
commit
dabec4c833
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      components/switch/__tests__/index.test.js
  2. 59
      components/switch/index.tsx
  3. 3
      tests/shared/focusTest.js

2
components/switch/__tests__/index.test.js

@ -7,7 +7,7 @@ import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
describe('Switch', () => { describe('Switch', () => {
focusTest(Switch); focusTest(Switch, { refFocus: true });
mountTest(Switch); mountTest(Switch);
rtlTest(Switch); rtlTest(Switch);

59
components/switch/index.tsx

@ -5,9 +5,9 @@ import omit from 'omit.js';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined'; import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import Wave from '../_util/wave'; import Wave from '../_util/wave';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; import { ConfigContext } from '../config-provider';
import warning from '../_util/warning';
import SizeContext from '../config-provider/SizeContext'; import SizeContext from '../config-provider/SizeContext';
import warning from '../_util/warning';
export type SwitchSize = 'small' | 'default'; export type SwitchSize = 'small' | 'default';
export type SwitchChangeEventHandler = (checked: boolean, event: MouseEvent) => void; export type SwitchChangeEventHandler = (checked: boolean, event: MouseEvent) => void;
@ -30,48 +30,31 @@ export interface SwitchProps {
title?: string; title?: string;
} }
export default class Switch extends React.Component<SwitchProps, {}> { interface CompoundedComponent
static __ANT_SWITCH = true; extends React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLElement>> {
__ANT_SWITCH: boolean;
private rcSwitch: typeof RcSwitch; }
constructor(props: SwitchProps) {
super(props);
const Switch = React.forwardRef<unknown, SwitchProps>((props, ref) => {
warning( warning(
'checked' in props || !('value' in props), 'checked' in props || !('value' in props),
'Switch', 'Switch',
'`value` is not a valid prop, do you mean `checked`?', '`value` is not a valid prop, do you mean `checked`?',
); );
}
saveSwitch = (node: typeof RcSwitch) => {
this.rcSwitch = node;
};
focus() {
this.rcSwitch.focus();
}
blur() {
this.rcSwitch.blur();
}
renderSwitch = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
const { const {
prefixCls: customizePrefixCls, prefixCls: customizePrefixCls,
size: customizeSize, size: customizeSize,
loading, loading,
className = '', className = '',
disabled, disabled,
} = this.props; } = props;
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const size = React.useContext(SizeContext);
const prefixCls = getPrefixCls('switch', customizePrefixCls); const prefixCls = getPrefixCls('switch', customizePrefixCls);
const loadingIcon = loading ? ( const loadingIcon = loading ? <LoadingOutlined className={`${prefixCls}-loading-icon`} /> : null;
<LoadingOutlined className={`${prefixCls}-loading-icon`} />
) : null;
return (
<SizeContext.Consumer>
{size => {
const classes = classNames(className, { const classes = classNames(className, {
[`${prefixCls}-small`]: (customizeSize || size) === 'small', [`${prefixCls}-small`]: (customizeSize || size) === 'small',
[`${prefixCls}-loading`]: loading, [`${prefixCls}-loading`]: loading,
@ -81,21 +64,17 @@ export default class Switch extends React.Component<SwitchProps, {}> {
return ( return (
<Wave insertExtraNode> <Wave insertExtraNode>
<RcSwitch <RcSwitch
{...omit(this.props, ['loading'])} {...omit(props, ['loading'])}
prefixCls={prefixCls} prefixCls={prefixCls}
className={classes} className={classes}
disabled={disabled || loading} disabled={disabled || loading}
ref={this.saveSwitch} ref={ref}
loadingIcon={loadingIcon} loadingIcon={loadingIcon}
/> />
</Wave> </Wave>
); );
}} }) as CompoundedComponent;
</SizeContext.Consumer>
);
};
render() { Switch.__ANT_SWITCH = true;
return <ConfigConsumer>{this.renderSwitch}</ConfigConsumer>;
} export default Switch;
}

3
tests/shared/focusTest.js

@ -40,6 +40,9 @@ export default function focusTest(Component, { refFocus = false } = {}) {
const getElement = wrapper => { const getElement = wrapper => {
let ele = wrapper.find('input').first(); let ele = wrapper.find('input').first();
if (ele.length === 0) {
ele = wrapper.find('button').first();
}
if (ele.length === 0) { if (ele.length === 0) {
ele = wrapper.find('div[tabIndex]').first(); ele = wrapper.find('div[tabIndex]').first();
} }

Loading…
Cancel
Save