* feat(button): add methods for focus and blur closed #6483 * docs(button): add focus methods docpull/6509/head
parent
634adae1c3
commit
8e5431886a
|
@ -5,10 +5,12 @@ import { nextTick } from 'vue';
|
||||||
import { asyncExpect, sleep } from '../../../tests/utils';
|
import { asyncExpect, sleep } from '../../../tests/utils';
|
||||||
import mountTest from '../../../tests/shared/mountTest';
|
import mountTest from '../../../tests/shared/mountTest';
|
||||||
import { resetWarned } from '../../_util/warning';
|
import { resetWarned } from '../../_util/warning';
|
||||||
|
import focusTest from '../../../tests/shared/focusTest';
|
||||||
|
|
||||||
describe('Button', () => {
|
describe('Button', () => {
|
||||||
mountTest(Button);
|
mountTest(Button);
|
||||||
mountTest(Button.Group);
|
mountTest(Button.Group);
|
||||||
|
focusTest(Button);
|
||||||
it('renders correctly', () => {
|
it('renders correctly', () => {
|
||||||
const wrapper = mount({
|
const wrapper = mount({
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -36,7 +36,7 @@ export default defineComponent({
|
||||||
props: initDefaultProps(buttonProps(), { type: 'default' }),
|
props: initDefaultProps(buttonProps(), { type: 'default' }),
|
||||||
slots: ['icon'],
|
slots: ['icon'],
|
||||||
// emits: ['click', 'mousedown'],
|
// emits: ['click', 'mousedown'],
|
||||||
setup(props, { slots, attrs, emit }) {
|
setup(props, { slots, attrs, emit, expose }) {
|
||||||
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);
|
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);
|
||||||
|
|
||||||
const buttonNodeRef = ref<HTMLElement>(null);
|
const buttonNodeRef = ref<HTMLElement>(null);
|
||||||
|
@ -146,6 +146,17 @@ export default defineComponent({
|
||||||
delayTimeoutRef.value && clearTimeout(delayTimeoutRef.value);
|
delayTimeoutRef.value && clearTimeout(delayTimeoutRef.value);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const focus = () => {
|
||||||
|
buttonNodeRef.value?.focus();
|
||||||
|
};
|
||||||
|
const blur = () => {
|
||||||
|
buttonNodeRef.value?.blur();
|
||||||
|
};
|
||||||
|
expose({
|
||||||
|
focus,
|
||||||
|
blur,
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const { icon = slots.icon?.() } = props;
|
const { icon = slots.icon?.() } = props;
|
||||||
const children = flattenChildren(slots.default?.());
|
const children = flattenChildren(slots.default?.());
|
||||||
|
|
|
@ -55,6 +55,15 @@ It accepts all props which native buttons support.
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
|
### Methods
|
||||||
|
|
||||||
|
#### Checkbox
|
||||||
|
|
||||||
|
| Name | Description | Version |
|
||||||
|
| ------- | ------------ | ------- |
|
||||||
|
| blur() | remove focus | |
|
||||||
|
| focus() | get focus | |
|
||||||
|
|
||||||
### How to remove space between 2 chinese characters
|
### 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`.
|
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`.
|
||||||
|
|
|
@ -56,6 +56,15 @@ cover: https://gw.alipayobjects.com/zos/alicdn/fNUKzY1sk/Button.svg
|
||||||
|
|
||||||
支持原生 button 的其他所有属性。
|
支持原生 button 的其他所有属性。
|
||||||
|
|
||||||
|
### 方法
|
||||||
|
|
||||||
|
#### Button
|
||||||
|
|
||||||
|
| 名称 | 描述 | 版本 |
|
||||||
|
| ------- | -------- | ---- |
|
||||||
|
| blur() | 移除焦点 | |
|
||||||
|
| focus() | 获取焦点 | |
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
### 如何移除 2 个汉字之间的空格
|
### 如何移除 2 个汉字之间的空格
|
||||||
|
|
Loading…
Reference in New Issue