@wordpress/dataviews 15.0.0 → 16.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/build/components/dataform-controls/datetime.cjs.map +2 -2
- package/build/components/dataviews-context/index.cjs.map +2 -2
- package/build/components/dataviews-layouts/table/use-scroll-state.cjs.map +1 -1
- package/build/components/dataviews-layouts/utils/item-click-wrapper.cjs.map +2 -2
- package/build/hooks/use-form-validity.cjs.map +1 -1
- package/build-module/components/dataform-controls/datetime.mjs.map +2 -2
- package/build-module/components/dataviews-context/index.mjs.map +2 -2
- package/build-module/components/dataviews-layouts/table/use-scroll-state.mjs.map +1 -1
- package/build-module/components/dataviews-layouts/utils/item-click-wrapper.mjs.map +2 -2
- package/build-module/hooks/use-form-validity.mjs.map +1 -1
- package/build-types/components/dataviews-context/index.d.ts +2 -2
- package/build-types/components/dataviews-context/index.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/table/use-scroll-state.d.ts +5 -9
- package/build-types/components/dataviews-layouts/table/use-scroll-state.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/utils/item-click-wrapper.d.ts.map +1 -1
- package/build-wp/index.js +106 -118
- package/package.json +18 -18
- package/src/components/dataform-controls/datetime.tsx +1 -1
- package/src/components/dataviews-context/index.ts +4 -2
- package/src/components/dataviews-layouts/table/use-scroll-state.ts +6 -6
- package/src/components/dataviews-layouts/utils/item-click-wrapper.tsx +1 -3
- package/src/hooks/use-form-validity.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/components/dataform-controls/datetime.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tBaseControl,\n\tprivateApis as componentsPrivateApis,\n} from '@wordpress/components';\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport { dateI18n, getDate, getSettings } from '@wordpress/date';\nimport { Stack } from '@wordpress/ui';\n\n/**\n * Internal dependencies\n */\nimport type { DataFormControlProps, FormatDatetime } from '../../types';\nimport { OPERATOR_IN_THE_PAST, OPERATOR_OVER } from '../../constants';\nimport RelativeDateControl from './utils/relative-date-control';\nimport useDisabledDateMatchers from './utils/use-disabled-date-matchers';\nimport getCustomValidity from './utils/get-custom-validity';\nimport parseDateTime from '../../field-types/utils/parse-date-time';\nimport { unlock } from '../../lock-unlock';\n\nconst { DateCalendar, ValidatedInputControl } = unlock( componentsPrivateApis );\n\nconst formatDateTime = ( value?: string ): string => {\n\tif ( ! value ) {\n\t\treturn '';\n\t}\n\t// Format in WordPress timezone for datetime-local input: YYYY-MM-DDTHH:mm\n\treturn dateI18n( 'Y-m-d\\\\TH:i', getDate( value ) );\n};\n\nfunction CalendarDateTimeControl< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tconst { compact } = config || {};\n\tconst { id, label, description, setValue, getValue, isValid } = field;\n\tconst disabled = field.isDisabled( { item: data, field } );\n\tconst fieldValue = getValue( { item: data } );\n\tconst value = typeof fieldValue === 'string' ? fieldValue : undefined;\n\n\tconst [ calendarMonth, setCalendarMonth ] = useState< Date >( () => {\n\t\tconst parsedDate = parseDateTime( value );\n\t\treturn parsedDate || new Date(); // Default to current month\n\t} );\n\n\tconst inputControlRef = useRef< HTMLInputElement >( null );\n\tconst validationTimeoutRef =\n\t\tuseRef< ReturnType< typeof setTimeout > >( undefined );\n\tconst previousFocusRef = useRef< Element >( null );\n\n\tconst { minConstraint, maxConstraint, disabledMatchers } =\n\t\tuseDisabledDateMatchers( isValid, parseDateTime );\n\n\tconst onChangeCallback = useCallback(\n\t\t( newValue: string | undefined ) =>\n\t\t\tonChange( setValue( { item: data, value: newValue } ) ),\n\t\t[ data, onChange, setValue ]\n\t);\n\n\t// Cleanup timeout on unmount\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n\n\tconst onSelectDate = useCallback(\n\t\t( newDate: Date | undefined | null ) => {\n\t\t\tlet dateTimeValue: string | undefined;\n\t\t\tif ( newDate ) {\n\t\t\t\t// Extract the date part in WP timezone from the calendar selection\n\t\t\t\tconst wpDate = dateI18n( 'Y-m-d', newDate );\n\n\t\t\t\t// Preserve time if it exists in current value, otherwise use current time\n\t\t\t\tlet wpTime: string;\n\t\t\t\tif ( value ) {\n\t\t\t\t\twpTime = dateI18n( 'H:i', getDate( value ) );\n\t\t\t\t} else {\n\t\t\t\t\twpTime = dateI18n( 'H:i', newDate );\n\t\t\t\t}\n\n\t\t\t\t// Combine date and time in WP timezone and convert to ISO\n\t\t\t\tconst finalDateTime = getDate( `${ wpDate }T${ wpTime }` );\n\t\t\t\tdateTimeValue = finalDateTime.toISOString();\n\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t// Clear any existing timeout\n\t\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t\t// Save the currently focused element\n\t\t\tpreviousFocusRef.current =\n\t\t\t\tinputControlRef.current &&\n\t\t\t\tinputControlRef.current.ownerDocument.activeElement;\n\n\t\t\t// Trigger validation display by simulating focus, blur, and changes.\n\t\t\t// Use a timeout to ensure it runs after the value update.\n\t\t\tvalidationTimeoutRef.current = setTimeout( () => {\n\t\t\t\tif ( inputControlRef.current ) {\n\t\t\t\t\tinputControlRef.current.focus();\n\t\t\t\t\tinputControlRef.current.blur();\n\t\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t\t// Restore focus to the previously focused element\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviousFocusRef.current &&\n\t\t\t\t\t\tpreviousFocusRef.current instanceof HTMLElement\n\t\t\t\t\t) {\n\t\t\t\t\t\tpreviousFocusRef.current.focus();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}, 0 );\n\t\t},\n\t\t[ onChangeCallback, value ]\n\t);\n\n\tconst handleManualDateTimeChange = useCallback(\n\t\t( newValue?: string ) => {\n\t\t\tif ( newValue ) {\n\t\t\t\t// Interpret the datetime-local value in WordPress timezone\n\t\t\t\tconst dateTime = getDate( newValue );\n\t\t\t\tonChangeCallback( dateTime.toISOString() );\n\n\t\t\t\t// Update calendar month to match\n\t\t\t\tconst parsedDate = parseDateTime( dateTime.toISOString() );\n\t\t\t\tif ( parsedDate ) {\n\t\t\t\t\tsetCalendarMonth( parsedDate );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t},\n\t\t[ onChangeCallback ]\n\t);\n\n\tconst { format: fieldFormat } = field;\n\tconst weekStartsOn =\n\t\t( fieldFormat as FormatDatetime ).weekStartsOn ??\n\t\tgetSettings().l10n.startOfWeek;\n\tconst {\n\t\ttimezone: { string: timezoneString },\n\t} = getSettings();\n\n\tlet displayLabel = label;\n\tif ( isValid?.required && ! markWhenOptional && ! hideLabelFromVision ) {\n\t\tdisplayLabel = `${ label } (${ __( 'Required' ) })`;\n\t} else if (\n\t\t! isValid?.required &&\n\t\tmarkWhenOptional &&\n\t\t! hideLabelFromVision\n\t) {\n\t\tdisplayLabel = `${ label } (${ __( 'Optional' ) })`;\n\t}\n\n\treturn (\n\t\t<BaseControl\n\t\t\tid={ id }\n\t\t\tlabel={ displayLabel }\n\t\t\thelp={ description }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t>\n\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t{ /* Manual datetime input */ }\n\t\t\t\t<ValidatedInputControl\n\t\t\t\t\tref={ inputControlRef }\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\trequired={ !! isValid?.required }\n\t\t\t\t\tcustomValidity={ getCustomValidity( isValid, validity ) }\n\t\t\t\t\ttype=\"datetime-local\"\n\t\t\t\t\tlabel={ __( 'Date time' ) }\n\t\t\t\t\thideLabelFromVision\n\t\t\t\t\tvalue={ formatDateTime( value ) }\n\t\t\t\t\tonChange={ handleManualDateTimeChange }\n\t\t\t\t\tdisabled={ disabled }\n\t\t\t\t\tmin={\n\t\t\t\t\t\tminConstraint\n\t\t\t\t\t\t\t? formatDateTime( minConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t\tmax={\n\t\t\t\t\t\tmaxConstraint\n\t\t\t\t\t\t\t? formatDateTime( maxConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t\t{ /* Calendar widget */ }\n\t\t\t\t{ ! compact && (\n\t\t\t\t\t<DateCalendar\n\t\t\t\t\t\tstyle={ { width: '100%' } }\n\t\t\t\t\t\tselected={\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t? parseDateTime( value ) || undefined\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonSelect={ onSelectDate }\n\t\t\t\t\t\tmonth={ calendarMonth }\n\t\t\t\t\t\tonMonthChange={ setCalendarMonth }\n\t\t\t\t\t\ttimeZone={ timezoneString || undefined }\n\t\t\t\t\t\tweekStartsOn={ weekStartsOn }\n\t\t\t\t\t\tdisabled={ disabled || disabledMatchers }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</Stack>\n\t\t</BaseControl>\n\t);\n}\n\nexport default function DateTime< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\toperator,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tif ( operator === OPERATOR_IN_THE_PAST || operator === OPERATOR_OVER ) {\n\t\treturn (\n\t\t\t<RelativeDateControl\n\t\t\t\tclassName=\"dataviews-controls__datetime\"\n\t\t\t\tdata={ data }\n\t\t\t\tfield={ field }\n\t\t\t\tonChange={ onChange }\n\t\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\t\toperator={ operator }\n\t\t\t/>\n\t\t);\n\t}\n\n\treturn (\n\t\t<CalendarDateTimeControl\n\t\t\tdata={ data }\n\t\t\tfield={ field }\n\t\t\tonChange={ onChange }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\tmarkWhenOptional={ markWhenOptional }\n\t\t\tvalidity={ validity }\n\t\t\tconfig={ config }\n\t\t/>\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAGO;AACP,qBAAyD;AACzD,kBAAmB;AACnB,kBAA+C;AAC/C,gBAAsB;AAMtB,uBAAoD;AACpD,mCAAgC;AAChC,wCAAoC;AACpC,iCAA8B;AAC9B,6BAA0B;AAC1B,yBAAuB;AAyJpB;AAvJH,IAAM,EAAE,cAAc,sBAAsB,QAAI,2BAAQ,kBAAAA,WAAsB;AAE9E,IAAM,iBAAiB,CAAE,UAA4B;AACpD,MAAK,CAAE,OAAQ;AACd,WAAO;AAAA,EACR;AAEA,aAAO,sBAAU,mBAAe,qBAAS,KAAM,CAAE;AAClD;AAEA,SAAS,wBAAiC;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAkC;AACjC,QAAM,EAAE,QAAQ,IAAI,UAAU,CAAC;AAC/B,QAAM,EAAE,IAAI,OAAO,aAAa,UAAU,UAAU,QAAQ,IAAI;AAChE,QAAM,WAAW,MAAM,WAAY,EAAE,MAAM,MAAM,MAAM,CAAE;AACzD,QAAM,aAAa,SAAU,EAAE,MAAM,KAAK,CAAE;AAC5C,QAAM,QAAQ,OAAO,eAAe,WAAW,aAAa;AAE5D,QAAM,CAAE,eAAe,gBAAiB,QAAI,yBAAkB,MAAM;AACnE,UAAM,iBAAa,uBAAAC,SAAe,KAAM;AACxC,WAAO,cAAc,oBAAI,KAAK;AAAA,EAC/B,CAAE;AAEF,QAAM,sBAAkB,uBAA4B,IAAK;AACzD,QAAM,2BACL,uBAA2C,MAAU;AACtD,QAAM,uBAAmB,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tBaseControl,\n\tprivateApis as componentsPrivateApis,\n} from '@wordpress/components';\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport { dateI18n, getDate, getSettings } from '@wordpress/date';\nimport { Stack } from '@wordpress/ui';\n\n/**\n * Internal dependencies\n */\nimport type { DataFormControlProps, FormatDatetime } from '../../types';\nimport { OPERATOR_IN_THE_PAST, OPERATOR_OVER } from '../../constants';\nimport RelativeDateControl from './utils/relative-date-control';\nimport useDisabledDateMatchers from './utils/use-disabled-date-matchers';\nimport getCustomValidity from './utils/get-custom-validity';\nimport parseDateTime from '../../field-types/utils/parse-date-time';\nimport { unlock } from '../../lock-unlock';\n\nconst { DateCalendar, ValidatedInputControl } = unlock( componentsPrivateApis );\n\nconst formatDateTime = ( value?: string ): string => {\n\tif ( ! value ) {\n\t\treturn '';\n\t}\n\t// Format in WordPress timezone for datetime-local input: YYYY-MM-DDTHH:mm\n\treturn dateI18n( 'Y-m-d\\\\TH:i', getDate( value ) );\n};\n\nfunction CalendarDateTimeControl< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tconst { compact } = config || {};\n\tconst { id, label, description, setValue, getValue, isValid } = field;\n\tconst disabled = field.isDisabled( { item: data, field } );\n\tconst fieldValue = getValue( { item: data } );\n\tconst value = typeof fieldValue === 'string' ? fieldValue : undefined;\n\n\tconst [ calendarMonth, setCalendarMonth ] = useState< Date >( () => {\n\t\tconst parsedDate = parseDateTime( value );\n\t\treturn parsedDate || new Date(); // Default to current month\n\t} );\n\n\tconst inputControlRef = useRef< HTMLInputElement >( null );\n\tconst validationTimeoutRef =\n\t\tuseRef< ReturnType< typeof setTimeout > >( undefined );\n\tconst previousFocusRef = useRef< Element | null >( null );\n\n\tconst { minConstraint, maxConstraint, disabledMatchers } =\n\t\tuseDisabledDateMatchers( isValid, parseDateTime );\n\n\tconst onChangeCallback = useCallback(\n\t\t( newValue: string | undefined ) =>\n\t\t\tonChange( setValue( { item: data, value: newValue } ) ),\n\t\t[ data, onChange, setValue ]\n\t);\n\n\t// Cleanup timeout on unmount\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n\n\tconst onSelectDate = useCallback(\n\t\t( newDate: Date | undefined | null ) => {\n\t\t\tlet dateTimeValue: string | undefined;\n\t\t\tif ( newDate ) {\n\t\t\t\t// Extract the date part in WP timezone from the calendar selection\n\t\t\t\tconst wpDate = dateI18n( 'Y-m-d', newDate );\n\n\t\t\t\t// Preserve time if it exists in current value, otherwise use current time\n\t\t\t\tlet wpTime: string;\n\t\t\t\tif ( value ) {\n\t\t\t\t\twpTime = dateI18n( 'H:i', getDate( value ) );\n\t\t\t\t} else {\n\t\t\t\t\twpTime = dateI18n( 'H:i', newDate );\n\t\t\t\t}\n\n\t\t\t\t// Combine date and time in WP timezone and convert to ISO\n\t\t\t\tconst finalDateTime = getDate( `${ wpDate }T${ wpTime }` );\n\t\t\t\tdateTimeValue = finalDateTime.toISOString();\n\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t// Clear any existing timeout\n\t\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t\t// Save the currently focused element\n\t\t\tpreviousFocusRef.current =\n\t\t\t\tinputControlRef.current &&\n\t\t\t\tinputControlRef.current.ownerDocument.activeElement;\n\n\t\t\t// Trigger validation display by simulating focus, blur, and changes.\n\t\t\t// Use a timeout to ensure it runs after the value update.\n\t\t\tvalidationTimeoutRef.current = setTimeout( () => {\n\t\t\t\tif ( inputControlRef.current ) {\n\t\t\t\t\tinputControlRef.current.focus();\n\t\t\t\t\tinputControlRef.current.blur();\n\t\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t\t// Restore focus to the previously focused element\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviousFocusRef.current &&\n\t\t\t\t\t\tpreviousFocusRef.current instanceof HTMLElement\n\t\t\t\t\t) {\n\t\t\t\t\t\tpreviousFocusRef.current.focus();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}, 0 );\n\t\t},\n\t\t[ onChangeCallback, value ]\n\t);\n\n\tconst handleManualDateTimeChange = useCallback(\n\t\t( newValue?: string ) => {\n\t\t\tif ( newValue ) {\n\t\t\t\t// Interpret the datetime-local value in WordPress timezone\n\t\t\t\tconst dateTime = getDate( newValue );\n\t\t\t\tonChangeCallback( dateTime.toISOString() );\n\n\t\t\t\t// Update calendar month to match\n\t\t\t\tconst parsedDate = parseDateTime( dateTime.toISOString() );\n\t\t\t\tif ( parsedDate ) {\n\t\t\t\t\tsetCalendarMonth( parsedDate );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t},\n\t\t[ onChangeCallback ]\n\t);\n\n\tconst { format: fieldFormat } = field;\n\tconst weekStartsOn =\n\t\t( fieldFormat as FormatDatetime ).weekStartsOn ??\n\t\tgetSettings().l10n.startOfWeek;\n\tconst {\n\t\ttimezone: { string: timezoneString },\n\t} = getSettings();\n\n\tlet displayLabel = label;\n\tif ( isValid?.required && ! markWhenOptional && ! hideLabelFromVision ) {\n\t\tdisplayLabel = `${ label } (${ __( 'Required' ) })`;\n\t} else if (\n\t\t! isValid?.required &&\n\t\tmarkWhenOptional &&\n\t\t! hideLabelFromVision\n\t) {\n\t\tdisplayLabel = `${ label } (${ __( 'Optional' ) })`;\n\t}\n\n\treturn (\n\t\t<BaseControl\n\t\t\tid={ id }\n\t\t\tlabel={ displayLabel }\n\t\t\thelp={ description }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t>\n\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t{ /* Manual datetime input */ }\n\t\t\t\t<ValidatedInputControl\n\t\t\t\t\tref={ inputControlRef }\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\trequired={ !! isValid?.required }\n\t\t\t\t\tcustomValidity={ getCustomValidity( isValid, validity ) }\n\t\t\t\t\ttype=\"datetime-local\"\n\t\t\t\t\tlabel={ __( 'Date time' ) }\n\t\t\t\t\thideLabelFromVision\n\t\t\t\t\tvalue={ formatDateTime( value ) }\n\t\t\t\t\tonChange={ handleManualDateTimeChange }\n\t\t\t\t\tdisabled={ disabled }\n\t\t\t\t\tmin={\n\t\t\t\t\t\tminConstraint\n\t\t\t\t\t\t\t? formatDateTime( minConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t\tmax={\n\t\t\t\t\t\tmaxConstraint\n\t\t\t\t\t\t\t? formatDateTime( maxConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t\t{ /* Calendar widget */ }\n\t\t\t\t{ ! compact && (\n\t\t\t\t\t<DateCalendar\n\t\t\t\t\t\tstyle={ { width: '100%' } }\n\t\t\t\t\t\tselected={\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t? parseDateTime( value ) || undefined\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonSelect={ onSelectDate }\n\t\t\t\t\t\tmonth={ calendarMonth }\n\t\t\t\t\t\tonMonthChange={ setCalendarMonth }\n\t\t\t\t\t\ttimeZone={ timezoneString || undefined }\n\t\t\t\t\t\tweekStartsOn={ weekStartsOn }\n\t\t\t\t\t\tdisabled={ disabled || disabledMatchers }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</Stack>\n\t\t</BaseControl>\n\t);\n}\n\nexport default function DateTime< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\toperator,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tif ( operator === OPERATOR_IN_THE_PAST || operator === OPERATOR_OVER ) {\n\t\treturn (\n\t\t\t<RelativeDateControl\n\t\t\t\tclassName=\"dataviews-controls__datetime\"\n\t\t\t\tdata={ data }\n\t\t\t\tfield={ field }\n\t\t\t\tonChange={ onChange }\n\t\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\t\toperator={ operator }\n\t\t\t/>\n\t\t);\n\t}\n\n\treturn (\n\t\t<CalendarDateTimeControl\n\t\t\tdata={ data }\n\t\t\tfield={ field }\n\t\t\tonChange={ onChange }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\tmarkWhenOptional={ markWhenOptional }\n\t\t\tvalidity={ validity }\n\t\t\tconfig={ config }\n\t\t/>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAGO;AACP,qBAAyD;AACzD,kBAAmB;AACnB,kBAA+C;AAC/C,gBAAsB;AAMtB,uBAAoD;AACpD,mCAAgC;AAChC,wCAAoC;AACpC,iCAA8B;AAC9B,6BAA0B;AAC1B,yBAAuB;AAyJpB;AAvJH,IAAM,EAAE,cAAc,sBAAsB,QAAI,2BAAQ,kBAAAA,WAAsB;AAE9E,IAAM,iBAAiB,CAAE,UAA4B;AACpD,MAAK,CAAE,OAAQ;AACd,WAAO;AAAA,EACR;A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AA,QACA;AAAA,QACA;AAAA;AAAA,IACD;AAAA,EAEF;AAEA,SACC;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,EACD;AAEF;",
|
|
6
6
|
"names": ["componentsPrivateApis", "parseDateTime", "useDisabledDateMatchers", "getCustomValidity", "RelativeDateControl"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/components/dataviews-context/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { createContext, createRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tView,\n\tAction,\n\tNormalizedField,\n\tNormalizedSupportedLayouts,\n\tNormalizedFilter,\n} from '../../types';\nimport type { SetSelection } from '../../types/private';\nimport { LAYOUT_TABLE } from '../../constants';\n\ntype DataViewsContextType< Item > = {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n\tfields: NormalizedField< Item >[];\n\tactions?: Action< Item >[];\n\tdata: Item[];\n\tisLoading?: boolean;\n\tpaginationInfo: {\n\t\ttotalItems: number;\n\t\ttotalPages: number;\n\t};\n\tselection: string[];\n\tonChangeSelection: SetSelection;\n\topenedFilter: string | null;\n\tsetOpenedFilter: ( openedFilter: string | null ) => void;\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tcontainerWidth: number;\n\tcontainerRef: React.
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAAyC;AAazC,uBAA6B;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { createContext, createRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tView,\n\tAction,\n\tNormalizedField,\n\tNormalizedSupportedLayouts,\n\tNormalizedFilter,\n} from '../../types';\nimport type { SetSelection } from '../../types/private';\nimport { LAYOUT_TABLE } from '../../constants';\n\ntype DataViewsContextType< Item > = {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n\tfields: NormalizedField< Item >[];\n\tactions?: Action< Item >[];\n\tdata: Item[];\n\tisLoading?: boolean;\n\tpaginationInfo: {\n\t\ttotalItems: number;\n\t\ttotalPages: number;\n\t};\n\tselection: string[];\n\tonChangeSelection: SetSelection;\n\topenedFilter: string | null;\n\tsetOpenedFilter: ( openedFilter: string | null ) => void;\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tcontainerWidth: number;\n\tcontainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tresizeObserverRef:\n\t\t| ( ( element?: HTMLDivElement | null ) => void )\n\t\t| React.RefObject< HTMLDivElement >;\n\tdefaultLayouts: NormalizedSupportedLayouts;\n\tfilters: NormalizedFilter[];\n\tisShowingFilter: boolean;\n\tsetIsShowingFilter: ( value: boolean ) => void;\n\tconfig: { perPageSizes: number[] };\n\tempty?: ReactNode;\n\thasInitiallyLoaded?: boolean;\n\titemListLabel?: string;\n\tonReset?: ( () => void ) | false;\n\tintersectionObserver?: IntersectionObserver | null;\n};\n\nconst DataViewsContext = createContext< DataViewsContextType< any > >( {\n\tview: { type: LAYOUT_TABLE },\n\tonChangeView: () => {},\n\tfields: [],\n\tdata: [],\n\tpaginationInfo: {\n\t\ttotalItems: 0,\n\t\ttotalPages: 0,\n\t},\n\tselection: [],\n\tonChangeSelection: () => {},\n\tsetOpenedFilter: () => {},\n\topenedFilter: null,\n\tgetItemId: ( item ) => item.id,\n\tisItemClickable: () => true,\n\trenderItemLink: undefined,\n\tcontainerWidth: 0,\n\tcontainerRef: createRef(),\n\tresizeObserverRef: () => {},\n\tdefaultLayouts: { list: {}, grid: {}, table: {} },\n\tfilters: [],\n\tisShowingFilter: false,\n\tsetIsShowingFilter: () => {},\n\thasInitiallyLoaded: false,\n\tconfig: {\n\t\tperPageSizes: [],\n\t},\n\tintersectionObserver: null,\n} );\n\nDataViewsContext.displayName = 'DataViewsContext';\n\nexport default DataViewsContext;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAAyC;AAazC,uBAA6B;AA2C7B,IAAM,uBAAmB,8BAA8C;AAAA,EACtE,MAAM,EAAE,MAAM,8BAAa;AAAA,EAC3B,cAAc,MAAM;AAAA,EAAC;AAAA,EACrB,QAAQ,CAAC;AAAA,EACT,MAAM,CAAC;AAAA,EACP,gBAAgB;AAAA,IACf,YAAY;AAAA,IACZ,YAAY;AAAA,EACb;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,iBAAiB,MAAM;AAAA,EAAC;AAAA,EACxB,cAAc;AAAA,EACd,WAAW,CAAE,SAAU,KAAK;AAAA,EAC5B,iBAAiB,MAAM;AAAA,EACvB,gBAAgB;AAAA,EAChB,gBAAgB;AAAA,EAChB,kBAAc,0BAAU;AAAA,EACxB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,gBAAgB,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,EAAE;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,iBAAiB;AAAA,EACjB,oBAAoB,MAAM;AAAA,EAAC;AAAA,EAC3B,oBAAoB;AAAA,EACpB,QAAQ;AAAA,IACP,cAAc,CAAC;AAAA,EAChB;AAAA,EACA,sBAAsB;AACvB,CAAE;AAEF,iBAAiB,cAAc;AAE/B,IAAO,4BAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/components/dataviews-layouts/table/use-scroll-state.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type {
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { MutableRefObject } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useState } from '@wordpress/element';\nimport { isRTL } from '@wordpress/i18n';\n\nconst isScrolledToEnd = ( element: Element ) => {\n\tif ( isRTL() ) {\n\t\tconst scrollLeft = Math.abs( element.scrollLeft );\n\t\treturn scrollLeft <= 1;\n\t}\n\n\treturn element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;\n};\n\n/**\n * A hook to track the scroll state of a container element.\n *\n * Returns whether the container has been scrolled vertically (for sticky header styling)\n * and whether it has reached the horizontal scroll end (for sticky actions column styling).\n *\n * The current way receives \"refs\" as arguments, but it lacks a mechanism to detect when a ref has changed.\n * As a result, when the \"ref\" is updated and attached to a new div, the computation should trigger again.\n * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.\n *\n * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.\n *\n * @param {Object} params The parameters for the hook.\n * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.\n * @param {boolean} [params.enabledHorizontal=false] Whether to track horizontal scroll end.\n * @return {{ isHorizontalScrollEnd: boolean, isVerticallyScrolled: boolean }} The scroll state.\n */\nexport function useScrollState( {\n\tscrollContainerRef,\n\tenabledHorizontal = false,\n}: {\n\tscrollContainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tenabledHorizontal?: boolean;\n} ): { isHorizontalScrollEnd: boolean; isVerticallyScrolled: boolean } {\n\tconst [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =\n\t\tuseState( false );\n\tconst [ isVerticallyScrolled, setIsVerticallyScrolled ] = useState( false );\n\n\tconst handleScroll = useCallback( () => {\n\t\tconst scrollContainer = scrollContainerRef.current;\n\t\tif ( ! scrollContainer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( enabledHorizontal ) {\n\t\t\tsetIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );\n\t\t}\n\n\t\tsetIsVerticallyScrolled( scrollContainer.scrollTop > 0 );\n\t}, [ scrollContainerRef, enabledHorizontal ] );\n\tuseEffect( () => {\n\t\tif ( typeof window === 'undefined' || ! scrollContainerRef.current ) {\n\t\t\treturn () => {};\n\t\t}\n\n\t\tconst scrollContainer = scrollContainerRef.current;\n\n\t\thandleScroll();\n\t\tscrollContainer.addEventListener( 'scroll', handleScroll );\n\t\twindow.addEventListener( 'resize', handleScroll );\n\n\t\treturn () => {\n\t\t\tscrollContainer.removeEventListener( 'scroll', handleScroll );\n\t\t\twindow.removeEventListener( 'resize', handleScroll );\n\t\t};\n\t}, [ scrollContainerRef, enabledHorizontal, handleScroll ] );\n\n\treturn { isHorizontalScrollEnd, isVerticallyScrolled };\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAAiD;AACjD,kBAAsB;AAEtB,IAAM,kBAAkB,CAAE,YAAsB;AAC/C,UAAK,mBAAM,GAAI;AACd,UAAM,aAAa,KAAK,IAAK,QAAQ,UAAW;AAChD,WAAO,cAAc;AAAA,EACtB;AAEA,SAAO,QAAQ,aAAa,QAAQ,eAAe,QAAQ,cAAc;AAC1E;AAmBO,SAAS,eAAgB;AAAA,EAC/B;AAAA,EACA,oBAAoB;AACrB,GAGuE;AACtE,QAAM,CAAE,uBAAuB,wBAAyB,QACvD,yBAAU,KAAM;AACjB,QAAM,CAAE,sBAAsB,uBAAwB,QAAI,yBAAU,KAAM;AAE1E,QAAM,mBAAe,4BAAa,MAAM;AACvC,UAAM,kBAAkB,mBAAmB;AAC3C,QAAK,CAAE,iBAAkB;AACxB;AAAA,IACD;AAEA,QAAK,mBAAoB;AACxB,+BAA0B,gBAAiB,eAAgB,CAAE;AAAA,IAC9D;AAEA,4BAAyB,gBAAgB,YAAY,CAAE;AAAA,EACxD,GAAG,CAAE,oBAAoB,iBAAkB,CAAE;AAC7C,gCAAW,MAAM;AAChB,QAAK,OAAO,WAAW,eAAe,CAAE,mBAAmB,SAAU;AACpE,aAAO,MAAM;AAAA,MAAC;AAAA,IACf;AAEA,UAAM,kBAAkB,mBAAmB;AAE3C,iBAAa;AACb,oBAAgB,iBAAkB,UAAU,YAAa;AACzD,WAAO,iBAAkB,UAAU,YAAa;AAEhD,WAAO,MAAM;AACZ,sBAAgB,oBAAqB,UAAU,YAAa;AAC5D,aAAO,oBAAqB,UAAU,YAAa;AAAA,IACpD;AAAA,EACD,GAAG,CAAE,oBAAoB,mBAAmB,YAAa,CAAE;AAE3D,SAAO,EAAE,uBAAuB,qBAAqB;AACtD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/components/dataviews-layouts/utils/item-click-wrapper.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactNode, ReactElement, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { cloneElement } from '@wordpress/element';\n\nfunction getClickableItemProps< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\tclassName,\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\tclassName?: string;\n} ) {\n\tif ( ! isItemClickable( item ) || ! onClickItem ) {\n\t\treturn { className };\n\t}\n\n\treturn {\n\t\tclassName: className\n\t\t\t? `${ className } ${ className }--clickable`\n\t\t\t: undefined,\n\t\trole: 'button',\n\t\ttabIndex: 0,\n\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\tevent.stopPropagation();\n\t\t\tonClickItem( item );\n\t\t},\n\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\tif (\n\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\tevent.key === '' ||\n\t\t\t\tevent.key === ' '\n\t\t\t) {\n\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonClickItem( item );\n\t\t\t}\n\t\t},\n\t};\n}\n\nexport function ItemClickWrapper< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\trenderItemLink,\n\tclassName,\n\tchildren,\n\t...extraProps\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tclassName?: string;\n\ttitle?: string;\n\tchildren: ReactNode;\n} ) {\n\t// Always render a wrapper element so layout and styling relying on the wrapper\n\t// still works even if the item is not clickable.\n\tif ( ! isItemClickable( item ) ) {\n\t\treturn (\n\t\t\t<div className={ className } { ...extraProps }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t);\n\t}\n\n\t// If we have a renderItemLink, use it\n\tif ( renderItemLink ) {\n\t\tconst renderedElement = renderItemLink( {\n\t\t\titem,\n\t\t\tclassName: `${ className } ${ className }--clickable`,\n\t\t\t...extraProps,\n\t\t\tchildren,\n\t\t} )
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAA6B;AAmE1B;AAjEH,SAAS,sBAA+B;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAKI;AACH,MAAK,CAAE,gBAAiB,IAAK,KAAK,CAAE,aAAc;AACjD,WAAO,EAAE,UAAU;AAAA,EACpB;AAEA,SAAO;AAAA,IACN,WAAW,YACR,GAAI,SAAU,IAAK,SAAU,gBAC7B;AAAA,IACH,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS,CAAE,UAA6B;AAEvC,YAAM,gBAAgB;AACtB,kBAAa,IAAK;AAAA,IACnB;AAAA,IACA,WAAW,CAAE,UAAgC;AAC5C,UACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,cAAM,gBAAgB;AACtB,oBAAa,IAAK;AAAA,MACnB;AAAA,IACD;AAAA,EACD;AACD;AAEO,SAAS,iBAA0B;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,GAYI;AAGH,MAAK,CAAE,gBAAiB,IAAK,GAAI;AAChC,WACC,4CAAC,SAAI,WAA0B,GAAG,YAC/B,UACH;AAAA,EAEF;AAGA,MAAK,gBAAiB;AACrB,UAAM,kBAAkB,eAAgB;AAAA,MACvC;AAAA,MACA,WAAW,GAAI,SAAU,IAAK,SAAU;AAAA,MACxC,GAAG;AAAA,MACH;AAAA,IACD,CAAE;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactNode, ReactElement, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { cloneElement } from '@wordpress/element';\n\nfunction getClickableItemProps< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\tclassName,\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\tclassName?: string;\n} ) {\n\tif ( ! isItemClickable( item ) || ! onClickItem ) {\n\t\treturn { className };\n\t}\n\n\treturn {\n\t\tclassName: className\n\t\t\t? `${ className } ${ className }--clickable`\n\t\t\t: undefined,\n\t\trole: 'button',\n\t\ttabIndex: 0,\n\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\tevent.stopPropagation();\n\t\t\tonClickItem( item );\n\t\t},\n\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\tif (\n\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\tevent.key === '' ||\n\t\t\t\tevent.key === ' '\n\t\t\t) {\n\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonClickItem( item );\n\t\t\t}\n\t\t},\n\t};\n}\n\nexport function ItemClickWrapper< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\trenderItemLink,\n\tclassName,\n\tchildren,\n\t...extraProps\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tclassName?: string;\n\ttitle?: string;\n\tchildren: ReactNode;\n} ) {\n\t// Always render a wrapper element so layout and styling relying on the wrapper\n\t// still works even if the item is not clickable.\n\tif ( ! isItemClickable( item ) ) {\n\t\treturn (\n\t\t\t<div className={ className } { ...extraProps }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t);\n\t}\n\n\t// If we have a renderItemLink, use it\n\tif ( renderItemLink ) {\n\t\tconst renderedElement = renderItemLink( {\n\t\t\titem,\n\t\t\tclassName: `${ className } ${ className }--clickable`,\n\t\t\t...extraProps,\n\t\t\tchildren,\n\t\t} );\n\n\t\t// Clone the element and enhance onClick to stop propagation\n\t\treturn cloneElement( renderedElement, {\n\t\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t\t// Always stop propagation to prevent selection\n\t\t\t\tevent.stopPropagation();\n\n\t\t\t\t// If consumer provided an onClick, call it\n\t\t\t\tif ( renderedElement.props.onClick ) {\n\t\t\t\t\trenderedElement.props.onClick( event );\n\t\t\t\t}\n\t\t\t},\n\t\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\t\tif (\n\t\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\t\tevent.key === '' ||\n\t\t\t\t\tevent.key === ' '\n\t\t\t\t) {\n\t\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\t\tevent.stopPropagation();\n\t\t\t\t\t// If consumer provided an onKeyDown, call it\n\t\t\t\t\tif ( renderedElement.props.onKeyDown ) {\n\t\t\t\t\t\trenderedElement.props.onKeyDown( event );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t} );\n\t}\n\n\t// Otherwise use the classic click handler approach\n\tconst clickProps = getClickableItemProps( {\n\t\titem,\n\t\tisItemClickable,\n\t\tonClickItem,\n\t\tclassName,\n\t} );\n\n\treturn (\n\t\t<div { ...clickProps } { ...extraProps }>\n\t\t\t{ children }\n\t\t</div>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAA6B;AAmE1B;AAjEH,SAAS,sBAA+B;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAKI;AACH,MAAK,CAAE,gBAAiB,IAAK,KAAK,CAAE,aAAc;AACjD,WAAO,EAAE,UAAU;AAAA,EACpB;AAEA,SAAO;AAAA,IACN,WAAW,YACR,GAAI,SAAU,IAAK,SAAU,gBAC7B;AAAA,IACH,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS,CAAE,UAA6B;AAEvC,YAAM,gBAAgB;AACtB,kBAAa,IAAK;AAAA,IACnB;AAAA,IACA,WAAW,CAAE,UAAgC;AAC5C,UACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,cAAM,gBAAgB;AACtB,oBAAa,IAAK;AAAA,MACnB;AAAA,IACD;AAAA,EACD;AACD;AAEO,SAAS,iBAA0B;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,GAYI;AAGH,MAAK,CAAE,gBAAiB,IAAK,GAAI;AAChC,WACC,4CAAC,SAAI,WAA0B,GAAG,YAC/B,UACH;AAAA,EAEF;AAGA,MAAK,gBAAiB;AACrB,UAAM,kBAAkB,eAAgB;AAAA,MACvC;AAAA,MACA,WAAW,GAAI,SAAU,IAAK,SAAU;AAAA,MACxC,GAAG;AAAA,MACH;AAAA,IACD,CAAE;AAGF,eAAO,6BAAc,iBAAiB;AAAA,MACrC,SAAS,CAAE,UAA6B;AAEvC,cAAM,gBAAgB;AAGtB,YAAK,gBAAgB,MAAM,SAAU;AACpC,0BAAgB,MAAM,QAAS,KAAM;AAAA,QACtC;AAAA,MACD;AAAA,MACA,WAAW,CAAE,UAAgC;AAC5C,YACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,gBAAM,gBAAgB;AAEtB,cAAK,gBAAgB,MAAM,WAAY;AACtC,4BAAgB,MAAM,UAAW,KAAM;AAAA,UACxC;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH;AAGA,QAAM,aAAa,sBAAuB;AAAA,IACzC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAEF,SACC,4CAAC,SAAM,GAAG,YAAe,GAAG,YACzB,UACH;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/use-form-validity.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport deepMerge from 'deepmerge';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport normalizeFields from '../field-types';\nimport normalizeForm from '../components/dataform-layouts/normalize-form';\nimport type {\n\tField,\n\tFieldValidity,\n\tForm,\n\tFormValidity,\n\tNormalizedField,\n\tNormalizedFormField,\n} from '../types';\n\nfunction isFormValid( formValidity: FormValidity | undefined ): boolean {\n\tif ( ! formValidity ) {\n\t\treturn true;\n\t}\n\n\treturn Object.values( formValidity ).every( ( fieldValidation ) => {\n\t\treturn Object.entries( fieldValidation ).every(\n\t\t\t( [ key, validation ] ) => {\n\t\t\t\tif (\n\t\t\t\t\tkey === 'children' &&\n\t\t\t\t\tvalidation &&\n\t\t\t\t\ttypeof validation === 'object'\n\t\t\t\t) {\n\t\t\t\t\t// Recursively check children validations\n\t\t\t\t\treturn isFormValid( validation as FormValidity );\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\tvalidation.type !== 'invalid' &&\n\t\t\t\t\tvalidation.type !== 'validating'\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t} );\n}\n\ntype FormFieldToValidate< Item > = {\n\tid: string;\n\tchildren: FormFieldToValidate< Item >[];\n\tfield?: NormalizedField< Item >;\n};\n\nfunction getFormFieldsToValidate< Item >(\n\tform: Form,\n\tfields: Field< Item >[]\n): FormFieldToValidate< Item >[] {\n\tconst normalizedForm = normalizeForm( form );\n\tif ( normalizedForm.fields.length === 0 ) {\n\t\treturn [];\n\t}\n\n\t// Create a map of field IDs to Field definitions for fast lookup\n\tconst fieldsMap = new Map< string, Field< Item > >();\n\tfields.forEach( ( field ) => {\n\t\tfieldsMap.set( field.id, field );\n\t} );\n\n\t// Recursive function to process form fields and their children\n\tfunction processFormField(\n\t\tformField: NormalizedFormField\n\t): FormFieldToValidate< Item > | null {\n\t\t// Handle combined fields (fields with children)\n\t\tif ( 'children' in formField && Array.isArray( formField.children ) ) {\n\t\t\tconst processedChildren = formField.children\n\t\t\t\t.map( processFormField )\n\t\t\t\t.filter( ( child ) => child !== null );\n\n\t\t\tif ( processedChildren.length === 0 ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\t\tif ( fieldDef ) {\n\t\t\t\tconst [ normalizedField ] = normalizeFields< Item >( [\n\t\t\t\t\tfieldDef,\n\t\t\t\t] );\n\n\t\t\t\treturn {\n\t\t\t\t\tid: formField.id,\n\t\t\t\t\tchildren: processedChildren,\n\t\t\t\t\tfield: normalizedField,\n\t\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tid: formField.id,\n\t\t\t\tchildren: processedChildren,\n\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t}\n\n\t\t// Handle leaf fields (fields without children)\n\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\tif ( ! fieldDef ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst [ normalizedField ] = normalizeFields< Item >( [ fieldDef ] );\n\t\treturn {\n\t\t\tid: formField.id,\n\t\t\tchildren: [],\n\t\t\tfield: normalizedField,\n\t\t} as FormFieldToValidate< Item >;\n\t}\n\n\tconst toValidate = normalizedForm.fields\n\t\t.map( processFormField )\n\t\t.filter( ( field ) => field !== null );\n\n\treturn toValidate;\n}\n\nfunction setValidityAtPath(\n\tformValidity: FormValidity | undefined,\n\tfieldValidity: FieldValidity,\n\tpath: string[]\n): FormValidity {\n\t// Handle empty validity or empty path\n\tif ( ! formValidity ) {\n\t\tformValidity = {};\n\t}\n\n\tif ( path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\n\t// Clone the root to avoid mutations\n\tconst result = { ...formValidity };\n\n\t// Navigate through the result tree,\n\t// setting up empty paths if they don't exist.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\tcurrent[ segment ] = {};\n\t\t}\n\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\n\t// At the final destination, merge the new validity with the existing.\n\tconst finalKey = path[ path.length - 1 ];\n\tcurrent[ finalKey ] = {\n\t\t...( current[ finalKey ] || {} ),\n\t\t...fieldValidity,\n\t};\n\n\treturn result;\n}\n\nfunction removeValidationProperty(\n\tformValidity: FormValidity | undefined,\n\tpath: string[],\n\tproperty: keyof FieldValidity\n): FormValidity | undefined {\n\tif ( ! formValidity || path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\tconst result = { ...formValidity };\n\t// Navigate to parent of target.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\treturn formValidity; // Path doesn't exist\n\t\t}\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\tconst finalKey = path[ path.length - 1 ];\n\tif ( ! current[ finalKey ] ) {\n\t\treturn formValidity;\n\t}\n\tconst fieldValidity = { ...current[ finalKey ] };\n\tdelete fieldValidity[ property ];\n\t// If field has no more validations, remove it entirely.\n\tif ( Object.keys( fieldValidity ).length === 0 ) {\n\t\tdelete current[ finalKey ];\n\t} else {\n\t\t// Keep the field if it has other validations (including children).\n\t\tcurrent[ finalKey ] = fieldValidity;\n\t}\n\t// If root is empty, return undefined\n\tif ( Object.keys( result ).length === 0 ) {\n\t\treturn undefined;\n\t}\n\treturn result;\n}\n\nfunction handleElementsValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { elementsCounterRef, setFormValidity, path, item } = promiseHandler;\n\tconst currentToken =\n\t\t( elementsCounterRef.current[ formField.id ] || 0 ) + 1;\n\telementsCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! Array.isArray( result ) ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __( 'Could not validate elements.' ),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tformField.field?.isValid.elements &&\n\t\t\t\t! formField.field.isValid.elements.validate( item, {\n\t\t\t\t\t...formField.field,\n\t\t\t\t\telements: result,\n\t\t\t\t} )\n\t\t\t) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t\t\t\t'Value must be one of the elements.'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Validation passed so we need to remove `elements` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'elements'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running elements validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\nfunction handleCustomValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { customCounterRef, setFormValidity, path } = promiseHandler;\n\tconst currentToken = ( customCounterRef.current[ formField.id ] || 0 ) + 1;\n\tcustomCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( result === null ) {\n\t\t\t\t// Validation passed so we need to remove `custom` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'custom'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( typeof result === 'string' ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: result,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: __( 'Validation could not be processed.' ),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running custom validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\ntype PromiseHandler< Item > = {\n\tcustomCounterRef: React.RefObject< Record< string, number > >;\n\telementsCounterRef: React.RefObject< Record< string, number > >;\n\tsetFormValidity: React.Dispatch< React.SetStateAction< FormValidity > >;\n\tpath: string[];\n\titem: Item;\n};\n\nfunction validateFormField< Item >(\n\titem: Item,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n): FieldValidity | undefined {\n\t// Validate the field: isValid.required\n\tif (\n\t\tformField.field?.isValid.required &&\n\t\t! formField.field.isValid.required.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\trequired: { type: 'invalid' },\n\t\t};\n\t}\n\n\t// Validate the field: isValid.pattern\n\tif (\n\t\tformField.field?.isValid.pattern &&\n\t\t! formField.field.isValid.pattern.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tpattern: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value does not match the required pattern.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.min\n\tif (\n\t\tformField.field?.isValid.min &&\n\t\t! formField.field.isValid.min.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmin: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is below the minimum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.max\n\tif (\n\t\tformField.field?.isValid.max &&\n\t\t! formField.field.isValid.max.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmax: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is above the maximum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.minLength\n\tif (\n\t\tformField.field?.isValid.minLength &&\n\t\t! formField.field.isValid.minLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tminLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too short.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.maxLength\n\tif (\n\t\tformField.field?.isValid.maxLength &&\n\t\t! formField.field.isValid.maxLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmaxLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too long.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.elements (static)\n\tif (\n\t\tformField.field?.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\t! formField.field.getElements &&\n\t\tArray.isArray( formField.field.elements ) &&\n\t\t! formField.field.isValid.elements.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\telements: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value must be one of the elements.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (sync)\n\tlet customError;\n\tif ( !! formField.field && formField.field.isValid.custom ) {\n\t\ttry {\n\t\t\tconst value = formField.field.getValue( { item } );\n\t\t\tcustomError = formField.field.isValid.custom(\n\t\t\t\tdeepMerge(\n\t\t\t\t\titem,\n\t\t\t\t\tformField.field.setValue( {\n\t\t\t\t\t\titem,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} ) as Partial< Item >\n\t\t\t\t),\n\t\t\t\tformField.field\n\t\t\t);\n\t\t} catch ( error ) {\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__( 'Unknown error when running custom validation.' );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcustom: {\n\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t}\n\n\tif ( typeof customError === 'string' ) {\n\t\treturn {\n\t\t\tcustom: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: customError,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Aggregate async validations (`elements` and `custom`).\n\tconst fieldValidity: FieldValidity = {};\n\t// Validate the field: isValid.elements (async)\n\tif (\n\t\t!! formField.field &&\n\t\tformField.field.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\ttypeof formField.field.getElements === 'function'\n\t) {\n\t\thandleElementsValidationAsync(\n\t\t\tformField.field.getElements(),\n\t\t\tformField,\n\t\t\tpromiseHandler\n\t\t);\n\t\tfieldValidity.elements = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (async)\n\tif ( customError instanceof Promise ) {\n\t\thandleCustomValidationAsync( customError, formField, promiseHandler );\n\n\t\tfieldValidity.custom = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Return aggregated validations if any exist\n\tif ( Object.keys( fieldValidity ).length > 0 ) {\n\t\treturn fieldValidity;\n\t}\n\n\t// Validate its children.\n\tif ( formField.children.length > 0 ) {\n\t\tconst result: Record< string, FieldValidity | undefined > = {};\n\t\tformField.children.forEach( ( child ) => {\n\t\t\tresult[ child.id ] = validateFormField( item, child, {\n\t\t\t\t...promiseHandler,\n\t\t\t\tpath: [ ...promiseHandler.path, formField.id, 'children' ],\n\t\t\t} );\n\t\t} );\n\n\t\tconst filteredResult: Record< string, FieldValidity > = {};\n\t\tObject.entries( result ).forEach( ( [ key, value ] ) => {\n\t\t\tif ( value !== undefined ) {\n\t\t\t\tfilteredResult[ key ] = value;\n\t\t\t}\n\t\t} );\n\n\t\tif ( Object.keys( filteredResult ).length === 0 ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tchildren: filteredResult,\n\t\t};\n\t}\n\n\t// No errors for this field or its children.\n\treturn undefined;\n}\n\nfunction getFormFieldValue< Item >(\n\tformField: FormFieldToValidate< Item >,\n\titem: Item\n): any {\n\tconst fieldValue = formField?.field?.getValue( { item } );\n\tif ( formField.children.length === 0 ) {\n\t\treturn fieldValue;\n\t}\n\n\tconst childrenValues = formField.children.map( ( child ) =>\n\t\tgetFormFieldValue( child, item )\n\t);\n\tif ( ! childrenValues ) {\n\t\treturn fieldValue;\n\t}\n\n\treturn {\n\t\tvalue: fieldValue,\n\t\tchildren: childrenValues,\n\t};\n}\n\n/**\n * Hook that validates a form item and returns an object with error messages for each field.\n *\n * @param item The item to validate.\n * @param fields Fields config.\n * @param form Form config.\n *\n * @return Record of field IDs to error messages (undefined means no error).\n */\nexport function useFormValidity< Item >(\n\titem: Item,\n\tfields: Field< Item >[],\n\tform: Form\n): { validity: FormValidity; isValid: boolean } {\n\tconst [ formValidity, setFormValidity ] = useState< FormValidity >();\n\tconst customCounterRef = useRef< Record< string, number > >( {} );\n\tconst elementsCounterRef = useRef< Record< string, number > >( {} );\n\tconst previousValuesRef = useRef< Record< string, any > >( {} );\n\n\tconst validate = useCallback( () => {\n\t\tconst promiseHandler = {\n\t\t\tcustomCounterRef,\n\t\t\telementsCounterRef,\n\t\t\tsetFormValidity,\n\t\t\tpath: [],\n\t\t\titem,\n\t\t};\n\n\t\tconst formFieldsToValidate = getFormFieldsToValidate( form, fields );\n\t\tif ( formFieldsToValidate.length === 0 ) {\n\t\t\tsetFormValidity( undefined );\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFormValidity: FormValidity = {};\n\t\tconst untouchedFields: string[] = [];\n\t\tformFieldsToValidate.forEach( ( formField ) => {\n\t\t\t// Skip fields that did not change.\n\t\t\tconst value = getFormFieldValue< Item >( formField, item );\n\t\t\tif (\n\t\t\t\tpreviousValuesRef.current.hasOwnProperty( formField.id ) &&\n\t\t\t\tfastDeepEqual(\n\t\t\t\t\tpreviousValuesRef.current[ formField.id ],\n\t\t\t\t\tvalue\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tuntouchedFields.push( formField.id );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tpreviousValuesRef.current[ formField.id ] = value;\n\n\t\t\t// Calculate validity for those fields that changed.\n\t\t\tconst fieldValidity = validateFormField(\n\t\t\t\titem,\n\t\t\t\tformField,\n\t\t\t\tpromiseHandler\n\t\t\t);\n\t\t\tif ( fieldValidity !== undefined ) {\n\t\t\t\tnewFormValidity[ formField.id ] = fieldValidity;\n\t\t\t}\n\t\t} );\n\n\t\tsetFormValidity( ( existingFormValidity ) => {\n\t\t\tlet validity: FormValidity = {\n\t\t\t\t...existingFormValidity,\n\t\t\t\t...newFormValidity,\n\t\t\t};\n\n\t\t\tconst fieldsToKeep = [\n\t\t\t\t...untouchedFields,\n\t\t\t\t...Object.keys( newFormValidity ),\n\t\t\t];\n\t\t\tObject.keys( validity ).forEach( ( key ) => {\n\t\t\t\tif ( validity && ! fieldsToKeep.includes( key ) ) {\n\t\t\t\t\tdelete validity[ key ];\n\t\t\t\t}\n\t\t\t} );\n\t\t\tif ( Object.keys( validity ).length === 0 ) {\n\t\t\t\tvalidity = undefined;\n\t\t\t}\n\n\t\t\tconst areEqual = fastDeepEqual( existingFormValidity, validity );\n\t\t\tif ( areEqual ) {\n\t\t\t\treturn existingFormValidity;\n\t\t\t}\n\n\t\t\treturn validity;\n\t\t} );\n\t}, [ item, fields, form ] );\n\n\tuseEffect( () => {\n\t\tvalidate();\n\t}, [ validate ] );\n\n\treturn {\n\t\tvalidity: formValidity,\n\t\tisValid: isFormValid( formValidity ),\n\t};\n}\n\nexport default useFormValidity;\n"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport deepMerge from 'deepmerge';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport normalizeFields from '../field-types';\nimport normalizeForm from '../components/dataform-layouts/normalize-form';\nimport type {\n\tField,\n\tFieldValidity,\n\tForm,\n\tFormValidity,\n\tNormalizedField,\n\tNormalizedFormField,\n} from '../types';\n\nfunction isFormValid( formValidity: FormValidity | undefined ): boolean {\n\tif ( ! formValidity ) {\n\t\treturn true;\n\t}\n\n\treturn Object.values( formValidity ).every( ( fieldValidation ) => {\n\t\treturn Object.entries( fieldValidation ).every(\n\t\t\t( [ key, validation ] ) => {\n\t\t\t\tif (\n\t\t\t\t\tkey === 'children' &&\n\t\t\t\t\tvalidation &&\n\t\t\t\t\ttypeof validation === 'object'\n\t\t\t\t) {\n\t\t\t\t\t// Recursively check children validations\n\t\t\t\t\treturn isFormValid( validation as FormValidity );\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\tvalidation.type !== 'invalid' &&\n\t\t\t\t\tvalidation.type !== 'validating'\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t} );\n}\n\ntype FormFieldToValidate< Item > = {\n\tid: string;\n\tchildren: FormFieldToValidate< Item >[];\n\tfield?: NormalizedField< Item >;\n};\n\nfunction getFormFieldsToValidate< Item >(\n\tform: Form,\n\tfields: Field< Item >[]\n): FormFieldToValidate< Item >[] {\n\tconst normalizedForm = normalizeForm( form );\n\tif ( normalizedForm.fields.length === 0 ) {\n\t\treturn [];\n\t}\n\n\t// Create a map of field IDs to Field definitions for fast lookup\n\tconst fieldsMap = new Map< string, Field< Item > >();\n\tfields.forEach( ( field ) => {\n\t\tfieldsMap.set( field.id, field );\n\t} );\n\n\t// Recursive function to process form fields and their children\n\tfunction processFormField(\n\t\tformField: NormalizedFormField\n\t): FormFieldToValidate< Item > | null {\n\t\t// Handle combined fields (fields with children)\n\t\tif ( 'children' in formField && Array.isArray( formField.children ) ) {\n\t\t\tconst processedChildren = formField.children\n\t\t\t\t.map( processFormField )\n\t\t\t\t.filter( ( child ) => child !== null );\n\n\t\t\tif ( processedChildren.length === 0 ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\t\tif ( fieldDef ) {\n\t\t\t\tconst [ normalizedField ] = normalizeFields< Item >( [\n\t\t\t\t\tfieldDef,\n\t\t\t\t] );\n\n\t\t\t\treturn {\n\t\t\t\t\tid: formField.id,\n\t\t\t\t\tchildren: processedChildren,\n\t\t\t\t\tfield: normalizedField,\n\t\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tid: formField.id,\n\t\t\t\tchildren: processedChildren,\n\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t}\n\n\t\t// Handle leaf fields (fields without children)\n\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\tif ( ! fieldDef ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst [ normalizedField ] = normalizeFields< Item >( [ fieldDef ] );\n\t\treturn {\n\t\t\tid: formField.id,\n\t\t\tchildren: [],\n\t\t\tfield: normalizedField,\n\t\t} as FormFieldToValidate< Item >;\n\t}\n\n\tconst toValidate = normalizedForm.fields\n\t\t.map( processFormField )\n\t\t.filter( ( field ) => field !== null );\n\n\treturn toValidate;\n}\n\nfunction setValidityAtPath(\n\tformValidity: FormValidity | undefined,\n\tfieldValidity: FieldValidity,\n\tpath: string[]\n): FormValidity {\n\t// Handle empty validity or empty path\n\tif ( ! formValidity ) {\n\t\tformValidity = {};\n\t}\n\n\tif ( path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\n\t// Clone the root to avoid mutations\n\tconst result = { ...formValidity };\n\n\t// Navigate through the result tree,\n\t// setting up empty paths if they don't exist.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\tcurrent[ segment ] = {};\n\t\t}\n\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\n\t// At the final destination, merge the new validity with the existing.\n\tconst finalKey = path[ path.length - 1 ];\n\tcurrent[ finalKey ] = {\n\t\t...( current[ finalKey ] || {} ),\n\t\t...fieldValidity,\n\t};\n\n\treturn result;\n}\n\nfunction removeValidationProperty(\n\tformValidity: FormValidity | undefined,\n\tpath: string[],\n\tproperty: keyof FieldValidity\n): FormValidity | undefined {\n\tif ( ! formValidity || path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\tconst result = { ...formValidity };\n\t// Navigate to parent of target.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\treturn formValidity; // Path doesn't exist\n\t\t}\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\tconst finalKey = path[ path.length - 1 ];\n\tif ( ! current[ finalKey ] ) {\n\t\treturn formValidity;\n\t}\n\tconst fieldValidity = { ...current[ finalKey ] };\n\tdelete fieldValidity[ property ];\n\t// If field has no more validations, remove it entirely.\n\tif ( Object.keys( fieldValidity ).length === 0 ) {\n\t\tdelete current[ finalKey ];\n\t} else {\n\t\t// Keep the field if it has other validations (including children).\n\t\tcurrent[ finalKey ] = fieldValidity;\n\t}\n\t// If root is empty, return undefined\n\tif ( Object.keys( result ).length === 0 ) {\n\t\treturn undefined;\n\t}\n\treturn result;\n}\n\nfunction handleElementsValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { elementsCounterRef, setFormValidity, path, item } = promiseHandler;\n\tconst currentToken =\n\t\t( elementsCounterRef.current[ formField.id ] || 0 ) + 1;\n\telementsCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! Array.isArray( result ) ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __( 'Could not validate elements.' ),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tformField.field?.isValid.elements &&\n\t\t\t\t! formField.field.isValid.elements.validate( item, {\n\t\t\t\t\t...formField.field,\n\t\t\t\t\telements: result,\n\t\t\t\t} )\n\t\t\t) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t\t\t\t'Value must be one of the elements.'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Validation passed so we need to remove `elements` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'elements'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running elements validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\nfunction handleCustomValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { customCounterRef, setFormValidity, path } = promiseHandler;\n\tconst currentToken = ( customCounterRef.current[ formField.id ] || 0 ) + 1;\n\tcustomCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( result === null ) {\n\t\t\t\t// Validation passed so we need to remove `custom` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'custom'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( typeof result === 'string' ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: result,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: __( 'Validation could not be processed.' ),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running custom validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\ntype PromiseHandler< Item > = {\n\tcustomCounterRef: React.MutableRefObject< Record< string, number > >;\n\telementsCounterRef: React.MutableRefObject< Record< string, number > >;\n\tsetFormValidity: React.Dispatch< React.SetStateAction< FormValidity > >;\n\tpath: string[];\n\titem: Item;\n};\n\nfunction validateFormField< Item >(\n\titem: Item,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n): FieldValidity | undefined {\n\t// Validate the field: isValid.required\n\tif (\n\t\tformField.field?.isValid.required &&\n\t\t! formField.field.isValid.required.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\trequired: { type: 'invalid' },\n\t\t};\n\t}\n\n\t// Validate the field: isValid.pattern\n\tif (\n\t\tformField.field?.isValid.pattern &&\n\t\t! formField.field.isValid.pattern.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tpattern: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value does not match the required pattern.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.min\n\tif (\n\t\tformField.field?.isValid.min &&\n\t\t! formField.field.isValid.min.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmin: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is below the minimum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.max\n\tif (\n\t\tformField.field?.isValid.max &&\n\t\t! formField.field.isValid.max.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmax: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is above the maximum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.minLength\n\tif (\n\t\tformField.field?.isValid.minLength &&\n\t\t! formField.field.isValid.minLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tminLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too short.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.maxLength\n\tif (\n\t\tformField.field?.isValid.maxLength &&\n\t\t! formField.field.isValid.maxLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmaxLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too long.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.elements (static)\n\tif (\n\t\tformField.field?.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\t! formField.field.getElements &&\n\t\tArray.isArray( formField.field.elements ) &&\n\t\t! formField.field.isValid.elements.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\telements: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value must be one of the elements.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (sync)\n\tlet customError;\n\tif ( !! formField.field && formField.field.isValid.custom ) {\n\t\ttry {\n\t\t\tconst value = formField.field.getValue( { item } );\n\t\t\tcustomError = formField.field.isValid.custom(\n\t\t\t\tdeepMerge(\n\t\t\t\t\titem,\n\t\t\t\t\tformField.field.setValue( {\n\t\t\t\t\t\titem,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} ) as Partial< Item >\n\t\t\t\t),\n\t\t\t\tformField.field\n\t\t\t);\n\t\t} catch ( error ) {\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__( 'Unknown error when running custom validation.' );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcustom: {\n\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t}\n\n\tif ( typeof customError === 'string' ) {\n\t\treturn {\n\t\t\tcustom: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: customError,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Aggregate async validations (`elements` and `custom`).\n\tconst fieldValidity: FieldValidity = {};\n\t// Validate the field: isValid.elements (async)\n\tif (\n\t\t!! formField.field &&\n\t\tformField.field.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\ttypeof formField.field.getElements === 'function'\n\t) {\n\t\thandleElementsValidationAsync(\n\t\t\tformField.field.getElements(),\n\t\t\tformField,\n\t\t\tpromiseHandler\n\t\t);\n\t\tfieldValidity.elements = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (async)\n\tif ( customError instanceof Promise ) {\n\t\thandleCustomValidationAsync( customError, formField, promiseHandler );\n\n\t\tfieldValidity.custom = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Return aggregated validations if any exist\n\tif ( Object.keys( fieldValidity ).length > 0 ) {\n\t\treturn fieldValidity;\n\t}\n\n\t// Validate its children.\n\tif ( formField.children.length > 0 ) {\n\t\tconst result: Record< string, FieldValidity | undefined > = {};\n\t\tformField.children.forEach( ( child ) => {\n\t\t\tresult[ child.id ] = validateFormField( item, child, {\n\t\t\t\t...promiseHandler,\n\t\t\t\tpath: [ ...promiseHandler.path, formField.id, 'children' ],\n\t\t\t} );\n\t\t} );\n\n\t\tconst filteredResult: Record< string, FieldValidity > = {};\n\t\tObject.entries( result ).forEach( ( [ key, value ] ) => {\n\t\t\tif ( value !== undefined ) {\n\t\t\t\tfilteredResult[ key ] = value;\n\t\t\t}\n\t\t} );\n\n\t\tif ( Object.keys( filteredResult ).length === 0 ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tchildren: filteredResult,\n\t\t};\n\t}\n\n\t// No errors for this field or its children.\n\treturn undefined;\n}\n\nfunction getFormFieldValue< Item >(\n\tformField: FormFieldToValidate< Item >,\n\titem: Item\n): any {\n\tconst fieldValue = formField?.field?.getValue( { item } );\n\tif ( formField.children.length === 0 ) {\n\t\treturn fieldValue;\n\t}\n\n\tconst childrenValues = formField.children.map( ( child ) =>\n\t\tgetFormFieldValue( child, item )\n\t);\n\tif ( ! childrenValues ) {\n\t\treturn fieldValue;\n\t}\n\n\treturn {\n\t\tvalue: fieldValue,\n\t\tchildren: childrenValues,\n\t};\n}\n\n/**\n * Hook that validates a form item and returns an object with error messages for each field.\n *\n * @param item The item to validate.\n * @param fields Fields config.\n * @param form Form config.\n *\n * @return Record of field IDs to error messages (undefined means no error).\n */\nexport function useFormValidity< Item >(\n\titem: Item,\n\tfields: Field< Item >[],\n\tform: Form\n): { validity: FormValidity; isValid: boolean } {\n\tconst [ formValidity, setFormValidity ] = useState< FormValidity >();\n\tconst customCounterRef = useRef< Record< string, number > >( {} );\n\tconst elementsCounterRef = useRef< Record< string, number > >( {} );\n\tconst previousValuesRef = useRef< Record< string, any > >( {} );\n\n\tconst validate = useCallback( () => {\n\t\tconst promiseHandler = {\n\t\t\tcustomCounterRef,\n\t\t\telementsCounterRef,\n\t\t\tsetFormValidity,\n\t\t\tpath: [],\n\t\t\titem,\n\t\t};\n\n\t\tconst formFieldsToValidate = getFormFieldsToValidate( form, fields );\n\t\tif ( formFieldsToValidate.length === 0 ) {\n\t\t\tsetFormValidity( undefined );\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFormValidity: FormValidity = {};\n\t\tconst untouchedFields: string[] = [];\n\t\tformFieldsToValidate.forEach( ( formField ) => {\n\t\t\t// Skip fields that did not change.\n\t\t\tconst value = getFormFieldValue< Item >( formField, item );\n\t\t\tif (\n\t\t\t\tpreviousValuesRef.current.hasOwnProperty( formField.id ) &&\n\t\t\t\tfastDeepEqual(\n\t\t\t\t\tpreviousValuesRef.current[ formField.id ],\n\t\t\t\t\tvalue\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tuntouchedFields.push( formField.id );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tpreviousValuesRef.current[ formField.id ] = value;\n\n\t\t\t// Calculate validity for those fields that changed.\n\t\t\tconst fieldValidity = validateFormField(\n\t\t\t\titem,\n\t\t\t\tformField,\n\t\t\t\tpromiseHandler\n\t\t\t);\n\t\t\tif ( fieldValidity !== undefined ) {\n\t\t\t\tnewFormValidity[ formField.id ] = fieldValidity;\n\t\t\t}\n\t\t} );\n\n\t\tsetFormValidity( ( existingFormValidity ) => {\n\t\t\tlet validity: FormValidity = {\n\t\t\t\t...existingFormValidity,\n\t\t\t\t...newFormValidity,\n\t\t\t};\n\n\t\t\tconst fieldsToKeep = [\n\t\t\t\t...untouchedFields,\n\t\t\t\t...Object.keys( newFormValidity ),\n\t\t\t];\n\t\t\tObject.keys( validity ).forEach( ( key ) => {\n\t\t\t\tif ( validity && ! fieldsToKeep.includes( key ) ) {\n\t\t\t\t\tdelete validity[ key ];\n\t\t\t\t}\n\t\t\t} );\n\t\t\tif ( Object.keys( validity ).length === 0 ) {\n\t\t\t\tvalidity = undefined;\n\t\t\t}\n\n\t\t\tconst areEqual = fastDeepEqual( existingFormValidity, validity );\n\t\t\tif ( areEqual ) {\n\t\t\t\treturn existingFormValidity;\n\t\t\t}\n\n\t\t\treturn validity;\n\t\t} );\n\t}, [ item, fields, form ] );\n\n\tuseEffect( () => {\n\t\tvalidate();\n\t}, [ validate ] );\n\n\treturn {\n\t\tvalidity: formValidity,\n\t\tisValid: isFormValid( formValidity ),\n\t};\n}\n\nexport default useFormValidity;\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAsB;AACtB,iBAA0B;AAK1B,qBAAyD;AACzD,kBAAmB;AAKnB,yBAA4B;AAC5B,4BAA0B;AAU1B,SAAS,YAAa,cAAkD;AACvE,MAAK,CAAE,cAAe;AACrB,WAAO;AAAA,EACR;AAEA,SAAO,OAAO,OAAQ,YAAa,EAAE,MAAO,CAAE,oBAAqB;AAClE,WAAO,OAAO,QAAS,eAAgB,EAAE;AAAA,MACxC,CAAE,CAAE,KAAK,UAAW,MAAO;AAC1B,YACC,QAAQ,cACR,cACA,OAAO,eAAe,UACrB;AAED,iBAAO,YAAa,UAA2B;AAAA,QAChD;AACA,eACC,WAAW,SAAS,aACpB,WAAW,SAAS;AAAA,MAEtB;AAAA,IACD;AAAA,EACD,CAAE;AACH;AAQA,SAAS,wBACR,MACA,QACgC;AAChC,QAAM,q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3
3
|
"sources": ["../../../src/components/dataform-controls/datetime.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tBaseControl,\n\tprivateApis as componentsPrivateApis,\n} from '@wordpress/components';\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport { dateI18n, getDate, getSettings } from '@wordpress/date';\nimport { Stack } from '@wordpress/ui';\n\n/**\n * Internal dependencies\n */\nimport type { DataFormControlProps, FormatDatetime } from '../../types';\nimport { OPERATOR_IN_THE_PAST, OPERATOR_OVER } from '../../constants';\nimport RelativeDateControl from './utils/relative-date-control';\nimport useDisabledDateMatchers from './utils/use-disabled-date-matchers';\nimport getCustomValidity from './utils/get-custom-validity';\nimport parseDateTime from '../../field-types/utils/parse-date-time';\nimport { unlock } from '../../lock-unlock';\n\nconst { DateCalendar, ValidatedInputControl } = unlock( componentsPrivateApis );\n\nconst formatDateTime = ( value?: string ): string => {\n\tif ( ! value ) {\n\t\treturn '';\n\t}\n\t// Format in WordPress timezone for datetime-local input: YYYY-MM-DDTHH:mm\n\treturn dateI18n( 'Y-m-d\\\\TH:i', getDate( value ) );\n};\n\nfunction CalendarDateTimeControl< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tconst { compact } = config || {};\n\tconst { id, label, description, setValue, getValue, isValid } = field;\n\tconst disabled = field.isDisabled( { item: data, field } );\n\tconst fieldValue = getValue( { item: data } );\n\tconst value = typeof fieldValue === 'string' ? fieldValue : undefined;\n\n\tconst [ calendarMonth, setCalendarMonth ] = useState< Date >( () => {\n\t\tconst parsedDate = parseDateTime( value );\n\t\treturn parsedDate || new Date(); // Default to current month\n\t} );\n\n\tconst inputControlRef = useRef< HTMLInputElement >( null );\n\tconst validationTimeoutRef =\n\t\tuseRef< ReturnType< typeof setTimeout > >( undefined );\n\tconst previousFocusRef = useRef< Element >( null );\n\n\tconst { minConstraint, maxConstraint, disabledMatchers } =\n\t\tuseDisabledDateMatchers( isValid, parseDateTime );\n\n\tconst onChangeCallback = useCallback(\n\t\t( newValue: string | undefined ) =>\n\t\t\tonChange( setValue( { item: data, value: newValue } ) ),\n\t\t[ data, onChange, setValue ]\n\t);\n\n\t// Cleanup timeout on unmount\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n\n\tconst onSelectDate = useCallback(\n\t\t( newDate: Date | undefined | null ) => {\n\t\t\tlet dateTimeValue: string | undefined;\n\t\t\tif ( newDate ) {\n\t\t\t\t// Extract the date part in WP timezone from the calendar selection\n\t\t\t\tconst wpDate = dateI18n( 'Y-m-d', newDate );\n\n\t\t\t\t// Preserve time if it exists in current value, otherwise use current time\n\t\t\t\tlet wpTime: string;\n\t\t\t\tif ( value ) {\n\t\t\t\t\twpTime = dateI18n( 'H:i', getDate( value ) );\n\t\t\t\t} else {\n\t\t\t\t\twpTime = dateI18n( 'H:i', newDate );\n\t\t\t\t}\n\n\t\t\t\t// Combine date and time in WP timezone and convert to ISO\n\t\t\t\tconst finalDateTime = getDate( `${ wpDate }T${ wpTime }` );\n\t\t\t\tdateTimeValue = finalDateTime.toISOString();\n\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t// Clear any existing timeout\n\t\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t\t// Save the currently focused element\n\t\t\tpreviousFocusRef.current =\n\t\t\t\tinputControlRef.current &&\n\t\t\t\tinputControlRef.current.ownerDocument.activeElement;\n\n\t\t\t// Trigger validation display by simulating focus, blur, and changes.\n\t\t\t// Use a timeout to ensure it runs after the value update.\n\t\t\tvalidationTimeoutRef.current = setTimeout( () => {\n\t\t\t\tif ( inputControlRef.current ) {\n\t\t\t\t\tinputControlRef.current.focus();\n\t\t\t\t\tinputControlRef.current.blur();\n\t\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t\t// Restore focus to the previously focused element\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviousFocusRef.current &&\n\t\t\t\t\t\tpreviousFocusRef.current instanceof HTMLElement\n\t\t\t\t\t) {\n\t\t\t\t\t\tpreviousFocusRef.current.focus();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}, 0 );\n\t\t},\n\t\t[ onChangeCallback, value ]\n\t);\n\n\tconst handleManualDateTimeChange = useCallback(\n\t\t( newValue?: string ) => {\n\t\t\tif ( newValue ) {\n\t\t\t\t// Interpret the datetime-local value in WordPress timezone\n\t\t\t\tconst dateTime = getDate( newValue );\n\t\t\t\tonChangeCallback( dateTime.toISOString() );\n\n\t\t\t\t// Update calendar month to match\n\t\t\t\tconst parsedDate = parseDateTime( dateTime.toISOString() );\n\t\t\t\tif ( parsedDate ) {\n\t\t\t\t\tsetCalendarMonth( parsedDate );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t},\n\t\t[ onChangeCallback ]\n\t);\n\n\tconst { format: fieldFormat } = field;\n\tconst weekStartsOn =\n\t\t( fieldFormat as FormatDatetime ).weekStartsOn ??\n\t\tgetSettings().l10n.startOfWeek;\n\tconst {\n\t\ttimezone: { string: timezoneString },\n\t} = getSettings();\n\n\tlet displayLabel = label;\n\tif ( isValid?.required && ! markWhenOptional && ! hideLabelFromVision ) {\n\t\tdisplayLabel = `${ label } (${ __( 'Required' ) })`;\n\t} else if (\n\t\t! isValid?.required &&\n\t\tmarkWhenOptional &&\n\t\t! hideLabelFromVision\n\t) {\n\t\tdisplayLabel = `${ label } (${ __( 'Optional' ) })`;\n\t}\n\n\treturn (\n\t\t<BaseControl\n\t\t\tid={ id }\n\t\t\tlabel={ displayLabel }\n\t\t\thelp={ description }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t>\n\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t{ /* Manual datetime input */ }\n\t\t\t\t<ValidatedInputControl\n\t\t\t\t\tref={ inputControlRef }\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\trequired={ !! isValid?.required }\n\t\t\t\t\tcustomValidity={ getCustomValidity( isValid, validity ) }\n\t\t\t\t\ttype=\"datetime-local\"\n\t\t\t\t\tlabel={ __( 'Date time' ) }\n\t\t\t\t\thideLabelFromVision\n\t\t\t\t\tvalue={ formatDateTime( value ) }\n\t\t\t\t\tonChange={ handleManualDateTimeChange }\n\t\t\t\t\tdisabled={ disabled }\n\t\t\t\t\tmin={\n\t\t\t\t\t\tminConstraint\n\t\t\t\t\t\t\t? formatDateTime( minConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t\tmax={\n\t\t\t\t\t\tmaxConstraint\n\t\t\t\t\t\t\t? formatDateTime( maxConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t\t{ /* Calendar widget */ }\n\t\t\t\t{ ! compact && (\n\t\t\t\t\t<DateCalendar\n\t\t\t\t\t\tstyle={ { width: '100%' } }\n\t\t\t\t\t\tselected={\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t? parseDateTime( value ) || undefined\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonSelect={ onSelectDate }\n\t\t\t\t\t\tmonth={ calendarMonth }\n\t\t\t\t\t\tonMonthChange={ setCalendarMonth }\n\t\t\t\t\t\ttimeZone={ timezoneString || undefined }\n\t\t\t\t\t\tweekStartsOn={ weekStartsOn }\n\t\t\t\t\t\tdisabled={ disabled || disabledMatchers }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</Stack>\n\t\t</BaseControl>\n\t);\n}\n\nexport default function DateTime< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\toperator,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tif ( operator === OPERATOR_IN_THE_PAST || operator === OPERATOR_OVER ) {\n\t\treturn (\n\t\t\t<RelativeDateControl\n\t\t\t\tclassName=\"dataviews-controls__datetime\"\n\t\t\t\tdata={ data }\n\t\t\t\tfield={ field }\n\t\t\t\tonChange={ onChange }\n\t\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\t\toperator={ operator }\n\t\t\t/>\n\t\t);\n\t}\n\n\treturn (\n\t\t<CalendarDateTimeControl\n\t\t\tdata={ data }\n\t\t\tfield={ field }\n\t\t\tonChange={ onChange }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\tmarkWhenOptional={ markWhenOptional }\n\t\t\tvalidity={ validity }\n\t\t\tconfig={ config }\n\t\t/>\n\t);\n}\n"],
|
|
5
|
-
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA,eAAe;AAAA,OACT;AACP,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,UAAU;AACnB,SAAS,UAAU,SAAS,mBAAmB;AAC/C,SAAS,aAAa;AAMtB,SAAS,sBAAsB,qBAAqB;AACpD,OAAO,yBAAyB;AAChC,OAAO,6BAA6B;AACpC,OAAO,uBAAuB;AAC9B,OAAO,mBAAmB;AAC1B,SAAS,cAAc;AAyJpB,SAEC,KAFD;AAvJH,IAAM,EAAE,cAAc,sBAAsB,IAAI,OAAQ,qBAAsB;AAE9E,IAAM,iBAAiB,CAAE,UAA4B;AACpD,MAAK,CAAE,OAAQ;AACd,WAAO;AAAA,EACR;AAEA,SAAO,SAAU,eAAe,QAAS,KAAM,CAAE;AAClD;AAEA,SAAS,wBAAiC;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAkC;AACjC,QAAM,EAAE,QAAQ,IAAI,UAAU,CAAC;AAC/B,QAAM,EAAE,IAAI,OAAO,aAAa,UAAU,UAAU,QAAQ,IAAI;AAChE,QAAM,WAAW,MAAM,WAAY,EAAE,MAAM,MAAM,MAAM,CAAE;AACzD,QAAM,aAAa,SAAU,EAAE,MAAM,KAAK,CAAE;AAC5C,QAAM,QAAQ,OAAO,eAAe,WAAW,aAAa;AAE5D,QAAM,CAAE,eAAe,gBAAiB,IAAI,SAAkB,MAAM;AACnE,UAAM,aAAa,cAAe,KAAM;AACxC,WAAO,cAAc,oBAAI,KAAK;AAAA,EAC/B,CAAE;AAEF,QAAM,kBAAkB,OAA4B,IAAK;AACzD,QAAM,uBACL,OAA2C,MAAU;AACtD,QAAM,mBAAmB,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tBaseControl,\n\tprivateApis as componentsPrivateApis,\n} from '@wordpress/components';\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport { dateI18n, getDate, getSettings } from '@wordpress/date';\nimport { Stack } from '@wordpress/ui';\n\n/**\n * Internal dependencies\n */\nimport type { DataFormControlProps, FormatDatetime } from '../../types';\nimport { OPERATOR_IN_THE_PAST, OPERATOR_OVER } from '../../constants';\nimport RelativeDateControl from './utils/relative-date-control';\nimport useDisabledDateMatchers from './utils/use-disabled-date-matchers';\nimport getCustomValidity from './utils/get-custom-validity';\nimport parseDateTime from '../../field-types/utils/parse-date-time';\nimport { unlock } from '../../lock-unlock';\n\nconst { DateCalendar, ValidatedInputControl } = unlock( componentsPrivateApis );\n\nconst formatDateTime = ( value?: string ): string => {\n\tif ( ! value ) {\n\t\treturn '';\n\t}\n\t// Format in WordPress timezone for datetime-local input: YYYY-MM-DDTHH:mm\n\treturn dateI18n( 'Y-m-d\\\\TH:i', getDate( value ) );\n};\n\nfunction CalendarDateTimeControl< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tconst { compact } = config || {};\n\tconst { id, label, description, setValue, getValue, isValid } = field;\n\tconst disabled = field.isDisabled( { item: data, field } );\n\tconst fieldValue = getValue( { item: data } );\n\tconst value = typeof fieldValue === 'string' ? fieldValue : undefined;\n\n\tconst [ calendarMonth, setCalendarMonth ] = useState< Date >( () => {\n\t\tconst parsedDate = parseDateTime( value );\n\t\treturn parsedDate || new Date(); // Default to current month\n\t} );\n\n\tconst inputControlRef = useRef< HTMLInputElement >( null );\n\tconst validationTimeoutRef =\n\t\tuseRef< ReturnType< typeof setTimeout > >( undefined );\n\tconst previousFocusRef = useRef< Element | null >( null );\n\n\tconst { minConstraint, maxConstraint, disabledMatchers } =\n\t\tuseDisabledDateMatchers( isValid, parseDateTime );\n\n\tconst onChangeCallback = useCallback(\n\t\t( newValue: string | undefined ) =>\n\t\t\tonChange( setValue( { item: data, value: newValue } ) ),\n\t\t[ data, onChange, setValue ]\n\t);\n\n\t// Cleanup timeout on unmount\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n\n\tconst onSelectDate = useCallback(\n\t\t( newDate: Date | undefined | null ) => {\n\t\t\tlet dateTimeValue: string | undefined;\n\t\t\tif ( newDate ) {\n\t\t\t\t// Extract the date part in WP timezone from the calendar selection\n\t\t\t\tconst wpDate = dateI18n( 'Y-m-d', newDate );\n\n\t\t\t\t// Preserve time if it exists in current value, otherwise use current time\n\t\t\t\tlet wpTime: string;\n\t\t\t\tif ( value ) {\n\t\t\t\t\twpTime = dateI18n( 'H:i', getDate( value ) );\n\t\t\t\t} else {\n\t\t\t\t\twpTime = dateI18n( 'H:i', newDate );\n\t\t\t\t}\n\n\t\t\t\t// Combine date and time in WP timezone and convert to ISO\n\t\t\t\tconst finalDateTime = getDate( `${ wpDate }T${ wpTime }` );\n\t\t\t\tdateTimeValue = finalDateTime.toISOString();\n\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t// Clear any existing timeout\n\t\t\t\tif ( validationTimeoutRef.current ) {\n\t\t\t\t\tclearTimeout( validationTimeoutRef.current );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t\t// Save the currently focused element\n\t\t\tpreviousFocusRef.current =\n\t\t\t\tinputControlRef.current &&\n\t\t\t\tinputControlRef.current.ownerDocument.activeElement;\n\n\t\t\t// Trigger validation display by simulating focus, blur, and changes.\n\t\t\t// Use a timeout to ensure it runs after the value update.\n\t\t\tvalidationTimeoutRef.current = setTimeout( () => {\n\t\t\t\tif ( inputControlRef.current ) {\n\t\t\t\t\tinputControlRef.current.focus();\n\t\t\t\t\tinputControlRef.current.blur();\n\t\t\t\t\tonChangeCallback( dateTimeValue );\n\n\t\t\t\t\t// Restore focus to the previously focused element\n\t\t\t\t\tif (\n\t\t\t\t\t\tpreviousFocusRef.current &&\n\t\t\t\t\t\tpreviousFocusRef.current instanceof HTMLElement\n\t\t\t\t\t) {\n\t\t\t\t\t\tpreviousFocusRef.current.focus();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}, 0 );\n\t\t},\n\t\t[ onChangeCallback, value ]\n\t);\n\n\tconst handleManualDateTimeChange = useCallback(\n\t\t( newValue?: string ) => {\n\t\t\tif ( newValue ) {\n\t\t\t\t// Interpret the datetime-local value in WordPress timezone\n\t\t\t\tconst dateTime = getDate( newValue );\n\t\t\t\tonChangeCallback( dateTime.toISOString() );\n\n\t\t\t\t// Update calendar month to match\n\t\t\t\tconst parsedDate = parseDateTime( dateTime.toISOString() );\n\t\t\t\tif ( parsedDate ) {\n\t\t\t\t\tsetCalendarMonth( parsedDate );\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tonChangeCallback( undefined );\n\t\t\t}\n\t\t},\n\t\t[ onChangeCallback ]\n\t);\n\n\tconst { format: fieldFormat } = field;\n\tconst weekStartsOn =\n\t\t( fieldFormat as FormatDatetime ).weekStartsOn ??\n\t\tgetSettings().l10n.startOfWeek;\n\tconst {\n\t\ttimezone: { string: timezoneString },\n\t} = getSettings();\n\n\tlet displayLabel = label;\n\tif ( isValid?.required && ! markWhenOptional && ! hideLabelFromVision ) {\n\t\tdisplayLabel = `${ label } (${ __( 'Required' ) })`;\n\t} else if (\n\t\t! isValid?.required &&\n\t\tmarkWhenOptional &&\n\t\t! hideLabelFromVision\n\t) {\n\t\tdisplayLabel = `${ label } (${ __( 'Optional' ) })`;\n\t}\n\n\treturn (\n\t\t<BaseControl\n\t\t\tid={ id }\n\t\t\tlabel={ displayLabel }\n\t\t\thelp={ description }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t>\n\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t{ /* Manual datetime input */ }\n\t\t\t\t<ValidatedInputControl\n\t\t\t\t\tref={ inputControlRef }\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\trequired={ !! isValid?.required }\n\t\t\t\t\tcustomValidity={ getCustomValidity( isValid, validity ) }\n\t\t\t\t\ttype=\"datetime-local\"\n\t\t\t\t\tlabel={ __( 'Date time' ) }\n\t\t\t\t\thideLabelFromVision\n\t\t\t\t\tvalue={ formatDateTime( value ) }\n\t\t\t\t\tonChange={ handleManualDateTimeChange }\n\t\t\t\t\tdisabled={ disabled }\n\t\t\t\t\tmin={\n\t\t\t\t\t\tminConstraint\n\t\t\t\t\t\t\t? formatDateTime( minConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t\tmax={\n\t\t\t\t\t\tmaxConstraint\n\t\t\t\t\t\t\t? formatDateTime( maxConstraint )\n\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t\t{ /* Calendar widget */ }\n\t\t\t\t{ ! compact && (\n\t\t\t\t\t<DateCalendar\n\t\t\t\t\t\tstyle={ { width: '100%' } }\n\t\t\t\t\t\tselected={\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t? parseDateTime( value ) || undefined\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t\tonSelect={ onSelectDate }\n\t\t\t\t\t\tmonth={ calendarMonth }\n\t\t\t\t\t\tonMonthChange={ setCalendarMonth }\n\t\t\t\t\t\ttimeZone={ timezoneString || undefined }\n\t\t\t\t\t\tweekStartsOn={ weekStartsOn }\n\t\t\t\t\t\tdisabled={ disabled || disabledMatchers }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</Stack>\n\t\t</BaseControl>\n\t);\n}\n\nexport default function DateTime< Item >( {\n\tdata,\n\tfield,\n\tonChange,\n\thideLabelFromVision,\n\tmarkWhenOptional,\n\toperator,\n\tvalidity,\n\tconfig,\n}: DataFormControlProps< Item > ) {\n\tif ( operator === OPERATOR_IN_THE_PAST || operator === OPERATOR_OVER ) {\n\t\treturn (\n\t\t\t<RelativeDateControl\n\t\t\t\tclassName=\"dataviews-controls__datetime\"\n\t\t\t\tdata={ data }\n\t\t\t\tfield={ field }\n\t\t\t\tonChange={ onChange }\n\t\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\t\toperator={ operator }\n\t\t\t/>\n\t\t);\n\t}\n\n\treturn (\n\t\t<CalendarDateTimeControl\n\t\t\tdata={ data }\n\t\t\tfield={ field }\n\t\t\tonChange={ onChange }\n\t\t\thideLabelFromVision={ hideLabelFromVision }\n\t\t\tmarkWhenOptional={ markWhenOptional }\n\t\t\tvalidity={ validity }\n\t\t\tconfig={ config }\n\t\t/>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAGA;AAAA,EACC;AAAA,EACA,eAAe;AAAA,OACT;AACP,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,UAAU;AACnB,SAAS,UAAU,SAAS,mBAAmB;AAC/C,SA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;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAkC;AACjC,MAAK,aAAa,wBAAwB,aAAa,eAAgB;AACtE,WACC;AAAA,MAAC;AAAA;AAAA,QACA,WAAU;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACD;AAAA,EAEF;AAEA,SACC;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,EACD;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/components/dataviews-context/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { createContext, createRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tView,\n\tAction,\n\tNormalizedField,\n\tNormalizedSupportedLayouts,\n\tNormalizedFilter,\n} from '../../types';\nimport type { SetSelection } from '../../types/private';\nimport { LAYOUT_TABLE } from '../../constants';\n\ntype DataViewsContextType< Item > = {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n\tfields: NormalizedField< Item >[];\n\tactions?: Action< Item >[];\n\tdata: Item[];\n\tisLoading?: boolean;\n\tpaginationInfo: {\n\t\ttotalItems: number;\n\t\ttotalPages: number;\n\t};\n\tselection: string[];\n\tonChangeSelection: SetSelection;\n\topenedFilter: string | null;\n\tsetOpenedFilter: ( openedFilter: string | null ) => void;\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tcontainerWidth: number;\n\tcontainerRef: React.
|
|
5
|
-
"mappings": ";AAQA,SAAS,eAAe,iBAAiB;AAazC,SAAS,oBAAoB;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { createContext, createRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tView,\n\tAction,\n\tNormalizedField,\n\tNormalizedSupportedLayouts,\n\tNormalizedFilter,\n} from '../../types';\nimport type { SetSelection } from '../../types/private';\nimport { LAYOUT_TABLE } from '../../constants';\n\ntype DataViewsContextType< Item > = {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n\tfields: NormalizedField< Item >[];\n\tactions?: Action< Item >[];\n\tdata: Item[];\n\tisLoading?: boolean;\n\tpaginationInfo: {\n\t\ttotalItems: number;\n\t\ttotalPages: number;\n\t};\n\tselection: string[];\n\tonChangeSelection: SetSelection;\n\topenedFilter: string | null;\n\tsetOpenedFilter: ( openedFilter: string | null ) => void;\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tcontainerWidth: number;\n\tcontainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tresizeObserverRef:\n\t\t| ( ( element?: HTMLDivElement | null ) => void )\n\t\t| React.RefObject< HTMLDivElement >;\n\tdefaultLayouts: NormalizedSupportedLayouts;\n\tfilters: NormalizedFilter[];\n\tisShowingFilter: boolean;\n\tsetIsShowingFilter: ( value: boolean ) => void;\n\tconfig: { perPageSizes: number[] };\n\tempty?: ReactNode;\n\thasInitiallyLoaded?: boolean;\n\titemListLabel?: string;\n\tonReset?: ( () => void ) | false;\n\tintersectionObserver?: IntersectionObserver | null;\n};\n\nconst DataViewsContext = createContext< DataViewsContextType< any > >( {\n\tview: { type: LAYOUT_TABLE },\n\tonChangeView: () => {},\n\tfields: [],\n\tdata: [],\n\tpaginationInfo: {\n\t\ttotalItems: 0,\n\t\ttotalPages: 0,\n\t},\n\tselection: [],\n\tonChangeSelection: () => {},\n\tsetOpenedFilter: () => {},\n\topenedFilter: null,\n\tgetItemId: ( item ) => item.id,\n\tisItemClickable: () => true,\n\trenderItemLink: undefined,\n\tcontainerWidth: 0,\n\tcontainerRef: createRef(),\n\tresizeObserverRef: () => {},\n\tdefaultLayouts: { list: {}, grid: {}, table: {} },\n\tfilters: [],\n\tisShowingFilter: false,\n\tsetIsShowingFilter: () => {},\n\thasInitiallyLoaded: false,\n\tconfig: {\n\t\tperPageSizes: [],\n\t},\n\tintersectionObserver: null,\n} );\n\nDataViewsContext.displayName = 'DataViewsContext';\n\nexport default DataViewsContext;\n"],
|
|
5
|
+
"mappings": ";AAQA,SAAS,eAAe,iBAAiB;AAazC,SAAS,oBAAoB;AA2C7B,IAAM,mBAAmB,cAA8C;AAAA,EACtE,MAAM,EAAE,MAAM,aAAa;AAAA,EAC3B,cAAc,MAAM;AAAA,EAAC;AAAA,EACrB,QAAQ,CAAC;AAAA,EACT,MAAM,CAAC;AAAA,EACP,gBAAgB;AAAA,IACf,YAAY;AAAA,IACZ,YAAY;AAAA,EACb;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,iBAAiB,MAAM;AAAA,EAAC;AAAA,EACxB,cAAc;AAAA,EACd,WAAW,CAAE,SAAU,KAAK;AAAA,EAC5B,iBAAiB,MAAM;AAAA,EACvB,gBAAgB;AAAA,EAChB,gBAAgB;AAAA,EAChB,cAAc,UAAU;AAAA,EACxB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,gBAAgB,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,EAAE;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,iBAAiB;AAAA,EACjB,oBAAoB,MAAM;AAAA,EAAC;AAAA,EAC3B,oBAAoB;AAAA,EACpB,QAAQ;AAAA,IACP,cAAc,CAAC;AAAA,EAChB;AAAA,EACA,sBAAsB;AACvB,CAAE;AAEF,iBAAiB,cAAc;AAE/B,IAAO,4BAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/components/dataviews-layouts/table/use-scroll-state.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type {
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { MutableRefObject } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useState } from '@wordpress/element';\nimport { isRTL } from '@wordpress/i18n';\n\nconst isScrolledToEnd = ( element: Element ) => {\n\tif ( isRTL() ) {\n\t\tconst scrollLeft = Math.abs( element.scrollLeft );\n\t\treturn scrollLeft <= 1;\n\t}\n\n\treturn element.scrollLeft + element.clientWidth >= element.scrollWidth - 1;\n};\n\n/**\n * A hook to track the scroll state of a container element.\n *\n * Returns whether the container has been scrolled vertically (for sticky header styling)\n * and whether it has reached the horizontal scroll end (for sticky actions column styling).\n *\n * The current way receives \"refs\" as arguments, but it lacks a mechanism to detect when a ref has changed.\n * As a result, when the \"ref\" is updated and attached to a new div, the computation should trigger again.\n * However, this isn't possible in the current setup because the hook is unaware that the ref has changed.\n *\n * See https://github.com/Automattic/wp-calypso/pull/103005#discussion_r2077567912.\n *\n * @param {Object} params The parameters for the hook.\n * @param {MutableRefObject<HTMLDivElement | null>} params.scrollContainerRef The ref to the scroll container element.\n * @param {boolean} [params.enabledHorizontal=false] Whether to track horizontal scroll end.\n * @return {{ isHorizontalScrollEnd: boolean, isVerticallyScrolled: boolean }} The scroll state.\n */\nexport function useScrollState( {\n\tscrollContainerRef,\n\tenabledHorizontal = false,\n}: {\n\tscrollContainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tenabledHorizontal?: boolean;\n} ): { isHorizontalScrollEnd: boolean; isVerticallyScrolled: boolean } {\n\tconst [ isHorizontalScrollEnd, setIsHorizontalScrollEnd ] =\n\t\tuseState( false );\n\tconst [ isVerticallyScrolled, setIsVerticallyScrolled ] = useState( false );\n\n\tconst handleScroll = useCallback( () => {\n\t\tconst scrollContainer = scrollContainerRef.current;\n\t\tif ( ! scrollContainer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( enabledHorizontal ) {\n\t\t\tsetIsHorizontalScrollEnd( isScrolledToEnd( scrollContainer ) );\n\t\t}\n\n\t\tsetIsVerticallyScrolled( scrollContainer.scrollTop > 0 );\n\t}, [ scrollContainerRef, enabledHorizontal ] );\n\tuseEffect( () => {\n\t\tif ( typeof window === 'undefined' || ! scrollContainerRef.current ) {\n\t\t\treturn () => {};\n\t\t}\n\n\t\tconst scrollContainer = scrollContainerRef.current;\n\n\t\thandleScroll();\n\t\tscrollContainer.addEventListener( 'scroll', handleScroll );\n\t\twindow.addEventListener( 'resize', handleScroll );\n\n\t\treturn () => {\n\t\t\tscrollContainer.removeEventListener( 'scroll', handleScroll );\n\t\t\twindow.removeEventListener( 'resize', handleScroll );\n\t\t};\n\t}, [ scrollContainerRef, enabledHorizontal, handleScroll ] );\n\n\treturn { isHorizontalScrollEnd, isVerticallyScrolled };\n}\n"],
|
|
5
5
|
"mappings": ";AAQA,SAAS,aAAa,WAAW,gBAAgB;AACjD,SAAS,aAAa;AAEtB,IAAM,kBAAkB,CAAE,YAAsB;AAC/C,MAAK,MAAM,GAAI;AACd,UAAM,aAAa,KAAK,IAAK,QAAQ,UAAW;AAChD,WAAO,cAAc;AAAA,EACtB;AAEA,SAAO,QAAQ,aAAa,QAAQ,eAAe,QAAQ,cAAc;AAC1E;AAmBO,SAAS,eAAgB;AAAA,EAC/B;AAAA,EACA,oBAAoB;AACrB,GAGuE;AACtE,QAAM,CAAE,uBAAuB,wBAAyB,IACvD,SAAU,KAAM;AACjB,QAAM,CAAE,sBAAsB,uBAAwB,IAAI,SAAU,KAAM;AAE1E,QAAM,eAAe,YAAa,MAAM;AACvC,UAAM,kBAAkB,mBAAmB;AAC3C,QAAK,CAAE,iBAAkB;AACxB;AAAA,IACD;AAEA,QAAK,mBAAoB;AACxB,+BAA0B,gBAAiB,eAAgB,CAAE;AAAA,IAC9D;AAEA,4BAAyB,gBAAgB,YAAY,CAAE;AAAA,EACxD,GAAG,CAAE,oBAAoB,iBAAkB,CAAE;AAC7C,YAAW,MAAM;AAChB,QAAK,OAAO,WAAW,eAAe,CAAE,mBAAmB,SAAU;AACpE,aAAO,MAAM;AAAA,MAAC;AAAA,IACf;AAEA,UAAM,kBAAkB,mBAAmB;AAE3C,iBAAa;AACb,oBAAgB,iBAAkB,UAAU,YAAa;AACzD,WAAO,iBAAkB,UAAU,YAAa;AAEhD,WAAO,MAAM;AACZ,sBAAgB,oBAAqB,UAAU,YAAa;AAC5D,aAAO,oBAAqB,UAAU,YAAa;AAAA,IACpD;AAAA,EACD,GAAG,CAAE,oBAAoB,mBAAmB,YAAa,CAAE;AAE3D,SAAO,EAAE,uBAAuB,qBAAqB;AACtD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/components/dataviews-layouts/utils/item-click-wrapper.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactNode, ReactElement, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { cloneElement } from '@wordpress/element';\n\nfunction getClickableItemProps< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\tclassName,\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\tclassName?: string;\n} ) {\n\tif ( ! isItemClickable( item ) || ! onClickItem ) {\n\t\treturn { className };\n\t}\n\n\treturn {\n\t\tclassName: className\n\t\t\t? `${ className } ${ className }--clickable`\n\t\t\t: undefined,\n\t\trole: 'button',\n\t\ttabIndex: 0,\n\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\tevent.stopPropagation();\n\t\t\tonClickItem( item );\n\t\t},\n\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\tif (\n\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\tevent.key === '' ||\n\t\t\t\tevent.key === ' '\n\t\t\t) {\n\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonClickItem( item );\n\t\t\t}\n\t\t},\n\t};\n}\n\nexport function ItemClickWrapper< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\trenderItemLink,\n\tclassName,\n\tchildren,\n\t...extraProps\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tclassName?: string;\n\ttitle?: string;\n\tchildren: ReactNode;\n} ) {\n\t// Always render a wrapper element so layout and styling relying on the wrapper\n\t// still works even if the item is not clickable.\n\tif ( ! isItemClickable( item ) ) {\n\t\treturn (\n\t\t\t<div className={ className } { ...extraProps }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t);\n\t}\n\n\t// If we have a renderItemLink, use it\n\tif ( renderItemLink ) {\n\t\tconst renderedElement = renderItemLink( {\n\t\t\titem,\n\t\t\tclassName: `${ className } ${ className }--clickable`,\n\t\t\t...extraProps,\n\t\t\tchildren,\n\t\t} )
|
|
5
|
-
"mappings": ";AAQA,SAAS,oBAAoB;AAmE1B;AAjEH,SAAS,sBAA+B;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAKI;AACH,MAAK,CAAE,gBAAiB,IAAK,KAAK,CAAE,aAAc;AACjD,WAAO,EAAE,UAAU;AAAA,EACpB;AAEA,SAAO;AAAA,IACN,WAAW,YACR,GAAI,SAAU,IAAK,SAAU,gBAC7B;AAAA,IACH,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS,CAAE,UAA6B;AAEvC,YAAM,gBAAgB;AACtB,kBAAa,IAAK;AAAA,IACnB;AAAA,IACA,WAAW,CAAE,UAAgC;AAC5C,UACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,cAAM,gBAAgB;AACtB,oBAAa,IAAK;AAAA,MACnB;AAAA,IACD;AAAA,EACD;AACD;AAEO,SAAS,iBAA0B;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,GAYI;AAGH,MAAK,CAAE,gBAAiB,IAAK,GAAI;AAChC,WACC,oBAAC,SAAI,WAA0B,GAAG,YAC/B,UACH;AAAA,EAEF;AAGA,MAAK,gBAAiB;AACrB,UAAM,kBAAkB,eAAgB;AAAA,MACvC;AAAA,MACA,WAAW,GAAI,SAAU,IAAK,SAAU;AAAA,MACxC,GAAG;AAAA,MACH;AAAA,IACD,CAAE;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactNode, ReactElement, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { cloneElement } from '@wordpress/element';\n\nfunction getClickableItemProps< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\tclassName,\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\tclassName?: string;\n} ) {\n\tif ( ! isItemClickable( item ) || ! onClickItem ) {\n\t\treturn { className };\n\t}\n\n\treturn {\n\t\tclassName: className\n\t\t\t? `${ className } ${ className }--clickable`\n\t\t\t: undefined,\n\t\trole: 'button',\n\t\ttabIndex: 0,\n\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\tevent.stopPropagation();\n\t\t\tonClickItem( item );\n\t\t},\n\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\tif (\n\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\tevent.key === '' ||\n\t\t\t\tevent.key === ' '\n\t\t\t) {\n\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\tevent.stopPropagation();\n\t\t\t\tonClickItem( item );\n\t\t\t}\n\t\t},\n\t};\n}\n\nexport function ItemClickWrapper< Item >( {\n\titem,\n\tisItemClickable,\n\tonClickItem,\n\trenderItemLink,\n\tclassName,\n\tchildren,\n\t...extraProps\n}: {\n\titem: Item;\n\tisItemClickable: ( item: Item ) => boolean;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tclassName?: string;\n\ttitle?: string;\n\tchildren: ReactNode;\n} ) {\n\t// Always render a wrapper element so layout and styling relying on the wrapper\n\t// still works even if the item is not clickable.\n\tif ( ! isItemClickable( item ) ) {\n\t\treturn (\n\t\t\t<div className={ className } { ...extraProps }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t);\n\t}\n\n\t// If we have a renderItemLink, use it\n\tif ( renderItemLink ) {\n\t\tconst renderedElement = renderItemLink( {\n\t\t\titem,\n\t\t\tclassName: `${ className } ${ className }--clickable`,\n\t\t\t...extraProps,\n\t\t\tchildren,\n\t\t} );\n\n\t\t// Clone the element and enhance onClick to stop propagation\n\t\treturn cloneElement( renderedElement, {\n\t\t\tonClick: ( event: React.MouseEvent ) => {\n\t\t\t\t// Always stop propagation to prevent selection\n\t\t\t\tevent.stopPropagation();\n\n\t\t\t\t// If consumer provided an onClick, call it\n\t\t\t\tif ( renderedElement.props.onClick ) {\n\t\t\t\t\trenderedElement.props.onClick( event );\n\t\t\t\t}\n\t\t\t},\n\t\t\tonKeyDown: ( event: React.KeyboardEvent ) => {\n\t\t\t\tif (\n\t\t\t\t\tevent.key === 'Enter' ||\n\t\t\t\t\tevent.key === '' ||\n\t\t\t\t\tevent.key === ' '\n\t\t\t\t) {\n\t\t\t\t\t// Prevents onChangeSelection from triggering.\n\t\t\t\t\tevent.stopPropagation();\n\t\t\t\t\t// If consumer provided an onKeyDown, call it\n\t\t\t\t\tif ( renderedElement.props.onKeyDown ) {\n\t\t\t\t\t\trenderedElement.props.onKeyDown( event );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t} );\n\t}\n\n\t// Otherwise use the classic click handler approach\n\tconst clickProps = getClickableItemProps( {\n\t\titem,\n\t\tisItemClickable,\n\t\tonClickItem,\n\t\tclassName,\n\t} );\n\n\treturn (\n\t\t<div { ...clickProps } { ...extraProps }>\n\t\t\t{ children }\n\t\t</div>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAQA,SAAS,oBAAoB;AAmE1B;AAjEH,SAAS,sBAA+B;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAKI;AACH,MAAK,CAAE,gBAAiB,IAAK,KAAK,CAAE,aAAc;AACjD,WAAO,EAAE,UAAU;AAAA,EACpB;AAEA,SAAO;AAAA,IACN,WAAW,YACR,GAAI,SAAU,IAAK,SAAU,gBAC7B;AAAA,IACH,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS,CAAE,UAA6B;AAEvC,YAAM,gBAAgB;AACtB,kBAAa,IAAK;AAAA,IACnB;AAAA,IACA,WAAW,CAAE,UAAgC;AAC5C,UACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,cAAM,gBAAgB;AACtB,oBAAa,IAAK;AAAA,MACnB;AAAA,IACD;AAAA,EACD;AACD;AAEO,SAAS,iBAA0B;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,GAAG;AACJ,GAYI;AAGH,MAAK,CAAE,gBAAiB,IAAK,GAAI;AAChC,WACC,oBAAC,SAAI,WAA0B,GAAG,YAC/B,UACH;AAAA,EAEF;AAGA,MAAK,gBAAiB;AACrB,UAAM,kBAAkB,eAAgB;AAAA,MACvC;AAAA,MACA,WAAW,GAAI,SAAU,IAAK,SAAU;AAAA,MACxC,GAAG;AAAA,MACH;AAAA,IACD,CAAE;AAGF,WAAO,aAAc,iBAAiB;AAAA,MACrC,SAAS,CAAE,UAA6B;AAEvC,cAAM,gBAAgB;AAGtB,YAAK,gBAAgB,MAAM,SAAU;AACpC,0BAAgB,MAAM,QAAS,KAAM;AAAA,QACtC;AAAA,MACD;AAAA,MACA,WAAW,CAAE,UAAgC;AAC5C,YACC,MAAM,QAAQ,WACd,MAAM,QAAQ,MACd,MAAM,QAAQ,KACb;AAED,gBAAM,gBAAgB;AAEtB,cAAK,gBAAgB,MAAM,WAAY;AACtC,4BAAgB,MAAM,UAAW,KAAM;AAAA,UACxC;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH;AAGA,QAAM,aAAa,sBAAuB;AAAA,IACzC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAEF,SACC,oBAAC,SAAM,GAAG,YAAe,GAAG,YACzB,UACH;AAEF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/use-form-validity.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport deepMerge from 'deepmerge';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport normalizeFields from '../field-types';\nimport normalizeForm from '../components/dataform-layouts/normalize-form';\nimport type {\n\tField,\n\tFieldValidity,\n\tForm,\n\tFormValidity,\n\tNormalizedField,\n\tNormalizedFormField,\n} from '../types';\n\nfunction isFormValid( formValidity: FormValidity | undefined ): boolean {\n\tif ( ! formValidity ) {\n\t\treturn true;\n\t}\n\n\treturn Object.values( formValidity ).every( ( fieldValidation ) => {\n\t\treturn Object.entries( fieldValidation ).every(\n\t\t\t( [ key, validation ] ) => {\n\t\t\t\tif (\n\t\t\t\t\tkey === 'children' &&\n\t\t\t\t\tvalidation &&\n\t\t\t\t\ttypeof validation === 'object'\n\t\t\t\t) {\n\t\t\t\t\t// Recursively check children validations\n\t\t\t\t\treturn isFormValid( validation as FormValidity );\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\tvalidation.type !== 'invalid' &&\n\t\t\t\t\tvalidation.type !== 'validating'\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t} );\n}\n\ntype FormFieldToValidate< Item > = {\n\tid: string;\n\tchildren: FormFieldToValidate< Item >[];\n\tfield?: NormalizedField< Item >;\n};\n\nfunction getFormFieldsToValidate< Item >(\n\tform: Form,\n\tfields: Field< Item >[]\n): FormFieldToValidate< Item >[] {\n\tconst normalizedForm = normalizeForm( form );\n\tif ( normalizedForm.fields.length === 0 ) {\n\t\treturn [];\n\t}\n\n\t// Create a map of field IDs to Field definitions for fast lookup\n\tconst fieldsMap = new Map< string, Field< Item > >();\n\tfields.forEach( ( field ) => {\n\t\tfieldsMap.set( field.id, field );\n\t} );\n\n\t// Recursive function to process form fields and their children\n\tfunction processFormField(\n\t\tformField: NormalizedFormField\n\t): FormFieldToValidate< Item > | null {\n\t\t// Handle combined fields (fields with children)\n\t\tif ( 'children' in formField && Array.isArray( formField.children ) ) {\n\t\t\tconst processedChildren = formField.children\n\t\t\t\t.map( processFormField )\n\t\t\t\t.filter( ( child ) => child !== null );\n\n\t\t\tif ( processedChildren.length === 0 ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\t\tif ( fieldDef ) {\n\t\t\t\tconst [ normalizedField ] = normalizeFields< Item >( [\n\t\t\t\t\tfieldDef,\n\t\t\t\t] );\n\n\t\t\t\treturn {\n\t\t\t\t\tid: formField.id,\n\t\t\t\t\tchildren: processedChildren,\n\t\t\t\t\tfield: normalizedField,\n\t\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tid: formField.id,\n\t\t\t\tchildren: processedChildren,\n\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t}\n\n\t\t// Handle leaf fields (fields without children)\n\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\tif ( ! fieldDef ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst [ normalizedField ] = normalizeFields< Item >( [ fieldDef ] );\n\t\treturn {\n\t\t\tid: formField.id,\n\t\t\tchildren: [],\n\t\t\tfield: normalizedField,\n\t\t} as FormFieldToValidate< Item >;\n\t}\n\n\tconst toValidate = normalizedForm.fields\n\t\t.map( processFormField )\n\t\t.filter( ( field ) => field !== null );\n\n\treturn toValidate;\n}\n\nfunction setValidityAtPath(\n\tformValidity: FormValidity | undefined,\n\tfieldValidity: FieldValidity,\n\tpath: string[]\n): FormValidity {\n\t// Handle empty validity or empty path\n\tif ( ! formValidity ) {\n\t\tformValidity = {};\n\t}\n\n\tif ( path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\n\t// Clone the root to avoid mutations\n\tconst result = { ...formValidity };\n\n\t// Navigate through the result tree,\n\t// setting up empty paths if they don't exist.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\tcurrent[ segment ] = {};\n\t\t}\n\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\n\t// At the final destination, merge the new validity with the existing.\n\tconst finalKey = path[ path.length - 1 ];\n\tcurrent[ finalKey ] = {\n\t\t...( current[ finalKey ] || {} ),\n\t\t...fieldValidity,\n\t};\n\n\treturn result;\n}\n\nfunction removeValidationProperty(\n\tformValidity: FormValidity | undefined,\n\tpath: string[],\n\tproperty: keyof FieldValidity\n): FormValidity | undefined {\n\tif ( ! formValidity || path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\tconst result = { ...formValidity };\n\t// Navigate to parent of target.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\treturn formValidity; // Path doesn't exist\n\t\t}\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\tconst finalKey = path[ path.length - 1 ];\n\tif ( ! current[ finalKey ] ) {\n\t\treturn formValidity;\n\t}\n\tconst fieldValidity = { ...current[ finalKey ] };\n\tdelete fieldValidity[ property ];\n\t// If field has no more validations, remove it entirely.\n\tif ( Object.keys( fieldValidity ).length === 0 ) {\n\t\tdelete current[ finalKey ];\n\t} else {\n\t\t// Keep the field if it has other validations (including children).\n\t\tcurrent[ finalKey ] = fieldValidity;\n\t}\n\t// If root is empty, return undefined\n\tif ( Object.keys( result ).length === 0 ) {\n\t\treturn undefined;\n\t}\n\treturn result;\n}\n\nfunction handleElementsValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { elementsCounterRef, setFormValidity, path, item } = promiseHandler;\n\tconst currentToken =\n\t\t( elementsCounterRef.current[ formField.id ] || 0 ) + 1;\n\telementsCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! Array.isArray( result ) ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __( 'Could not validate elements.' ),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tformField.field?.isValid.elements &&\n\t\t\t\t! formField.field.isValid.elements.validate( item, {\n\t\t\t\t\t...formField.field,\n\t\t\t\t\telements: result,\n\t\t\t\t} )\n\t\t\t) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t\t\t\t'Value must be one of the elements.'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Validation passed so we need to remove `elements` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'elements'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running elements validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\nfunction handleCustomValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { customCounterRef, setFormValidity, path } = promiseHandler;\n\tconst currentToken = ( customCounterRef.current[ formField.id ] || 0 ) + 1;\n\tcustomCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( result === null ) {\n\t\t\t\t// Validation passed so we need to remove `custom` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'custom'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( typeof result === 'string' ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: result,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: __( 'Validation could not be processed.' ),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running custom validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\ntype PromiseHandler< Item > = {\n\tcustomCounterRef: React.RefObject< Record< string, number > >;\n\telementsCounterRef: React.RefObject< Record< string, number > >;\n\tsetFormValidity: React.Dispatch< React.SetStateAction< FormValidity > >;\n\tpath: string[];\n\titem: Item;\n};\n\nfunction validateFormField< Item >(\n\titem: Item,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n): FieldValidity | undefined {\n\t// Validate the field: isValid.required\n\tif (\n\t\tformField.field?.isValid.required &&\n\t\t! formField.field.isValid.required.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\trequired: { type: 'invalid' },\n\t\t};\n\t}\n\n\t// Validate the field: isValid.pattern\n\tif (\n\t\tformField.field?.isValid.pattern &&\n\t\t! formField.field.isValid.pattern.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tpattern: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value does not match the required pattern.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.min\n\tif (\n\t\tformField.field?.isValid.min &&\n\t\t! formField.field.isValid.min.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmin: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is below the minimum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.max\n\tif (\n\t\tformField.field?.isValid.max &&\n\t\t! formField.field.isValid.max.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmax: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is above the maximum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.minLength\n\tif (\n\t\tformField.field?.isValid.minLength &&\n\t\t! formField.field.isValid.minLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tminLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too short.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.maxLength\n\tif (\n\t\tformField.field?.isValid.maxLength &&\n\t\t! formField.field.isValid.maxLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmaxLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too long.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.elements (static)\n\tif (\n\t\tformField.field?.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\t! formField.field.getElements &&\n\t\tArray.isArray( formField.field.elements ) &&\n\t\t! formField.field.isValid.elements.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\telements: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value must be one of the elements.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (sync)\n\tlet customError;\n\tif ( !! formField.field && formField.field.isValid.custom ) {\n\t\ttry {\n\t\t\tconst value = formField.field.getValue( { item } );\n\t\t\tcustomError = formField.field.isValid.custom(\n\t\t\t\tdeepMerge(\n\t\t\t\t\titem,\n\t\t\t\t\tformField.field.setValue( {\n\t\t\t\t\t\titem,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} ) as Partial< Item >\n\t\t\t\t),\n\t\t\t\tformField.field\n\t\t\t);\n\t\t} catch ( error ) {\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__( 'Unknown error when running custom validation.' );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcustom: {\n\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t}\n\n\tif ( typeof customError === 'string' ) {\n\t\treturn {\n\t\t\tcustom: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: customError,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Aggregate async validations (`elements` and `custom`).\n\tconst fieldValidity: FieldValidity = {};\n\t// Validate the field: isValid.elements (async)\n\tif (\n\t\t!! formField.field &&\n\t\tformField.field.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\ttypeof formField.field.getElements === 'function'\n\t) {\n\t\thandleElementsValidationAsync(\n\t\t\tformField.field.getElements(),\n\t\t\tformField,\n\t\t\tpromiseHandler\n\t\t);\n\t\tfieldValidity.elements = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (async)\n\tif ( customError instanceof Promise ) {\n\t\thandleCustomValidationAsync( customError, formField, promiseHandler );\n\n\t\tfieldValidity.custom = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Return aggregated validations if any exist\n\tif ( Object.keys( fieldValidity ).length > 0 ) {\n\t\treturn fieldValidity;\n\t}\n\n\t// Validate its children.\n\tif ( formField.children.length > 0 ) {\n\t\tconst result: Record< string, FieldValidity | undefined > = {};\n\t\tformField.children.forEach( ( child ) => {\n\t\t\tresult[ child.id ] = validateFormField( item, child, {\n\t\t\t\t...promiseHandler,\n\t\t\t\tpath: [ ...promiseHandler.path, formField.id, 'children' ],\n\t\t\t} );\n\t\t} );\n\n\t\tconst filteredResult: Record< string, FieldValidity > = {};\n\t\tObject.entries( result ).forEach( ( [ key, value ] ) => {\n\t\t\tif ( value !== undefined ) {\n\t\t\t\tfilteredResult[ key ] = value;\n\t\t\t}\n\t\t} );\n\n\t\tif ( Object.keys( filteredResult ).length === 0 ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tchildren: filteredResult,\n\t\t};\n\t}\n\n\t// No errors for this field or its children.\n\treturn undefined;\n}\n\nfunction getFormFieldValue< Item >(\n\tformField: FormFieldToValidate< Item >,\n\titem: Item\n): any {\n\tconst fieldValue = formField?.field?.getValue( { item } );\n\tif ( formField.children.length === 0 ) {\n\t\treturn fieldValue;\n\t}\n\n\tconst childrenValues = formField.children.map( ( child ) =>\n\t\tgetFormFieldValue( child, item )\n\t);\n\tif ( ! childrenValues ) {\n\t\treturn fieldValue;\n\t}\n\n\treturn {\n\t\tvalue: fieldValue,\n\t\tchildren: childrenValues,\n\t};\n}\n\n/**\n * Hook that validates a form item and returns an object with error messages for each field.\n *\n * @param item The item to validate.\n * @param fields Fields config.\n * @param form Form config.\n *\n * @return Record of field IDs to error messages (undefined means no error).\n */\nexport function useFormValidity< Item >(\n\titem: Item,\n\tfields: Field< Item >[],\n\tform: Form\n): { validity: FormValidity; isValid: boolean } {\n\tconst [ formValidity, setFormValidity ] = useState< FormValidity >();\n\tconst customCounterRef = useRef< Record< string, number > >( {} );\n\tconst elementsCounterRef = useRef< Record< string, number > >( {} );\n\tconst previousValuesRef = useRef< Record< string, any > >( {} );\n\n\tconst validate = useCallback( () => {\n\t\tconst promiseHandler = {\n\t\t\tcustomCounterRef,\n\t\t\telementsCounterRef,\n\t\t\tsetFormValidity,\n\t\t\tpath: [],\n\t\t\titem,\n\t\t};\n\n\t\tconst formFieldsToValidate = getFormFieldsToValidate( form, fields );\n\t\tif ( formFieldsToValidate.length === 0 ) {\n\t\t\tsetFormValidity( undefined );\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFormValidity: FormValidity = {};\n\t\tconst untouchedFields: string[] = [];\n\t\tformFieldsToValidate.forEach( ( formField ) => {\n\t\t\t// Skip fields that did not change.\n\t\t\tconst value = getFormFieldValue< Item >( formField, item );\n\t\t\tif (\n\t\t\t\tpreviousValuesRef.current.hasOwnProperty( formField.id ) &&\n\t\t\t\tfastDeepEqual(\n\t\t\t\t\tpreviousValuesRef.current[ formField.id ],\n\t\t\t\t\tvalue\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tuntouchedFields.push( formField.id );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tpreviousValuesRef.current[ formField.id ] = value;\n\n\t\t\t// Calculate validity for those fields that changed.\n\t\t\tconst fieldValidity = validateFormField(\n\t\t\t\titem,\n\t\t\t\tformField,\n\t\t\t\tpromiseHandler\n\t\t\t);\n\t\t\tif ( fieldValidity !== undefined ) {\n\t\t\t\tnewFormValidity[ formField.id ] = fieldValidity;\n\t\t\t}\n\t\t} );\n\n\t\tsetFormValidity( ( existingFormValidity ) => {\n\t\t\tlet validity: FormValidity = {\n\t\t\t\t...existingFormValidity,\n\t\t\t\t...newFormValidity,\n\t\t\t};\n\n\t\t\tconst fieldsToKeep = [\n\t\t\t\t...untouchedFields,\n\t\t\t\t...Object.keys( newFormValidity ),\n\t\t\t];\n\t\t\tObject.keys( validity ).forEach( ( key ) => {\n\t\t\t\tif ( validity && ! fieldsToKeep.includes( key ) ) {\n\t\t\t\t\tdelete validity[ key ];\n\t\t\t\t}\n\t\t\t} );\n\t\t\tif ( Object.keys( validity ).length === 0 ) {\n\t\t\t\tvalidity = undefined;\n\t\t\t}\n\n\t\t\tconst areEqual = fastDeepEqual( existingFormValidity, validity );\n\t\t\tif ( areEqual ) {\n\t\t\t\treturn existingFormValidity;\n\t\t\t}\n\n\t\t\treturn validity;\n\t\t} );\n\t}, [ item, fields, form ] );\n\n\tuseEffect( () => {\n\t\tvalidate();\n\t}, [ validate ] );\n\n\treturn {\n\t\tvalidity: formValidity,\n\t\tisValid: isFormValid( formValidity ),\n\t};\n}\n\nexport default useFormValidity;\n"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport deepMerge from 'deepmerge';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useEffect, useRef, useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport normalizeFields from '../field-types';\nimport normalizeForm from '../components/dataform-layouts/normalize-form';\nimport type {\n\tField,\n\tFieldValidity,\n\tForm,\n\tFormValidity,\n\tNormalizedField,\n\tNormalizedFormField,\n} from '../types';\n\nfunction isFormValid( formValidity: FormValidity | undefined ): boolean {\n\tif ( ! formValidity ) {\n\t\treturn true;\n\t}\n\n\treturn Object.values( formValidity ).every( ( fieldValidation ) => {\n\t\treturn Object.entries( fieldValidation ).every(\n\t\t\t( [ key, validation ] ) => {\n\t\t\t\tif (\n\t\t\t\t\tkey === 'children' &&\n\t\t\t\t\tvalidation &&\n\t\t\t\t\ttypeof validation === 'object'\n\t\t\t\t) {\n\t\t\t\t\t// Recursively check children validations\n\t\t\t\t\treturn isFormValid( validation as FormValidity );\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\tvalidation.type !== 'invalid' &&\n\t\t\t\t\tvalidation.type !== 'validating'\n\t\t\t\t);\n\t\t\t}\n\t\t);\n\t} );\n}\n\ntype FormFieldToValidate< Item > = {\n\tid: string;\n\tchildren: FormFieldToValidate< Item >[];\n\tfield?: NormalizedField< Item >;\n};\n\nfunction getFormFieldsToValidate< Item >(\n\tform: Form,\n\tfields: Field< Item >[]\n): FormFieldToValidate< Item >[] {\n\tconst normalizedForm = normalizeForm( form );\n\tif ( normalizedForm.fields.length === 0 ) {\n\t\treturn [];\n\t}\n\n\t// Create a map of field IDs to Field definitions for fast lookup\n\tconst fieldsMap = new Map< string, Field< Item > >();\n\tfields.forEach( ( field ) => {\n\t\tfieldsMap.set( field.id, field );\n\t} );\n\n\t// Recursive function to process form fields and their children\n\tfunction processFormField(\n\t\tformField: NormalizedFormField\n\t): FormFieldToValidate< Item > | null {\n\t\t// Handle combined fields (fields with children)\n\t\tif ( 'children' in formField && Array.isArray( formField.children ) ) {\n\t\t\tconst processedChildren = formField.children\n\t\t\t\t.map( processFormField )\n\t\t\t\t.filter( ( child ) => child !== null );\n\n\t\t\tif ( processedChildren.length === 0 ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\t\tif ( fieldDef ) {\n\t\t\t\tconst [ normalizedField ] = normalizeFields< Item >( [\n\t\t\t\t\tfieldDef,\n\t\t\t\t] );\n\n\t\t\t\treturn {\n\t\t\t\t\tid: formField.id,\n\t\t\t\t\tchildren: processedChildren,\n\t\t\t\t\tfield: normalizedField,\n\t\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tid: formField.id,\n\t\t\t\tchildren: processedChildren,\n\t\t\t} satisfies FormFieldToValidate< Item >;\n\t\t}\n\n\t\t// Handle leaf fields (fields without children)\n\t\tconst fieldDef = fieldsMap.get( formField.id );\n\t\tif ( ! fieldDef ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst [ normalizedField ] = normalizeFields< Item >( [ fieldDef ] );\n\t\treturn {\n\t\t\tid: formField.id,\n\t\t\tchildren: [],\n\t\t\tfield: normalizedField,\n\t\t} as FormFieldToValidate< Item >;\n\t}\n\n\tconst toValidate = normalizedForm.fields\n\t\t.map( processFormField )\n\t\t.filter( ( field ) => field !== null );\n\n\treturn toValidate;\n}\n\nfunction setValidityAtPath(\n\tformValidity: FormValidity | undefined,\n\tfieldValidity: FieldValidity,\n\tpath: string[]\n): FormValidity {\n\t// Handle empty validity or empty path\n\tif ( ! formValidity ) {\n\t\tformValidity = {};\n\t}\n\n\tif ( path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\n\t// Clone the root to avoid mutations\n\tconst result = { ...formValidity };\n\n\t// Navigate through the result tree,\n\t// setting up empty paths if they don't exist.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\tcurrent[ segment ] = {};\n\t\t}\n\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\n\t// At the final destination, merge the new validity with the existing.\n\tconst finalKey = path[ path.length - 1 ];\n\tcurrent[ finalKey ] = {\n\t\t...( current[ finalKey ] || {} ),\n\t\t...fieldValidity,\n\t};\n\n\treturn result;\n}\n\nfunction removeValidationProperty(\n\tformValidity: FormValidity | undefined,\n\tpath: string[],\n\tproperty: keyof FieldValidity\n): FormValidity | undefined {\n\tif ( ! formValidity || path.length === 0 ) {\n\t\treturn formValidity;\n\t}\n\tconst result = { ...formValidity };\n\t// Navigate to parent of target.\n\tlet current: any = result;\n\tfor ( let i = 0; i < path.length - 1; i++ ) {\n\t\tconst segment = path[ i ];\n\t\tif ( ! current[ segment ] ) {\n\t\t\treturn formValidity; // Path doesn't exist\n\t\t}\n\t\tcurrent[ segment ] = { ...current[ segment ] };\n\t\tcurrent = current[ segment ];\n\t}\n\tconst finalKey = path[ path.length - 1 ];\n\tif ( ! current[ finalKey ] ) {\n\t\treturn formValidity;\n\t}\n\tconst fieldValidity = { ...current[ finalKey ] };\n\tdelete fieldValidity[ property ];\n\t// If field has no more validations, remove it entirely.\n\tif ( Object.keys( fieldValidity ).length === 0 ) {\n\t\tdelete current[ finalKey ];\n\t} else {\n\t\t// Keep the field if it has other validations (including children).\n\t\tcurrent[ finalKey ] = fieldValidity;\n\t}\n\t// If root is empty, return undefined\n\tif ( Object.keys( result ).length === 0 ) {\n\t\treturn undefined;\n\t}\n\treturn result;\n}\n\nfunction handleElementsValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { elementsCounterRef, setFormValidity, path, item } = promiseHandler;\n\tconst currentToken =\n\t\t( elementsCounterRef.current[ formField.id ] || 0 ) + 1;\n\telementsCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! Array.isArray( result ) ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __( 'Could not validate elements.' ),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tformField.field?.isValid.elements &&\n\t\t\t\t! formField.field.isValid.elements.validate( item, {\n\t\t\t\t\t...formField.field,\n\t\t\t\t\telements: result,\n\t\t\t\t} )\n\t\t\t) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: __(\n\t\t\t\t\t\t\t\t\t'Value must be one of the elements.'\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Validation passed so we need to remove `elements` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'elements'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== elementsCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running elements validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\telements: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\nfunction handleCustomValidationAsync< Item >(\n\tpromise: Promise< any >,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n) {\n\tconst { customCounterRef, setFormValidity, path } = promiseHandler;\n\tconst currentToken = ( customCounterRef.current[ formField.id ] || 0 ) + 1;\n\tcustomCounterRef.current[ formField.id ] = currentToken;\n\n\tpromise\n\t\t.then( ( result ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( result === null ) {\n\t\t\t\t// Validation passed so we need to remove `custom` from validity.\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\treturn removeValidationProperty(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t[ ...path, formField.id ],\n\t\t\t\t\t\t'custom'\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( typeof result === 'string' ) {\n\t\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\t\tprev,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\t\tmessage: result,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t\t);\n\t\t\t\t\treturn newFormValidity;\n\t\t\t\t} );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: __( 'Validation could not be processed.' ),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} )\n\t\t.catch( ( error ) => {\n\t\t\tif ( currentToken !== customCounterRef.current[ formField.id ] ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__(\n\t\t\t\t\t\t'Unknown error when running custom validation asynchronously.'\n\t\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetFormValidity( ( prev ) => {\n\t\t\t\tconst newFormValidity = setValidityAtPath(\n\t\t\t\t\tprev,\n\t\t\t\t\t{\n\t\t\t\t\t\tcustom: {\n\t\t\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t[ ...path, formField.id ]\n\t\t\t\t);\n\t\t\t\treturn newFormValidity;\n\t\t\t} );\n\t\t} );\n}\n\ntype PromiseHandler< Item > = {\n\tcustomCounterRef: React.MutableRefObject< Record< string, number > >;\n\telementsCounterRef: React.MutableRefObject< Record< string, number > >;\n\tsetFormValidity: React.Dispatch< React.SetStateAction< FormValidity > >;\n\tpath: string[];\n\titem: Item;\n};\n\nfunction validateFormField< Item >(\n\titem: Item,\n\tformField: FormFieldToValidate< Item >,\n\tpromiseHandler: PromiseHandler< Item >\n): FieldValidity | undefined {\n\t// Validate the field: isValid.required\n\tif (\n\t\tformField.field?.isValid.required &&\n\t\t! formField.field.isValid.required.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\trequired: { type: 'invalid' },\n\t\t};\n\t}\n\n\t// Validate the field: isValid.pattern\n\tif (\n\t\tformField.field?.isValid.pattern &&\n\t\t! formField.field.isValid.pattern.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tpattern: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value does not match the required pattern.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.min\n\tif (\n\t\tformField.field?.isValid.min &&\n\t\t! formField.field.isValid.min.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmin: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is below the minimum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.max\n\tif (\n\t\tformField.field?.isValid.max &&\n\t\t! formField.field.isValid.max.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmax: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is above the maximum.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.minLength\n\tif (\n\t\tformField.field?.isValid.minLength &&\n\t\t! formField.field.isValid.minLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tminLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too short.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.maxLength\n\tif (\n\t\tformField.field?.isValid.maxLength &&\n\t\t! formField.field.isValid.maxLength.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\tmaxLength: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value is too long.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.elements (static)\n\tif (\n\t\tformField.field?.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\t! formField.field.getElements &&\n\t\tArray.isArray( formField.field.elements ) &&\n\t\t! formField.field.isValid.elements.validate( item, formField.field )\n\t) {\n\t\treturn {\n\t\t\telements: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: __( 'Value must be one of the elements.' ),\n\t\t\t},\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (sync)\n\tlet customError;\n\tif ( !! formField.field && formField.field.isValid.custom ) {\n\t\ttry {\n\t\t\tconst value = formField.field.getValue( { item } );\n\t\t\tcustomError = formField.field.isValid.custom(\n\t\t\t\tdeepMerge(\n\t\t\t\t\titem,\n\t\t\t\t\tformField.field.setValue( {\n\t\t\t\t\t\titem,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} ) as Partial< Item >\n\t\t\t\t),\n\t\t\t\tformField.field\n\t\t\t);\n\t\t} catch ( error ) {\n\t\t\tlet errorMessage;\n\t\t\tif ( error instanceof Error ) {\n\t\t\t\terrorMessage = error.message;\n\t\t\t} else {\n\t\t\t\terrorMessage =\n\t\t\t\t\tString( error ) ||\n\t\t\t\t\t__( 'Unknown error when running custom validation.' );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcustom: {\n\t\t\t\t\ttype: 'invalid',\n\t\t\t\t\tmessage: errorMessage,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t}\n\n\tif ( typeof customError === 'string' ) {\n\t\treturn {\n\t\t\tcustom: {\n\t\t\t\ttype: 'invalid',\n\t\t\t\tmessage: customError,\n\t\t\t},\n\t\t};\n\t}\n\n\t// Aggregate async validations (`elements` and `custom`).\n\tconst fieldValidity: FieldValidity = {};\n\t// Validate the field: isValid.elements (async)\n\tif (\n\t\t!! formField.field &&\n\t\tformField.field.isValid.elements &&\n\t\tformField.field.hasElements &&\n\t\ttypeof formField.field.getElements === 'function'\n\t) {\n\t\thandleElementsValidationAsync(\n\t\t\tformField.field.getElements(),\n\t\t\tformField,\n\t\t\tpromiseHandler\n\t\t);\n\t\tfieldValidity.elements = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Validate the field: isValid.custom (async)\n\tif ( customError instanceof Promise ) {\n\t\thandleCustomValidationAsync( customError, formField, promiseHandler );\n\n\t\tfieldValidity.custom = {\n\t\t\ttype: 'validating',\n\t\t\tmessage: __( 'Validating\u2026' ),\n\t\t};\n\t}\n\n\t// Return aggregated validations if any exist\n\tif ( Object.keys( fieldValidity ).length > 0 ) {\n\t\treturn fieldValidity;\n\t}\n\n\t// Validate its children.\n\tif ( formField.children.length > 0 ) {\n\t\tconst result: Record< string, FieldValidity | undefined > = {};\n\t\tformField.children.forEach( ( child ) => {\n\t\t\tresult[ child.id ] = validateFormField( item, child, {\n\t\t\t\t...promiseHandler,\n\t\t\t\tpath: [ ...promiseHandler.path, formField.id, 'children' ],\n\t\t\t} );\n\t\t} );\n\n\t\tconst filteredResult: Record< string, FieldValidity > = {};\n\t\tObject.entries( result ).forEach( ( [ key, value ] ) => {\n\t\t\tif ( value !== undefined ) {\n\t\t\t\tfilteredResult[ key ] = value;\n\t\t\t}\n\t\t} );\n\n\t\tif ( Object.keys( filteredResult ).length === 0 ) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tchildren: filteredResult,\n\t\t};\n\t}\n\n\t// No errors for this field or its children.\n\treturn undefined;\n}\n\nfunction getFormFieldValue< Item >(\n\tformField: FormFieldToValidate< Item >,\n\titem: Item\n): any {\n\tconst fieldValue = formField?.field?.getValue( { item } );\n\tif ( formField.children.length === 0 ) {\n\t\treturn fieldValue;\n\t}\n\n\tconst childrenValues = formField.children.map( ( child ) =>\n\t\tgetFormFieldValue( child, item )\n\t);\n\tif ( ! childrenValues ) {\n\t\treturn fieldValue;\n\t}\n\n\treturn {\n\t\tvalue: fieldValue,\n\t\tchildren: childrenValues,\n\t};\n}\n\n/**\n * Hook that validates a form item and returns an object with error messages for each field.\n *\n * @param item The item to validate.\n * @param fields Fields config.\n * @param form Form config.\n *\n * @return Record of field IDs to error messages (undefined means no error).\n */\nexport function useFormValidity< Item >(\n\titem: Item,\n\tfields: Field< Item >[],\n\tform: Form\n): { validity: FormValidity; isValid: boolean } {\n\tconst [ formValidity, setFormValidity ] = useState< FormValidity >();\n\tconst customCounterRef = useRef< Record< string, number > >( {} );\n\tconst elementsCounterRef = useRef< Record< string, number > >( {} );\n\tconst previousValuesRef = useRef< Record< string, any > >( {} );\n\n\tconst validate = useCallback( () => {\n\t\tconst promiseHandler = {\n\t\t\tcustomCounterRef,\n\t\t\telementsCounterRef,\n\t\t\tsetFormValidity,\n\t\t\tpath: [],\n\t\t\titem,\n\t\t};\n\n\t\tconst formFieldsToValidate = getFormFieldsToValidate( form, fields );\n\t\tif ( formFieldsToValidate.length === 0 ) {\n\t\t\tsetFormValidity( undefined );\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFormValidity: FormValidity = {};\n\t\tconst untouchedFields: string[] = [];\n\t\tformFieldsToValidate.forEach( ( formField ) => {\n\t\t\t// Skip fields that did not change.\n\t\t\tconst value = getFormFieldValue< Item >( formField, item );\n\t\t\tif (\n\t\t\t\tpreviousValuesRef.current.hasOwnProperty( formField.id ) &&\n\t\t\t\tfastDeepEqual(\n\t\t\t\t\tpreviousValuesRef.current[ formField.id ],\n\t\t\t\t\tvalue\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tuntouchedFields.push( formField.id );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tpreviousValuesRef.current[ formField.id ] = value;\n\n\t\t\t// Calculate validity for those fields that changed.\n\t\t\tconst fieldValidity = validateFormField(\n\t\t\t\titem,\n\t\t\t\tformField,\n\t\t\t\tpromiseHandler\n\t\t\t);\n\t\t\tif ( fieldValidity !== undefined ) {\n\t\t\t\tnewFormValidity[ formField.id ] = fieldValidity;\n\t\t\t}\n\t\t} );\n\n\t\tsetFormValidity( ( existingFormValidity ) => {\n\t\t\tlet validity: FormValidity = {\n\t\t\t\t...existingFormValidity,\n\t\t\t\t...newFormValidity,\n\t\t\t};\n\n\t\t\tconst fieldsToKeep = [\n\t\t\t\t...untouchedFields,\n\t\t\t\t...Object.keys( newFormValidity ),\n\t\t\t];\n\t\t\tObject.keys( validity ).forEach( ( key ) => {\n\t\t\t\tif ( validity && ! fieldsToKeep.includes( key ) ) {\n\t\t\t\t\tdelete validity[ key ];\n\t\t\t\t}\n\t\t\t} );\n\t\t\tif ( Object.keys( validity ).length === 0 ) {\n\t\t\t\tvalidity = undefined;\n\t\t\t}\n\n\t\t\tconst areEqual = fastDeepEqual( existingFormValidity, validity );\n\t\t\tif ( areEqual ) {\n\t\t\t\treturn existingFormValidity;\n\t\t\t}\n\n\t\t\treturn validity;\n\t\t} );\n\t}, [ item, fields, form ] );\n\n\tuseEffect( () => {\n\t\tvalidate();\n\t}, [ validate ] );\n\n\treturn {\n\t\tvalidity: formValidity,\n\t\tisValid: isFormValid( formValidity ),\n\t};\n}\n\nexport default useFormValidity;\n"],
|
|
5
5
|
"mappings": ";AAGA,OAAO,eAAe;AACtB,OAAO,mBAAmB;AAK1B,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,UAAU;AAKnB,OAAO,qBAAqB;AAC5B,OAAO,mBAAmB;AAU1B,SAAS,YAAa,cAAkD;AACvE,MAAK,CAAE,cAAe;AACrB,WAAO;AAAA,EACR;AAEA,SAAO,OAAO,OAAQ,YAAa,EAAE,MAAO,CAAE,oBAAqB;AAClE,WAAO,OAAO,QAAS,eAAgB,EAAE;AAAA,MACxC,CAAE,CAAE,KAAK,UAAW,MAAO;AAC1B,YACC,QAAQ,cACR,cACA,OAAO,eAAe,UACrB;AAED,iBAAO,YAAa,UAA2B;AAAA,QAChD;AACA,eACC,WAAW,SAAS,aACpB,WAAW,SAAS;AAAA,MAEtB;AAAA,IACD;AAAA,EACD,CAAE;AACH;AAQA,SAAS,wBACR,MACA,QACgC;AAChC,QAAM,iBAAiB,cAAe,IAAK;AAC3C,MAAK,eAAe,OAAO,WAAW,GAAI;AACzC,WAAO,CAAC;AAAA,E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AAAA,MACD;AACA,wBAAkB,QAAS,UAAU,EAAG,IAAI;AAG5C,YAAM,gBAAgB;AAAA,QACrB;AAAA,QACA;AAAA,QACA;AAAA,MACD;AACA,UAAK,kBAAkB,QAAY;AAClC,wBAAiB,UAAU,EAAG,IAAI;AAAA,MACnC;AAAA,IACD,CAAE;AAEF,oBAAiB,CAAE,yBAA0B;AAC5C,UAAI,WAAyB;AAAA,QAC5B,GAAG;AAAA,QACH,GAAG;AAAA,MACJ;AAEA,YAAM,eAAe;AAAA,QACpB,GAAG;AAAA,QACH,GAAG,OAAO,KAAM,eAAgB;AAAA,MACjC;AACA,aAAO,KAAM,QAAS,EAAE,QAAS,CAAE,QAAS;AAC3C,YAAK,YAAY,CAAE,aAAa,SAAU,GAAI,GAAI;AACjD,iBAAO,SAAU,GAAI;AAAA,QACtB;AAAA,MACD,CAAE;AACF,UAAK,OAAO,KAAM,QAAS,EAAE,WAAW,GAAI;AAC3C,mBAAW;AAAA,MACZ;AAEA,YAAM,WAAW,cAAe,sBAAsB,QAAS;AAC/D,UAAK,UAAW;AACf,eAAO;AAAA,MACR;AAEA,aAAO;AAAA,IACR,CAAE;AAAA,EACH,GAAG,CAAE,MAAM,QAAQ,IAAK,CAAE;AAE1B,YAAW,MAAM;AAChB,aAAS;AAAA,EACV,GAAG,CAAE,QAAS,CAAE;AAEhB,SAAO;AAAA,IACN,UAAU;AAAA,IACV,SAAS,YAAa,YAAa;AAAA,EACpC;AACD;AAEA,IAAO,4BAAQ;",
|
|
6
6
|
"names": ["fieldDef", "normalizedField"]
|
|
7
7
|
}
|
|
@@ -30,8 +30,8 @@ type DataViewsContextType<Item> = {
|
|
|
30
30
|
} & ComponentProps<'a'>) => ReactElement;
|
|
31
31
|
isItemClickable: (item: Item) => boolean;
|
|
32
32
|
containerWidth: number;
|
|
33
|
-
containerRef: React.
|
|
34
|
-
resizeObserverRef:
|
|
33
|
+
containerRef: React.MutableRefObject<HTMLDivElement | null>;
|
|
34
|
+
resizeObserverRef: ((element?: HTMLDivElement | null) => void) | React.RefObject<HTMLDivElement>;
|
|
35
35
|
defaultLayouts: NormalizedSupportedLayouts;
|
|
36
36
|
filters: NormalizedFilter[];
|
|
37
37
|
isShowingFilter: boolean;
|