import classnames from 'classnames'; const { __ } = wp.i18n; const { Fragment, Component, } = wp.element; const { IconButton, Dashicon, withFocusOutside, } = wp.components; const { RichText, URLInput, } = wp.editor; const EnhancedComponent = withFocusOutside( class Edit extends Component { constructor() { super( ...arguments ); this.state = { focusedIndex: null, selectedTable: null, }; } handleFocusOutside() { this.setState( { focusedIndex: false } ); } stopKeyPropagation = ( event ) => event.stopPropagation(); render() { const { attributes, setAttributes, className, isSelected } = this.props; const hasFeatured = attributes.tables.some( ( table ) => table.featured ); return (
{ ( attributes.tables.length ) ? ( attributes.tables.map( ( table, i ) => (
this.setState( { selectedTable: i } ) } tabIndex="-1" role="textbox" onKeyDown={ () => false } style={ { backgroundColor: attributes.backgroundColor, color: attributes.textColor, } } > { ( attributes.featured_text ) && ( table.featured ) && { attributes.featured_text } } { ( isSelected && this.state.selectedTable === i ) &&
{ const tables = [ ...attributes.tables ]; setAttributes( { tables: tables.filter( ( el, index ) => ! ( index === i ) ) } ); } } className="blocks-gallery-item__remove" label={ __( 'Remove Table' ) } /> { const tables = [ ...attributes.tables ]; // Cache orginal featured state. const originalState = tables[ i ].featured; tables.map( ( t ) => t.featured = false ); // Restore orginal state if already defined, else true. tables[ i ].featured = ( originalState ? ! originalState : true ); setAttributes( { tables } ); } } icon={ table.featured ? : } />
}
{ const tables = [ ...attributes.tables ]; tables[ i ].title = value; setAttributes( { tables } ); } } style={ { color: ! table.featured ? attributes.accent : attributes.featuredAccent, } } />
{ const tables = [ ...attributes.tables ]; tables[ i ].price = value; setAttributes( { tables } ); } } inlineToolbar keepPlaceholderOnFocus style={ { color: ! table.featured ? attributes.accent : attributes.featuredAccent, } } /> { const tables = [ ...attributes.tables ]; tables[ i ].price_term = value; setAttributes( { tables } ); } } keepPlaceholderOnFocus inlineToolbar />
{ const tables = [ ...attributes.tables ]; tables[ i ].features = value; setAttributes( { tables } ); } } inlineToolbar />
this.setState( { focusedIndex: i } ) } onKeyPress={ this.stopKeyPropagation } tabIndex={ i } className="wp-block-button is-style-squared" > { const tables = [ ...attributes.tables ]; tables[ i ].buttonText = value; setAttributes( { tables } ); } } formattingControls={ [ 'bold', 'italic', 'strikethrough' ] } className={ classnames( 'wp-block-button__link' ) } keepPlaceholderOnFocus style={ { backgroundColor: ! table.featured ? attributes.accent : attributes.featuredAccent, } } inlineToolbar /> { ( this.state.focusedIndex === i ) && (
event.preventDefault() }> { const tables = [ ...attributes.tables ]; tables[ i ].buttonURL = value; setAttributes( { tables } ); } } /> ) }
) ) ) : null }
{ isSelected &&
{ const tables = [ ...attributes.tables ]; tables.push( { title: '', description: '', price: '', price_term: '', features: [], buttonText: 'Purchase', } ); setAttributes( { tables } ); } } > { __( 'Add new table' ) }
}
); } } ); export default EnhancedComponent;