Browse Source

fix: Form warning (#27343)

* fix: Not warning in drawer

* add test case
pull/27344/head
二货机器人 4 years ago
committed by GitHub
parent
commit
d6bd89e370
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      components/config-provider/context.tsx
  2. 15
      components/drawer/__tests__/Drawer.test.js
  3. 146
      components/drawer/index.tsx

2
components/config-provider/context.tsx

@ -61,6 +61,8 @@ interface ConsumerConfig {
interface ConstructorProps {
displayName?: string;
}
/** @deprecated Use hooks instead. This is a legacy function */
export function withConfigConsumer<ExportProps extends BasicExportProps>(config: ConsumerConfig) {
return function withConfigConsumerFunc<ComponentDef>(
Component: IReactComponent,

15
components/drawer/__tests__/Drawer.test.js

@ -1,6 +1,7 @@
import React from 'react';
import { render, mount } from 'enzyme';
import Drawer from '..';
import ConfigProvider from '../../config-provider';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
@ -134,4 +135,18 @@ describe('Drawer', () => {
);
expect(wrapper).toMatchSnapshot();
});
it('ConfigProvider should not warning', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
mount(
<ConfigProvider virtual>
<Drawer visible>Bamboo is Light</Drawer>
</ConfigProvider>,
);
expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockRestore();
});
});

146
components/drawer/index.tsx

@ -4,9 +4,7 @@ import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import classNames from 'classnames';
import omit from 'omit.js';
import { ConfigConsumerProps } from '../config-provider';
import { withConfigConsumer, ConfigConsumer } from '../config-provider/context';
import { ConfigContext } from '../config-provider';
import { tuple } from '../_util/type';
const DrawerContext = React.createContext<Drawer | null>(null);
@ -58,8 +56,12 @@ export interface IDrawerState {
push?: boolean;
}
interface InternalDrawerProps extends DrawerProps {
direction: 'rtl' | 'ltr' | undefined;
}
const defaultPushState: PushState = { distance: 180 };
class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerState> {
class Drawer extends React.Component<InternalDrawerProps, IDrawerState> {
static defaultProps = {
width: 256,
height: 256,
@ -275,80 +277,48 @@ class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerS
renderProvider = (value: Drawer) => {
this.parentDrawer = value;
const { prefixCls, placement, className, mask, direction, visible, ...rest } = this.props;
const drawerClassName = classNames(
{
'no-mask': !mask,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
const offsetStyle = mask ? this.getOffsetStyle() : {};
return (
<ConfigConsumer>
{({ getPopupContainer, getPrefixCls }) => {
const {
prefixCls: customizePrefixCls,
placement,
className,
mask,
direction,
visible,
...rest
} = this.props;
const prefixCls = getPrefixCls('select', customizePrefixCls);
const drawerClassName = classNames(
{
'no-mask': !mask,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
const offsetStyle = mask ? this.getOffsetStyle() : {};
return (
<DrawerContext.Provider value={this}>
<RcDrawer
handler={false}
{...omit(rest, [
'zIndex',
'style',
'closable',
'closeIcon',
'destroyOnClose',
'drawerStyle',
'headerStyle',
'bodyStyle',
'footerStyle',
'footer',
'locale',
'title',
'push',
'visible',
'getPopupContainer',
'rootPrefixCls',
'getPrefixCls',
'renderEmpty',
'csp',
'pageHeader',
'autoInsertSpaceInButton',
'width',
'height',
'dropdownMatchSelectWidth',
'getTargetContainer',
])}
getContainer={
// 有可能为 false,所以不能直接判断
rest.getContainer === undefined && getPopupContainer
? () => getPopupContainer(document.body)
: rest.getContainer
}
{...offsetStyle}
prefixCls={prefixCls}
open={visible}
showMask={mask}
placement={placement}
style={this.getRcDrawerStyle()}
className={drawerClassName}
>
{this.renderBody()}
</RcDrawer>
</DrawerContext.Provider>
);
}}
</ConfigConsumer>
<DrawerContext.Provider value={this}>
<RcDrawer
handler={false}
{...omit(rest, [
'zIndex',
'closable',
'closeIcon',
'destroyOnClose',
'drawerStyle',
'headerStyle',
'bodyStyle',
'footerStyle',
'footer',
'title',
'push',
'visible',
'width',
'height',
])}
{...offsetStyle}
prefixCls={prefixCls}
open={visible}
showMask={mask}
placement={placement}
style={this.getRcDrawerStyle()}
className={drawerClassName}
>
{this.renderBody()}
</RcDrawer>
</DrawerContext.Provider>
);
};
@ -357,6 +327,22 @@ class Drawer extends React.Component<DrawerProps & ConfigConsumerProps, IDrawerS
}
}
export default withConfigConsumer<DrawerProps>({
prefixCls: 'drawer',
})(Drawer);
const DrawerWrapper: React.FC<DrawerProps> = props => {
const { prefixCls: customizePrefixCls, getContainer: customizeGetContainer } = props;
const { getPopupContainer, getPrefixCls, direction } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('drawer', customizePrefixCls);
const getContainer =
// 有可能为 false,所以不能直接判断
customizeGetContainer === undefined && getPopupContainer
? () => getPopupContainer(document.body)
: customizeGetContainer;
return (
<Drawer {...props} prefixCls={prefixCls} getContainer={getContainer} direction={direction} />
);
};
DrawerWrapper.displayName = 'DrawerWrapper';
export default DrawerWrapper;

Loading…
Cancel
Save