Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BulkController support for shift+click behaviour #10861

Merged
merged 1 commit into from Oct 19, 2023

Conversation

lb-
Copy link
Member

@lb- lb- commented Sep 5, 2023

  • Allow multiple selections or un-selections with shift+click
  • Pull out activeItems getter and instead use this as a method for select all checkboxes and individual checkboxes
  • Pull out ToggleAllOptions to a type and support this being passed in via a CustomEvent also, as per pattern being set up in other Controllers
  • Minor refactoring to make code easier to extend, clean up of JSDoc and adjust how we set up tests to make it easier to use different html.
  • Do the tests still pass? Including Jest tests for all new behaviour introduced by this PR
  • Does the code comply with the style guide?
  • For front-end changes: Did you test on all of Wagtail’s supported environments?
  • For new features: Has the documentation been updated accordingly? N/A

additional details for testing this change

  1. In bakerydemo, navigate to the form submissions listing and have at least 5 form submissions loaded.
  2. Click any item near the top, now shift+click any item near the bottom. Observe that the ones in between are now selected.
  3. Now select all, then click (to unselect) any item near the bottom, now shift+click any item near the top, observe that the ones in between are all un-clicked.
  4. Try this a few different ways.
  5. Finally, do the same, on the simple translation form (where you translate a page and you are asked if you want to translate to all other supported translations).

Recording

screencast-bulk-actions-shift-click-2023.09.06-07_54_23.webm

Context

This PR builds on the work done in #10292 & #10613 and sets up the Stimulus controller to be closer to what is needed for the full 'bulk actions' checkbox support. This PR will be one of the final pieces before that work can start.

@lb- lb- added status:Needs Review component:Form builder component:UI localization l10n & i18n for admin interface only javascript Pull requests that update Javascript code component:Stimulus labels Sep 5, 2023
@squash-labs
Copy link

squash-labs bot commented Sep 5, 2023

Manage this branch in Squash

Test this branch here: https://lb-feature5836-bulk-toggle-shi-k26mj.squash.io

@lb- lb- force-pushed the feature/5836-bulk-toggle-shift-click branch 3 times, most recently from f142866 to c8cace9 Compare September 7, 2023 21:06
@lb-
Copy link
Member Author

lb- commented Sep 7, 2023

Rebased and fixed a few bugs encountered for edge cases.

@lb-
Copy link
Member Author

lb- commented Sep 14, 2023

Rebased

@lb- lb- requested a review from zerolab September 14, 2023 22:06
@lb- lb- force-pushed the feature/5836-bulk-toggle-shift-click branch from a70738a to 2ce47a9 Compare September 25, 2023 20:01
@lb- lb- force-pushed the feature/5836-bulk-toggle-shift-click branch 2 times, most recently from dba9b8c to 4f22059 Compare October 9, 2023 10:02
@thibaudcolas thibaudcolas added this to the 5.2 milestone Oct 18, 2023
@lb- lb- force-pushed the feature/5836-bulk-toggle-shift-click branch from 4f22059 to 3665196 Compare October 19, 2023 11:34
Copy link
Member

@thibaudcolas thibaudcolas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working great @lb-, thank you! I’ve spotted minor things, which I’ll address myself as part of merging this.

@@ -1,5 +1,10 @@
import { Controller } from '@hotwired/stimulus';

type ToggleAllOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m surprised this isn’t an interface, but it works :)

<div id="bulk-container-multi" data-controller="w-bulk">
<input id="select-all-multi" type="checkbox" data-w-bulk-target="all" data-action="w-bulk#toggleAll">
<div id="checkboxes">
<input id="0" type="checkbox" data-w-bulk-target="item" data-action="w-bulk#toggle">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML element ids normally can’t start with a number. Seems to work fine here but I’ll add a letter prefix just to be safe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call thank you, I think id can be a number but CSS identifiers cannot start with a number.

Comment on lines 157 to 163
toggleAll(
event: CustomEvent<ToggleAllOptions> & { params?: ToggleAllOptions },
) {
const { force = null } = {
...event?.detail,
...event?.params,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few things I’m confused about here:

  • The types say event is a CustomEvent, while the code below has an optional chaining as if this could be called without an event. But the code later accesses event.target anyway.
  • Unclear why we need to pass the data via detail in addition to params. I don’t see either of those two options used in Wagtail aside from the test suite. If we did use them, the use case for detail feels very small.
  • force seems like it doesn’t need a default value (but perhaps worth setting one for clarity?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thibaudcolas - yes, I can see that this is a bit confusing. I would actually love some suggestions how to do this pattern better but let me explain the goals.

The purpose of this odd syntax is so that toggleAll can be called a few ways.

  1. Internally from the class if needed this.toggleAll()
  2. Via a simple Stimulus action <input ... data-action"change->w-bulk#toggleAll" />
  3. Via dispatching some other event to this, also via a Stimulus action <input ... data-action"some-event@document->w-bulk#toggleAll" />

However, if we want to use force, we can now do this via the actions either with our custom event or with Stimulus action params.

<input ... data-action"change->w-bulk#toggleAll" data-w-bulk-force-param="true" />

OR

<input ... data-action"some-event@document->w-bulk#toggleAll" />
// JS
document.dispatchEvent(new CustomEvent({ detail: {force:true} }));

I could not find a nice way in TypeScript to reflect these three ways of calling, along with have a shared 'options' Type (to contain that force could be X type). I would love any tips for this as this approach is super useful in many other controllers.

As for the force defaulting to null, I do not recall why, it's probably just a habit of mine to always think of the default as an intentional value. Instead of accidental undefined we are better off saying that this may not be passed in and if so it should default to something.

@thibaudcolas thibaudcolas force-pushed the feature/5836-bulk-toggle-shift-click branch 2 times, most recently from d0573a2 to 8717bd2 Compare October 19, 2023 12:14
- Allow multiple selections or un-selections with shift+click
- Pull out activeItems getter and instead use this as a method for select all checkboxes and individual checkboxes
- Pull out ToggleAllOptions to a type and support this being passed in via a CustomEvent also, as per pattern being set up in other Controllers
- Adjust unit test structure to be easier to work with different HTML values
@thibaudcolas thibaudcolas force-pushed the feature/5836-bulk-toggle-shift-click branch from 8717bd2 to 8fa7ee4 Compare October 19, 2023 12:15
@thibaudcolas thibaudcolas merged commit 74aada0 into wagtail:main Oct 19, 2023
6 of 20 checks passed
@lb- lb- deleted the feature/5836-bulk-toggle-shift-click branch October 19, 2023 12:54
@lb-
Copy link
Member Author

lb- commented Oct 19, 2023

Thanks Thibaud, let me know if you would like to see any improvement to this code as a follow up PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:Form builder component:Stimulus component:UI localization l10n & i18n for admin interface only javascript Pull requests that update Javascript code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants