Skip to content

Add focus and blur methods to buttons #6490

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

Merged
merged 2 commits into from
Apr 22, 2023
Merged
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
2 changes: 2 additions & 0 deletions components/button/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { nextTick } from 'vue';
import { asyncExpect, sleep } from '../../../tests/utils';
import mountTest from '../../../tests/shared/mountTest';
import { resetWarned } from '../../_util/warning';
import focusTest from '../../../tests/shared/focusTest';

describe('Button', () => {
mountTest(Button);
mountTest(Button.Group);
focusTest(Button);
it('renders correctly', () => {
const wrapper = mount({
render() {
Expand Down
13 changes: 12 additions & 1 deletion components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default defineComponent({
props: initDefaultProps(buttonProps(), { type: 'default' }),
slots: ['icon'],
// emits: ['click', 'mousedown'],
setup(props, { slots, attrs, emit }) {
setup(props, { slots, attrs, emit, expose }) {
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);

const buttonNodeRef = ref<HTMLElement>(null);
Expand Down Expand Up @@ -146,6 +146,17 @@ export default defineComponent({
delayTimeoutRef.value && clearTimeout(delayTimeoutRef.value);
});

const focus = () => {
buttonNodeRef.value?.focus();
};
const blur = () => {
buttonNodeRef.value?.blur();
};
expose({
focus,
blur,
});

return () => {
const { icon = slots.icon?.() } = props;
const children = flattenChildren(slots.default?.());
Expand Down
9 changes: 9 additions & 0 deletions components/button/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ It accepts all props which native buttons support.

## FAQ

### Methods

#### Checkbox

| Name | Description | Version |
| ------- | ------------ | ------- |
| blur() | remove focus | |
| focus() | get focus | |

### How to remove space between 2 chinese characters

Following the Ant Design specification, we will add one space between if Button (exclude Text button and Link button) contains two Chinese characters only. If you don't need that, you can use [ConfigProvider](/components/config-provider/#API) to set `autoInsertSpaceInButton` as `false`.
Expand Down
9 changes: 9 additions & 0 deletions components/button/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ cover: https://gw.alipayobjects.com/zos/alicdn/fNUKzY1sk/Button.svg

支持原生 button 的其他所有属性。

### 方法

#### Button

| 名称 | 描述 | 版本 |
| ------- | -------- | ---- |
| blur() | 移除焦点 | |
| focus() | 获取焦点 | |

## FAQ

### 如何移除 2 个汉字之间的空格
Expand Down