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

DSD-1740/close dropdown multiselect #1607

Merged
merged 11 commits into from
Jun 25, 2024

Conversation

oliviawongnyc
Copy link
Collaborator

Fixes JIRA ticket DSD-1740

This PR does the following:

  • Adds the ability to close the Accordion if an element within the panel is focused (for example, if a checkbox within the MultiSelect is focused, this PR will close the parent panel when the user presses esc).

How has this been tested?

  • Locally, in Storybook.
  • Also added another unit test.

Accessibility concerns or updates

  • None.

Checklist:

  • I have updated the Storybook documentation accordingly.
  • I have added relevant accessibility documentation for this pull request.
  • All new and existing tests passed.

Front End Review:

  • Review the Vercel preview deployment once it is ready.

Copy link

vercel bot commented May 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nypl-design-system ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 24, 2024 5:52pm

@oliviawongnyc oliviawongnyc added the Needs Review Pull requests that are ready for peer review. label May 22, 2024
Copy link
Member

@EdwinGuzman EdwinGuzman left a comment

Choose a reason for hiding this comment

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

I think your tests may break based on my comment so will review that once this is updated.

@@ -14,7 +14,18 @@ export const changelogData: ChangelogData[] = [
version: "Prerelease",
type: "Update",
affects: ["Accessibility", "Functionality"],
notes: ["Adds logic to close accordion when 'esc' key is pressed"],
notes: [
"Adds logic to close accordion when element within panel is focused and 'esc' key is pressed",
Copy link
Member

Choose a reason for hiding this comment

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

This is duplicated below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They're slightly different, but I can combine them if you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

You can keep both but what I mean is you only need one object with "Prerelease" but now there are two. Unless if this was done because you don't think this will get deployed by 5/23. If that's the case then it's okay to keep both.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see! Okay. Will combine.

Comment on lines 243 to 245
setExpandedPanels(
expandedPanels.filter((i) => i !== focusedPanelIndex)
);
Copy link
Member

Choose a reason for hiding this comment

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

This is within the else block and it doesn't seem intentional because the esc key should still be functional when the user is focused on the accordion button.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes! Good catch. Thank you.

} else {
// If the user is focused on an element within the panel...
focusedPanelIndex = Number(
e.target.closest("[role='region']").id.split("-").pop()
Copy link
Member

Choose a reason for hiding this comment

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

Nice, but also brittle if the DOM and/or id ever changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I know :/, I didn't want to do it this way, but it seemed like the best option...the id is really the only way the panel is connected to the button that needs to close.

@@ -160,19 +160,17 @@ describe("Accordion", () => {
});

it("closes the accordion when the 'esc' key is pressed", async () => {
render(<Accordion accordionData={[accordionData[0]]} isDefaultOpen />);
render(<Accordion accordionData={[accordionData[0]]} />);
Copy link
Member

Choose a reason for hiding this comment

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

Can you add another test where the entire accordionData array is passed, so now there are three accordions. For one of the accordions, add a button or checkbox, focus on it, and then press escape. This is to test the new feature in this PR but also that it works when there are more than just one accordion.

Copy link
Member

Choose a reason for hiding this comment

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

I know there's a test that does this for MultiSelect but the feature is in the Accordion so it should also be tested there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Copy link
Collaborator

@bigfishdesign13 bigfishdesign13 left a comment

Choose a reason for hiding this comment

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

Please see my comment about how focus should be handled.

@@ -4,7 +4,7 @@ import {
ChakraComponent,
useMultiStyleConfig,
} from "@chakra-ui/react";
import React, { useState, forwardRef, useRef } from "react";
import React, { forwardRef, useRef, useState } from "react";
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was not sure where to put this comment, but the focus needs to be addressed after the ESC key is pressed when a checkbox has focus. In that scenario, the focus should return to the main button after the ESC key is pressed.

Copy link
Member

Choose a reason for hiding this comment

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

That's a good point. If it involves having state or ref we should talk about this because it's getting overly complex and from what I understood, accordions don't generally behave this way. So if we don't have to implement this then perhaps we should not.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need it for Accordion, but we do need it for MultiSelect.

Copy link
Member

Choose a reason for hiding this comment

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

That's true but the Accordion is a big part of the MultiSelect and, to me, MultiSelect is inheriting all the features of Accordion. There is currently a ref that is being passed from the MultiSelect to the Accordion to keep track of focus for when the items count button is clicked. This could be used as well when the esc key is pressed. But it we have to keep track of pressing the ESC key in the MultiSelect to close the Accordion, then it feels like the work is being duplicated because the Accordion already does check for when the ESC key is pressed.

Perhaps we should reconsider and move all the logic just to the MultiSelect from the Accordion. @oliviawongnyc As you try to implement the focus management in the Accordion, let me know if my comment makes sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay @EdwinGuzman and @bigfishdesign13, focus should now return to the button when the Accordion (or MultiSelect) is closed with the esc key.

@bigfishdesign13 bigfishdesign13 added the Changes Requested Pull requests where changes have been requested in peer review. label May 23, 2024
Copy link
Member

@EdwinGuzman EdwinGuzman left a comment

Choose a reason for hiding this comment

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

Update looks good but tests are failing

Copy link
Collaborator

@bigfishdesign13 bigfishdesign13 left a comment

Choose a reason for hiding this comment

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

The esc focus handling works great in MultiSelect, but it is not working in Accordion.

@bigfishdesign13 bigfishdesign13 added the x2 This PR needs at least two approvals. label Jun 6, 2024
Copy link
Member

@EdwinGuzman EdwinGuzman left a comment

Choose a reason for hiding this comment

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

The update looks good to me and it works in both the Accordion and MultiSelect.

// user is focused on or within, and remove it as
// an expanded panel. (Nothing will happen if the
// panel is already collapsed.)
if (e.code === "Escape") {
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, why the update from e.keyCode to e.code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Two reasons. 1) I thought it was clearer than. using a keyCode, and 2) for some reason in the tests, the keyCode for Escape was a different number than it is in the code? So I used the code 'Escape' instead for the tests and wanted to be consistent.

Copy link
Member

@EdwinGuzman EdwinGuzman left a comment

Choose a reason for hiding this comment

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

Really nice refactor in the last commit.

@EdwinGuzman EdwinGuzman removed the Changes Requested Pull requests where changes have been requested in peer review. label Jun 24, 2024
Copy link
Collaborator

@bigfishdesign13 bigfishdesign13 left a comment

Choose a reason for hiding this comment

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

Awesome!

@bigfishdesign13 bigfishdesign13 added Ship It Pull requests that have been reviewed and approved. and removed Needs Review Pull requests that are ready for peer review. x2 This PR needs at least two approvals. labels Jun 24, 2024
@oliviawongnyc oliviawongnyc merged commit bae5120 into development Jun 25, 2024
5 checks passed
@oliviawongnyc oliviawongnyc deleted the DSD-1740/close-dropdown-multiselect branch June 25, 2024 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ship It Pull requests that have been reviewed and approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants