From 85973c58eded62cac566a37a6182648a167bba0d Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Wed, 2 Feb 2022 09:42:30 -0500 Subject: [PATCH] Update preview logic, render select dialog conditionally --- src/components/select-dialog.tsx | 50 ++++-------- src/edit.tsx | 135 ++++++++++++++++--------------- src/styles/editor.scss | 83 ++++++++++--------- 3 files changed, 130 insertions(+), 138 deletions(-) diff --git a/src/components/select-dialog.tsx b/src/components/select-dialog.tsx index e689782..c995e2c 100644 --- a/src/components/select-dialog.tsx +++ b/src/components/select-dialog.tsx @@ -2,27 +2,16 @@ * WordPress dependencies */ import * as React from 'react'; -import { Button, Modal, FocusableIframe } from '@wordpress/components'; +import { Modal, FocusableIframe } from '@wordpress/components'; import { baseUrl } from '@/globals'; type DialogProps = { + setOpen: Function; onSelectForm: Function; }; -type DialogState = { - isOpen: boolean; -}; - -class SelectDialog extends React.Component { - constructor( props: any ) { - super( props ); - - this.state = { - isOpen: false, - }; - } - +class SelectDialog extends React.Component { componentDidMount() { window.addEventListener( 'message', this.handlePostMessage, false ); } @@ -32,32 +21,25 @@ class SelectDialog extends React.Component { } handlePostMessage = ( event: MessageEvent ) => { - if (event.origin === baseUrl && event.data.type === 'cog-form-selected') { + if ( event.origin === baseUrl && event.data.type === 'cog-form-selected' ) { this.props.onSelectForm( event.data ); - this.setState({ isOpen: false }); // Close Dialog + this.props.setOpen( false ); } } render() { return ( -
- - { this.state.isOpen && ( - this.setState( { isOpen: false } ) } - shouldCloseOnClickOutside={ false } - > - - - ) } -
+ this.props.setOpen( false ) } + shouldCloseOnClickOutside={ false } + > + + ); } } diff --git a/src/edit.tsx b/src/edit.tsx index d04077d..b5a7475 100644 --- a/src/edit.tsx +++ b/src/edit.tsx @@ -1,5 +1,4 @@ import * as React from 'react'; -import { __ } from '@wordpress/i18n'; import { BlockEditProps } from '@wordpress/blocks'; import { @@ -9,17 +8,23 @@ import { PanelBody, PanelRow, ExternalLink, + Button, + Icon, } from '@wordpress/components'; -import { edit } from '@wordpress/icons'; - +import { edit, external } from '@wordpress/icons'; +import { useState } from '@wordpress/element'; import { BlockControls, InspectorControls } from '@wordpress/block-editor'; +import { BlockAttributes, EmbedMode } from '@/types'; +import { baseUrl } from './globals'; + import { ReactComponent as CogIcon } from '@/assets/cogicon.svg'; import SelectDialog from '@/components/select-dialog'; import PreviewForm from '@/components/preview-form'; -import { BlockAttributes, EmbedMode } from '@/types'; const Edit: React.FC> = ( { attributes, setAttributes, className } ) => { + const [ selectDialogOpen, setSelectDialogOpen ] = useState( false ); + const handleForm = ( form: { [key: string]: any } ) => { setAttributes( { formId: form.formId, @@ -30,71 +35,71 @@ const Edit: React.FC> = ( { attributes, setAttri } ); }; - const resetForm = () => { - // Create a new object which sets all attributes to null - const nullAttributes = Object.keys( attributes ).reduce( - ( accumulator, current ) => { - // @ts-ignore - accumulator[ current ] = null; - return accumulator; - }, {} ); - - setAttributes( nullAttributes ); - }; - - if ( ! attributes.formId ) { - return ( -
- - - -
- ); - } - return ( -
- { - - resetForm() } +
+ { attributes.formId ? + // A form has already been selected, so render the form preview. +
+ + setSelectDialogOpen( true ) } + /> + + + + + + + The type of embed code to use with your form. Learn more + + } + selected={ attributes.embedMode } + options={ [ + { label: 'Seamless', value: EmbedMode.Seamless }, + { label: 'Iframe', value: EmbedMode.IFrame }, + ] } + onChange={ ( value: EmbedMode ) => setAttributes( { embedMode: parseInt( value.toString() ) } ) } + /> + + + + + - +
+ : + // No form has been selected yet, so render the placeholder +
+ +
+ + +
+
+
} - - - - - The type of embed code to use with your form. Learn more - - } - selected={ attributes.embedMode } - options={ [ - { label: 'Seamless', value: EmbedMode.Seamless }, - { label: 'Iframe', value: EmbedMode.IFrame }, - ] } - onChange={ ( value: EmbedMode ) => setAttributes( { embedMode: parseInt( value.toString() ) } ) } - /> - - - - - + { selectDialogOpen && + // Selectively render the select dialog + setSelectDialogOpen( val ) } + onSelectForm={ handleForm } + > + }
); }; diff --git a/src/styles/editor.scss b/src/styles/editor.scss index b989508..2ef407c 100644 --- a/src/styles/editor.scss +++ b/src/styles/editor.scss @@ -7,51 +7,56 @@ */ // Remove modal padding -.cognito-modal > .components-modal__content { - padding: initial; -} - -.cognito-modal > .components-modal__content > .components-modal__header { - margin: initial; -} - -// .cog-wp-embed__preview > iframe { -// height: 500px; -// } - -.form-preview { - min-height: 200px; - - .focus-grabber { - position: absolute; - width: 100%; - height: 100%; - z-index: 1; +.wp-block-cognito-forms-cognito-embed { + .components-placeholder__label svg { + height: 24px; + width: 24px; } - .loader { - padding: 1em; - background: #fff; - text-align: center; + .form-preview { min-height: 200px; - border-radius: 2px; - box-shadow: inset 0 0 0 1px #1e1e1e; - display: flex; - gap: 0.5em; - flex-direction: column; - align-items: center; - justify-content: center; - - .icon { - width: 2em; - height: 2em; - margin: 0 auto; + .focus-grabber { + position: absolute; + width: 100%; + height: 100%; + z-index: 1; } - p { - font-size: 13px; - margin: 0; + .loader { + padding: 1em; + background: #fff; + text-align: center; + min-height: 200px; + border-radius: 2px; + box-shadow: inset 0 0 0 1px #1e1e1e; + + display: flex; + gap: 0.5em; + flex-direction: column; + align-items: center; + justify-content: center; + + .icon { + width: 2em; + height: 2em; + margin: 0 auto; + } + + p { + font-size: 13px; + margin: 0; + } } } } + +.cognito-modal { + .components-modal__content { + padding: initial; + } + + .components-modal__content .components-modal__header { + margin: initial; + } +}