Skip to content

Commit 8e54318

Browse files
authored
feat: Add focus and blur methods to buttons (#6490), close #6483
* feat(button): add methods for focus and blur closed #6483 * docs(button): add focus methods doc
1 parent 634adae commit 8e54318

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

components/button/__tests__/index.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { nextTick } from 'vue';
55
import { asyncExpect, sleep } from '../../../tests/utils';
66
import mountTest from '../../../tests/shared/mountTest';
77
import { resetWarned } from '../../_util/warning';
8+
import focusTest from '../../../tests/shared/focusTest';
89

910
describe('Button', () => {
1011
mountTest(Button);
1112
mountTest(Button.Group);
13+
focusTest(Button);
1214
it('renders correctly', () => {
1315
const wrapper = mount({
1416
render() {

components/button/button.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default defineComponent({
3636
props: initDefaultProps(buttonProps(), { type: 'default' }),
3737
slots: ['icon'],
3838
// emits: ['click', 'mousedown'],
39-
setup(props, { slots, attrs, emit }) {
39+
setup(props, { slots, attrs, emit, expose }) {
4040
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);
4141

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

149+
const focus = () => {
150+
buttonNodeRef.value?.focus();
151+
};
152+
const blur = () => {
153+
buttonNodeRef.value?.blur();
154+
};
155+
expose({
156+
focus,
157+
blur,
158+
});
159+
149160
return () => {
150161
const { icon = slots.icon?.() } = props;
151162
const children = flattenChildren(slots.default?.());

components/button/index.en-US.md

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ It accepts all props which native buttons support.
5555

5656
## FAQ
5757

58+
### Methods
59+
60+
#### Checkbox
61+
62+
| Name | Description | Version |
63+
| ------- | ------------ | ------- |
64+
| blur() | remove focus | |
65+
| focus() | get focus | |
66+
5867
### How to remove space between 2 chinese characters
5968

6069
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`.

components/button/index.zh-CN.md

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ cover: https://gw.alipayobjects.com/zos/alicdn/fNUKzY1sk/Button.svg
5656

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

59+
### 方法
60+
61+
#### Button
62+
63+
| 名称 | 描述 | 版本 |
64+
| ------- | -------- | ---- |
65+
| blur() | 移除焦点 | |
66+
| focus() | 获取焦点 | |
67+
5968
## FAQ
6069

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

0 commit comments

Comments
 (0)