diff --git a/components/config-provider/context.tsx b/components/config-provider/context.tsx index cd9d8c2442..e9ff1c8c09 100644 --- a/components/config-provider/context.tsx +++ b/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(config: ConsumerConfig) { return function withConfigConsumerFunc( Component: IReactComponent, diff --git a/components/drawer/__tests__/Drawer.test.js b/components/drawer/__tests__/Drawer.test.js index 77085693ae..94ab065887 100644 --- a/components/drawer/__tests__/Drawer.test.js +++ b/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( + + Bamboo is Light + , + ); + + expect(errorSpy).not.toHaveBeenCalled(); + + errorSpy.mockRestore(); + }); }); diff --git a/components/drawer/index.tsx b/components/drawer/index.tsx index 659a2e13ac..bd069736fa 100644 --- a/components/drawer/index.tsx +++ b/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(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 { +class Drawer extends React.Component { static defaultProps = { width: 256, height: 256, @@ -275,80 +277,48 @@ class Drawer extends React.Component { 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 ( - - {({ 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 ( - - getPopupContainer(document.body) - : rest.getContainer - } - {...offsetStyle} - prefixCls={prefixCls} - open={visible} - showMask={mask} - placement={placement} - style={this.getRcDrawerStyle()} - className={drawerClassName} - > - {this.renderBody()} - - - ); - }} - + + + {this.renderBody()} + + ); }; @@ -357,6 +327,22 @@ class Drawer extends React.Component({ - prefixCls: 'drawer', -})(Drawer); +const DrawerWrapper: React.FC = 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 ( + + ); +}; + +DrawerWrapper.displayName = 'DrawerWrapper'; + +export default DrawerWrapper;