2019-05-02 10:22:34 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
2019-05-07 07:21:34 +00:00
|
|
|
import { xor } from 'lodash';
|
2019-05-02 10:22:34 +00:00
|
|
|
|
2019-05-09 01:13:14 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import SectionControls from './section-controls';
|
|
|
|
|
2019-05-02 10:22:34 +00:00
|
|
|
export default class Section extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
const { title } = props;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
titleInput: title,
|
|
|
|
};
|
|
|
|
|
2019-05-07 07:21:34 +00:00
|
|
|
this.onToggleHiddenBlock = this.onToggleHiddenBlock.bind( this );
|
2019-05-02 10:22:34 +00:00
|
|
|
this.onTitleChange = this.onTitleChange.bind( this );
|
|
|
|
this.onTitleBlur = this.onTitleBlur.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
onTitleChange( updatedTitle ) {
|
|
|
|
this.setState( { titleInput: updatedTitle } );
|
|
|
|
}
|
|
|
|
|
|
|
|
onTitleBlur() {
|
|
|
|
const { onTitleUpdate, title } = this.props;
|
|
|
|
const { titleInput } = this.state;
|
|
|
|
|
|
|
|
if ( titleInput === '' ) {
|
|
|
|
this.setState( { titleInput: title } );
|
|
|
|
} else if ( onTitleUpdate ) {
|
|
|
|
onTitleUpdate( titleInput );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:21:34 +00:00
|
|
|
onToggleHiddenBlock( key ) {
|
|
|
|
return () => {
|
|
|
|
const hiddenBlocks = xor( this.props.hiddenBlocks, [ key ] );
|
|
|
|
this.props.onChangeHiddenBlocks( hiddenBlocks );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-02 10:22:34 +00:00
|
|
|
render() {
|
2020-07-28 02:32:58 +00:00
|
|
|
const { component: SectionComponent, ...props } = this.props;
|
2019-05-02 10:22:34 +00:00
|
|
|
const { titleInput } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-dashboard-section">
|
|
|
|
<SectionComponent
|
|
|
|
onTitleChange={ this.onTitleChange }
|
|
|
|
onTitleBlur={ this.onTitleBlur }
|
2019-05-07 07:21:34 +00:00
|
|
|
onToggleHiddenBlock={ this.onToggleHiddenBlock }
|
2019-05-02 10:22:34 +00:00
|
|
|
titleInput={ titleInput }
|
2019-05-09 01:13:14 +00:00
|
|
|
controls={ SectionControls }
|
2019-05-02 10:22:34 +00:00
|
|
|
{ ...props }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|