input-data.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <script lang="uts">
  2. export default {
  3. name: "input-data",
  4. emits: ['confirm'],
  5. props: {
  6. title: {
  7. type: String,
  8. required: true
  9. },
  10. type: {
  11. type: String,
  12. required: true
  13. },
  14. defaultValue: {
  15. type: String,
  16. required: true,
  17. default: ''
  18. }
  19. },
  20. data() {
  21. return {
  22. inputClearValue: '' as any,
  23. showClearIcon: false,
  24. inputType: 'text'
  25. }
  26. },
  27. created() {
  28. switch (this.type) {
  29. case 'number':
  30. this.inputType = 'number'
  31. break;
  32. }
  33. this.inputClearValue = this.getValue(this.defaultValue)
  34. },
  35. methods: {
  36. input: function (event : InputEvent) {
  37. // @ts-ignore
  38. this.inputClearValue = event.detail.value
  39. if ((this.inputClearValue as string).length > 0) {
  40. this.showClearIcon = true
  41. } else {
  42. this.showClearIcon = false
  43. }
  44. this.$emit('confirm', this.getValue(this.inputClearValue))
  45. },
  46. clearIcon: function () {
  47. this.inputClearValue = ''
  48. this.showClearIcon = false
  49. this.$emit('confirm', this.getValue(this.inputClearValue))
  50. },
  51. blur() {
  52. this.showClearIcon = false
  53. },
  54. focus() {
  55. let inputValue = this.inputClearValue
  56. if (typeof inputValue !== 'string') {
  57. inputValue = inputValue.toString()
  58. }
  59. if ((inputValue as string).length > 0) {
  60. this.showClearIcon = true
  61. } else {
  62. this.showClearIcon = false
  63. }
  64. },
  65. getValue(value : any) : any {
  66. switch (this.type) {
  67. case 'number':
  68. return parseFloat(value as string)
  69. }
  70. return value
  71. }
  72. }
  73. }
  74. </script>
  75. <template>
  76. <view class="uni-padding-wrap">
  77. <view class="uni-title uni-common-mt">
  78. <text class="uni-title-text"> {{title}} </text>
  79. </view>
  80. </view>
  81. <view class="input-wrapper">
  82. <input class="uni-input" :type="inputType" :value="inputClearValue" :placeholder="title" maxlength="-1" @input="input" @blur="blur"
  83. @focus="focus" />
  84. <!-- #ifdef WEB || MP -->
  85. <image class="input-wrapper_image" src="/static/icons/clear.png" v-if="showClearIcon" @touchstart="clearIcon" @mousedown="clearIcon">
  86. <!-- #endif -->
  87. <!-- #ifndef WEB || MP -->
  88. <image class="input-wrapper_image" src="/static/icons/clear.png" v-if="showClearIcon" @touchstart="clearIcon">
  89. <!-- #endif -->
  90. </image>
  91. </view>
  92. </template>
  93. <style>
  94. .input-wrapper {
  95. border: 1px solid rgba(0, 0, 0, .08);
  96. flex-direction: row;
  97. justify-content: center;
  98. padding: 0;
  99. margin: 0 10px;
  100. flex-wrap: nowrap;
  101. background-color: #ffffff;
  102. }
  103. .input-wrapper_image {
  104. width: 22px;
  105. height: 22px;
  106. align-self: center;
  107. margin-right: 5px;
  108. }
  109. </style>