Skip to content

Commit

Permalink
feat: disable save button for unchanged settings (#2259)
Browse files Browse the repository at this point in the history
closes #1531
  • Loading branch information
hbhalodia authored Mar 2, 2023
1 parent 5eb2336 commit e06d72f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 5 additions & 2 deletions assets/components/src/plugin-settings/SettingsSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { Fragment, useState } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import { CheckboxControl } from '@wordpress/components';
Expand Down Expand Up @@ -52,6 +52,7 @@ const getControlType = setting => {
};

const SettingsSection = props => {
const [ saveDisabled, setSaveDisabled ] = useState( true );
const { error, sectionKey, active, title, description, fields, disabled, onChange, onUpdate } =
props;
const getControlProps = setting => ( {
Expand All @@ -70,6 +71,7 @@ const SettingsSection = props => {
checked: setting.type === 'boolean' ? !! setting.value : null,
onChange: value => {
onChange( setting.key, value );
setSaveDisabled( false );
},
} );
const createFilter = ( name, defaultComponent = null ) => {
Expand Down Expand Up @@ -100,9 +102,10 @@ const SettingsSection = props => {
'buttons',
<Button
variant="primary"
disabled={ disabled }
disabled={ disabled || saveDisabled }
onClick={ () => {
onUpdate();
setSaveDisabled( true );
} }
>
{ __( 'Save Settings', 'newspack' ) }
Expand Down
2 changes: 0 additions & 2 deletions assets/components/src/plugin-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class PluginSettings extends Component {
},
} )
.then( settings => {
this.setState( { settings, error: null } );

if ( 'function' === typeof afterUpdate ) {
afterUpdate( settings );
}
Expand Down

0 comments on commit e06d72f

Please sign in to comment.