diff --git a/packages/e2e-tests/specs/preview.test.js b/packages/e2e-tests/specs/preview.test.js index 5a1d7f4cc2754..4f1fa04ba06eb 100644 --- a/packages/e2e-tests/specs/preview.test.js +++ b/packages/e2e-tests/specs/preview.test.js @@ -70,8 +70,10 @@ async function toggleCustomFieldsOption( shouldBeChecked ) { ); if ( isChecked !== shouldBeChecked ) { - const navigationCompleted = page.waitForNavigation(); await checkboxHandle.click(); + const [ saveButton ] = await page.$x( shouldBeChecked ? '//button[text()="Enable & Reload"]' : '//button[text()="Disable & Reload"]' ); + const navigationCompleted = page.waitForNavigation(); + saveButton.click(); await navigationCompleted; return; } diff --git a/packages/edit-post/src/components/options-modal/options/base.js b/packages/edit-post/src/components/options-modal/options/base.js index 1dd5b9d013af0..cef8fe130b268 100644 --- a/packages/edit-post/src/components/options-modal/options/base.js +++ b/packages/edit-post/src/components/options-modal/options/base.js @@ -3,14 +3,16 @@ */ import { CheckboxControl } from '@wordpress/components'; -function BaseOption( { label, isChecked, onChange } ) { +function BaseOption( { label, isChecked, onChange, children } ) { return ( - +
+ + { children } +
); } diff --git a/packages/edit-post/src/components/options-modal/options/enable-custom-fields.js b/packages/edit-post/src/components/options-modal/options/enable-custom-fields.js index 140c6f1c46d2d..82b8002dca148 100644 --- a/packages/edit-post/src/components/options-modal/options/enable-custom-fields.js +++ b/packages/edit-post/src/components/options-modal/options/enable-custom-fields.js @@ -1,7 +1,9 @@ /** * WordPress dependencies */ -import { Component } from '@wordpress/element'; +import { useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { Button } from '@wordpress/components'; import { withSelect } from '@wordpress/data'; /** @@ -9,39 +11,44 @@ import { withSelect } from '@wordpress/data'; */ import BaseOption from './base'; -export class EnableCustomFieldsOption extends Component { - constructor( { isChecked } ) { - super( ...arguments ); - - this.toggleCustomFields = this.toggleCustomFields.bind( this ); - - this.state = { isChecked }; - } - - toggleCustomFields() { - // Submit a hidden form which triggers the toggle_custom_fields admin action. - // This action will toggle the setting and reload the editor with the meta box - // assets included on the page. - document.getElementById( 'toggle-custom-fields-form' ).submit(); - - // Make it look like something happened while the page reloads. - this.setState( { isChecked: ! this.props.isChecked } ); - } - - render() { - const { label } = this.props; - const { isChecked } = this.state; +export function CustomFieldsConfirmation( { willEnable } ) { + const [ isReloading, setIsReloading ] = useState( false ); + + return ( + <> +

+ { __( 'A page reload is required for this change. Make sure your content is saved before reloading.' ) } +

+ + + ); +} - return ( - - ); - } +export function EnableCustomFieldsOption( { label, areCustomFieldsEnabled } ) { + const [ isChecked, setIsChecked ] = useState( areCustomFieldsEnabled ); + + return ( + + { isChecked !== areCustomFieldsEnabled && } + + ); } export default withSelect( ( select ) => ( { - isChecked: !! select( 'core/editor' ).getEditorSettings().enableCustomFields, + areCustomFieldsEnabled: !! select( 'core/editor' ).getEditorSettings().enableCustomFields, } ) )( EnableCustomFieldsOption ); diff --git a/packages/edit-post/src/components/options-modal/options/test/__snapshots__/enable-custom-fields.js.snap b/packages/edit-post/src/components/options-modal/options/test/__snapshots__/enable-custom-fields.js.snap index 14bed9e08237e..d4fa948a77844 100644 --- a/packages/edit-post/src/components/options-modal/options/test/__snapshots__/enable-custom-fields.js.snap +++ b/packages/edit-post/src/components/options-modal/options/test/__snapshots__/enable-custom-fields.js.snap @@ -1,17 +1,135 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`EnableCustomFieldsOption renders properly when checked 1`] = ` - +exports[`EnableCustomFieldsOption renders a checked checkbox and a confirmation message when toggled on 1`] = ` +
+
+
+ +
+
+

+ A page reload is required for this change. Make sure your content is saved before reloading. +

+ +
`; -exports[`EnableCustomFieldsOption renders properly when unchecked 1`] = ` - +exports[`EnableCustomFieldsOption renders a checked checkbox when custom fields are enabled 1`] = ` +
+
+
+ +
+
+
+`; + +exports[`EnableCustomFieldsOption renders an unchecked checkbox and a confirmation message when toggled off 1`] = ` +
+
+
+ +
+
+

+ A page reload is required for this change. Make sure your content is saved before reloading. +

+ +
+`; + +exports[`EnableCustomFieldsOption renders an unchecked checkbox when custom fields are disabled 1`] = ` +
+
+
+ +
+
+
`; diff --git a/packages/edit-post/src/components/options-modal/options/test/enable-custom-fields.js b/packages/edit-post/src/components/options-modal/options/test/enable-custom-fields.js index 8ce8f78eb4be6..5ac60c92753af 100644 --- a/packages/edit-post/src/components/options-modal/options/test/enable-custom-fields.js +++ b/packages/edit-post/src/components/options-modal/options/test/enable-custom-fields.js @@ -1,62 +1,63 @@ /** * External dependencies */ -import { shallow } from 'enzyme'; +import { default as TestRenderer, act } from 'react-test-renderer'; + +/** + * WordPress dependencies + */ +import { Button } from '@wordpress/components'; /** * Internal dependencies */ -import { EnableCustomFieldsOption } from '../enable-custom-fields'; +import { EnableCustomFieldsOption, CustomFieldsConfirmation } from '../enable-custom-fields'; +import BaseOption from '../base'; describe( 'EnableCustomFieldsOption', () => { - it( 'renders properly when checked', () => { - const wrapper = shallow( ); - expect( wrapper ).toMatchSnapshot(); + it( 'renders a checked checkbox when custom fields are enabled', () => { + const renderer = TestRenderer.create( ); + expect( renderer ).toMatchSnapshot(); } ); - it( 'can be unchecked', () => { - const submit = jest.fn(); - const getElementById = jest.spyOn( document, 'getElementById' ).mockImplementation( () => ( { - submit, - } ) ); - - const wrapper = shallow( ); - - expect( wrapper.prop( 'isChecked' ) ).toBe( true ); - - wrapper.prop( 'onChange' )(); - wrapper.update(); - - expect( wrapper.prop( 'isChecked' ) ).toBe( false ); - expect( getElementById ).toHaveBeenCalledWith( 'toggle-custom-fields-form' ); - expect( submit ).toHaveBeenCalled(); + it( 'renders an unchecked checkbox when custom fields are disabled', () => { + const renderer = TestRenderer.create( + + ); + expect( renderer ).toMatchSnapshot(); + } ); - getElementById.mockRestore(); + it( 'renders an unchecked checkbox and a confirmation message when toggled off', () => { + const renderer = new TestRenderer.create( ); + act( () => { + renderer.root.findByType( BaseOption ).props.onChange( false ); + } ); + expect( renderer ).toMatchSnapshot(); } ); - it( 'renders properly when unchecked', () => { - const wrapper = shallow( - + it( 'renders a checked checkbox and a confirmation message when toggled on', () => { + const renderer = new TestRenderer.create( + ); - expect( wrapper ).toMatchSnapshot(); + act( () => { + renderer.root.findByType( BaseOption ).props.onChange( true ); + } ); + expect( renderer ).toMatchSnapshot(); } ); +} ); - it( 'can be checked', () => { +describe( 'CustomFieldsConfirmation', () => { + it( 'submits the toggle-custom-fields-form', () => { const submit = jest.fn(); const getElementById = jest.spyOn( document, 'getElementById' ).mockImplementation( () => ( { submit, } ) ); - const wrapper = shallow( - - ); - - expect( wrapper.prop( 'isChecked' ) ).toBe( false ); - - wrapper.prop( 'onChange' )(); - wrapper.update(); + const renderer = new TestRenderer.create( ); + act( () => { + renderer.root.findByType( Button ).props.onClick(); + } ); - expect( wrapper.prop( 'isChecked' ) ).toBe( true ); expect( getElementById ).toHaveBeenCalledWith( 'toggle-custom-fields-form' ); expect( submit ).toHaveBeenCalled(); diff --git a/packages/edit-post/src/components/options-modal/style.scss b/packages/edit-post/src/components/options-modal/style.scss index 01b5f816c0ca0..8592234beb244 100644 --- a/packages/edit-post/src/components/options-modal/style.scss +++ b/packages/edit-post/src/components/options-modal/style.scss @@ -21,13 +21,21 @@ margin: 0; } - &.components-base-control + &.components-base-control { - margin-bottom: 0; - } - .components-checkbox-control__label { flex-grow: 1; padding: 0.6rem 0 0.6rem 10px; } } + + &__custom-fields-confirmation-message, + &__custom-fields-confirmation-button { + margin: 0 0 0.6rem 48px; + + @include break-medium() { + margin-left: 38px; + } + @include break-small() { + max-width: 300px; + } + } }