Skip to content

Commit ee25f9b

Browse files
authored
Forms: Update "Action after submit" sidebar section (#45502)
* add types to form edit props * add confirmationType and disableSummary attributes * changelog * update tests
1 parent 71b3dbb commit ee25f9b

File tree

8 files changed

+250
-125
lines changed

8 files changed

+250
-125
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Forms: Update "Action after submit" sidebar section

projects/packages/forms/src/blocks/contact-form/attributes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export default {
2828
type: 'string',
2929
default: '',
3030
},
31+
confirmationType: {
32+
enum: [ 'text', 'redirect' ],
33+
},
3134
jetpackCRM: {
3235
type: 'boolean',
3336
},
@@ -64,6 +67,10 @@ export default {
6467
type: 'boolean',
6568
default: false,
6669
},
70+
disableSummary: {
71+
type: 'boolean',
72+
default: false,
73+
},
6774
formNotifications: {
6875
type: 'boolean',
6976
default: true,

projects/packages/forms/src/blocks/contact-form/edit.tsx

Lines changed: 99 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import { createBlock } from '@wordpress/blocks';
1818
import {
1919
ExternalLink,
2020
PanelBody,
21-
SelectControl,
2221
TextareaControl,
2322
TextControl,
2423
ToggleControl,
24+
RadioControl,
2525
} from '@wordpress/components';
2626
import { useInstanceId } from '@wordpress/compose';
2727
import { store as coreStore } from '@wordpress/core-data';
@@ -41,7 +41,6 @@ import {
4141
NAVIGATION_TEMPLATE,
4242
} from '../form-step-navigation/edit';
4343
import StepControls from '../shared/components/form-step-controls';
44-
import InspectorHint from '../shared/components/inspector-hint';
4544
import JetpackManageResponsesSettings from '../shared/components/jetpack-manage-responses-settings';
4645
import { useFindBlockRecursively } from '../shared/hooks/use-find-block-recursively';
4746
import useFormSteps from '../shared/hooks/use-form-steps';
@@ -114,7 +113,43 @@ const isInputWithRequiredField = ( fullName?: string ): boolean => {
114113
return hasRequired && ! isHidden;
115114
};
116115

117-
function JetpackContactFormEdit( { name, attributes, setAttributes, clientId, className } ) {
116+
type CustomThankyouType =
117+
| '' // default message
118+
| 'noSummary' // default message without a summary
119+
| 'message' // custom message
120+
| 'redirect'; // redirect to a new URL
121+
122+
type JetpackContactFormAttributes = {
123+
to: string;
124+
subject: string;
125+
// Legacy support for the customThankyou attribute
126+
customThankyou: CustomThankyouType;
127+
customThankyouHeading: string;
128+
customThankyouMessage: string;
129+
customThankyouRedirect: string;
130+
confirmationType: 'text' | 'redirect';
131+
formTitle: string;
132+
variationName: string;
133+
emailNotifications: boolean;
134+
disableGoBack: boolean;
135+
disableSummary: boolean;
136+
notificationRecipients: string[];
137+
};
138+
type JetpackContactFormEditProps = {
139+
name: string;
140+
attributes: JetpackContactFormAttributes;
141+
setAttributes: ( attributes: Partial< JetpackContactFormAttributes > ) => void;
142+
clientId: string;
143+
className: string;
144+
};
145+
146+
function JetpackContactFormEdit( {
147+
name,
148+
attributes,
149+
setAttributes,
150+
clientId,
151+
className,
152+
}: JetpackContactFormEditProps ) {
118153
// Initialize default form block settings as needed.
119154
useFormBlockDefaults( { attributes, setAttributes } );
120155

@@ -125,16 +160,37 @@ function JetpackContactFormEdit( { name, attributes, setAttributes, clientId, cl
125160
customThankyouHeading,
126161
customThankyouMessage,
127162
customThankyouRedirect,
163+
confirmationType,
128164
formTitle,
129165
variationName,
130166
emailNotifications,
131167
disableGoBack,
168+
disableSummary,
132169
notificationRecipients,
133170
} = attributes;
134171
const formsConfig = useFormsConfig();
135172
const showFormIntegrations = Boolean( formsConfig?.isIntegrationsEnabled );
136173
const instanceId = useInstanceId( JetpackContactFormEdit );
137174

175+
// Backward compatibility for the deprecated customThankyou attribute.
176+
// Older forms will have a customThankyou attribute set, but not a confirmationType attribute
177+
// and not a disableSummary attribute, so we need to set it here.
178+
useEffect( () => {
179+
if ( confirmationType ) {
180+
return;
181+
}
182+
183+
if ( customThankyou === 'redirect' ) {
184+
setAttributes( { confirmationType: 'redirect' } );
185+
} else {
186+
setAttributes( { confirmationType: 'text' } );
187+
188+
if ( [ 'noSummary', 'message' ].includes( customThankyou ) ) {
189+
setAttributes( { disableSummary: true } );
190+
}
191+
}
192+
}, [ confirmationType, customThankyou, setAttributes ] );
193+
138194
const steps = useFormSteps( clientId );
139195

140196
// Get current step info for context
@@ -764,41 +820,20 @@ function JetpackContactFormEdit( { name, attributes, setAttributes, clientId, cl
764820
initialOpen={ false }
765821
className="jetpack-contact-form__panel"
766822
>
767-
<InspectorHint>
768-
{ __( 'Customize the view after form submission:', 'jetpack-forms' ) }
769-
</InspectorHint>
770-
<SelectControl
771-
label={ __( 'On submission', 'jetpack-forms' ) }
772-
value={ customThankyou }
823+
<RadioControl
824+
label={ __( 'Confirmation type', 'jetpack-forms' ) }
825+
selected={ confirmationType }
773826
options={ [
774-
{ label: __( 'Show a summary of submitted fields', 'jetpack-forms' ), value: '' },
775-
{
776-
label: __( 'Show the default text message without a summary', 'jetpack-forms' ),
777-
value: 'noSummary',
778-
},
779-
{ label: __( 'Show a custom text message', 'jetpack-forms' ), value: 'message' },
780-
{
781-
label: __( 'Redirect to another webpage', 'jetpack-forms' ),
782-
value: 'redirect',
783-
},
827+
{ label: __( 'Text', 'jetpack-forms' ), value: 'text' },
828+
{ label: __( 'Redirect link', 'jetpack-forms' ), value: 'redirect' },
784829
] }
785-
onChange={ newMessage => setAttributes( { customThankyou: newMessage } ) }
786-
__nextHasNoMarginBottom={ true }
787-
__next40pxDefaultSize={ true }
830+
onChange={ ( newValue: 'text' | 'redirect' ) =>
831+
setAttributes( { confirmationType: newValue } )
832+
}
788833
/>
789834

790-
{ 'redirect' !== customThankyou && (
835+
{ 'text' === confirmationType && (
791836
<>
792-
<ToggleControl
793-
label={ __( 'Show "Go back" link', 'jetpack-forms' ) }
794-
checked={ ! disableGoBack }
795-
onChange={ ( newDisableGoBack: boolean ) =>
796-
setAttributes( { disableGoBack: ! newDisableGoBack } )
797-
}
798-
__nextHasNoMarginBottom={ true }
799-
__next40pxDefaultSize={ true }
800-
/>
801-
802837
<TextControl
803838
label={ __( 'Message heading', 'jetpack-forms' ) }
804839
value={ customThankyouHeading }
@@ -809,22 +844,40 @@ function JetpackContactFormEdit( { name, attributes, setAttributes, clientId, cl
809844
__nextHasNoMarginBottom={ true }
810845
__next40pxDefaultSize={ true }
811846
/>
812-
</>
813-
) }
814847

815-
{ 'message' === customThankyou && (
816-
<TextareaControl
817-
label={ __( 'Message text', 'jetpack-forms' ) }
818-
value={ customThankyouMessage }
819-
placeholder={ __( 'Thank you for your submission!', 'jetpack-forms' ) }
820-
onChange={ ( newMessage: string ) =>
821-
setAttributes( { customThankyouMessage: newMessage } )
822-
}
823-
__nextHasNoMarginBottom={ true }
824-
/>
848+
<TextareaControl
849+
label={ __( 'Message text', 'jetpack-forms' ) }
850+
value={ customThankyouMessage }
851+
placeholder={ __( 'Thank you for your submission!', 'jetpack-forms' ) }
852+
onChange={ ( newMessage: string ) =>
853+
setAttributes( { customThankyouMessage: newMessage } )
854+
}
855+
__nextHasNoMarginBottom={ true }
856+
/>
857+
858+
<ToggleControl
859+
label={ __( 'Show summary', 'jetpack-forms' ) }
860+
checked={ ! disableSummary }
861+
onChange={ ( newDisableSummary: boolean ) =>
862+
setAttributes( { disableSummary: ! newDisableSummary } )
863+
}
864+
__nextHasNoMarginBottom={ true }
865+
__next40pxDefaultSize={ true }
866+
/>
867+
868+
<ToggleControl
869+
label={ __( 'Show "Go back" link', 'jetpack-forms' ) }
870+
checked={ ! disableGoBack }
871+
onChange={ ( newDisableGoBack: boolean ) =>
872+
setAttributes( { disableGoBack: ! newDisableGoBack } )
873+
}
874+
__nextHasNoMarginBottom={ true }
875+
__next40pxDefaultSize={ true }
876+
/>
877+
</>
825878
) }
826879

827-
{ 'redirect' === customThankyou && (
880+
{ 'redirect' === confirmationType && (
828881
<div>
829882
<URLInput
830883
label={ __( 'Redirect address', 'jetpack-forms' ) }

0 commit comments

Comments
 (0)