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

feat(multi-action-button, split-button): add programmatic focus support #7193

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tomdavies73
Copy link
Contributor

@tomdavies73 tomdavies73 commented Feb 6, 2025

Proposed behaviour

Both MultiActionButton and SplitButton now use forwardRef and support focusing their respective buttons using useImperativeHandle. Each component exports a handle type that can be used for programmatic focus via this handle.

Current behaviour

None of the components have programmatic focus support.

Checklist

  • Commits follow our style guide
  • Related issues linked in commit messages if required
  • Screenshots are included in the PR if useful
  • All themes are supported if required
  • Unit tests added or updated if required
  • Playwright automation tests added or updated if required
  • Storybook added or updated if required
  • Translations added or updated (including creating or amending translation keys table in storybook) if required
  • Typescript d.ts file added or updated if required
  • Related docs have been updated if required

QA

  • Tested in provided StackBlitz sandbox/Storybook
  • Add new Playwright test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

Testing instructions

@@ -1,2 +1,3 @@
export { default } from "./multi-action-button.component";
export type { MultiActionButtonHandle } from "./multi-action-button.component";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export type { MultiActionButtonHandle } from "./multi-action-button.component";
export type { MultiActionButtonHandle, MultiActionButtonProps } from "./multi-action-button.component";

const buttonRef = useRef<HTMLButtonElement>(null);
export type MultiActionButtonHandle = {
/** Programmatically focus the button */
focusButton: () => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): might be worth calling this focusMainButton so it matches the same naming as used in SplitButton and it's a bit more explicit about what it does etc

@@ -34,6 +34,17 @@ import MultiActionButton from "carbon-react/lib/components/multi-action-button";

<Canvas of={MultiActionButtonStories.DefaultStory} />

### Focusing Programmatically
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking

Suggested change
### Focusing Programmatically
### Focusing Main Button Programmatically

```

The `MultiActionButtonHandle` type provides an imperative handle for programmatic control over `MultiActionButton`.
Using a `ref`, you can access its `focusButton()` method to set focus on the button as needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking

Suggested change
Using a `ref`, you can access its `focusButton()` method to set focus on the button as needed.
Using a `ref`, you can access its `focusButton()` method to set focus on the main button as needed.

@@ -37,6 +37,17 @@ import SplitButton from "carbon-react/lib/components/split-button";

<Canvas of={SplitButtonStories.Default} />

### Focusing Programmatically
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking

Suggested change
### Focusing Programmatically
### Focusing Main and Toggle Buttons Programmatically

@@ -92,6 +110,30 @@ test("renders child buttons when toggle button is clicked", async () => {
).toBeVisible();
});

test("should focus the SplitButton's main button via focus method on the ref handle when invoked", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking

Suggested change
test("should focus the SplitButton's main button via focus method on the ref handle when invoked", async () => {
test("should focus the main button when the focusMainButton on the ref handle is invoked", async () => {

expect(screen.getByRole("button", { name: "Main Button" })).toHaveFocus();
});

test("should focus the SplitButton's toggle button via focus method on the ref handle when invoked", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking

Suggested change
test("should focus the SplitButton's toggle button via focus method on the ref handle when invoked", async () => {
test("should focus the toggle button when the focusToggleButton on the ref handle is invoked", async () => {

@edleeks87 edleeks87 marked this pull request as ready for review February 7, 2025 10:39
@DipperTheDan DipperTheDan self-requested a review February 7, 2025 10:46
@edleeks87 edleeks87 marked this pull request as draft February 7, 2025 10:48
Copy link
Contributor

@DipperTheDan DipperTheDan left a comment

Choose a reason for hiding this comment

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

Nothing more to add from me. Just address the comments left by @edleeks87 please 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants