Skip to content

Forms: Add field preview component for response view#46706

Merged
CGastrell merged 39 commits intotrunkfrom
add/forms-reponse-preview-fields-style
Jan 26, 2026
Merged

Forms: Add field preview component for response view#46706
CGastrell merged 39 commits intotrunkfrom
add/forms-reponse-preview-fields-style

Conversation

@CGastrell
Copy link
Copy Markdown
Contributor

@CGastrell CGastrell commented Jan 21, 2026

Part of FORMS-520
Part of FORMS-521
Part of FORMS-522
Part of FORMS-523
Part of FORMS-524
Part of FORMS-525
Part of FORMS-526
Part of FORMS-527
Part of FORMS-528
Part of FORMS-529
Part of FORMS-530

Proposed changes:

  • Add new FieldPreview component for rendering form response fields in the dashboard
  • Introduce new collection format for API field responses with structured field properties
  • Add type definitions for legacy and new field formats (ResponseField, FieldType)
  • Render appropriate icons for each field type using block definitions
  • Properly handle different value types (arrays, files, image-select, emails, URLs, phone numbers)
  • Scope styles to prevent old format rendering from affecting new FieldPreview styles
Before After
image image

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

8TmDfrB54NU4Rzj19Qs9Tg-fi-4935_76204
p1769009515362959-slack-C052XEUUBL4

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Go to the Jetpack Forms dashboard
  • Submit a form with various field types (text, email, phone, URL, file upload, image select, checkbox, radio, etc.)
  • View the form responses in the dashboard
  • Verify that each field displays with the correct icon matching its field type
  • Verify that field values render correctly:
    • Email fields show clickable mailto links
    • Phone fields show clickable tel links
    • URL fields show external links
    • File uploads show file previews
    • Image selects show image choices
    • Arrays (multiple choice) display as comma-separated values
  • Confirm styles are properly scoped and don't affect other response view elements

Copilot AI review requested due to automatic review settings January 21, 2026 14:47
@CGastrell CGastrell added Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Package] Forms Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR labels Jan 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 21, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/forms-reponse-preview-fields-style branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/forms-reponse-preview-fields-style

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: February 3, 2026
    • Code freeze: February 3, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@CGastrell CGastrell self-assigned this Jan 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request introduces a new field preview component for displaying form responses in the Jetpack Forms dashboard with improved type-specific rendering and icons.

Changes:

  • Adds a new FieldPreview React component that renders form response fields with appropriate icons based on field type
  • Introduces a new collection API format for form response fields with structured metadata (type, id, key, meta)
  • Updates type definitions to support both the new collection format and legacy label-value format for backwards compatibility

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
projects/packages/forms/src/types/index.ts Adds ResponseField interface, FieldType union, FileItem type, and ResponseFields union type to support both new and legacy field formats
projects/packages/forms/src/dashboard/components/response-view/field-preview/index.tsx Implements new FieldPreview component with field type detection, icon rendering, and value formatting logic
projects/packages/forms/src/dashboard/components/response-view/field-preview/style.scss Adds scoped styles for the field preview component including icon, label, and value containers
projects/packages/forms/src/dashboard/components/inspector/body.tsx Updates response viewer to detect and render fields using new FieldPreview component when collection format is present
projects/packages/forms/src/dashboard/components/inspector/style.scss Adds conditional styling using :has() selector to maintain legacy format styles while supporting new field preview styles
projects/packages/forms/src/contact-form/class-feedback.php Adds 'collection' array shape option to get_compiled_fields() method that returns structured field data
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php Changes API endpoint to use 'collection' format instead of 'label-value' format for response fields
projects/packages/forms/changelog/add-forms-reponse-preview-fields-style Adds changelog entry describing the new feature

Comment thread projects/packages/forms/src/types/index.ts Outdated
Comment thread projects/packages/forms/src/types/index.ts
Comment thread projects/packages/forms/src/dashboard/components/inspector/body.tsx Outdated
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control Bot commented Jan 21, 2026

Code Coverage Summary

Coverage changed in 6 files. Only the first 5 are listed here.

File Coverage Δ% Δ Uncovered
projects/packages/forms/src/dashboard/components/inspector/body.tsx 0/106 (0.00%) 0.00% 13 💔
projects/packages/forms/src/contact-form/class-feedback-field.php 148/191 (77.49%) -0.29% 1 ❤️‍🩹
projects/packages/forms/src/dashboard/class-dashboard.php 29/143 (20.28%) -0.14% 1 ❤️‍🩹
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php 807/932 (86.59%) 0.13% 0 💚
projects/packages/forms/src/contact-form/class-feedback.php 677/706 (95.89%) 0.06% 0 💚

20 files are newly checked for coverage. Only the first 5 are listed here.

File Coverage
projects/packages/forms/src/dashboard/components/response-view/field-preview/index.tsx 0/62 (0.00%) 💔
projects/packages/forms/src/dashboard/components/response-view/field-preview/field-preview-utils.ts 12/12 (100.00%) 💚
projects/packages/forms/src/blocks/field-checkbox/icon.js 0/0 (—%) 🤷

Full summary · PHP report · JS report

Coverage check overridden by Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR .

Copilot AI review requested due to automatic review settings January 21, 2026 15:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Comment thread projects/packages/forms/src/contact-form/class-feedback.php
Comment thread projects/packages/forms/src/dashboard/components/inspector/style.scss Outdated
Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 21, 2026

@CGastrell I've opened a new pull request, #46710, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI review requested due to automatic review settings January 21, 2026 16:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 12 comments.

Comments suppressed due to low confidence (3)

projects/packages/forms/src/dashboard/components/response-view/field-preview/index.tsx:195

  • The URL validation regex /^https?:\/\// is too permissive and could allow malformed URLs. Consider using a more robust URL validation or the URL constructor to validate the URL format before creating a link. For example, wrap in a try-catch when using new URL(stringValue) to ensure it's a valid URL.

export default FieldPreview;

projects/packages/forms/src/dashboard/components/response-view/field-preview/index.tsx:202

  • The icon rendering in the field preview lacks proper accessibility attributes. Consider adding an aria-label or aria-hidden attribute to the icon container to ensure screen readers handle the icons appropriately. Since the icons are decorative and the field label provides context, aria-hidden="true" would likely be appropriate.
    projects/packages/forms/src/dashboard/components/response-view/field-preview/index.tsx:213
  • The new FieldPreview component lacks test coverage. Consider adding unit tests to verify icon rendering, field type detection, value formatting (arrays, files, emails, URLs, phone numbers), and edge cases like null/undefined values or unexpected field types.
		if ( value === null || value === undefined ) {
			return '-';
		}

		// Handle arrays (e.g., multiple choice selections)
		if ( Array.isArray( value ) ) {
			return value.join( ', ' );
		}

		// Handle objects that aren't special types - convert to string representation
		if ( typeof value === 'object' ) {
			return JSON.stringify( value );
		}

		const stringValue = String( value );

		// Emails
		if ( fieldType === 'email' && EMAIL_REGEX.test( stringValue ) ) {
			return <FieldEmail email={ stringValue } />;
		}

		// Phone numbers
		if ( fieldType === 'phone' || fieldType === 'telephone' ) {
			return <a href={ `tel:${ stringValue }` }>{ stringValue }</a>;
		}

		if ( fieldType === 'url' && /^https?:\/\//.test( stringValue ) ) {
			return <ExternalLink href={ stringValue }>{ stringValue }</ExternalLink>;
		}

		return stringValue;
	};

	return (
		<HStack alignment="topLeft" spacing="4" className="jp-forms__field-preview">
			<div className="jp-forms__field-preview-icon">{ icon }</div>
			<VStack spacing="0" className="jp-forms__field-preview-content">
				<div className="jp-forms__field-preview-label">
					{ label.endsWith( '?' ) ? label : `${ label }:` }
				</div>
				<div className="jp-forms__field-preview-value">{ renderFieldValue() }</div>
			</VStack>
		</HStack>
	);
};

export default FieldPreview;

Comment thread projects/packages/forms/src/dashboard/components/inspector/body.tsx Outdated
Comment on lines +128 to +130

const FieldPreview = ( { field, onFilePreview }: FieldPreviewProps ) => {
const { label, value, type } = field;
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc comment uses incorrect parameter syntax. Change "@param {ResponseField} field" and "@return {FieldType}" to "@param field" and "@return" since this is TypeScript. The type information is already captured in the TypeScript function signature.

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +77
* @param {FieldType} fieldType - The field type.
* @return {React.ReactNode} The icon element.
*/
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc comment uses incorrect parameter syntax. Change "@param {FieldType} fieldType" and "@return {React.ReactNode}" to "@param fieldType" and "@return" since this is TypeScript. The type information is already in the TypeScript signature.

Copilot uses AI. Check for mistakes.
Comment on lines +43 to +47
*
* @param {object} blockDef - The block definition object.
* @param {object} blockDef.settings - The block settings object.
* @param {object} blockDef.settings.icon - The icon object.
* @return {React.ReactNode} The rendered icon element.
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function documentation is missing information about the return value when the icon cannot be found or is invalid. Consider documenting what happens in edge cases, such as when a block definition doesn't have a properly structured icon property.

Suggested change
*
* @param {object} blockDef - The block definition object.
* @param {object} blockDef.settings - The block settings object.
* @param {object} blockDef.settings.icon - The icon object.
* @return {React.ReactNode} The rendered icon element.
* If a valid icon cannot be resolved (for example, when the icon property is missing
* or not in a supported format), the function returns `undefined` and no icon is rendered.
*
* @param {object} blockDef - The block definition object.
* @param {object} blockDef.settings - The block settings object.
* @param {object} blockDef.settings.icon - The icon object.
* @return {React.ReactNode | undefined} The rendered icon element, or `undefined` when no valid icon is available.

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +47
* @param {object} blockDef - The block definition object.
* @param {object} blockDef.settings - The block settings object.
* @param {object} blockDef.settings.icon - The icon object.
* @return {React.ReactNode} The rendered icon element.
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc comment uses incorrect parameter syntax. Change "@param {object} blockDef" to "@param blockDef" since this is TypeScript, not JSDoc. TypeScript type annotations should be used instead of JSDoc type annotations.

Suggested change
* @param {object} blockDef - The block definition object.
* @param {object} blockDef.settings - The block settings object.
* @param {object} blockDef.settings.icon - The icon object.
* @return {React.ReactNode} The rendered icon element.
* @param blockDef - The block definition object.
* @param blockDef.settings - The block settings object.
* @param blockDef.settings.icon - The icon object.
* @returns The rendered icon element.

Copilot uses AI. Check for mistakes.
Comment on lines +49 to +70
const getBlockIcon = ( blockDef: { settings: { icon: unknown } } ): React.ReactNode => {
const { icon } = blockDef.settings;

// Handle icon: { src: ... } structure
if ( icon && typeof icon === 'object' && 'src' in icon ) {
const src = ( icon as { src: unknown } ).src;
// If src is a function (renderMaterialIcon returns a function), call it
if ( typeof src === 'function' ) {
return ( src as () => JSX.Element )();
}
// Otherwise it's already a React element
return src as React.ReactNode;
}

// Handle icon directly being a function (e.g., field-number)
if ( typeof icon === 'function' ) {
return ( icon as () => JSX.Element )();
}

// Otherwise icon is already a React element
return icon as React.ReactNode;
};
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no error handling for the case when getBlockIcon is called with a block definition that has an unexpected icon structure. If the icon is undefined or null, the function will return it as-is, which could lead to rendering issues. Consider adding a fallback icon or null check.

Copilot uses AI. Check for mistakes.
Comment on lines +35 to +41
import type { ResponseField, FieldType, FileItem } from '../../../../types/index.ts';
import './style.scss';

const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;

/**
* Extracts and renders the icon from a block definition.
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EMAIL_REGEX constant at line 38 is duplicated from the emailRegEx at line 203 in body.tsx. Consider extracting this into a shared constant to avoid duplication and ensure consistency across the codebase.

Suggested change
import type { ResponseField, FieldType, FileItem } from '../../../../types/index.ts';
import './style.scss';
const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
/**
* Extracts and renders the icon from a block definition.
import { emailRegEx as EMAIL_REGEX } from '../body';
import type { ResponseField, FieldType, FileItem } from '../../../../types/index.ts';
import './style.scss';
/**
* Extracts and renders the icon from a block definition.
* Extracts and renders the icon from a block definition.

Copilot uses AI. Check for mistakes.
Comment on lines +172 to +173
}

Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When joining array values with ', ', there's no handling for arrays containing non-string types or complex objects. This could result in displaying "[object Object]" or other unexpected output. Consider using a more robust approach such as filtering or mapping array items to strings before joining.

Copilot uses AI. Check for mistakes.

if ( rest_is_field_included( 'fields', $fields ) ) {
$data['fields'] = $feedback_response->get_compiled_fields( 'api', 'label-value' );
$data['fields'] = $feedback_response->get_compiled_fields( 'api', 'collection' );
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the API response format from 'label-value' to 'collection' is a breaking change that could affect existing API consumers or cached responses. Consider implementing API versioning or maintaining backward compatibility by checking a query parameter to allow clients to opt into the new format while maintaining the old format as default. Alternatively, ensure all consumers are updated to handle the new format before deploying this change.

Suggested change
$data['fields'] = $feedback_response->get_compiled_fields( 'api', 'collection' );
$fields_format = 'label-value';
if ( isset( $request ) && $request instanceof \WP_REST_Request ) {
$request_fields_format = $request->get_param( 'fields_format' );
if ( in_array( $request_fields_format, array( 'label-value', 'collection' ), true ) ) {
$fields_format = $request_fields_format;
}
}
$data['fields'] = $feedback_response->get_compiled_fields( 'api', $fields_format );

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 23, 2026 19:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

}

if ( $this->is_of_type( 'checkbox-multiple' ) ) {
// Since API gets format: collection, return the array as is.
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment implies checkbox-multiple is returned as an array only because the API uses collection format, but get_render_api_value() is independent of the compiled fields shape and may be used with other shapes in the future. Consider rewording to describe the real reason (preserve arrays for checkbox-multiple in API responses) so this doesn’t become misleading if the endpoint shape changes again.

Suggested change
// Since API gets format: collection, return the array as is.
// Preserve the array structure for checkbox-multiple selections in API responses.

Copilot uses AI. Check for mistakes.
Comment on lines +4 to +10
import { Icon } from '@wordpress/components';
/**
* Internal dependencies
*/
import type { FieldType } from '../../../../types/index.ts';

export type BlockIcon = React.ComponentProps< typeof Icon >[ 'icon' ];
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Icon is imported as a runtime value but only used for a type (React.ComponentProps<typeof Icon>...). With the Babel TypeScript preset, this import will be emitted into JS even though it’s type-only. Switch to a type-only import (import type) or refactor the type extraction so this file doesn’t add an unnecessary runtime dependency.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Comment on lines 109 to +121
.jp-forms__inbox-response-data {
border-top: 1px solid var(--jp-forms-border-color);

@include mixins.flex-column;
font-size: var(--jp-forms-font-size-regular);
line-height: 24px;
padding: 24px 16px;
row-gap: 24px;
}
padding: 0 16px;

.jp-forms__inbox-response-data-label {
font-weight: 600;
}
// old field format renders without .jp-forms__field-preview
&:not(.is-collection-format) {

@include mixins.flex-column;
row-gap: 24px;
padding-top: 24px;
padding-bottom: 24px;
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block introduces new physical paddings (padding: 0 16px, padding-top, padding-bottom). For RTL support, prefer logical equivalents like padding-inline and padding-block (see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties).

Copilot uses AI. Check for mistakes.
Comment on lines +360 to +364
if ( $this->is_of_type( 'checkbox-multiple' ) ) {
// Since API gets format: collection, return the array as is.
return $this->value;
}

Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In get_render_api_value(), checkbox-multiple now returns the raw array unconditionally. This changes the existing label-value API output (which previously returned a comma-separated string via the is_array() branch) and breaks the stated backwards-compatibility guarantee for the default fields format. Consider keeping the current string behavior for label-value responses and only returning an array for fields_format=collection (e.g., by using $field->get_value() specifically in the collection branch of Feedback::get_compiled_fields(), or by passing the requested fields format down so get_render_api_value() can decide).

Suggested change
if ( $this->is_of_type( 'checkbox-multiple' ) ) {
// Since API gets format: collection, return the array as is.
return $this->value;
}

Copilot uses AI. Check for mistakes.
page: 1,
per_page: 20,
status: 'draft,publish',
fields_format: 'collection',
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaulting fields_format to collection in the dashboard query changes the shape of FormResponse.fields from an object (label-value) to an array. There is existing dashboard code that still assumes fields is an object and iterates via Object.entries (e.g., src/dashboard/hooks/use-inbox-data.ts), which will break when records are fetched in collection format. Either update the remaining consumers to support ResponseFields, or avoid requesting collection for list views and only request it where the rich field objects are actually needed.

Suggested change
fields_format: 'collection',

Copilot uses AI. Check for mistakes.
'per_page' => 20,
'status' => 'draft,publish',
'context' => 'edit',
'fields_format' => 'collection',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

}

export type FieldType =
| 'name'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep this update if we introduce a new field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely, yes.

} );

describe( 'FIELD_TYPE_LABEL_PREFIXES', () => {
it( 'is an array of tuples with string prefix and field type', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test? What are we testing here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sure FIELD_TYPE_LABEL_PREFIXES is an array of tuples ([ string, string ]) which is then used to map the inferred field types. The test just makes sure it is well formed.

* Labels are stored in lowercase for case-insensitive matching.
* Order matters: more specific labels should come before generic ones.
*/
export const FIELD_TYPE_LABEL_PREFIXES: Array< [ string, FieldType ] > = [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be done on the backend. So that it could be used across other types of UI. Also it seems really English dependent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could. It is English dependent. This is merely a measure to get the right icon for "guessing" the field type. I don't hold high hopes for this as it's basically just mapping default labels to field types. It works but in all honesty it shouldn't even be there. Worst case, the field type defaults to Text Field icon.
Passing it down from backend and making it translatable notches the complexity a bit, if we are going to do it, I'd leave it to a follow up.

* Internal dependencies
*/
// Field block definitions - used to get consistent icons
import CheckboxFieldBlock from '../../../../blocks/field-checkbox/index.js';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this. Could we just make sure that the icons are exportable?
I think this could really increase the size of the dashboard bundle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought these would be tree-shaken. I guess we could export a simple object { src: icon-here } on all those blocks and then just import those directly. Good catch, let me see to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { label, value, type } = field;
// For legacy responses without a proper type (undefined or "basic"), try to infer from label
const fieldType: FieldType =
type && type !== 'basic' ? type : inferFieldTypeFromLabel( label ) ?? 'text';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not bother with this... for V1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then all the other measures would be obsolete as well (prefix tuples, test). Worst case, is a good way of defaulting to some icon, then fallback to Text Field icon. But if you're paramount about it, then we can get rid of this and the PREFIX array, they are all for there for the same reason.

}

// Handle null/undefined
if ( value === null || value === undefined || value === '' ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we convert the value into string and just check if it is empty ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done at line 144, just making sure it has some content when needed. I think it can be removed, most of this is taken care on backend anyways, this is simply fail safe mechanics. Here's the change for a simpler approach 8b3e986

/** The field type (e.g., 'name', 'email', 'text', 'file', etc.). 'basic' is a legacy value for older responses. */
type?: FieldType | 'basic';
/** The form field ID from the form schema. */
id?: string;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care about all these (id, key, meta)? How are they important?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment we only use key, in the past having the ids has proven valuable but it has no use right now. Same goes for meta. I can remove those from the typing, but feels natural to have the entire object typed on frontend as it is on backend. The props are optional so the format could be capped if we need to (and it would just be a change on backend, no client update needed).

Copilot AI review requested due to automatic review settings January 26, 2026 15:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 36 out of 36 changed files in this pull request and generated 1 comment.

Comment on lines +135 to +137
if ( typeof value === 'object' ) {
return JSON.stringify( value );
}
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof value === 'object' will also match null, which will render as the literal string "null" via JSON.stringify(null). Consider handling null/undefined explicitly (e.g., treat them as empty and render -) before the object branch to avoid showing "null" to users.

Copilot uses AI. Check for mistakes.
@CGastrell CGastrell merged commit a8abd79 into trunk Jan 26, 2026
70 checks passed
@CGastrell CGastrell deleted the add/forms-reponse-preview-fields-style branch January 26, 2026 18:33
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Jan 26, 2026
@github-actions github-actions Bot added this to the jetpack/15.5 milestone Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Form Form block (also see Form package label) Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Forms [Package] Forms [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants