feat(Field): improve number keyboard

This commit is contained in:
chenjiahan
2020-03-11 11:23:24 +08:00
parent 1f4ab5bb56
commit e89baa12ae
5 changed files with 13 additions and 19 deletions
+2 -2
View File
@@ -45,9 +45,9 @@ export default {
<van-field v-model="text" label="文本" />
<!-- 输入手机号,调起手机号键盘 -->
<van-field v-model="tel" type="tel" label="手机号" />
<!-- 允许输入整数,调起数字键盘 -->
<!-- 允许输入整数,调起数字键盘 -->
<van-field v-model="digit" type="digit" label="整数" />
<!-- 允许输入数字,调起键盘 -->
<!-- 允许输入数字,调起带符号的纯数字键盘 -->
<van-field v-model="number" type="number" label="数字" />
<!-- 输入密码 -->
<van-field v-model="password" type="password" label="密码" />
+7 -11
View File
@@ -1,6 +1,5 @@
// Utils
import { formatNumber } from './utils';
import { isIOS } from '../utils/validate/system';
import { preventDefault } from '../utils/dom/event';
import { resetScroll } from '../utils/dom/reset-scroll';
import {
@@ -419,24 +418,21 @@ export default createComponent({
}
let inputType = type;
let inputMode;
// type="number" is weired in iOS
// type="number" is weired in iOS, and can't prevent dot in Android
// so use inputmode to set keyboard in mordern browers
if (type === 'number') {
inputType = 'text';
inputMode = 'decimal';
}
if (type === 'digit') {
// set pattern to show number keyboard in iOS
if (isIOS()) {
inputType = 'number';
inputProps.attrs.pattern = '\\d*';
// cannot prevent dot when type is number in Android, so use tel
} else {
inputType = 'tel';
}
inputType = 'tel';
inputMode = 'numeric';
}
return <input type={inputType} {...inputProps} />;
return <input type={inputType} inputmode={inputMode} {...inputProps} />;
},
genLeftIcon() {
@@ -27,13 +27,13 @@ exports[`renders demo correctly 1`] = `
<div class="van-cell van-field">
<div class="van-cell__title van-field__label"><span>整数</span></div>
<div class="van-cell__value van-field__value">
<div class="van-field__body"><input type="tel" placeholder="请输入整数" class="van-field__control"></div>
<div class="van-field__body"><input type="tel" inputmode="numeric" placeholder="请输入整数" class="van-field__control"></div>
</div>
</div>
<div class="van-cell van-field">
<div class="van-cell__title van-field__label"><span>数字</span></div>
<div class="van-cell__value van-field__value">
<div class="van-field__body"><input type="text" placeholder="请输入数字(支持小数)" class="van-field__control"></div>
<div class="van-field__body"><input type="text" inputmode="decimal" placeholder="请输入数字(支持小数)" class="van-field__control"></div>
</div>
</div>
<div class="van-cell van-field">