index.vue 3.27 KB
<!--
 * @Date: 2022-09-14 14:44:30
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2022-11-18 13:50:50
 * @FilePath: /data-table/src/components/NumberField/index.vue
 * @Description: 数字输入框
-->
<template>
  <div class="number-page">
    <div class="label">
      {{ item.component_props.label }}
      <span v-if="item.component_props.required">&nbsp;*</span>
    </div>
    <van-field
      v-model="item.value"
      :name="item.name"
      :placeholder="item.component_props.placeholder"
      :rules="item.rules"
      :required="item.component_props.required"
      readonly
      @touchstart.stop="showKeyboard($event)"
      :border="false"
    >
    </van-field>
    <van-number-keyboard
      v-model="item.value"
      :show="showInteger"
      @blur="blurKeyboard()"
      @input="onInput"
      @delete="onDelete"
    />
    <van-number-keyboard
      v-model="item.value"
      :show="showDecimal"
      theme="custom"
      extra-key="."
      close-button-text="完成"
      @blur="blurKeyboard()"
      @input="onInput"
      @delete="onDelete"
    />
  </div>
</template>

<script setup>
import $ from "jquery";

const props = defineProps({
  item: Object,
});

const showKeyboard = (e) => {
  if (props.item.component_props.type === "decimal") {
    // 显示小数键盘
    showDecimal.value = true;
  } else {
    // 显示整数键盘
    showInteger.value = true;
  }
  // 键盘上移动
  // const target_to_view_height = window.innerHeight - e.target.getBoundingClientRect().y; // 元素到适口高度
  const target_top = document.body.scrollHeight - $(e.target).offset().top; // 元素到正文高度
  let scroll_height = "";
  document.getElementById("app").style.paddingBottom = "244px";
  if (target_top < 244) {
    window.scrollTo(0, $("#app").height());
  } else {
    document.documentElement.scrollTop = 400;
  }
};
const blurKeyboard = () => {
  if (props.item.component_props.type === "decimal") {
    // 显示小数键盘
    showDecimal.value = false;
  } else {
    // 显示整数键盘
    showInteger.value = false;
  }
  document.getElementById("app").style.paddingBottom = "0";
};

const showDecimal = ref(false);
const showInteger = ref(false);

// // 校验函数返回 true 表示校验通过,false 表示不通过
// // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
// const validator = (val) => {
//   if (!props.item.component_props.required) {
//     // 非必填
//     return true;
//   } else {
//     return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(val);
//   }
// };
// // 错误提示文案
// const validatorMessage = (val, rule) => {
//   if (!val) {
//     return "身份证号码不能为空";
//   } else if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(val)) {
//     return "请输入正确身份证号码";
//   }
// };
// const rules = [{ validator, message: validatorMessage }];

const onInput = (value) => {};
const onDelete = () => {};
</script>

<style lang="less" scoped>
.number-page {
  .label {
    padding: 1rem 1rem 0 1rem;
    font-size: 0.9rem;
    font-weight: bold;
    span {
      color: red;
    }
  }
}

:deep(.van-field__body) {
  border: 1px solid #eaeaea;
  border-radius: 0.25rem;
  padding: 0.25rem 0.5rem;
}
</style>