File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -215,6 +215,45 @@ describe('Drawer', () => {
215215 } ) ;
216216 } ) ;
217217
218+ it ( 'calls onRequestClose when closeOnOverlayClick is not defined' , async ( ) => {
219+ const onRequestClose = jest . fn ( ) ;
220+
221+ const { container } = render (
222+ < SetupDrawerWithChildren
223+ visible
224+ onRequestClose = { onRequestClose }
225+ /> ,
226+ ) ;
227+
228+ const [ overlay ] = Array . from ( container . getElementsByClassName ( 'DrawerBackgroundOverlay' ) ) ;
229+
230+ userEvent . click ( overlay ) ;
231+
232+ await waitFor ( ( ) => {
233+ expect ( onRequestClose ) . toHaveBeenCalled ( ) ;
234+ } ) ;
235+ } ) ;
236+
237+ it ( 'does not call onRequestClose when closeOnOverlayClick is set to false' , async ( ) => {
238+ const onRequestClose = jest . fn ( ) ;
239+
240+ const { container } = render (
241+ < SetupDrawerWithChildren
242+ closeOnOverlayClick = { false }
243+ visible
244+ onRequestClose = { onRequestClose }
245+ /> ,
246+ ) ;
247+
248+ const [ overlay ] = Array . from ( container . getElementsByClassName ( 'DrawerBackgroundOverlay' ) ) ;
249+
250+ userEvent . click ( overlay ) ;
251+
252+ await waitFor ( ( ) => {
253+ expect ( onRequestClose ) . not . toHaveBeenCalled ( ) ;
254+ } ) ;
255+ } ) ;
256+
218257 it ( 'body tag has Drawer--open' , ( ) => {
219258 const { container } = render ( < SetupDrawerWithChildren visible /> ) ;
220259 const body = container . closest ( 'body' ) ;
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ type DrawerProps = {
2828 behindNav ?: boolean ;
2929 children ?: React . ReactNode ;
3030 className ?: string ;
31+ closeOnOverlayClick ?: boolean ;
3132 defaultExpanded ?: boolean ;
3233 expandable ?: boolean ;
3334 hasBackgroundOverlay ?: boolean ;
@@ -42,6 +43,7 @@ function Drawer({
4243 children,
4344 className = '' ,
4445 defaultExpanded = false ,
46+ closeOnOverlayClick = true ,
4547 expandable = false ,
4648 hasBackgroundOverlay = true ,
4749 visible,
@@ -122,7 +124,7 @@ function Drawer({
122124 } )
123125 }
124126 role = "presentation"
125- onClick = { onRequestClose }
127+ onClick = { closeOnOverlayClick ? onRequestClose : undefined }
126128 onKeyDown = { handleEscKeyPress }
127129 />
128130 )
You can’t perform that action at this time.
0 commit comments