diff --git a/packages/dataviews/CHANGELOG.md b/packages/dataviews/CHANGELOG.md index 8229445a68249c..324905088f0800 100644 --- a/packages/dataviews/CHANGELOG.md +++ b/packages/dataviews/CHANGELOG.md @@ -15,6 +15,7 @@ ### Enhancements +- DataForm: Update trigger mechanism for panel layout. [#75290](https://github.com/WordPress/gutenberg/pull/75290) - DataViews: Add `onReset` prop to control view reset functionality from the view config dropdown. [#75093](https://github.com/WordPress/gutenberg/pull/75093) - DataForm: Add automatic field labeling - forms now automatically mark the minority of fields (required or optional) to reduce visual noise. [#74430](https://github.com/WordPress/gutenberg/pull/74430) - DataViews: Add details form layout validation. [#74996](https://github.com/WordPress/gutenberg/pull/74996) diff --git a/packages/dataviews/src/components/dataform-layouts/index.tsx b/packages/dataviews/src/components/dataform-layouts/index.tsx index f4d1c5bf0890fe..8ffd905298bd18 100644 --- a/packages/dataviews/src/components/dataform-layouts/index.tsx +++ b/packages/dataviews/src/components/dataform-layouts/index.tsx @@ -34,7 +34,7 @@ const FORM_FIELD_LAYOUTS = [ { children } diff --git a/packages/dataviews/src/components/dataform-layouts/panel/dropdown.tsx b/packages/dataviews/src/components/dataform-layouts/panel/dropdown.tsx index a25d4066e52320..74c1f7c193b796 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/dropdown.tsx +++ b/packages/dataviews/src/components/dataform-layouts/panel/dropdown.tsx @@ -8,25 +8,24 @@ import { Button, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; -import { useMemo, useRef } from '@wordpress/element'; +import { useMemo, useRef, useState } from '@wordpress/element'; import { closeSmall } from '@wordpress/icons'; -import { useFocusOnMount } from '@wordpress/compose'; +import { __experimentalUseDialog as useDialog } from '@wordpress/compose'; import { Stack } from '@wordpress/ui'; /** * Internal dependencies */ import type { - FieldValidity, + FieldLayoutProps, NormalizedForm, - NormalizedFormField, FormValidity, - NormalizedField, } from '../../../types'; import { DataFormLayout } from '../data-form-layout'; import { DEFAULT_LAYOUT } from '../normalize-form'; import SummaryButton from './summary-button'; import useReportValidity from '../../../hooks/use-report-validity'; +import useFieldFromFormField from './utils/use-field-from-form-field'; function DropdownHeader( { title, @@ -78,25 +77,29 @@ function PanelDropdown< Item >( { field, onChange, validity, - labelPosition = 'side', - summaryFields, - fieldDefinition, - popoverAnchor, - onClose: onCloseCallback, - touched, -}: { - data: Item; - field: NormalizedFormField; - onChange: ( value: any ) => void; - validity?: FieldValidity; - labelPosition: 'side' | 'top' | 'none'; - summaryFields: NormalizedField< Item >[]; - fieldDefinition: NormalizedField< Item >; - popoverAnchor: HTMLElement | null; - onClose?: () => void; - touched: boolean; -} ) { - const fieldLabel = !! field.children ? field.label : fieldDefinition?.label; +}: FieldLayoutProps< Item > ) { + const [ touched, setTouched ] = useState( false ); + + // Use internal state instead of a ref to make sure that the component + // re-renders when the popover's anchor updates. + const [ popoverAnchor, setPopoverAnchor ] = useState< HTMLElement | null >( + null + ); + // Memoize popoverProps to avoid returning a new object every time. + const popoverProps = useMemo( + () => ( { + // Anchor the popover to the middle of the entire row so that it doesn't + // move around when the label changes. + anchor: popoverAnchor, + placement: 'left-start', + offset: 36, + shift: true, + } ), + [ popoverAnchor ] + ); + const [ dialogRef, dialogProps ] = useDialog( { + focusOnMount: 'firstInputElement', + } ); const form: NormalizedForm = useMemo( () => ( { @@ -120,80 +123,76 @@ function PanelDropdown< Item >( { return { [ field.id ]: validity }; }, [ validity, field ] ); - // Memoize popoverProps to avoid returning a new object every time. - const popoverProps = useMemo( - () => ( { - // Anchor the popover to the middle of the entire row so that it doesn't - // move around when the label changes. - anchor: popoverAnchor, - placement: 'left-start', - offset: 36, - shift: true, - } ), - [ popoverAnchor ] - ); - - const focusOnMountRef = useFocusOnMount( 'firstInputElement' ); + const { fieldDefinition, fieldLabel, summaryFields } = + useFieldFromFormField( field ); + if ( ! fieldDefinition ) { + return null; + } return ( - { - if ( ! willOpen ) { - onCloseCallback?.(); - } - } } - toggleProps={ { - size: 'compact', - variant: 'tertiary', - tooltipPosition: 'middle left', - } } - renderToggle={ ( { isOpen, onToggle } ) => ( - - ) } - renderContent={ ( { onClose } ) => ( - - - - - { ( - FieldLayout, - childField, - childFieldValidity, - markWhenOptional - ) => ( - - ) } - - - - ) } - /> + + { + if ( ! willOpen ) { + setTouched( true ); + } + } } + renderToggle={ ( { isOpen, onToggle } ) => ( + + ) } + renderContent={ ( { onClose } ) => ( + + + + + { ( + FieldLayout, + childField, + childFieldValidity, + markWhenOptional + ) => ( + + ) } + + + + ) } + /> + ); } diff --git a/packages/dataviews/src/components/dataform-layouts/panel/index.tsx b/packages/dataviews/src/components/dataform-layouts/panel/index.tsx index d6ddfd92aa9e60..fbe81eb3c2e93f 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/index.tsx +++ b/packages/dataviews/src/components/dataform-layouts/panel/index.tsx @@ -1,136 +1,9 @@ -/** - * External dependencies - */ -import clsx from 'clsx'; - -/** - * WordPress dependencies - */ -import { Icon, Tooltip } from '@wordpress/components'; -import { useState, useContext } from '@wordpress/element'; -import { error as errorIcon } from '@wordpress/icons'; -import { Stack } from '@wordpress/ui'; - /** * Internal dependencies */ -import type { - FieldLayoutProps, - FieldValidity, - NormalizedField, - NormalizedFormField, - NormalizedPanelLayout, -} from '../../../types'; -import DataFormContext from '../../dataform-context'; -import PanelDropdown from './dropdown'; +import type { FieldLayoutProps, NormalizedPanelLayout } from '../../../types'; import PanelModal from './modal'; -import { getSummaryFields } from '../get-summary-fields'; - -function getFirstValidationError( - validity: FieldValidity | undefined -): string | undefined { - if ( ! validity ) { - return undefined; - } - - const validityRules = Object.keys( validity ).filter( - ( key ) => key !== 'children' - ); - - for ( const key of validityRules ) { - const rule = validity[ key as keyof Omit< FieldValidity, 'children' > ]; - if ( rule === undefined ) { - continue; - } - - if ( rule.type === 'invalid' ) { - if ( rule.message ) { - return rule.message; - } - - // Provide default message for required validation (message is optional) - if ( key === 'required' ) { - return 'A required field is empty'; - } - - return 'Unidentified validation error'; - } - } - - // Check children recursively - if ( validity.children ) { - for ( const childValidity of Object.values( validity.children ) ) { - const childError = getFirstValidationError( childValidity ); - if ( childError ) { - return childError; - } - } - } - - return undefined; -} - -const getFieldDefinition = < Item, >( - field: NormalizedFormField, - fields: NormalizedField< Item >[] -) => { - const fieldDefinition = fields.find( ( _field ) => _field.id === field.id ); - - if ( ! fieldDefinition ) { - return fields.find( ( _field ) => { - if ( !! field.children ) { - const simpleChildren = field.children.filter( - ( child ) => ! child.children - ); - - if ( simpleChildren.length === 0 ) { - return false; - } - - return _field.id === simpleChildren[ 0 ].id; - } - - return _field.id === field.id; - } ); - } - - return fieldDefinition; -}; - -/** - * Determines the field definition and summary fields for a panel layout. - * - * Summary fields are determined with the following priority: - * 1. Use layout.summary fields if they exist - * 2. Fall back to the field definition that matches the form field's id - * 3. If the form field id doesn't exist, pick the first child field - * 4. If no field definition is found, return empty summary fields - * - * @param layout - The normalized panel layout configuration - * @param field - The form field to get definition for - * @param fields - Array of normalized field definitions - * @return Object containing fieldDefinition and summaryFields - */ -const getFieldDefinitionAndSummaryFields = < Item, >( - layout: NormalizedPanelLayout, - field: NormalizedFormField, - fields: NormalizedField< Item >[] -) => { - const summaryFields = getSummaryFields( layout.summary, fields ); - const fieldDefinition = getFieldDefinition( field, fields ); - - if ( summaryFields.length === 0 ) { - return { - summaryFields: fieldDefinition ? [ fieldDefinition ] : [], - fieldDefinition, - }; - } - - return { - summaryFields, - fieldDefinition, - }; -}; +import PanelDropdown from './dropdown'; export default function FormPanelField< Item >( { data, @@ -138,133 +11,25 @@ export default function FormPanelField< Item >( { onChange, validity, }: FieldLayoutProps< Item > ) { - const { fields } = useContext( DataFormContext ); const layout = field.layout as NormalizedPanelLayout; - // Use internal state instead of a ref to make sure that the component - // re-renders when the popover's anchor updates. - const [ popoverAnchor, setPopoverAnchor ] = useState< HTMLElement | null >( - null - ); - - // Track if the panel has been closed (touched) to only show errors after interaction. - const [ touched, setTouched ] = useState( false ); - const handleClose = () => setTouched( true ); - - const { fieldDefinition, summaryFields } = - getFieldDefinitionAndSummaryFields( layout, field, fields ); - - if ( ! fieldDefinition ) { - return null; - } - - const labelPosition = layout.labelPosition; - const errorMessage = getFirstValidationError( validity ); - const showError = touched && !! errorMessage; - const labelClassName = clsx( - 'dataforms-layouts-panel__field-label', - `dataforms-layouts-panel__field-label--label-position-${ labelPosition }`, - { 'has-error': showError } - ); - const fieldLabel = !! field.children ? field.label : fieldDefinition?.label; - - const labelContent = showError ? ( - - - - <>{ fieldLabel }> - - - ) : ( - fieldLabel - ); - - const renderedControl = - layout.openAs === 'modal' ? ( + if ( layout.openAs === 'modal' ) { + return ( - ) : ( - ); - - if ( labelPosition === 'top' ) { - return ( - - - { labelContent } - - - { renderedControl } - - - ); - } - - if ( labelPosition === 'none' ) { - return ( - - { showError && ( - - - - ) } - - { renderedControl } - - - ); } - // Defaults to label position side. return ( - - { labelContent } - - { renderedControl } - - + ); } diff --git a/packages/dataviews/src/components/dataform-layouts/panel/modal.tsx b/packages/dataviews/src/components/dataform-layouts/panel/modal.tsx index 9c0502dc8efead..a0b66a4f484563 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/modal.tsx +++ b/packages/dataviews/src/components/dataform-layouts/panel/modal.tsx @@ -23,7 +23,7 @@ import type { Field, NormalizedForm, NormalizedFormField, - NormalizedField, + FieldLayoutProps, } from '../../../types'; import { DataFormLayout } from '../data-form-layout'; import { DEFAULT_LAYOUT } from '../normalize-form'; @@ -31,6 +31,7 @@ import SummaryButton from './summary-button'; import useFormValidity from '../../../hooks/use-form-validity'; import useReportValidity from '../../../hooks/use-report-validity'; import DataFormContext from '../../dataform-context'; +import useFieldFromFormField from './utils/use-field-from-form-field'; function ModalContent< Item >( { data, @@ -164,37 +165,32 @@ function PanelModal< Item >( { data, field, onChange, - labelPosition, - summaryFields, - fieldDefinition, - onClose: onCloseCallback, - touched, -}: { - data: Item; - field: NormalizedFormField; - onChange: ( value: any ) => void; - labelPosition: 'side' | 'top' | 'none'; - summaryFields: NormalizedField< Item >[]; - fieldDefinition: NormalizedField< Item >; - onClose?: () => void; - touched: boolean; -} ) { + validity, +}: FieldLayoutProps< Item > ) { + const [ touched, setTouched ] = useState( false ); + const [ isOpen, setIsOpen ] = useState( false ); - const fieldLabel = !! field.children ? field.label : fieldDefinition?.label; + const { fieldDefinition, fieldLabel, summaryFields } = + useFieldFromFormField( field ); + if ( ! fieldDefinition ) { + return null; + } const handleClose = () => { setIsOpen( false ); - onCloseCallback?.(); + setTouched( true ); }; return ( <> setIsOpen( true ) } aria-expanded={ isOpen } diff --git a/packages/dataviews/src/components/dataform-layouts/panel/style.scss b/packages/dataviews/src/components/dataform-layouts/panel/style.scss index 4a3df8cd7d8722..ea3e8466a268a1 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/style.scss +++ b/packages/dataviews/src/components/dataform-layouts/panel/style.scss @@ -2,28 +2,101 @@ @use "@wordpress/base-styles/colors" as *; @use "@wordpress/base-styles/z-index" as *; -.dataforms-layouts-panel__field { +.dataforms-layouts-panel__field-trigger { + position: relative; + color: inherit; + display: flex; width: 100%; - min-height: $grid-unit-40; - justify-content: flex-start !important; - align-items: flex-start !important; + min-height: $grid-unit-30; + cursor: pointer; + align-items: flex-start; + border-radius: $radius-small; + isolation: isolate; + + &--label-side { + flex-direction: row; + gap: var(--wpds-dimension-gap-md); + } + + &--label-top { + display: grid; + grid-template-columns: 1fr auto; + } - &--label-position-none { - align-items: center !important; + &--label-none { + align-items: center; + } + + &:not([aria-disabled="true"]):hover { + color: var(--wp-admin-theme-color); + + .dataforms-layouts-panel__field-trigger-icon { + opacity: 1; + } + + .dataforms-layouts-panel__field-label { + color: var(--wp-admin-theme-color); + } + } + + &[aria-disabled="true"] { + cursor: default; + + .dataforms-layouts-panel__field-control { + color: $gray-700; + font-weight: var(--wpds-font-weight-regular); + } + } +} + +.dataforms-layouts-panel__field-trigger-icon { + appearance: none; + background: none; + border: none; + padding: 0; + margin: 0; + cursor: pointer; + display: flex; + align-items: center; + fill: var(--wp-admin-theme-color); + flex: 0 0 auto; + opacity: 0; + border-radius: var(--wpds-border-radius-xs); + + &::after { + content: ""; + position: absolute; + inset: 0; + } + + &:focus-visible { + opacity: 1; + outline: var(--wpds-border-width-focus) solid var(--wp-admin-theme-color); + } +} + +.dataforms-layouts-panel__field-dropdown-anchor { + width: 100%; + + .components-dropdown { + width: 100%; } } .dataforms-layouts-panel__field-label { width: 38%; flex-shrink: 0; - min-height: $grid-unit-40; + min-height: $grid-unit-30; display: flex; align-items: center; line-height: $grid-unit-05 * 5; hyphens: auto; + color: $gray-700; - &--label-position-side { - align-self: center; + .components-base-control__label { + display: inline; + margin-bottom: 0; + line-height: inherit; } &.has-error { @@ -32,8 +105,14 @@ } .dataforms-layouts-panel__field-label-error-content { + position: relative; + z-index: 1; cursor: help; fill: $alert-red; + display: inline-flex; + flex-direction: row; + align-items: center; + gap: $grid-unit-05; svg { fill: currentColor; } @@ -41,26 +120,30 @@ .dataforms-layouts-panel__field-control { flex-grow: 1; - min-height: $grid-unit-40; + min-width: 0; + min-height: $grid-unit-30; + line-height: var(--wpds-font-line-height-md); display: flex; align-items: center; + overflow: hidden; + font-weight: var(--wpds-font-weight-medium); - .components-button { - max-width: 100%; - text-align: left; - white-space: normal; - text-wrap: balance; // Fallback for Safari. - text-wrap: pretty; - min-height: $button-size-compact; + > * { + min-width: 0; } +} - &.components-button.is-link[aria-disabled="true"] { - text-decoration: none; - } +.dataforms-layouts-panel__field-trigger--label-top .dataforms-layouts-panel__field-label { + width: 100%; +} - .components-dropdown { - max-width: 100%; - } +.dataforms-layouts-panel__field-trigger--label-top .dataforms-layouts-panel__field-control { + grid-column: 1 / -1; +} + +.dataforms-layouts-panel__field-trigger--label-top .dataforms-layouts-panel__field-trigger-icon { + grid-row: 1; + grid-column: 2; } .dataforms-layouts-panel__field-dropdown .components-popover__content { @@ -79,7 +162,3 @@ .components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown { z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown"); } - -.dataforms-layouts-panel__summary-button:empty { - min-width: $admin-sidebar-width; -} diff --git a/packages/dataviews/src/components/dataform-layouts/panel/summary-button.tsx b/packages/dataviews/src/components/dataform-layouts/panel/summary-button.tsx index 8ef49bc7471f35..64eb207dc3e660 100644 --- a/packages/dataviews/src/components/dataform-layouts/panel/summary-button.tsx +++ b/packages/dataviews/src/components/dataform-layouts/panel/summary-button.tsx @@ -1,92 +1,139 @@ +/** + * External dependencies + */ +import clsx from 'clsx'; + /** * WordPress dependencies */ -import { Button } from '@wordpress/components'; +import { Icon, Tooltip } from '@wordpress/components'; import { sprintf, _x } from '@wordpress/i18n'; +import { error as errorIcon, pencil } from '@wordpress/icons'; +import { useInstanceId } from '@wordpress/compose'; /** * Internal dependencies */ -import type { NormalizedField } from '../../../types'; +import type { + FieldValidity, + NormalizedField, + NormalizedFormField, + NormalizedPanelLayout, +} from '../../../types'; +import getLabelClassName from './utils/get-label-classname'; +import getLabelContent from './utils/get-label-content'; +import getFirstValidationError from './utils/get-first-validation-error'; -function SummaryButton< Item >( { - summaryFields, +export default function SummaryButton< Item >( { data, - labelPosition, + field, fieldLabel, + summaryFields, + validity, + touched, disabled, onClick, 'aria-expanded': ariaExpanded, }: { - summaryFields: NormalizedField< Item >[]; data: Item; - labelPosition: 'side' | 'top' | 'none'; + field: NormalizedFormField; fieldLabel?: string; + summaryFields: NormalizedField< Item >[]; + validity?: FieldValidity; + touched: boolean; disabled?: boolean; onClick: () => void; 'aria-expanded'?: boolean; } ) { - return ( - + { labelPosition !== 'none' && ( + { labelContent } + ) } + { labelPosition === 'none' && showError && ( + + + + + ) } - onClick={ onClick } - disabled={ disabled } - accessibleWhenDisabled - style={ - summaryFields.length > 1 - ? { - minHeight: 'auto', - height: 'auto', + + { summaryFields.length > 1 ? ( + - { summaryFields.length > 1 ? ( - - { summaryFields.map( ( summaryField ) => ( - + { summaryFields.map( ( summaryField ) => ( + + + + ) ) } + + ) : ( + summaryFields.map( ( summaryField ) => ( + - - - ) ) } - - ) : ( - summaryFields.map( ( summaryField ) => ( - - ) ) + item={ data } + field={ summaryField } + /> + ) ) + ) } + + { ! disabled && ( + + + ) } - + ); } - -export default SummaryButton; diff --git a/packages/dataviews/src/components/dataform-layouts/panel/utils/get-first-validation-error.ts b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-first-validation-error.ts new file mode 100644 index 00000000000000..2a47fca2f96a05 --- /dev/null +++ b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-first-validation-error.ts @@ -0,0 +1,47 @@ +import type { FieldValidity } from '../../../../types'; + +function getFirstValidationError( + validity: FieldValidity | undefined +): string | undefined { + if ( ! validity ) { + return undefined; + } + + const validityRules = Object.keys( validity ).filter( + ( key ) => key !== 'children' + ); + + for ( const key of validityRules ) { + const rule = validity[ key as keyof Omit< FieldValidity, 'children' > ]; + if ( rule === undefined ) { + continue; + } + + if ( rule.type === 'invalid' ) { + if ( rule.message ) { + return rule.message; + } + + // Provide default message for required validation (message is optional) + if ( key === 'required' ) { + return 'A required field is empty'; + } + + return 'Unidentified validation error'; + } + } + + // Check children recursively + if ( validity.children ) { + for ( const childValidity of Object.values( validity.children ) ) { + const childError = getFirstValidationError( childValidity ); + if ( childError ) { + return childError; + } + } + } + + return undefined; +} + +export default getFirstValidationError; diff --git a/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-classname.ts b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-classname.ts new file mode 100644 index 00000000000000..a5e131ac4913a9 --- /dev/null +++ b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-classname.ts @@ -0,0 +1,18 @@ +/** + * External dependencies + */ +import clsx from 'clsx'; +import type { LabelPosition } from '../../../../types'; + +function getLabelClassName( + labelPosition: LabelPosition, + showError?: boolean +) { + return clsx( + 'dataforms-layouts-panel__field-label', + `dataforms-layouts-panel__field-label--label-position-${ labelPosition }`, + { 'has-error': showError } + ); +} + +export default getLabelClassName; diff --git a/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-content.tsx b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-content.tsx new file mode 100644 index 00000000000000..41b507cce80fe0 --- /dev/null +++ b/packages/dataviews/src/components/dataform-layouts/panel/utils/get-label-content.tsx @@ -0,0 +1,26 @@ +/** + * WordPress dependencies + */ +import { BaseControl, Icon, Tooltip } from '@wordpress/components'; +import { error as errorIcon } from '@wordpress/icons'; + +function getLabelContent( + showError?: boolean, + errorMessage?: string, + fieldLabel?: string +) { + return showError ? ( + + + + + { fieldLabel } + + + + ) : ( + { fieldLabel } + ); +} + +export default getLabelContent; diff --git a/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts b/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts new file mode 100644 index 00000000000000..3734670a07d916 --- /dev/null +++ b/packages/dataviews/src/components/dataform-layouts/panel/utils/use-field-from-form-field.ts @@ -0,0 +1,78 @@ +/** + * WordPress dependencies + */ +import { useContext } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import type { + NormalizedFormField, + NormalizedField, + NormalizedPanelLayout, +} from '../../../../types'; +import { getSummaryFields } from '../../get-summary-fields'; +import DataFormContext from '../../../dataform-context'; + +const getFieldDefinition = < Item >( + field: NormalizedFormField, + fields: NormalizedField< Item >[] +) => { + const fieldDefinition = fields.find( ( _field ) => _field.id === field.id ); + + if ( ! fieldDefinition ) { + return fields.find( ( _field ) => { + if ( !! field.children ) { + const simpleChildren = field.children.filter( + ( child ) => ! child.children + ); + + if ( simpleChildren.length === 0 ) { + return false; + } + + return _field.id === simpleChildren[ 0 ].id; + } + + return _field.id === field.id; + } ); + } + + return fieldDefinition; +}; + +/** + * Determines the field definition and summary fields for a panel layout. + * + * Summary fields are determined with the following priority: + * 1. Use layout.summary fields if they exist + * 2. Fall back to the field definition that matches the form field's id + * 3. If the form field id doesn't exist, pick the first child field + * 4. If no field definition is found, return empty summary fields + * + * @param field The form field to get definition for + * @return Object containing fieldDefinition, fieldLabel, and summaryFields + */ +function useFieldFromFormField( field: NormalizedFormField ) { + const { fields } = useContext( DataFormContext ); + const layout = field.layout as NormalizedPanelLayout; + const summaryFields = getSummaryFields( layout.summary, fields ); + const fieldDefinition = getFieldDefinition( field, fields ); + const fieldLabel = !! field.children ? field.label : fieldDefinition?.label; + + if ( summaryFields.length === 0 ) { + return { + summaryFields: fieldDefinition ? [ fieldDefinition ] : [], + fieldDefinition, + fieldLabel, + }; + } + + return { + summaryFields, + fieldDefinition, + fieldLabel, + }; +} + +export default useFieldFromFormField; diff --git a/packages/dataviews/src/components/dataform-layouts/regular/style.scss b/packages/dataviews/src/components/dataform-layouts/regular/style.scss index ff766a869ea3cb..477187a6e116e8 100644 --- a/packages/dataviews/src/components/dataform-layouts/regular/style.scss +++ b/packages/dataviews/src/components/dataform-layouts/regular/style.scss @@ -1,3 +1,4 @@ +@use "@wordpress/base-styles/colors" as *; @use "@wordpress/base-styles/variables" as *; .dataforms-layouts-regular__field { @@ -5,6 +6,11 @@ min-height: $grid-unit-40; justify-content: flex-start !important; align-items: flex-start !important; + + .components-base-control__label, + .components-input-control__label { + color: $gray-700; + } } .dataforms-layouts-regular__field-label { diff --git a/packages/dataviews/src/dataform/test/dataform.tsx b/packages/dataviews/src/dataform/test/dataform.tsx index 838965854db9e5..972c613d588267 100644 --- a/packages/dataviews/src/dataform/test/dataform.tsx +++ b/packages/dataviews/src/dataform/test/dataform.tsx @@ -511,9 +511,9 @@ describe( 'DataForm component', () => { /> ); - const titleField = screen.getByText( data.title ); + const titleButton = fieldsSelector.title.view(); const user = await userEvent.setup(); - await user.click( titleField ); + await user.click( titleButton ); const titleEditField = screen.getByText( 'This is the Title Field' ); diff --git a/packages/edit-site/src/components/post-list/quick-edit-modal.js b/packages/edit-site/src/components/post-list/quick-edit-modal.js index a2b044f46cf22d..bf4184581bcbb8 100644 --- a/packages/edit-site/src/components/post-list/quick-edit-modal.js +++ b/packages/edit-site/src/components/post-list/quick-edit-modal.js @@ -25,35 +25,6 @@ const { usePostFields, PostCardPanel } = unlock( editorPrivateApis ); const fieldsWithBulkEditSupport = [ 'status', 'date', 'author', 'discussion' ]; export function QuickEditModal( { postType, postId, closeModal } ) { - // Before calling the onRequestClose callback, the modal introduces a animation delay - // that produces a visual glitch, see https://github.com/WordPress/gutenberg/pull/75173#pullrequestreview-3755585506 - // Handling the ESC key and click-outside ourselves fixes it. - useEffect( () => { - const handleKeyDown = ( event ) => { - if ( event.key === 'Escape' || event.code === 'Escape' ) { - event.preventDefault(); - event.stopPropagation(); - closeModal?.(); - } - }; - - const handleClickOutside = ( event ) => { - if ( - event.target.classList.contains( - 'dataviews-action-modal__quick-edit' - ) - ) { - closeModal?.(); - } - }; - document.addEventListener( 'keydown', handleKeyDown ); - document.addEventListener( 'mousedown', handleClickOutside ); - return () => { - document.removeEventListener( 'keydown', handleKeyDown ); - document.removeEventListener( 'mousedown', handleClickOutside ); - }; - }, [ closeModal ] ); - const isBulk = postId.length > 1; const [ localEdits, setLocalEdits ] = useState( {} ); @@ -221,8 +192,8 @@ export function QuickEditModal( { postType, postId, closeModal } ) { { }, statusVisibility: { performEdit: async ( page ) => { - const statusAndVisibility = page.getByLabel( - 'Status & Visibility' - ); - await statusAndVisibility.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Status & Visibility', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); + const statusAndVisibility = editButton.locator( '..' ); const options = [ 'Published', 'Draft', @@ -142,17 +144,21 @@ test.describe( 'Page List', () => { } }, assertInitialState: async ( page ) => { - const statusAndVisibility = page.getByLabel( - 'Status & Visibility' - ); + const statusAndVisibility = page + .getByRole( 'button', { + name: 'Edit Status & Visibility', + } ) + .locator( '..' ); await expect( statusAndVisibility ).toContainText( 'Published' ); }, assertEditedState: async ( page ) => { - const statusAndVisibility = page.getByLabel( - 'Status & Visibility' - ); + const statusAndVisibility = page + .getByRole( 'button', { + name: 'Edit Status & Visibility', + } ) + .locator( '..' ); await expect( statusAndVisibility ).toContainText( 'Private' ); @@ -160,12 +166,17 @@ test.describe( 'Page List', () => { }, author: { assertInitialState: async ( page ) => { - const author = page.getByLabel( 'Author' ); + const author = page + .getByRole( 'button', { name: 'Edit Author' } ) + .locator( '..' ); await expect( author ).toContainText( 'admin' ); }, performEdit: async ( page ) => { - const author = page.getByLabel( 'Author' ); - await author.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Author', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); const selectElement = page.locator( 'select:has(option[value="1"])' ); @@ -174,21 +185,28 @@ test.describe( 'Page List', () => { } ); }, assertEditedState: async ( page ) => { - const author = page.getByLabel( 'Edit Author' ); + const author = page + .getByRole( 'button', { name: 'Edit Author' } ) + .locator( '..' ); await expect( author ).toContainText( 'Test Author' ); }, }, date: { assertInitialState: async ( page ) => { - const dateEl = page.getByLabel( 'Edit Date' ); + const dateEl = page + .getByRole( 'button', { name: 'Edit Date' } ) + .locator( '..' ); const date = new Date(); const yy = String( date.getFullYear() ); await expect( dateEl ).toContainText( yy ); }, performEdit: async ( page ) => { - const dateEl = page.getByLabel( 'Edit Date' ); - await dateEl.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Date', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); // Wait for the datetime control to appear const datetimeInput = page.locator( @@ -213,18 +231,25 @@ test.describe( 'Page List', () => { assertEditedState: async ( page ) => { const date = new Date(); const yy = Number( date.getFullYear() ); - const dateEl = page.getByLabel( 'Edit Date' ); + const dateEl = page + .getByRole( 'button', { name: 'Edit Date' } ) + .locator( '..' ); await expect( dateEl ).toContainText( String( yy + 1 ) ); }, }, slug: { assertInitialState: async ( page ) => { - const slug = page.getByLabel( 'Edit Slug' ); + const slug = page + .getByRole( 'button', { name: 'Edit Slug' } ) + .locator( '..' ); await expect( slug ).toContainText( 'privacy-policy' ); }, performEdit: async ( page ) => { - const slug = page.getByLabel( 'Edit Slug' ); - await slug.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Slug', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); await expect( page.getByRole( 'link', { name: 'http://localhost:8889/?', @@ -235,12 +260,17 @@ test.describe( 'Page List', () => { }, parent: { assertInitialState: async ( page ) => { - const parent = page.getByLabel( 'Edit Parent' ); + const parent = page + .getByRole( 'button', { name: 'Edit Parent' } ) + .locator( '..' ); await expect( parent ).toContainText( 'None' ); }, performEdit: async ( page ) => { - const parent = page.getByLabel( 'Edit Parent' ); - await parent.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Parent', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); await page .getByLabel( 'Parent', { exact: true } ) .fill( 'Sample' ); @@ -250,7 +280,9 @@ test.describe( 'Page List', () => { .click(); }, assertEditedState: async ( page ) => { - const parent = page.getByLabel( 'Edit Parent' ); + const parent = page + .getByRole( 'button', { name: 'Edit Parent' } ) + .locator( '..' ); await expect( parent ).toContainText( 'Sample Page' ); }, }, @@ -277,12 +309,19 @@ test.describe( 'Page List', () => { // }, discussion: { assertInitialState: async ( page ) => { - const discussion = page.getByLabel( 'Edit Discussion' ); + const discussion = page + .getByRole( 'button', { + name: 'Edit Discussion', + } ) + .locator( '..' ); await expect( discussion ).toContainText( 'Closed' ); }, performEdit: async ( page ) => { - const discussion = page.getByLabel( 'Edit Discussion' ); - await discussion.click(); + const editButton = page.getByRole( 'button', { + name: 'Edit Discussion', + } ); + await editButton.locator( '..' ).hover(); + await editButton.click(); await page .getByLabel( 'Open', { exact: true, @@ -290,7 +329,11 @@ test.describe( 'Page List', () => { .check(); }, assertEditedState: async ( page ) => { - const discussion = page.getByLabel( 'Edit Discussion' ); + const discussion = page + .getByRole( 'button', { + name: 'Edit Discussion', + } ) + .locator( '..' ); await expect( discussion ).toContainText( 'Comments only' ); }, },