-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Fields option: Add confirmation step (#15688)
* Add confirmation step to Custom Fields option Add a confirmation step to the Custom Fields option so that the page doesn't reload without warning. Co-authored-by: Marek Hrabe <marekhrabe@me.com> * Update E2E tests for new Custom Fields setting flow * Remove notice in favor of plain text. * Custom Fields option: Adjust margin to align text * update description Co-Authored-By: Daniel Richards <daniel.richards@automattic.com> * simplify change handler * add dynamic button based on the next state to be saved * add notice about saved content and make sure it won't expand modal * update snapshot with new button label and confirm msg * make button label in test dynamic * simplify condition for determining label Co-Authored-By: Robert Anderson <robert@noisysocks.com> * use willEnable as the prop name
- Loading branch information
1 parent
693b395
commit f160e16
Showing
6 changed files
with
230 additions
and
92 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
71 changes: 39 additions & 32 deletions
71
packages/edit-post/src/components/options-modal/options/enable-custom-fields.js
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,47 +1,54 @@ | ||
/** | ||
* 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'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
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 ( | ||
<> | ||
<p className="edit-post-options-modal__custom-fields-confirmation-message"> | ||
{ __( 'A page reload is required for this change. Make sure your content is saved before reloading.' ) } | ||
</p> | ||
<Button | ||
className="edit-post-options-modal__custom-fields-confirmation-button" | ||
isDefault | ||
isBusy={ isReloading } | ||
disabled={ isReloading } | ||
onClick={ () => { | ||
setIsReloading( true ); | ||
document.getElementById( 'toggle-custom-fields-form' ).submit(); | ||
} } | ||
> | ||
{ willEnable ? __( 'Enable & Reload' ) : __( 'Disable & Reload' ) } | ||
</Button> | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<BaseOption | ||
label={ label } | ||
isChecked={ isChecked } | ||
onChange={ this.toggleCustomFields } | ||
/> | ||
); | ||
} | ||
export function EnableCustomFieldsOption( { label, areCustomFieldsEnabled } ) { | ||
const [ isChecked, setIsChecked ] = useState( areCustomFieldsEnabled ); | ||
|
||
return ( | ||
<BaseOption | ||
label={ label } | ||
isChecked={ isChecked } | ||
onChange={ setIsChecked } | ||
> | ||
{ isChecked !== areCustomFieldsEnabled && <CustomFieldsConfirmation willEnable={ isChecked } /> } | ||
</BaseOption> | ||
); | ||
} | ||
|
||
export default withSelect( ( select ) => ( { | ||
isChecked: !! select( 'core/editor' ).getEditorSettings().enableCustomFields, | ||
areCustomFieldsEnabled: !! select( 'core/editor' ).getEditorSettings().enableCustomFields, | ||
} ) )( EnableCustomFieldsOption ); |
142 changes: 130 additions & 12 deletions
142
...post/src/components/options-modal/options/test/__snapshots__/enable-custom-fields.js.snap
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,17 +1,135 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EnableCustomFieldsOption renders properly when checked 1`] = ` | ||
<BaseOption | ||
isChecked={true} | ||
label="Custom Fields" | ||
onChange={[Function]} | ||
/> | ||
exports[`EnableCustomFieldsOption renders a checked checkbox and a confirmation message when toggled on 1`] = ` | ||
<div | ||
className="edit-post-options-modal__option" | ||
> | ||
<div | ||
className="components-base-control" | ||
> | ||
<div | ||
className="components-base-control__field" | ||
> | ||
<input | ||
checked={true} | ||
className="components-checkbox-control__input" | ||
id="inspector-checkbox-control-3" | ||
onChange={[Function]} | ||
type="checkbox" | ||
value="1" | ||
/> | ||
<label | ||
className="components-checkbox-control__label" | ||
htmlFor="inspector-checkbox-control-3" | ||
/> | ||
</div> | ||
</div> | ||
<p | ||
className="edit-post-options-modal__custom-fields-confirmation-message" | ||
> | ||
A page reload is required for this change. Make sure your content is saved before reloading. | ||
</p> | ||
<button | ||
className="components-button edit-post-options-modal__custom-fields-confirmation-button is-button is-default" | ||
disabled={false} | ||
onClick={[Function]} | ||
type="button" | ||
> | ||
Enable & Reload | ||
</button> | ||
</div> | ||
`; | ||
|
||
exports[`EnableCustomFieldsOption renders properly when unchecked 1`] = ` | ||
<BaseOption | ||
isChecked={false} | ||
label="Custom Fields" | ||
onChange={[Function]} | ||
/> | ||
exports[`EnableCustomFieldsOption renders a checked checkbox when custom fields are enabled 1`] = ` | ||
<div | ||
className="edit-post-options-modal__option" | ||
> | ||
<div | ||
className="components-base-control" | ||
> | ||
<div | ||
className="components-base-control__field" | ||
> | ||
<input | ||
checked={true} | ||
className="components-checkbox-control__input" | ||
id="inspector-checkbox-control-0" | ||
onChange={[Function]} | ||
type="checkbox" | ||
value="1" | ||
/> | ||
<label | ||
className="components-checkbox-control__label" | ||
htmlFor="inspector-checkbox-control-0" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`EnableCustomFieldsOption renders an unchecked checkbox and a confirmation message when toggled off 1`] = ` | ||
<div | ||
className="edit-post-options-modal__option" | ||
> | ||
<div | ||
className="components-base-control" | ||
> | ||
<div | ||
className="components-base-control__field" | ||
> | ||
<input | ||
checked={false} | ||
className="components-checkbox-control__input" | ||
id="inspector-checkbox-control-2" | ||
onChange={[Function]} | ||
type="checkbox" | ||
value="1" | ||
/> | ||
<label | ||
className="components-checkbox-control__label" | ||
htmlFor="inspector-checkbox-control-2" | ||
/> | ||
</div> | ||
</div> | ||
<p | ||
className="edit-post-options-modal__custom-fields-confirmation-message" | ||
> | ||
A page reload is required for this change. Make sure your content is saved before reloading. | ||
</p> | ||
<button | ||
className="components-button edit-post-options-modal__custom-fields-confirmation-button is-button is-default" | ||
disabled={false} | ||
onClick={[Function]} | ||
type="button" | ||
> | ||
Disable & Reload | ||
</button> | ||
</div> | ||
`; | ||
|
||
exports[`EnableCustomFieldsOption renders an unchecked checkbox when custom fields are disabled 1`] = ` | ||
<div | ||
className="edit-post-options-modal__option" | ||
> | ||
<div | ||
className="components-base-control" | ||
> | ||
<div | ||
className="components-base-control__field" | ||
> | ||
<input | ||
checked={false} | ||
className="components-checkbox-control__input" | ||
id="inspector-checkbox-control-1" | ||
onChange={[Function]} | ||
type="checkbox" | ||
value="1" | ||
/> | ||
<label | ||
className="components-checkbox-control__label" | ||
htmlFor="inspector-checkbox-control-1" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
73 changes: 37 additions & 36 deletions
73
packages/edit-post/src/components/options-modal/options/test/enable-custom-fields.js
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