Skip to content

Commit c0b58f6

Browse files
committed
Improve multistep form step initialization and navigation
1 parent c92a056 commit c0b58f6

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Forms: default multistep forms to single step view in the editor and prevent viewport jumping during step navigation.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ function JetpackContactFormEdit( {
247247

248248
const { isSingleStep, isFirstStep, isLastStep, currentStepClientId } = useSelect(
249249
select => {
250-
const { getCurrentStepInfo, isSingleStepMode } = select( singleStepStore );
250+
const { getCurrentStepInfo, isSingleStepMode, getActiveStepId } = select( singleStepStore );
251251

252252
const info = getCurrentStepInfo( clientId, steps );
253253

254254
return {
255255
isSingleStep: isSingleStepMode( clientId ),
256256
isFirstStep: info ? info.isFirstStep : false,
257257
isLastStep: info ? info.isLastStep : false,
258-
currentStepClientId: info ? info.clientId : null,
258+
currentStepClientId: getActiveStepId( clientId ),
259259
};
260260
},
261261
[ clientId, steps ]
@@ -735,6 +735,17 @@ function JetpackContactFormEdit( {
735735

736736
const { setActiveStep } = useDispatch( singleStepStore );
737737

738+
useEffect( () => {
739+
if (
740+
variationName === 'multistep' &&
741+
isSingleStep &&
742+
steps.length > 0 &&
743+
! currentStepClientId
744+
) {
745+
setActiveStep( clientId, steps[ 0 ].clientId );
746+
}
747+
}, [ variationName, isSingleStep, steps, currentStepClientId, clientId, setActiveStep ] );
748+
738749
// Find the selected block and its parent step block
739750
const selectedBlock = useFindBlockRecursively(
740751
selectedBlockClientId,
@@ -750,8 +761,12 @@ function JetpackContactFormEdit( {
750761
return;
751762
}
752763

753-
// If a block is selected, make sure it's in the current step
754-
if ( selectedBlockClientId && stepBlock && stepBlock.clientId !== currentStepClientId ) {
764+
if (
765+
selectedBlockClientId &&
766+
stepBlock &&
767+
stepBlock.clientId !== currentStepClientId &&
768+
currentStepClientId
769+
) {
755770
setActiveStep( clientId, stepBlock.clientId );
756771
}
757772
}, [

projects/packages/forms/src/blocks/shared/components/form-step-controls/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BlockControls, store as blockEditorStore } from '@wordpress/block-editor';
1+
import { BlockControls } from '@wordpress/block-editor';
22
import {
33
ToolbarGroup,
44
ToolbarButton,
@@ -29,13 +29,10 @@ export default function StepControls( { formClientId } ) {
2929
const { setActiveStep, enableSingleStepMode, disableSingleStepMode } =
3030
useDispatch( singleStepStore );
3131

32-
// Access the block editor dispatcher to programmatically select blocks when needed.
33-
const { selectBlock } = useDispatch( blockEditorStore );
34-
3532
// Use our custom navigation hook
3633
const { navigateToNextStep, navigateToPreviousStep, currentStepInfo, steps } = useStepNavigation(
3734
formClientId,
38-
true // always update the selected block when navigating
35+
false
3936
);
4037

4138
const { selectedStepId, isSingleStep } = useSelect(
@@ -114,7 +111,6 @@ export default function StepControls( { formClientId } ) {
114111
onClick={ () => {
115112
setActiveStep( formClientId, step.clientId );
116113
enableSingleStepMode( formClientId );
117-
selectBlock( step.clientId );
118114
onClose();
119115
} }
120116
isSelected={ selectedStepId === step.clientId && isSingleStep }

projects/packages/forms/src/store/form-step-preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const selectors = {
5959
* @return {boolean} Whether the form is in single step mode.
6060
*/
6161
isSingleStepMode( state, formClientId ) {
62-
return !! state.singleStepMode[ formClientId ];
62+
return state.singleStepMode[ formClientId ] !== false;
6363
},
6464

6565
/**

0 commit comments

Comments
 (0)