mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 10:20:19 +08:00
[Improvement] Field: add left-icon prop (#1092)
This commit is contained in:
parent
e73e950662
commit
178b188439
@ -120,7 +120,8 @@ Filed support all native properties of input tag,such as `maxlength`、`placeh
|
|||||||
| error | Whether to show error info | `Boolean` | `false` |
|
| error | Whether to show error info | `Boolean` | `false` |
|
||||||
| error-message | Error message | `String` | `''` |
|
| error-message | Error message | `String` | `''` |
|
||||||
| autosize | Textarea auto resize,can accpet an object, e.g. { maxHeight: 100, minHeight: 50 } | `Boolean | Object` | `false` |
|
| autosize | Textarea auto resize,can accpet an object, e.g. { maxHeight: 100, minHeight: 50 } | `Boolean | Object` | `false` |
|
||||||
| icon | Right side Icon name | `String` | - |
|
| icon | Right side icon name | `String` | - |
|
||||||
|
| left-icon | Left side icon name | `String` | - |
|
||||||
|
|
||||||
### Event
|
### Event
|
||||||
Filed support all native events of input tag,such as `focus`、`blur`、`keypress`
|
Filed support all native events of input tag,such as `focus`、`blur`、`keypress`
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<cell
|
<cell
|
||||||
|
:icon="leftIcon"
|
||||||
:title="label"
|
:title="label"
|
||||||
:center="center"
|
:center="center"
|
||||||
:border="border"
|
:border="border"
|
||||||
@ -64,7 +65,9 @@ export default create({
|
|||||||
label: String,
|
label: String,
|
||||||
error: Boolean,
|
error: Boolean,
|
||||||
center: Boolean,
|
center: Boolean,
|
||||||
|
leftIcon: String,
|
||||||
required: Boolean,
|
required: Boolean,
|
||||||
|
onIconClick: Function,
|
||||||
autosize: [Boolean, Object],
|
autosize: [Boolean, Object],
|
||||||
errorMessage: String,
|
errorMessage: String,
|
||||||
type: {
|
type: {
|
||||||
@ -74,10 +77,6 @@ export default create({
|
|||||||
border: {
|
border: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true
|
default: true
|
||||||
},
|
|
||||||
onIconClick: {
|
|
||||||
type: Function,
|
|
||||||
default: () => {}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -112,7 +111,7 @@ export default create({
|
|||||||
|
|
||||||
onClickIcon() {
|
onClickIcon() {
|
||||||
this.$emit('click-icon');
|
this.$emit('click-icon');
|
||||||
this.onIconClick();
|
this.onIconClick && this.onIconClick();
|
||||||
},
|
},
|
||||||
|
|
||||||
onKeypress(event) {
|
onKeypress(event) {
|
||||||
|
15
packages/field/test/__snapshots__/field.spec.js.snap
Normal file
15
packages/field/test/__snapshots__/field.spec.js.snap
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`render textarea 1`] = `
|
||||||
|
<div class="van-cell van-hairline van-field">
|
||||||
|
<!---->
|
||||||
|
<!---->
|
||||||
|
<div class="van-cell__value van-cell__value--alone">
|
||||||
|
<textarea class="van-field__control" style="height: auto;"></textarea>
|
||||||
|
<!---->
|
||||||
|
<!---->
|
||||||
|
<!---->
|
||||||
|
</div>
|
||||||
|
<!---->
|
||||||
|
</div>
|
||||||
|
`;
|
101
packages/field/test/field.spec.js
Normal file
101
packages/field/test/field.spec.js
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
import Field from '../';
|
||||||
|
import { mount } from '@vue/test-utils';
|
||||||
|
|
||||||
|
test('input event', () => {
|
||||||
|
const wrapper = mount(Field);
|
||||||
|
const input = wrapper.find('input');
|
||||||
|
|
||||||
|
input.element.value = '1';
|
||||||
|
input.trigger('input');
|
||||||
|
expect(wrapper.emitted('input')[0][0]).toEqual('1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('click icon event', () => {
|
||||||
|
const onIconClick = jest.fn();
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
propsData: {
|
||||||
|
icon: 'search',
|
||||||
|
onIconClick
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wrapper.find('.van-field__icon').trigger('touchstart');
|
||||||
|
expect(wrapper.emitted('click-icon')).toBeTruthy();
|
||||||
|
expect(onIconClick.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('keypress event', () => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
propsData: {
|
||||||
|
value: '',
|
||||||
|
type: 'number'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
const { calls } = fn.mock;
|
||||||
|
const press = keyCode => wrapper.vm.onKeypress({
|
||||||
|
keyCode,
|
||||||
|
preventDefault: fn
|
||||||
|
});
|
||||||
|
|
||||||
|
press(0);
|
||||||
|
expect(calls.length).toBe(1);
|
||||||
|
|
||||||
|
press(50);
|
||||||
|
expect(calls.length).toBe(1);
|
||||||
|
|
||||||
|
wrapper.vm.value = '0.1';
|
||||||
|
press(46);
|
||||||
|
expect(calls.length).toBe(2);
|
||||||
|
|
||||||
|
wrapper.vm.type = 'text';
|
||||||
|
press(0);
|
||||||
|
expect(calls.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('render textarea', done => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
propsData: {
|
||||||
|
type: 'textarea',
|
||||||
|
autosize: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(wrapper.html()).toMatchSnapshot();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('autosize textarea field', () => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
propsData: {
|
||||||
|
type: 'textarea',
|
||||||
|
autosize: {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const longText = '1'.repeat(20);
|
||||||
|
const textarea = wrapper.find('.van-field__control');
|
||||||
|
|
||||||
|
wrapper.vm.value = longText;
|
||||||
|
expect(textarea.element.value).toEqual(longText);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('autosize object', done => {
|
||||||
|
const wrapper = mount(Field, {
|
||||||
|
propsData: {
|
||||||
|
type: 'textarea',
|
||||||
|
autosize: {
|
||||||
|
maxHeight: 100,
|
||||||
|
minHeight: 50
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const textarea = wrapper.find('.van-field__control');
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(textarea.element.style.height).toEqual(('50px'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
@ -123,6 +123,7 @@ Filed 默认支持 Input 标签所有的原生属性,比如 `maxlength`、`pla
|
|||||||
| error-message | 底部错误提示文案 | `String` | `''` |
|
| error-message | 底部错误提示文案 | `String` | `''` |
|
||||||
| autosize | 自适应内容高度,只对 textarea 有效,可传入对象,如 { maxHeight: 100, minHeight: 50 },单位为 px | `Boolean | Object` | `false` |
|
| autosize | 自适应内容高度,只对 textarea 有效,可传入对象,如 { maxHeight: 100, minHeight: 50 },单位为 px | `Boolean | Object` | `false` |
|
||||||
| icon | 输入框尾部图标 (可选值见 Icon 组件) | `String` | - |
|
| icon | 输入框尾部图标 (可选值见 Icon 组件) | `String` | - |
|
||||||
|
| left-icon | 输入框左侧图标 (可选值见 Icon 组件) | `String` | - |
|
||||||
|
|
||||||
### Event
|
### Event
|
||||||
Filed 默认支持 Input 标签所有的原生事件,如 `focus`、`blur`、`keypress` 等
|
Filed 默认支持 Input 标签所有的原生事件,如 `focus`、`blur`、`keypress` 等
|
||||||
|
Loading…
x
Reference in New Issue
Block a user