[Improvement] Field: add blur method (#1264)

This commit is contained in:
neverland 2018-06-11 20:08:14 +08:00 committed by GitHub
parent c86405dd9f
commit 409055195b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 13 deletions

View File

@ -1,4 +1,4 @@
## 更新日志
## Changelog
### [1.1.7](https://github.com/youzan/vant/tree/v1.1.7)
`2018-06-06`

View File

@ -110,6 +110,7 @@ Use button slot to insert button
```
### API
Field support all native properties of input tagsuch as `maxlength``placeholder``readonly``autofocus`
| Attribute | Description | Type | Default |
@ -125,12 +126,21 @@ Field support all native properties of input tagsuch as `maxlength`、`placeh
| left-icon | Left side icon name | `String` | - |
### Event
Field support all native events of input tagsuch as `focus``blur``keypress`
| Event | Description | Parameters |
|-----------|-----------|-----------|
| click-icon | Triggered when click the icon of Field | - |
### Methods
Use ref to get field instance and call instance methods
| Name | Attribute | Return value | Description |
|-----------|-----------|-----------|-------------|
| blur | - | - | Trigger input blur |
### Slot
| name | Description |

View File

@ -17,7 +17,7 @@
v-if="type === 'textarea'"
v-bind="$attrs"
v-on="listeners"
ref="textarea"
ref="input"
:class="b('control')"
:value="value"
/>
@ -25,6 +25,7 @@
v-else
v-bind="$attrs"
v-on="listeners"
ref="input"
:class="b('control')"
:type="type"
:value="value"
@ -104,6 +105,10 @@ export default create({
},
methods: {
blur() {
this.$refs.input && this.$refs.input.blur();
},
onInput(event) {
this.$emit('input', event.target.value);
},
@ -126,19 +131,14 @@ export default create({
},
adjustSize() {
if (!(this.type === 'textarea' && this.autosize)) {
const { input } = this.$refs;
if (!(this.type === 'textarea' && this.autosize) || !input) {
return;
}
const el = this.$refs.textarea;
/* istanbul ignore if */
if (!el) {
return;
}
input.style.height = 'auto';
el.style.height = 'auto';
let height = el.scrollHeight;
let height = input.scrollHeight;
if (isObj(this.autosize)) {
const { maxHeight, minHeight } = this.autosize;
if (maxHeight) {
@ -150,7 +150,7 @@ export default create({
}
if (height) {
el.style.height = height + 'px';
input.style.height = height + 'px';
}
}
}

View File

@ -99,3 +99,16 @@ test('autosize object', async() => {
await later();
expect(textarea.element.style.height).toEqual(('50px'));
});
test('blur method', () => {
const fn = jest.fn();
const wrapper = mount(Field, {
listeners: {
blur: fn
}
});
wrapper.find('input').element.focus();
wrapper.vm.blur();
expect(fn.mock.calls.length).toEqual(1);
});

View File

@ -112,6 +112,7 @@ Vue.use(Field);
```
### API
Field 默认支持 Input 标签所有的原生属性,比如 `maxlength``placeholder``readonly``autofocus`
| 参数 | 说明 | 类型 | 默认值 |
@ -127,11 +128,20 @@ Field 默认支持 Input 标签所有的原生属性,比如 `maxlength`、`pla
| left-icon | 输入框左侧图标 (可选值见 Icon 组件) | `String` | - |
### Event
Field 默认支持 Input 标签所有的原生事件,如 `focus``blur``keypress`
| 事件 | 说明 | 回调参数 |
|-----------|-----------|-----------|
| click-icon | 点击尾部图标时触发 | - |
| click-icon | 点击尾部图标时触发 | -
### 方法
通过 ref 可以获取到 field 实例并调用实例方法
| 方法名 | 参数 | 返回值 | 介绍 |
|-----------|-----------|-----------|-------------|
| blur | - | - | 取消输入框焦点 |
### Slot