Skip to content

Commit f713cf5

Browse files
enejbCopilot
andauthored
Forms: clear selection when switching tabs (#45543)
* Remove selection on tab switching. * Move the selection from the component state to the URL * changelog * Update projects/packages/forms/changelog/fix-forms-clear-selection-when-switching-tabs Co-authored-by: Copilot <[email protected]> * Update projects/packages/forms/src/dashboard/inbox/dataviews/index.js Co-authored-by: Copilot <[email protected]> * Update projects/packages/forms/src/dashboard/components/inbox-status-toggle/index.tsx Co-authored-by: Copilot <[email protected]> * Update projects/packages/forms/src/dashboard/inbox/dataviews/index.js Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 448b95e commit f713cf5

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Forms: reset the selection on tab switch

projects/packages/forms/src/dashboard/components/inbox-status-toggle/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function InboxStatusToggle( { onChange }: InboxStatusToggleProps
4444
const status = searchParams.get( 'status' ) || 'inbox';
4545
const [ isSm ] = useBreakpointMatch( 'sm' );
4646

47-
const { totalItemsInbox, totalItemsSpam, totalItemsTrash } = useInboxData();
47+
const { totalItemsInbox, totalItemsSpam, totalItemsTrash, setSelectedResponses } = useInboxData();
4848

4949
const statusTabs = [
5050
{ label: getTabLabel( __( 'Inbox', 'jetpack-forms' ), totalItemsInbox ), value: 'inbox' },
@@ -66,13 +66,13 @@ export default function InboxStatusToggle( { onChange }: InboxStatusToggleProps
6666
setSearchParams( prev => {
6767
const params = new URLSearchParams( prev );
6868
params.set( 'status', newStatus );
69-
69+
params.delete( 'r' ); // Clear selected responses when changing tabs.
7070
return params;
7171
} );
72-
72+
setSelectedResponses( [] );
7373
onChange( newStatus );
7474
},
75-
[ isSm, status, setSearchParams, onChange ]
75+
[ isSm, status, setSearchParams, onChange, setSelectedResponses ]
7676
);
7777

7878
return (

projects/packages/forms/src/dashboard/inbox/dataviews/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,22 @@ export default function InboxView() {
135135
setCurrentQuery( _queryArgs );
136136
}, [ view, statusFilter, setCurrentQuery ] );
137137

138-
const [ selection, setSelection ] = useState( selectedResponses?.split( ',' ) || EMPTY_ARRAY );
138+
const selection = selectedResponses?.split( ',' ) || EMPTY_ARRAY;
139+
139140
// We need to keep the valid selection item in state to be used in `export`.
140141
// We do this because a user can have in their selection either ids that
141142
// do not exist at all or ids that are not in the current data set.
142143
useEffect( () => {
143144
const validSelectedIds = ( selection || [] ).filter( id =>
144145
records?.some( record => getItemId( record ) === id )
145146
);
147+
146148
setSelectedResponses( validSelectedIds );
147149
}, [ records, selection, setSelectedResponses ] );
150+
148151
const [ sidePanelItem, setSidePanelItem ] = useState();
149152
const onChangeSelection = useCallback(
150153
items => {
151-
setSelection( items );
152154
// Set the side panel item only when we are not on mobile.
153155
if ( ! isMobile ) {
154156
setSidePanelItem(
@@ -158,8 +160,18 @@ export default function InboxView() {
158160
}
159161
setSearchParams( previousSearchParams => {
160162
const _searchParams = new URLSearchParams( previousSearchParams );
161-
if ( items.length ) {
162-
_searchParams.set( 'r', items.join( ',' ) );
163+
const currentURLSelection = _searchParams.get( 'r' )?.split( ',' ) || [];
164+
165+
// Filter out IDs from the current URL selection that are either already selected in the current view
166+
// or already present in the current records, to avoid duplication when merging selections across pages.
167+
const currentSelection = currentURLSelection.filter(
168+
id => ! ( items.includes( id ) || records?.some( record => getItemId( record ) === id ) )
169+
);
170+
171+
// merge items with the current URL
172+
const mergedItems = [ ...new Set( [ ...currentSelection, ...items ] ) ];
173+
if ( mergedItems.length ) {
174+
_searchParams.set( 'r', mergedItems.join( ',' ) );
163175
} else {
164176
_searchParams.delete( 'r' );
165177
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: bugfix
3+
4+
Forms: reset the selection on tab switch in dashboard

0 commit comments

Comments
 (0)