input-data.vue 2.6 KB
Newer Older
1
<script lang="uts">
D
DCloud_LXH 已提交
2 3
  export default {
    name: "input-data",
4
    emits: ['confirm'],
D
DCloud_LXH 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    props: {
      title: {
        type: String,
        required: true
      },
      type: {
        type: String,
        required: true
      },
      defaultValue: {
        type: String,
        required: true,
        default: ''
      }
    },
    data() {
      return {
        inputClearValue: '' as any,
        showClearIcon: false,
        inputType: 'text'
      }
    },
    created() {
      switch (this.type) {
        case 'number':
          this.inputType = 'number'
          break;
      }
      this.inputClearValue = this.getValue(this.defaultValue)
    },
    methods: {
      input: function (event : InputEvent) {
        // @ts-ignore
        this.inputClearValue = event.detail.value
        if ((this.inputClearValue as string).length > 0) {
          this.showClearIcon = true
        } else {
          this.showClearIcon = false
        }
44

D
DCloud_LXH 已提交
45 46 47 48 49 50 51 52
        this.$emit('confirm', this.getValue(this.inputClearValue))
      },
      clearIcon: function () {
        this.inputClearValue = ''
        this.showClearIcon = false
        this.$emit('confirm', this.getValue(this.inputClearValue))
      },
      blur() {
雪洛's avatar
雪洛 已提交
53 54 55
        setTimeout(() => {
          this.showClearIcon = false
        }, 100)
D
DCloud_LXH 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      },
      focus() {
        let inputValue = this.inputClearValue
        if (typeof inputValue !== 'string') {
          inputValue = inputValue.toString()
        }
        if ((inputValue as string).length > 0) {
          this.showClearIcon = true
        } else {
          this.showClearIcon = false
        }
      },
      getValue(value : any) : any {
        switch (this.type) {
          case 'number':
            return parseFloat(value as string)
        }
73

D
DCloud_LXH 已提交
74 75 76 77
        return value
      }
    }
  }
78 79 80
</script>

<template>
D
DCloud_LXH 已提交
81 82 83 84 85
  <view class="uni-padding-wrap">
    <view class="uni-title uni-common-mt">
      <text class="uni-title-text"> {{title}} </text>
    </view>
  </view>
雪洛's avatar
雪洛 已提交
86
  <view class="input-wrapper">
87
    <input class="uni-input" :type="inputType" :value="inputClearValue" :placeholder="title" maxlength="-1" @input="input" @blur="blur"
88
      @focus="focus" />
D
DCloud_LXH 已提交
89 90 91
    <image class="input-wrapper_image" src="/static/icons/clear.png" v-if="showClearIcon" @click="clearIcon">
    </image>
  </view>
92 93 94 95
</template>


<style>
D
DCloud_LXH 已提交
96 97 98 99 100
  .input-wrapper {
    border: 1px solid rgba(0, 0, 0, .08);
    flex-direction: row;
    justify-content: center;
    padding: 0;
101
    margin: 0 10px;
雪洛's avatar
雪洛 已提交
102 103 104
    flex-direction: row;
    flex-wrap: nowrap;
    background-color: #ffffff;
D
DCloud_LXH 已提交
105 106 107 108 109 110
  }

  .input-wrapper_image {
    width: 22px;
    height: 22px;
    align-self: center;
111
    margin-right: 5px;
D
DCloud_LXH 已提交
112
  }
113
</style>