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

chore(Button): add autoFocus property #1988

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React from 'react';

import {useForkRef} from '../../hooks';
import type {DOMProps, QAProps} from '../types';
import {block} from '../utils/cn';
import {isIcon, isSvg} from '../utils/common';
Expand Down Expand Up @@ -62,6 +63,7 @@ export interface ButtonProps extends DOMProps, QAProps {
onBlur?: React.FocusEventHandler<HTMLButtonElement | HTMLAnchorElement>;
/** Button content. You can mix button text with `<Icon/>` component */
children?: React.ReactNode;
autoFocus?: boolean;
}

const b = block('button');
Expand Down Expand Up @@ -93,9 +95,17 @@ const ButtonWithHandlers = React.forwardRef<HTMLElement, ButtonProps>(function B
style,
className,
qa,
autoFocus = false,
},
ref,
propsRef,
) {
const innerRef = React.useRef<HTMLElement>(null);
const ref = useForkRef(propsRef, innerRef);

React.useEffect(() => {
if (autoFocus) innerRef.current?.focus();
}, [autoFocus]);

const handleClickCapture = React.useCallback(
(event: React.SyntheticEvent) => {
eventBroker.publish({
Expand Down
1 change: 1 addition & 0 deletions src/components/Button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ LANDING_BLOCK-->
| type | HTML `type` attribute | `"button"` `"submit"` `"reset"` | `"button"` |
| view | Sets button appearance | `string` | `"normal"` |
| width | `"auto"` `"max"` | `"auto"` `"max"` | |
| autoFocus | Sets focus on | `boolean` | `false` |

## CSS API

Expand Down
Loading