VueTouchKeyboard——一個模擬鍵盤

功能需求:封裝一個帶有設計好的樣式的輸入組件,輸入方式爲模擬的數字鍵盤。

鍵盤組件爲VueTouchKeyboard,下載方式如下

npm install "vue-touch-keyboard" --save
 
20315386-3dc95db37a53e302.png
image.png

封裝的輸入組件

模板

<template>
  <div v-clickoutside="hide">
    <input v-bind="$attrs"
           @focus="show"
           @blur="submit" />
    <div class="keyboard__warn-mess keyboard__warn">
      <div v-if=warnMessage
           class="">
        {{warnMessage}}
      </div>

    </div>
    <vue-touch-keyboard class="keyboard-nav"
                        :options="options"
                        v-if="visible"
                        :cancel="hide"
                        :layout="layout"
                        :accept="hide"
                        :input="input" />
  </div>
</template>

很簡單,就包含了一個VueTouchKeyboard模擬鍵盤和input輸入框,以及一個div裏放動態的warnMessage
js內容:

<script>
import VueTouchKeyboard from 'vue-touch-keyboard';
import 'vue-touch-keyboard/dist/vue-touch-keyboard.css';
import $ from 'jquery';

const clickoutside = {//自定義命令,功能:點擊鍵盤以外的區域隱藏鍵盤
  // 初始化指令

  bind (el, binding, vnode) {
    function documentHandler (e) {
      // 這裏判斷點擊的元素是否是本身,是本身,則返回
      if (el.contains(e.target)) {
        return false;
      }
      // 判斷指令中是否綁定了函數
      if (binding.expression) {
        // 如果綁定了函數 則調用那個函數,此處binding.value就是handleClose方法
        binding.value(e);
      }
    }
    // 給當前元素綁定個私有變量,方便在unbind中可以解除事件監聽
    el.__vueClickOutside__ = documentHandler;
    document.addEventListener('click', documentHandler);
  },
  update () { },
  unbind (el, binding) {
    // 解除事件監聽
    document.removeEventListener('click', el.__vueClickOutside__);
    delete el.__vueClickOutside__;
  },
};

export default {
  components: {
    'vue-touch-keyboard': VueTouchKeyboard.component,
  },
  directives: { clickoutside },
  data: function () {
    return {
      visible: false,
      input: null,
      options: {
        useKbEvents: false,
        preventClickEvent: false
      }
    };
  },
  props: {
    warnMessage: { type: String, default: null },//錯誤提示
    layout: { type: String, default: 'normal' },//鍵盤的種類:normal:全鍵盤/numeric:數字/compact:字母
    instanceID: String,
  },
  methods: {
    accept () {
      this.hide();
    },

    show (e) {
      this.input = e.target;
      if (!this.visible) { this.visible = true }

    },
    hide () {
      this.visible = false;
    },
    submit () {
      if (this.input === null) {
        return;
      }
      this.$emit('data-change', this.input.value);
    },
  },
  watch: {
    warnMessage: {
      handler () {
        // debugger;
        const doubleId = `#${this.instanceID}`;
        if (this.warnMessage !== '' && this.warnMessage != null) {
          $(doubleId).find('input').css('border', '1px solid rgba(255, 0, 90, 1)');
        } else {
          $(doubleId).find('input').css('border', 'none');
        }
      },
    },
  },
}
</script>

頁面內容(只簡單的展示了下組件的使用)

<template>
  <div>
    <div class="display-flex-css">
      <span class="keyboard-lable">{{$t('messages.order_no')}}</span>
      <touch-keyboard type="number"
                      id="order-no-K"
                      :placeholder="$t('messages.pls_input_orderno')"
                      :maxlength="13"
                      v-model="orderno"
                      instanceID="order-no-K"
                      :warnMessage="ordernoWarn"
                      @data-change="getOrderNo"
                      layout="numeric"></touch-keyboard>
    </div>
    <div class="display-flex-css">
      <span class="keyboard-lable">{{$t('messages.phoneNum')}}</span>
      <touch-keyboard type="number"
                      id="phone-num-K"
                      :placeholder="$t('messages.pls_input_phoneNum')"
                      :maxlength="11"
                      v-model="phoneNum"
                      instanceID="phone-num-K"
                      :warnMessage="phoneNumWarn"
                      @data-change="getPhoneNum"
                      layout="numeric"></touch-keyboard>
    </div>
    <button class="confirm-btn"
            @click="confirm">確定</button>
  </div>
</template>

<script>
import touchKeyboard from '@/components/keyboard';

export default {
  components: {
    'touch-keyboard': touchKeyboard,
  },
  data () {
    return {
      orderno: '',
      ordernoWarn: '',
      phoneNum: '',
      phoneNumWarn: ''
    }
  },
  methods: {
    getOrderNo (value) {
      this.orderno = value;
    },
    getPhoneNum (value) {
      this.phoneNum = value;
    },
    confirm () {
      this.ordernoWarn = "";
      this.phoneNumWarn = "";
      if (this.orderno == "") {
        this.ordernoWarn = "訂單號不能爲空";
      } else if (this.phoneNumWarn == "") {
        this.phoneNumWarn = "手機號不能爲空";
      }
    }
  }
}
</script>
<style lang="scss">
.display-flex-css {
  display: flex;
  justify-content: center;
}
.keyboard-lable {
  font-size: 22px;
  padding-right: 26px;
  line-height: 46px;
}
.confirm-btn {
  width: 150px;
  height: 43px;
  font-size: 21px;
  border-radius: 8px;
  background: #9800ff29;
  border: 0px;
  margin-top: 17%;
}
</style>

頁面效果:(隨便畫的,比較醜)

 
20315386-d5ac4903118d7270.png
 

 

 
20315386-ba198828331babdb.png
 

 

可參考https://www.npmjs.com/package/vue-touch-keyboard

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章