mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-27 03:46:35 +08:00
[Improvement] Field: add blur method (#1264)
This commit is contained in:
parent
c86405dd9f
commit
409055195b
@ -1,4 +1,4 @@
|
|||||||
## 更新日志
|
## Changelog
|
||||||
|
|
||||||
### [1.1.7](https://github.com/youzan/vant/tree/v1.1.7)
|
### [1.1.7](https://github.com/youzan/vant/tree/v1.1.7)
|
||||||
`2018-06-06`
|
`2018-06-06`
|
||||||
|
@ -110,6 +110,7 @@ Use button slot to insert button
|
|||||||
```
|
```
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
Field support all native properties of input tag,such as `maxlength`、`placeholder`、`readonly`、`autofocus`
|
Field support all native properties of input tag,such as `maxlength`、`placeholder`、`readonly`、`autofocus`
|
||||||
|
|
||||||
| Attribute | Description | Type | Default |
|
| Attribute | Description | Type | Default |
|
||||||
@ -125,12 +126,21 @@ Field support all native properties of input tag,such as `maxlength`、`placeh
|
|||||||
| left-icon | Left side icon name | `String` | - |
|
| left-icon | Left side icon name | `String` | - |
|
||||||
|
|
||||||
### Event
|
### Event
|
||||||
|
|
||||||
Field support all native events of input tag,such as `focus`、`blur`、`keypress`
|
Field support all native events of input tag,such as `focus`、`blur`、`keypress`
|
||||||
|
|
||||||
| Event | Description | Parameters |
|
| Event | Description | Parameters |
|
||||||
|-----------|-----------|-----------|
|
|-----------|-----------|-----------|
|
||||||
| click-icon | Triggered when click the icon of Field | - |
|
| 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
|
### Slot
|
||||||
|
|
||||||
| name | Description |
|
| name | Description |
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
v-if="type === 'textarea'"
|
v-if="type === 'textarea'"
|
||||||
v-bind="$attrs"
|
v-bind="$attrs"
|
||||||
v-on="listeners"
|
v-on="listeners"
|
||||||
ref="textarea"
|
ref="input"
|
||||||
:class="b('control')"
|
:class="b('control')"
|
||||||
:value="value"
|
:value="value"
|
||||||
/>
|
/>
|
||||||
@ -25,6 +25,7 @@
|
|||||||
v-else
|
v-else
|
||||||
v-bind="$attrs"
|
v-bind="$attrs"
|
||||||
v-on="listeners"
|
v-on="listeners"
|
||||||
|
ref="input"
|
||||||
:class="b('control')"
|
:class="b('control')"
|
||||||
:type="type"
|
:type="type"
|
||||||
:value="value"
|
:value="value"
|
||||||
@ -104,6 +105,10 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
blur() {
|
||||||
|
this.$refs.input && this.$refs.input.blur();
|
||||||
|
},
|
||||||
|
|
||||||
onInput(event) {
|
onInput(event) {
|
||||||
this.$emit('input', event.target.value);
|
this.$emit('input', event.target.value);
|
||||||
},
|
},
|
||||||
@ -126,19 +131,14 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
adjustSize() {
|
adjustSize() {
|
||||||
if (!(this.type === 'textarea' && this.autosize)) {
|
const { input } = this.$refs;
|
||||||
|
if (!(this.type === 'textarea' && this.autosize) || !input) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const el = this.$refs.textarea;
|
input.style.height = 'auto';
|
||||||
/* istanbul ignore if */
|
|
||||||
if (!el) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
el.style.height = 'auto';
|
let height = input.scrollHeight;
|
||||||
|
|
||||||
let height = el.scrollHeight;
|
|
||||||
if (isObj(this.autosize)) {
|
if (isObj(this.autosize)) {
|
||||||
const { maxHeight, minHeight } = this.autosize;
|
const { maxHeight, minHeight } = this.autosize;
|
||||||
if (maxHeight) {
|
if (maxHeight) {
|
||||||
@ -150,7 +150,7 @@ export default create({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (height) {
|
if (height) {
|
||||||
el.style.height = height + 'px';
|
input.style.height = height + 'px';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,3 +99,16 @@ test('autosize object', async() => {
|
|||||||
await later();
|
await later();
|
||||||
expect(textarea.element.style.height).toEqual(('50px'));
|
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);
|
||||||
|
});
|
@ -112,6 +112,7 @@ Vue.use(Field);
|
|||||||
```
|
```
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
Field 默认支持 Input 标签所有的原生属性,比如 `maxlength`、`placeholder`、`readonly`、`autofocus` 等
|
Field 默认支持 Input 标签所有的原生属性,比如 `maxlength`、`placeholder`、`readonly`、`autofocus` 等
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 |
|
| 参数 | 说明 | 类型 | 默认值 |
|
||||||
@ -127,11 +128,20 @@ Field 默认支持 Input 标签所有的原生属性,比如 `maxlength`、`pla
|
|||||||
| left-icon | 输入框左侧图标 (可选值见 Icon 组件) | `String` | - |
|
| left-icon | 输入框左侧图标 (可选值见 Icon 组件) | `String` | - |
|
||||||
|
|
||||||
### Event
|
### Event
|
||||||
|
|
||||||
Field 默认支持 Input 标签所有的原生事件,如 `focus`、`blur`、`keypress` 等
|
Field 默认支持 Input 标签所有的原生事件,如 `focus`、`blur`、`keypress` 等
|
||||||
|
|
||||||
| 事件 | 说明 | 回调参数 |
|
| 事件 | 说明 | 回调参数 |
|
||||||
|-----------|-----------|-----------|
|
|-----------|-----------|-----------|
|
||||||
| click-icon | 点击尾部图标时触发 | - |
|
| click-icon | 点击尾部图标时触发 | -
|
||||||
|
|
||||||
|
### 方法
|
||||||
|
|
||||||
|
通过 ref 可以获取到 field 实例并调用实例方法
|
||||||
|
|
||||||
|
| 方法名 | 参数 | 返回值 | 介绍 |
|
||||||
|
|-----------|-----------|-----------|-------------|
|
||||||
|
| blur | - | - | 取消输入框焦点 |
|
||||||
|
|
||||||
### Slot
|
### Slot
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user