-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utils: Adding the "accept" utility as a replacement for window.confirm
- Loading branch information
1 parent
e6e7f14
commit 7242d03
Showing
8 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import clickOutside from 'react-click-outside'; | ||
|
||
/** | ||
* WordPress Dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
import { Button } from 'components'; | ||
import { __ } from 'i18n'; | ||
|
||
class AcceptDialog extends Component { | ||
handleClickOutside() { | ||
this.props.onClose( 'cancel' ); | ||
} | ||
|
||
render() { | ||
const { message, onClose, confirmButtonText, cancelButtonText } = this.props; | ||
const accept = () => onClose( 'accept' ); | ||
const cancel = () => onClose( 'cancel' ); | ||
|
||
return ( | ||
<div> | ||
<div className="utils-accept__dialog"> | ||
<div className="utils-accept__dialog-content"> | ||
{ message } | ||
</div> | ||
<div className="utils-accept__dialog-buttons"> | ||
<Button onClick={ cancel } className="button"> | ||
{ cancelButtonText || __( 'Cancel' ) } | ||
</Button> | ||
<Button isPrimary onClick={ accept }> | ||
{ confirmButtonText || __( 'OK' ) } | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default clickOutside( AcceptDialog ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import AcceptDialog from './dialog'; | ||
|
||
export default function accept( message, callback, confirmButtonText, cancelButtonText ) { | ||
let wrapper = document.createElement( 'div' ); | ||
wrapper.className = 'utils-accept__backdrop'; | ||
document.body.appendChild( wrapper ); | ||
|
||
function onClose( result ) { | ||
if ( wrapper ) { | ||
unmountComponentAtNode( wrapper ); | ||
document.body.removeChild( wrapper ); | ||
wrapper = null; | ||
} | ||
|
||
if ( callback ) { | ||
callback( result === 'accept' ); | ||
} | ||
} | ||
|
||
render( | ||
<AcceptDialog | ||
message={ message } | ||
onClose={ onClose } | ||
confirmButtonText={ confirmButtonText } | ||
cancelButtonText={ cancelButtonText } | ||
/>, | ||
wrapper | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.utils-accept__dialog { | ||
top: 10px; | ||
bottom: 10px; | ||
width: 400px; | ||
margin: auto; | ||
background: $white; | ||
box-shadow: $shadow-popover; | ||
border: 1px solid $light-gray-500; | ||
} | ||
|
||
.utils-accept__dialog-content { | ||
padding: 20px; | ||
} | ||
|
||
.utils-accept__dialog-buttons { | ||
text-align: right; | ||
padding: 10px 20px; | ||
border-top: 1px solid $light-gray-500; | ||
|
||
.components-button { | ||
margin-left: 10px; | ||
} | ||
} | ||
|
||
.utils-accept__backdrop { | ||
align-items: center; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
top: 0; | ||
display: flex; | ||
justify-content: center; | ||
position: fixed; | ||
z-index: z-index( '.utils-accept__backdrop' ); | ||
background-color: rgba( $light-gray-900, 0.8 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import accept from '../'; | ||
|
||
describe( '#accept()', () => { | ||
beforeEach( () => { | ||
document.body.innerHTML = ''; | ||
} ); | ||
|
||
it( 'should render a dialog to the document body', () => { | ||
const message = 'Are you sure?'; | ||
|
||
accept( message, () => {} ); | ||
|
||
const dialog = document.querySelector( '.utils-accept__dialog-content' ); | ||
expect( dialog ).to.be.an.instanceof( window.Element ); | ||
expect( dialog.textContent ).to.equal( message ); | ||
} ); | ||
|
||
it( 'should trigger the callback with an accepted prompt', ( done ) => { | ||
accept( 'Are you sure?', ( accepted ) => { | ||
expect( accepted ).to.be.be.true(); | ||
done(); | ||
} ); | ||
|
||
document.querySelector( '.components-button.button-primary' ).click(); | ||
} ); | ||
|
||
it( 'should trigger the callback with a denied prompt', ( done ) => { | ||
accept( 'Are you sure?', ( accepted ) => { | ||
expect( accepted ).to.be.be.false(); | ||
done(); | ||
} ); | ||
|
||
document.querySelector( '.components-button:not( .button-primary )' ).click(); | ||
} ); | ||
|
||
it( 'should clean up after itself once the prompt is closed', ( done ) => { | ||
accept( 'Are you sure?', () => { | ||
process.nextTick( () => { | ||
expect( document.querySelector( '.utils-accept__dialog' ) ).to.be.null(); | ||
|
||
done(); | ||
} ); | ||
} ); | ||
|
||
document.querySelector( '.components-button.button-primary' ).click(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as keycodes from './keycodes'; | ||
|
||
export { default as accept } from './accept'; | ||
export { keycodes }; |