-
Notifications
You must be signed in to change notification settings - Fork 83
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
base: master
Are you sure you want to change the base?
Conversation
85c434a
to
8fdf78c
Compare
@@ -1,2 +1,3 @@ | |||
export { default } from "./multi-action-button.component"; | |||
export type { MultiActionButtonHandle } from "./multi-action-button.component"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking
### 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking
### 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 () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking
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 () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking
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 () => { |
There was a problem hiding this 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 😄
Proposed behaviour
Both
MultiActionButton
andSplitButton
now useforwardRef
and support focusing their respective buttons usinguseImperativeHandle
. 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
d.ts
file added or updated if requiredQA
Additional context
Testing instructions