[improvement] Field: add focus method (#1987)

This commit is contained in:
neverland 2018-10-27 20:04:25 +08:00 committed by GitHub
parent a995c203dc
commit 7685ecd254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 0 deletions

View File

@ -147,6 +147,7 @@ Use ref to get field instance and call instance methods
| Name | Attribute | Return value | Description | | Name | Attribute | Return value | Description |
|------|------|------|------| |------|------|------|------|
| focus | - | - | Trigger input focus |
| blur | - | - | Trigger input blur | | blur | - | - | Trigger input blur |
### Slot ### Slot

View File

@ -127,6 +127,10 @@ export default create({
}, },
methods: { methods: {
focus() {
this.$refs.input && this.$refs.input.focus();
},
blur() { blur() {
this.$refs.input && this.$refs.input.blur(); this.$refs.input && this.$refs.input.blur();
}, },

View File

@ -110,6 +110,16 @@ test('blur method', () => {
expect(fn.mock.calls.length).toEqual(1); expect(fn.mock.calls.length).toEqual(1);
}); });
test('focus method', () => {
const fn = jest.fn();
const wrapper = mount(Field);
wrapper.vm.$on('focus', fn);
wrapper.vm.focus();
expect(fn.mock.calls.length).toEqual(1);
});
test('maxlength', async() => { test('maxlength', async() => {
const wrapper = mount(Field, { const wrapper = mount(Field, {
attrs: { attrs: {

View File

@ -149,6 +149,7 @@ Field 默认支持 Input 标签所有的原生事件,如 `focus`、`blur`、`k
| 方法名 | 参数 | 返回值 | 介绍 | | 方法名 | 参数 | 返回值 | 介绍 |
|------|------|------|------| |------|------|------|------|
| focus | - | - | 获取输入框焦点 |
| blur | - | - | 取消输入框焦点 | | blur | - | - | 取消输入框焦点 |
### Slot ### Slot