Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 5x 5x 5x 5x 2x 3x 3x 1x 2x 2x | import { store as blockEditorStore } from '@wordpress/block-editor';
import { useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { getInputFields } from '../../util/get-input-fields.ts';
/**
* Make a form's only input field required.
*
* A form with a single field has nothing to submit unless that field is filled
* in, so leaving it optional is never what the author meant.
*
* @param {object} params - Hook parameters.
* @param {Array} params.innerBlocks - The form's inner blocks.
*/
export default function useSingleInputFieldRequired( { innerBlocks } ) {
const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
useEffect( () => {
const inputFields = getInputFields( innerBlocks );
if ( inputFields.length !== 1 ) {
return;
}
const singleField = inputFields[ 0 ];
if ( singleField.attributes?.required ) {
return;
}
/*
* Nobody asked for this, so it must not count as an edit. Otherwise a
* one-field form -- a footer newsletter signup, say -- opens its post,
* template, or template part with unsaved changes on every load.
*/
__unstableMarkNextChangeAsNotPersistent();
updateBlockAttributes( singleField.clientId, { required: true } );
}, [ innerBlocks, updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent ] );
}
|