Skip to content

Commit

Permalink
Contact Form: Allow users to add multiple options to a dropdown field (
Browse files Browse the repository at this point in the history
…#37739)

* Contact Form: Allow users to add multiple options to a dropdown field

* Bump Forms package version

---------

Co-authored-by: Karen Attfield <[email protected]>
  • Loading branch information
monsieur-z and coder-karen authored Jun 7, 2024
1 parent 30a8df6 commit 91b2516
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 11 deletions.
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/fix-dropddown-field-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fixed

Allow users to add multiple options to a dropdown field
2 changes: 1 addition & 1 deletion projects/packages/forms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"link-template": "https://github.com/automattic/jetpack-forms/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.31.x-dev"
"dev-trunk": "0.32.x-dev"
},
"textdomain": "jetpack-forms",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/forms/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-forms",
"version": "0.31.5-alpha",
"version": "0.32.0-alpha",
"description": "Jetpack Forms",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/forms/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { isEmpty, isNil, noop, split, trim } from 'lodash';
import { getCaretPosition } from '../util/caret';
import { setFocus } from '../util/focus';
import { useFormStyle, useFormWrapper } from '../util/form';
import { withSharedFieldAttributes } from '../util/with-shared-field-attributes';
Expand Down Expand Up @@ -64,18 +65,29 @@ const JetpackDropdown = ( { attributes, clientId, isSelected, name, setAttribute
}
};

const handleSplitOption = index => ( value, isOriginal ) => {
if ( ! isOriginal ) {
const handleKeyDown = index => e => {
// Create a new dropdown option when the user hits Enter.
// Previously handled with the onSplit prop, which was removed in https://github.com/WordPress/gutenberg/pull/54543
if ( 'Enter' !== e.key ) {
return;
}

const splitValue = attributes.options[ index ].slice( value.length );
e.preventDefault();

if ( isEmpty( value ) && isEmpty( splitValue ) ) {
const value = attributes.options[ index ];

if ( ! value ) {
return;
}

handleMultiValues( index, [ value, splitValue ] );
const caretPos = getCaretPosition( e.target );
// splitValue is the value after the caret position when a user hits Enter
const splitValue = caretPos ? value.slice( caretPos ) : '';

handleMultiValues(
index,
splitValue ? [ value.slice( 0, caretPos ), splitValue ] : [ value, '' ]
);
};

const handleDeleteOption = index => () => {
Expand Down Expand Up @@ -134,7 +146,7 @@ const JetpackDropdown = ( { attributes, clientId, isSelected, name, setAttribute
key={ index }
value={ option }
onChange={ handleChangeOption( index ) }
onSplit={ handleSplitOption( index ) }
onKeyDown={ handleKeyDown( index ) }
onRemove={ handleDeleteOption( index ) }
onReplace={ noop }
placeholder={ __( 'Add option…', 'jetpack-forms' ) }
Expand Down
22 changes: 22 additions & 0 deletions projects/packages/forms/src/blocks/contact-form/util/caret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Get the caret position in an active contenteditable element
* From https://gist.github.com/loilo/f873a88631e660c59a1d5ab757ca9b1e
*
* @param {HTMLElement} target - Contenteditable element of which to get the caret position
* @returns {number} The caret position
*/
export const getCaretPosition = target => {
const sel = target.ownerDocument.defaultView.getSelection();

if ( sel.rangeCount === 0 ) {
return 0;
}

const range = sel.getRangeAt( 0 );

const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents( target );
preCaretRange.setEnd( range.endContainer, range.endOffset );

return preCaretRange.toString().length;
};
2 changes: 1 addition & 1 deletion projects/packages/forms/src/class-jetpack-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class Jetpack_Forms {

const PACKAGE_VERSION = '0.31.5-alpha';
const PACKAGE_VERSION = '0.32.0-alpha';

/**
* Load the contact form module.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91b2516

Please sign in to comment.