Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a5c00fe
Initial implementation
straku Sep 11, 2025
e0ce670
TEMP: Change fixtures to use nested data
straku Sep 11, 2025
6430200
Temp: Add story with nested data for DataForm
straku Sep 12, 2025
423d60e
Change approach to typing an item and update usage
straku Sep 12, 2025
3153f67
Fix tests
straku Sep 12, 2025
898024a
Add setValue to boolean field in the stories
straku Sep 12, 2025
4fdb8de
Allow `DeepPartial< Item >` to accept full `Item`
straku Sep 12, 2025
508f710
Rely on `DeepPartial` in `setValue`
straku Sep 12, 2025
1e15efe
Disable validation in filter controls
straku Sep 12, 2025
d1e45c8
Filters: prepare data and field
oandregal Sep 12, 2025
3e0c027
Update custom Edit to match the DeepPartial shape of onChange
oandregal Sep 12, 2025
2070e12
Use Item instead of DeepPartial<Item> as parameter to getValue and se…
oandregal Sep 12, 2025
d3467a3
Fix modal with nested data
oandregal Sep 12, 2025
cb4cdcd
Update story and fix the DeepPartial type definition.
oandregal Sep 12, 2025
e761011
Port new tests to existing test file
oandregal Sep 12, 2025
af025fb
Revert changes to fields
oandregal Sep 12, 2025
9f33dbe
Adapt new places after rebase
oandregal Sep 12, 2025
ed9a157
Fix author field in DataForm story
straku Sep 15, 2025
05fc231
Remove unused import, finer scope for useCallback dependencies
straku Sep 15, 2025
2bf7cb0
Add comment for DeepPartial type
straku Sep 15, 2025
6ffd2ec
Doc changes
straku Sep 15, 2025
684463a
Use setValue for custom validation functions item argument
straku Sep 15, 2025
a26aebd
Update readme after validation changes
straku Sep 15, 2025
ad84488
Story: return partial, not item
oandregal Sep 16, 2025
f848c91
Do not make password required in a custom rule
oandregal Sep 16, 2025
661c3f0
Filters: setValue should return a partial
oandregal Sep 16, 2025
6e1cfa7
Revert "Update readme after validation changes"
straku Sep 16, 2025
eb9be76
Use full item for custom validation functions
straku Sep 16, 2025
c69e9d2
Fix wrong conflict resolution
straku Sep 16, 2025
131bc7e
Fix onChange for date related controls
straku Sep 16, 2025
ed0226e
Improve internal types used in date control: DateRange
oandregal Sep 16, 2025
8c15df5
Improve internal types used in Date control: DateRelative
oandregal Sep 16, 2025
ad9aed6
Datetime: fix relative filter
oandregal Sep 16, 2025
5d332a3
Integer control: improve internal types and checks
oandregal Sep 16, 2025
1f7b912
ToggleControl: fix validation
oandregal Sep 16, 2025
28fc311
Add changelog entry
oandregal Sep 16, 2025
bfa4bdc
DataForm panel: fix modal (changes accumulate)
oandregal Sep 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ToggleControl: fix validation
  • Loading branch information
oandregal committed Sep 16, 2025
commit 1f7b91245e3425bb2fb65ada562ba1f7eff3854c
52 changes: 32 additions & 20 deletions packages/dataviews/src/dataform-controls/toggle-group.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import deepMerge from 'deepmerge';

/**
* WordPress dependencies
*/
Expand All @@ -21,7 +26,7 @@ export default function ToggleGroup< Item >( {
onChange,
hideLabelFromVision,
}: DataFormControlProps< Item > ) {
const { id, getValue, setValue } = field;
const { getValue, setValue } = field;
const [ customValidity, setCustomValidity ] =
useState<
React.ComponentProps<
Expand All @@ -35,6 +40,31 @@ export default function ToggleGroup< Item >( {
onChange( setValue( { item: data, value: newValue } ) ),
[ data, onChange, setValue ]
);
const onValidateControl = useCallback(
( newValue: any ) => {
const message = field.isValid?.custom?.(
deepMerge(
data,
setValue( {
item: data,
value: newValue,
} ) as Partial< Item >
),
field
);

if ( message ) {
setCustomValidity( {
type: 'invalid',
message,
} );
return;
}

setCustomValidity( undefined );
},
[ data, field, setValue ]
);

if ( field.elements ) {
const selectedOption = field.elements.find(
Expand All @@ -43,25 +73,7 @@ export default function ToggleGroup< Item >( {
return (
<ValidatedToggleGroupControl
required={ !! field.isValid?.required }
onValidate={ ( newValue: any ) => {
const message = field.isValid?.custom?.(
{
...data,
[ id ]: newValue,
},
field
);

if ( message ) {
setCustomValidity( {
type: 'invalid',
message,
} );
return;
}

setCustomValidity( undefined );
} }
onValidate={ onValidateControl }
customValidity={ customValidity }
__next40pxDefaultSize
__nextHasNoMarginBottom
Expand Down
Loading