Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide AMP Noloading toggle if block does not have specified setting #5574

Merged
merged 5 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions assets/src/block-editor/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,42 @@ AmpLayoutControl.propTypes = {
/**
* Get AMP Noloading toggle control.
*
* @deprecated As of v2.1. Blocks with the `ampNoLoading` attribute will still be able to use the control.
*
* @param {Object} props Props.
*
* @return {ReactElement} Element.
*/
const AmpNoloadingToggle = ( props ) => {
export const AmpNoloadingToggle = ( props ) => {
const { attributes: { ampNoLoading }, setAttributes } = props;

if ( undefined === ampNoLoading ) {
return null;
}

const label = __( 'AMP Noloading', 'amp' );

return (
<ToggleControl
label={ label }
checked={ ampNoLoading }
onChange={ () => setAttributes( { ampNoLoading: ! ampNoLoading } ) }
/>
<>
<Notice
status="warning"
isDismissible={ false }
>
<span dangerouslySetInnerHTML={ {
__html: sprintf(
/* translators: placeholder is link to support forum. */
__( 'The AMP Noloading setting is deprecated and is slated for removal. Please <a href="%s" target="_blank" rel="noreferrer">report</a> if you need it.', 'amp' ),
'https://wordpress.org/support/plugin/amp/#new-topic-0',
),
} } />
</Notice>

<ToggleControl
label={ label }
checked={ ampNoLoading }
onChange={ () => setAttributes( { ampNoLoading: ! ampNoLoading } ) }
/>
</>
);
};

Expand Down
46 changes: 46 additions & 0 deletions assets/src/block-editor/helpers/test/AmpNoloadingToggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import { act } from 'react-dom/test-utils';

/**
* WordPress dependencies
*/
import { render } from '@wordpress/element';

/**
* Internal dependencies
*/
import { AmpNoloadingToggle } from '../';

let container;

describe( 'AmpNoloadingToggle', () => {
beforeEach( () => {
container = document.createElement( 'div' );
document.body.appendChild( container );
} );

afterEach( () => {
document.body.removeChild( container );
container = null;
} );

it( 'should not render if ampNoLoading is undefined', function() {
act( () => {
render( <AmpNoloadingToggle setAttributes={ () => {} } attributes={ {} } />, container );
} );

const selectControl = container.querySelector( 'input' );
expect( selectControl ).toBeNull();
} );

it( 'should render if ampNoLoading is defined', function() {
act( () => {
render( <AmpNoloadingToggle setAttributes={ () => {} } attributes={ { ampNoLoading: true } } />, container );
} );

const selectControl = container.querySelector( 'input' );
expect( selectControl ).not.toBeNull();
} );
} );