feat(Field): add new props auto-focus & disable-default-padding & cursor (#2936)

add new event linechange

fix #2189
This commit is contained in:
rex 2020-04-01 15:42:00 +08:00 committed by GitHub
parent 92de5e9e34
commit eb586dda23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 227 additions and 129 deletions

View File

@ -43,6 +43,7 @@ Page({
``` ```
### 自定义类型 ### 自定义类型
根据`type`属性定义不同类型的输入框 根据`type`属性定义不同类型的输入框
```html ```html
@ -83,6 +84,7 @@ Page({
``` ```
### 错误提示 ### 错误提示
通过`error`或者`error-message`属性增加对应的错误提示 通过`error`或者`error-message`属性增加对应的错误提示
```html ```html
@ -102,21 +104,24 @@ Page({
/> />
</van-cell-group> </van-cell-group>
``` ```
### 内容对齐方式 ### 内容对齐方式
可以通过`input-align`属性设置内容的对齐方式 可以通过`input-align`属性设置内容的对齐方式
```html ```html
<van-cell-group> <van-cell-group>
<van-field <van-field
value="{{ username3 }}" value="{{ username3 }}"
label="用户名" label="用户名"
placeholder="请输入用户名" placeholder="请输入用户名"
input-align="right" input-align="right"
/> />
</van-cell-group> </van-cell-group>
``` ```
### 高度自适应 ### 高度自适应
对于 textarea可以通过`autosize`属性设置高度自适应 对于 textarea可以通过`autosize`属性设置高度自适应
```html ```html
@ -133,6 +138,7 @@ Page({
``` ```
### 插入按钮 ### 插入按钮
通过 button slot 可以在输入框尾部插入按钮 通过 button slot 可以在输入框尾部插入按钮
```html ```html
@ -146,7 +152,9 @@ Page({
border="{{ false }}" border="{{ false }}"
use-button-slot use-button-slot
> >
<van-button slot="button" size="small" type="primary">发送验证码</van-button> <van-button slot="button" size="small" type="primary"
>发送验证码</van-button
>
</van-field> </van-field>
</van-cell-group> </van-cell-group>
``` ```
@ -166,7 +174,7 @@ Page({
微信小程序的 textarea 组件在 Android 和 iOS 中默认样式不同,在 iOS 中会有默认的 `padding`,且无法置 0。 微信小程序的 textarea 组件在 Android 和 iOS 中默认样式不同,在 iOS 中会有默认的 `padding`,且无法置 0。
同时 `placeholder-style``vertical-align``line-height` 等大量css属性都不生效。 同时 `placeholder-style``vertical-align``line-height` 等大量 css 属性都不生效。
这一系列的问题导致了 placeholder 在真机上可能会出现偏移。@vant/weapp 已经尽量抹平 textarea 在不同环境下的差异。 这一系列的问题导致了 placeholder 在真机上可能会出现偏移。@vant/weapp 已经尽量抹平 textarea 在不同环境下的差异。
@ -182,70 +190,75 @@ Page({
### Props ### Props
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|-----------|-----------|-----------|-------------| | ----------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------- | ------- |
| name | 在表单内提交时的标识符 | *string* | - | | name | 在表单内提交时的标识符 | _string_ | - |
| label | 输入框左侧文本 | *string* | - | | label | 输入框左侧文本 | _string_ | - |
| size | 单元格大小,可选值为 `large` | *string* | - | | size | 单元格大小,可选值为 `large` | _string_ | - |
| value | 当前输入的值 | *string \| number* | - | | value | 当前输入的值 | _string \| number_ | - |
| type | 可设置为任意原生类型, 如 `number` `idcard` `textarea` `digit` | *string* | `text` | | type | 可设置为任意原生类型, 如 `number` `idcard` `textarea` `digit` | _string_ | `text` |
| fixed | 如果 type 为 `textarea` 且在一个 `position:fixed` 的区域,需要显示指定属性 fixed 为 true | *boolean* | `false` | | fixed | 如果 type 为 `textarea` 且在一个 `position:fixed` 的区域,需要显示指定属性 fixed 为 true | _boolean_ | `false` |
| focus | 获取焦点 | *boolean* | `false` | | focus | 获取焦点 | _boolean_ | `false` |
| border | 是否显示内边框 | *boolean* | `true` | | border | 是否显示内边框 | _boolean_ | `true` |
| disabled | 是否禁用输入框 | *boolean* | `false` | | disabled | 是否禁用输入框 | _boolean_ | `false` |
| readonly | 是否只读 | *boolean* | `false` | | readonly | 是否只读 | _boolean_ | `false` |
| clearable | 是否启用清除控件 | *boolean* | `false` | | clearable | 是否启用清除控件 | _boolean_ | `false` |
| clickable | 是否开启点击反馈 | *boolean* | `false` | | clickable | 是否开启点击反馈 | _boolean_ | `false` |
| required | 是否显示表单必填星号 | *boolean* | `false` | | required | 是否显示表单必填星号 | _boolean_ | `false` |
| password | 是否是密码类型 | *boolean* | `false` | | password | 是否是密码类型 | _boolean_ | `false` |
| title-width | 标题宽度 | *string* | `90px` | | title-width | 标题宽度 | _string_ | `90px` |
| maxlength | 最大输入长度,设置为 -1 的时候不限制最大长度 | *number* | `-1` | | maxlength | 最大输入长度,设置为 -1 的时候不限制最大长度 | _number_ | `-1` |
| placeholder | 输入框为空时占位符 | *string* | - | | placeholder | 输入框为空时占位符 | _string_ | - |
| placeholder-style | 指定 placeholder 的样式 | *string* | - | | placeholder-style | 指定 placeholder 的样式 | _string_ | - |
| custom-style | 自定义样式 | *string* | - | | custom-style | 自定义样式 | _string_ | - |
| is-link | 是否展示右侧箭头并开启点击反馈 | *boolean* | `false` | | is-link | 是否展示右侧箭头并开启点击反馈 | _boolean_ | `false` |
| arrow-direction | 箭头方向,可选值为 `left` `up` `down` | *string* | - | | arrow-direction | 箭头方向,可选值为 `left` `up` `down` | _string_ | - |
| show-word-limit | 是否显示字数统计,需要设置`maxlength`属性 | *boolean* | `false` | | show-word-limit | 是否显示字数统计,需要设置`maxlength`属性 | _boolean_ | `false` |
| error | 是否将输入内容标红 | *boolean* | `false` | | error | 是否将输入内容标红 | _boolean_ | `false` |
| error-message | 底部错误提示文案,为空时不展示 | *string* | `''` | | error-message | 底部错误提示文案,为空时不展示 | _string_ | `''` |
| error-message-align | 底部错误提示文案对齐方式,可选值为 `center` `right` | *string* | `''` | | error-message-align | 底部错误提示文案对齐方式,可选值为 `center` `right` | _string_ | `''` |
| input-align | 输入框内容对齐方式,可选值为 `center` `right` | *string* | `left` | | input-align | 输入框内容对齐方式,可选值为 `center` `right` | _string_ | `left` |
| autosize | 是否自适应内容高度,只对 textarea 有效,<br>可传入对象,如 { maxHeight: 100, minHeight: 50 }<br>单位为`px` | *boolean \| object* | `false` | | autosize | 是否自适应内容高度,只对 textarea 有效,<br>可传入对象,如 { maxHeight: 100, minHeight: 50 }<br>单位为`px` | _boolean \| object_ | `false` |
| left-icon | 左侧图标名称或图片链接,可选值见 [Icon 组件](#/icon) | *string* | - | | left-icon | 左侧图标名称或图片链接,可选值见 [Icon 组件](#/icon) | _string_ | - |
| right-icon | 右侧图标名称或图片链接,可选值见 [Icon 组件](#/icon) | *string* | - | | right-icon | 右侧图标名称或图片链接,可选值见 [Icon 组件](#/icon) | _string_ | - |
| confirm-type | 设置键盘右下角按钮的文字,仅在 type='text' 时生效 | *string* | `done` | | confirm-type | 设置键盘右下角按钮的文字,仅在 type='text' 时生效 | _string_ | `done` |
| confirm-hold | 点击键盘右下角按钮时是否保持键盘不收起,在 type='textarea' 时无效 | *boolean* | `false` | | confirm-hold | 点击键盘右下角按钮时是否保持键盘不收起,在 type='textarea' 时无效 | _boolean_ | `false` |
| hold-keyboard | focus 时,点击页面的时候不收起键盘 | *boolean* | `false` | | hold-keyboard | focus 时,点击页面的时候不收起键盘 | _boolean_ | `false` |
| cursor-spacing | 输入框聚焦时底部与键盘的距离 | *number* | `50` | | cursor-spacing | 输入框聚焦时底部与键盘的距离 | _number_ | `50` |
| adjust-position | 键盘弹起时,是否自动上推页面 | *boolean* | `true` | | adjust-position | 键盘弹起时,是否自动上推页面 | _boolean_ | `true` |
| show-confirm-bar | 是否显示键盘上方带有”完成“按钮那一栏,只对 textarea 有效 | *boolean* | `true` | | show-confirm-bar | 是否显示键盘上方带有”完成“按钮那一栏,只对 textarea 有效 | _boolean_ | `true` |
| selection-start | 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 | *number* | `-1` | | selection-start | 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 | _number_ | `-1` |
| selection-end | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | *number* | `-1` | | selection-end | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | _number_ | `-1` |
| auto-focus | 自动聚焦,拉起键盘 | _boolean_ | `false` |
| disable-default-padding | 是否去掉 iOS 下的默认内边距,只对 textarea 有效 | _boolean_ | `true` |
| cursor | 指定 focus 时的光标位置 | _number_ | `-1` |
### Events ### Events
| 事件 | 说明 | 回调参数 | | 事件 | 说明 | 回调参数 |
|-----------|-----------|-----------| | ------------------------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| bind:input | 输入内容时触发 | value: 当前输入值 | | bind:input | 输入内容时触发 | value: 当前输入值 |
| bind:change | 输入内容时触发 | value: 当前输入值 | | bind:change | 输入内容时触发 | value: 当前输入值 |
| bind:confirm | 点击完成按钮时触发 | value: 当前输入值 | | bind:confirm | 点击完成按钮时触发 | value: 当前输入值 |
| bind:click-icon | 点击尾部图标时触发 | - | | bind:click-icon | 点击尾部图标时触发 | - |
| bind:focus | 输入框聚焦时触发 | event.detail.value: 当前输入值; <br>event.detail.height: 键盘高度 | | bind:focus | 输入框聚焦时触发 | event.detail.value: 当前输入值; <br>event.detail.height: 键盘高度 |
| bind:blur | 输入框失焦时触发 | event.detail.value: 当前输入值; <br>event.detail.cursor: 游标位置(如果 `type` 不为 `textarea`,值为 `0`) | | bind:blur | 输入框失焦时触发 | event.detail.value: 当前输入值; <br>event.detail.cursor: 游标位置(如果 `type` 不为 `textarea`,值为 `0`) |
| bind:clear | 点击清空控件时触发 | - | | bind:clear | 点击清空控件时触发 | - |
| bind:linechange | 输入框行数变化时调用,只对 textarea 有效 | event.detail = { height: 0, heightRpx: 0, lineCount: 0 } |
| bind:keyboardheightchange | 键盘高度发生变化的时候触发此事件 | event.detail = { height: height, duration: duration } |
### Slot ### Slot
| 名称 | 说明 | | 名称 | 说明 |
|-----------|-----------| | ---------- | ----------------------------------------------- |
| label | 自定义输入框标签,如果设置了`label`属性则不生效 | | label | 自定义输入框标签,如果设置了`label`属性则不生效 |
| left-icon | 自定义输入框头部图标 | | left-icon | 自定义输入框头部图标 |
| right-icon | 自定义输入框尾部图标 | | right-icon | 自定义输入框尾部图标 |
| button | 自定义输入框尾部按钮 | | button | 自定义输入框尾部按钮 |
### 外部样式类 ### 外部样式类
| 类名 | 说明 | | 类名 | 说明 |
|-----------|-----------| | ---------------- | -------------- |
| input-class | 输入框样式类 | | input-class | 输入框样式类 |
| right-icon-class | 右侧图标样式类 | | right-icon-class | 右侧图标样式类 |

View File

@ -1,5 +1,6 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
import { commonProps, inputProps, textareaProps } from './props';
VantComponent({ VantComponent({
field: true, field: true,
@ -7,63 +8,35 @@ VantComponent({
classes: ['input-class', 'right-icon-class'], classes: ['input-class', 'right-icon-class'],
props: { props: {
...commonProps,
...inputProps,
...textareaProps,
size: String, size: String,
icon: String, icon: String,
label: String, label: String,
error: Boolean, error: Boolean,
fixed: Boolean,
focus: Boolean,
center: Boolean, center: Boolean,
isLink: Boolean, isLink: Boolean,
leftIcon: String, leftIcon: String,
rightIcon: String, rightIcon: String,
disabled: Boolean,
autosize: [Boolean, Object], autosize: [Boolean, Object],
readonly: Boolean, readonly: {
type: Boolean,
observer: 'setShowClear'
},
required: Boolean, required: Boolean,
password: Boolean,
iconClass: String, iconClass: String,
clearable: Boolean, clearable: {
type: Boolean,
observer: 'setShowClear'
},
clickable: Boolean, clickable: Boolean,
inputAlign: String, inputAlign: String,
placeholder: String,
customStyle: String, customStyle: String,
confirmType: String,
confirmHold: Boolean,
holdKeyboard: Boolean,
errorMessage: String, errorMessage: String,
arrowDirection: String, arrowDirection: String,
showWordLimit: Boolean, showWordLimit: Boolean,
placeholderStyle: String,
errorMessageAlign: String, errorMessageAlign: String,
selectionEnd: {
type: Number,
value: -1
},
selectionStart: {
type: Number,
value: -1
},
showConfirmBar: {
type: Boolean,
value: true
},
adjustPosition: {
type: Boolean,
value: true
},
cursorSpacing: {
type: Number,
value: 50
},
maxlength: {
type: Number,
value: -1
},
type: {
type: String,
value: 'text'
},
border: { border: {
type: Boolean, type: Boolean,
value: true value: true
@ -76,26 +49,33 @@ VantComponent({
data: { data: {
focused: false, focused: false,
innerValue: '',
showClear: false
},
created() {
this.value = this.data.value;
this.setData({ innerValue: this.value });
}, },
methods: { methods: {
onInput(event: Weapp.Event) { onInput(event: Weapp.Event) {
const { value = '' } = event.detail || {}; const { value = '' } = event.detail || {};
this.setData({ value }); this.value = value;
this.setShowClear();
wx.nextTick(() => { this.emitChange();
this.emitChange(value);
});
}, },
onFocus(event: Weapp.Event) { onFocus(event: Weapp.Event) {
this.setData({ focused: true }); this.focused = true;
this.setShowClear();
this.$emit('focus', event.detail); this.$emit('focus', event.detail);
}, },
onBlur(event: Weapp.Event) { onBlur(event: Weapp.Event) {
this.setData({ focused: false }); this.focused = false;
this.setShowClear();
this.$emit('blur', event.detail); this.$emit('blur', event.detail);
}, },
@ -104,21 +84,53 @@ VantComponent({
}, },
onClear() { onClear() {
this.setData({ value: '' }); this.setData({ innerValue: '' });
this.value = '';
this.setShowClear();
wx.nextTick(() => { wx.nextTick(() => {
this.emitChange(''); this.emitChange();
this.$emit('clear', ''); this.$emit('clear', '');
}); });
}, },
onConfirm() { onConfirm(event) {
this.$emit('confirm', this.data.value); const { value = '' } = event.detail || {};
this.value = value;
this.setShowClear();
this.$emit('confirm', value);
}, },
emitChange(value) { setValue(value) {
this.$emit('input', value); this.value = value;
this.$emit('change', value); this.setShowClear();
if (value === '') {
this.setData({ innerValue: '' });
}
this.emitChange();
},
onLineChange(event) {
this.$emit('linechange', event.detail);
},
onKeyboardHeightChange(event) {
this.$emit('keyboardheightchange', event.detail);
},
emitChange() {
this.$emit('input', this.value);
this.$emit('change', this.value);
},
setShowClear() {
const { clearable, readonly } = this.data;
const { focused, value } = this;
this.setData({
showClear: clearable && focused && !!value && !readonly
});
}, },
noop() {} noop() {}

View File

@ -23,7 +23,9 @@
class="input-class {{ utils.bem('field__input', [inputAlign, type, { disabled, error }]) }}" class="input-class {{ utils.bem('field__input', [inputAlign, type, { disabled, error }]) }}"
fixed="{{ fixed }}" fixed="{{ fixed }}"
focus="{{ focus }}" focus="{{ focus }}"
value="{{ value }}" cursor="{{ cursor }}"
value="{{ innerValue }}"
auto-focus="{{ autoFocus }}"
disabled="{{ disabled || readonly }}" disabled="{{ disabled || readonly }}"
maxlength="{{ maxlength }}" maxlength="{{ maxlength }}"
placeholder="{{ placeholder }}" placeholder="{{ placeholder }}"
@ -37,10 +39,13 @@
hold-keyboard="{{ holdKeyboard }}" hold-keyboard="{{ holdKeyboard }}"
selection-end="{{ selectionEnd }}" selection-end="{{ selectionEnd }}"
selection-start="{{ selectionStart }}" selection-start="{{ selectionStart }}"
disable-default-padding="{{ disableDefaultPadding }}"
bindinput="onInput" bindinput="onInput"
bind:blur="onBlur" bindblur="onBlur"
bind:focus="onFocus" bindfocus="onFocus"
bind:confirm="onConfirm" bindconfirm="onConfirm"
bindlinechange="onLineChange"
bindkeyboardheightchange="onKeyboardHeightChange"
> >
</textarea> </textarea>
<input <input
@ -48,7 +53,9 @@
class="input-class {{ utils.bem('field__input', [inputAlign, { disabled, error }]) }}" class="input-class {{ utils.bem('field__input', [inputAlign, { disabled, error }]) }}"
type="{{ type }}" type="{{ type }}"
focus="{{ focus }}" focus="{{ focus }}"
value="{{ value }}" cursor="{{ cursor }}"
value="{{ innerValue }}"
auto-focus="{{ autoFocus }}"
disabled="{{ disabled || readonly }}" disabled="{{ disabled || readonly }}"
maxlength="{{ maxlength }}" maxlength="{{ maxlength }}"
placeholder="{{ placeholder }}" placeholder="{{ placeholder }}"
@ -63,12 +70,13 @@
selection-start="{{ selectionStart }}" selection-start="{{ selectionStart }}"
password="{{ password || type === 'password' }}" password="{{ password || type === 'password' }}"
bindinput="onInput" bindinput="onInput"
bind:blur="onBlur" bindblur="onBlur"
bind:focus="onFocus" bindfocus="onFocus"
bind:confirm="onConfirm" bindconfirm="onConfirm"
bindkeyboardheightchange="onKeyboardHeightChange"
/> />
<van-icon <van-icon
wx:if="{{ clearable && focused && value && !readonly }}" wx:if="{{ showClear }}"
name="clear" name="clear"
class="van-field__clear-root van-field__icon-root" class="van-field__clear-root van-field__icon-root"
catch:touchstart="onClear" catch:touchstart="onClear"

65
packages/field/props.ts Normal file
View File

@ -0,0 +1,65 @@
export const commonProps = {
value: {
type: String,
observer(value: string) {
if (value !== this.value) {
this.setData({ innerValue: value });
this.value = value;
}
}
},
placeholder: String,
placeholderStyle: String,
placeholderClass: String,
disabled: Boolean,
maxlength: {
type: Number,
value: -1
},
cursorSpacing: {
type: Number,
value: 50
},
autoFocus: Boolean,
focus: Boolean,
cursor: {
type: Number,
value: -1
},
selectionStart: {
type: Number,
value: -1
},
selectionEnd: {
type: Number,
value: -1
},
adjustPosition: {
type: Boolean,
value: true
},
holdKeyboard: Boolean
};
export const inputProps = {
type: {
type: String,
value: 'text'
},
password: Boolean,
confirmType: String,
confirmHold: Boolean
};
export const textareaProps = {
autoHeight: Boolean,
fixed: Boolean,
showConfirmBar: {
type: Boolean,
value: true
},
disableDefaultPadding: {
type: Boolean,
value: true
},
};