網上一搜一大堆類似但大多都相對簡單,適用的場景並不多。多數也不支援賦值 不支援單個刪除更改
我就借鑑了一下網上文章的思路,為了達到自己想要的效果做了相對應的更改。
效果圖如下:
直接上程式碼!
WXML程式碼:
點選檢視程式碼
<!-- 車牌號碼輸入框 -->
<view wx:for="{{carNum}}" wx:key="index" class="text-center inline-block" bind:tap='openKeyboard'>
<!-- 點 -->
<text wx:if="{{ index === 2 }}" class="inline-block ml-5 mr-5 fs-40">.</text>
<!-- 普通車牌 -->
<input wx:if="{{ index !== 7 }}" model:value="{{ item.value }}" data-index="{{index}}" class="com-carNumber-item {{ item.focus ? 'com-focus' : ''}}" />
<!-- 新能源車牌 -->
<input wx:else model:value="{{!showNewPower ? '+' : item.value }}" data-index="{{index}}" class="com-carNumber-item {{showNewPower ? item.focus ? 'com-focus' : '': 'com-carNumber-item-newpower'}}" />
</view>
<!-- 虛擬鍵盤 -->
<view class="com-keyboard" hidden='{{!KeyboardState}}'>
<view class="com-keyboardConfirm flex-center">
<text class="com-vehicleTip bold flex-center">請輸入車牌號</text>
<view class="com-separator"></view>
<view class="com-keyboardConfirm_btn bold" bindtap='confirmKeyboard'>完成</view>
</view>
<!-- 省份簡寫鍵盤 -->
<view class="com-keyboard-item" hidden="{{ focusIndex !== 0 }}">
<view wx:for="{{provinces}}" wx:key="index" class="com-keyboard-line">
<view wx:for="{{item}}" wx:key="index" wx:for-item="char" data-val="{{char}}" class="com-keyboard-btn" bindtap='bindChoose'>{{char}}</view>
</view>
</view>
<!-- 車牌號碼選擇鍵盤 -->
<view class="com-keyboard-item com-carNumber" hidden="{{focusIndex === 0}}">
<!-- 數字鍵盤 -->
<view wx:for="{{numbers}}" wx:key="index" class="com-keyboard-line">
<view wx:for="{{item}}" wx:key="index" wx:for-item="char" data-val="{{char}}" class="{{ focusIndex !== 1 ? 'com-keyboard-btn' : 'com-keyboard-disabled-btn' }}" bindtap="{{ focusIndex !== 1 ? 'bindChoose' : '' }}">{{char}}</view>
</view>
<!-- 字母鍵盤 -->
<view wx:for="{{letters}}" wx:key="index" class="com-keyboard-line">
<view wx:for="{{item}}" wx:key="index" wx:for-item="char" data-val="{{char}}" class="{{ focusIndex !== 6 && (char === '掛' || char === '港' || char === '澳' || char === '學') ? 'com-keyboard-disabled-btn' : 'com-keyboard-btn'}}" bindtap="bindChoose">{{char}}</view>
</view>
<view class="com-keyboard-del" bindtap='bindDelChoose'>
<text class="fs-40">刪除</text>
</view>
</view>
</view>
JS程式碼:
點選檢視程式碼
import { isEmpty, validate } from '../../utils/util';
const icon = 'none', duration = 3000
const component = {
properties: {
vehicleNo: { type: String, value: undefined } // 車牌號
},
data: {
provinces: [ // 省份鍵盤
['京', '滬', '粵', '津', '冀', '晉', '蒙', '遼', '吉', '黑'],
['蘇', '浙', '皖', '閩', '贛', '魯', '豫', '鄂', '湘'],
['桂', '瓊', '渝', '川', '貴', '雲', '藏'],
['陝', '甘', '青', '寧', '新']
],
numbers: [ // 數字鍵盤
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
],
letters: [ // 字母鍵盤
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'],
['L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'],
['W', 'X', 'Y', 'Z', '掛', '港', '澳', '學']
],
specialChar: ['掛', '港', '澳', '學'], // 特殊字元
carNum: Array(8).fill({ value: undefined, focus: false }),
focusIndex: 0, // 輸入庫焦點索引
showNewPower: false, // 是否新能源車牌
KeyboardState: false // 是否彈出虛擬鍵盤
},
attached() {
const v = this
const vehcles = v.data.vehicleNo.split('')
const carNum = v.data.carNum
vehcles.forEach((m, i) => { carNum[i].value = m })
v.setData({ carNum })
},
/**
* 元件的方法列表
*/
methods: {
// 車牌輸入框點選展開車牌鍵盤
openKeyboard(e) {
const v = this
const focusIndex = e.target.dataset.index
let carNum = v.data.carNum
let showNewPower = v.data.showNewPower
// 新增新能源尾數
if (focusIndex === 7) {
if (isEmpty(carNum[6].value)) return wx.showToast({ title: '為新能源車牌時,前幾位車牌號不能為空', icon, duration })
if (v.data.specialChar.includes(carNum[6].value)) return wx.showToast({ title: `為新能源車牌時,第6位車牌號不能為'${carNum[6].value}'`, icon, duration })
showNewPower = true
v.setData({ showNewPower, KeyboardState: true })
}
// 當前點選獲得焦點,其餘失去焦點
carNum[focusIndex].focus = true
carNum = carNum.map((item, index) => {
return { ...item, focus: index === focusIndex ? item.focus : false }
})
// 點選索引不為新能源且新能源無值時
if (focusIndex !== 7 && isEmpty(carNum[7].value)) showNewPower = false
v.setData({ KeyboardState: true, focusIndex, carNum, showNewPower })
},
// 鍵盤選中點選設定
bindChoose(e) {
const v = this
const val = e.target.dataset.val
const carNum = v.data.carNum
let focusIndex = v.data.focusIndex
if (focusIndex !== 6 && v.data.specialChar.includes(val)) return
// 當前選中車牌無值時更新為輸入值
if (isEmpty(carNum[focusIndex].value)) {
carNum[focusIndex].value = val
carNum[focusIndex].focus = false
const validate = v.data.showNewPower ? 7 : 6
// 上一位車牌取消聚焦
if (focusIndex < validate) focusIndex++
carNum[focusIndex].focus = true
v.setData({ carNum, focusIndex })
}
},
// 刪除
bindDelChoose() {
const v = this
const carNum = v.data.carNum
let focusIndex = v.data.focusIndex
let showNewPower = v.data.showNewPower
// 如果刪除第6位後繼續刪除 則focusIndex後退一位並刪除第5位車牌 依此類推
// 當刪除至省份位車牌時,介面會控制取消刪除按鈕
if (isEmpty(carNum[focusIndex].value)) {
// 如果當前索引是新能源車牌
if (focusIndex == 7) showNewPower = false
focusIndex--
carNum[focusIndex].value = undefined
carNum[focusIndex].focus = true
// 後一位車牌取消聚焦
carNum[focusIndex + 1].focus = false
}
else {
carNum[v.data.focusIndex].value = undefined
carNum[v.data.focusIndex].focus = true
}
v.setData({ carNum, focusIndex, showNewPower })
},
// 關閉虛擬鍵盤
confirmKeyboard() {
const v = this
const vheicleNo = v.data.carNum.map(m => m.value).join('');
if (!validate.vehicle(vheicleNo)) return wx.showToast({ title: '請輸入正確的車牌', icon, duration })
let carNum = v.data.carNum
carNum = carNum.map(item => { return { ...item, focus: false } })
v.setData({ carNum, KeyboardState: false })
v.triggerEvent('confirm', { vheicleNo })
}
}
}
Component(component);
Component(component);
WXSS程式碼:
點選檢視程式碼
/* 車牌號碼 */
.com-carNumber-item {
width: 55rpx;
height: 60rpx;
font-size: 18px;
border: 2rpx solid #CCCCCC;
border-radius: 8rpx;
display: inline-block;
line-height: 30px;
margin: 0 2rpx;
vertical-align: middle;
caret-color: transparent;
}
.com-focus {
border: 4rpx solid #A8BFF3;
}
/* 新能源 */
.com-carNumber-item-newpower {
border: 2rpx dashed #19e622;
background-color: #e1fde2;
color: #19e622;
font-size: 25px;
line-height: 45rpx;
}
/* 虛擬鍵盤 */
.com-keyboard {
height: auto;
background: #d1d5d9;
position: fixed;
bottom: 0;
width: 100%;
left: 0;
padding-bottom: 30rpx;
}
.com-keyboard-item {
padding: 10rpx 0 5rpx 0;
position: relative;
display: block;
}
.com-vehicleTip {
flex: 1;
color: rgba(150, 179, 253, 1);
}
.com-separator {
width: 1px;
height: 50%;
background-color: #ccc;
margin: 0 10px;
}
.com-keyboardConfirm {
height: 70rpx;
background-color: #f7f7f7;
}
.com-keyboardConfirm_btn {
float: right;
line-height: 70rpx;
font-size: 15px;
margin-right: 30rpx;
color: #FFA500;
}
.com-keyboard-line {
margin: 0 auto;
text-align: center;
}
.com-carNumber .com-keyboard-line {
text-align: left;
margin-left: 5rpx;
}
.com-keyboard-btn {
font-size: 18px;
color: #333333;
background: #fff;
display: inline-block;
padding: 18rpx 0;
width: 63rpx;
text-align: center;
box-shadow: 0 2rpx 0 0 #999999;
border-radius: 10rpx;
margin: 5rpx 6rpx;
}
.com-keyboard-disabled-btn {
font-size: 18px;
color: #333333;
background: #cacaca;
display: inline-block;
padding: 18rpx 0;
width: 63rpx;
text-align: center;
box-shadow: 0 2rpx 0 0 #999999;
border-radius: 10rpx;
margin: 5rpx 6rpx;
}
.com-keyboard-del {
color: black;
background: #A7B0BC;
display: inline-block;
padding: 10rpx 25rpx;
box-shadow: 0 2rpx 0 0 #999999;
border-radius: 10rpx;
margin: 5rpx;
position: absolute;
bottom: 10rpx;
right: 12rpx;
}
公用樣式程式碼(app.wxss):
點選檢視程式碼
.text-center {
text-align: center;
}
.inline-block {
display: inline-block;
}
.ml-5 {
margin-left: 5rpx;
}
.mr-5 {
margin-right: 5;
}
.fs-40 {
font-size: 40rpx;
}
.bold {
font-weight: bold;
}
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
公用JS程式碼:
點選檢視程式碼
/**
* 是否都是空資料(型別:字串,數值,物件,陣列,布林)
*/
const isEmpty = (...values) => {
const isEmptyFunc = (val) => {
switch (typeof val) {
case 'string':
case 'number': return !val // 數值0為空
case 'object':
// 陣列
if (Array.isArray(val)) return val.length === 0
// null/物件
return val === null || Object.keys(val).length === 0
case 'boolean': return false // 布林型別即非空
// undefined / function
default: return true
}
}
let result = true
values.forEach(m => { result = result && isEmptyFunc(m) })
return result
}
/**
* 驗證
*/
const validate = {
/** 是車牌號 */
vehicle(value) {
let result = false
if (value && [7, 8].includes(value.length)) {
const express = /^[京津滬渝冀豫雲遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陝吉閩貴粵青藏川寧瓊使領A-Z]{1}[A-Z]{1}[A-Z0-9]{4,5}[A-Z0-9掛學警港澳]{1}$/
result = express.test(value)
}
return result
}
}
呼叫方式:
json檔案:
點選檢視程式碼
{
"navigationBarTitleText": "車牌號輸入",
"usingComponents": {
"vehicle-keyboard": "../../../../components/vehicle-keyboard/index"
}
}
js:
點選檢視程式碼
// 車牌輸入元件完成
confirmVehicle(e) { this.setData({ shipNo: e.detail.vheicleNo }) }
wxml:
點選檢視程式碼
<vehicle-keyboard vehicleNo="{{vehicleNo}}" bind:confirm="confirmVehicle"></vehicle-keyboard>
上面gif生成是用 Windows 系統 Win+Shift+S 將操作截圖成影片,再去這個網站 https://www.aconvert.com/cn/video/mp4-to-gif/ 將影片轉成GIF
預覽的時候把圖片下載下來
錄製gif的時候有個小bug,已被我修復。