Skip to content

Commit 52a99c9

Browse files
committed
Enforce at least one category must be selected
1 parent fb75525 commit 52a99c9

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

projects/plugins/jetpack/extensions/blocks/subscriptions/controls.js

+8
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ export default function SubscriptionControls( {
334334
<fieldset>
335335
<legend className="wp-block-jetpack-subscriptions__legend">
336336
{ __( 'Categories', 'jetpack' ) }
337+
<small className="wp-block-jetpack-subscriptions__help">
338+
{ __( 'Select at least one category.', 'jetpack' ) }
339+
</small>
337340
</legend>
338341
{ availableNewsletterCategories.map( category => (
339342
<CheckboxControl
@@ -347,6 +350,11 @@ export default function SubscriptionControls( {
347350
? excludedNewsletterCategoryIds.filter( id => id !== category.id )
348351
: [ ...excludedNewsletterCategoryIds, category.id ];
349352

353+
// One or more categories must be selected
354+
if ( newExcludedIds.length === availableNewsletterCategories.length ) {
355+
return;
356+
}
357+
350358
setAttributes( {
351359
excludedNewsletterCategoryIds: newExcludedIds,
352360
} );

projects/plugins/jetpack/extensions/blocks/subscriptions/editor.scss

+10
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77
margin-bottom: 8px;
88
padding: 0px;
99
}
10+
11+
.wp-block-jetpack-subscriptions__help { // Meant to mimic .components-control__help
12+
font-size: 12px;
13+
font-style: normal;
14+
font-weight: normal;
15+
color: rgb(117, 117, 117);
16+
line-height: 1.5;
17+
display: block;
18+
text-transform: none;
19+
}

projects/plugins/jetpack/extensions/blocks/subscriptions/test/controls.js

+16
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@ describe( 'Inspector controls', () => {
350350
excludedNewsletterCategoryIds: [ defaultProps.availableNewsletterCategories[ 0 ].id ],
351351
} );
352352
} );
353+
354+
test( 'enforces one category must be selected when enabled', async () => {
355+
const user = userEvent.setup();
356+
render(
357+
<SubscriptionsInspectorControls
358+
{ ...defaultProps }
359+
autoSubscribeToNewsletterCategories
360+
excludedNewsletterCategoryIds={ [ defaultProps.availableNewsletterCategories[ 1 ].id ] }
361+
/>
362+
);
363+
364+
await user.click( screen.getByText( 'Settings' ), { selector: 'button' } );
365+
await user.click( screen.getByLabelText( 'Category 1' ) );
366+
367+
expect( setAttributes ).not.toHaveBeenCalled();
368+
} );
353369
} );
354370
} );
355371
} );

0 commit comments

Comments
 (0)