search-input.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!--输入搜索框用此组件,限制了中英文输入长度-->
  2. <template>
  3. <div class="input-box">
  4. <!--普通输入框-->
  5. <el-input v-model="value" @input="searchInput($event)" :placeholder="placeholdertip" :maxlength="maxLen" :ref="inputName" v-if="mode == 'input'"></el-input>
  6. <!--搜索框,支持自动搜索-->
  7. <el-input v-if="mode == 'search' && openAutoSearch" :maxlength="maxLen" @compositionend.native="handlecompositionend" @compositionstart.native="handlecompositionstart" size="mini" class="console-tree-search" :placeholder="placeholdertip" suffix-icon="tree-search-icon" @click.native="handleClickOnTableSearchInput" @keyup.native="handleTreeEnter" v-model="value" @input="searchInput($event)" :ref="inputName">
  8. <i slot="suffix" v-if="value" @click="handleTreeEnter(value = '')" class="el-input__icon el-icon-circle-close el-input__clear" style="margin-right: 14px;"></i>
  9. </el-input>
  10. <!--搜索框,支持回车搜索-->
  11. <el-input v-if="mode == 'search' && !openAutoSearch" :maxlength="maxLen" size="mini" class="console-tree-search" :placeholder="placeholdertip" suffix-icon="tree-search-icon" @click.native="handleClickOnTableSearchInput" @keyup.enter.native="handleTreeEnter" v-model="value" @input="searchInput($event)" :ref="inputName">
  12. <i slot="suffix" v-if="value" @click="handleTreeEnter(value = '')" class="el-input__icon el-icon-circle-close el-input__clear" style="margin-right: 14px;"></i>
  13. </el-input>
  14. </div>
  15. </template>
  16. <script>
  17. module.exports = {
  18. data() {
  19. return {
  20. compositioning: false,
  21. searchTimer: null,
  22. keyStatus: null,
  23. value: ''
  24. }
  25. },
  26. props: {
  27. //输入框提示信息
  28. placeholdertip: {
  29. type: String,
  30. },
  31. //输入框模式 input 仅输入功能 search 输入并带搜索功能
  32. mode: {
  33. default: 'input',
  34. type: String
  35. },
  36. maxLen: {
  37. type: Number,
  38. default: 120
  39. },
  40. inputName: {
  41. type: String,
  42. default: 'searchInput'
  43. },
  44. //是否开启自动搜索
  45. openAutoSearch: {
  46. type: Boolean,
  47. default: false
  48. }
  49. },
  50. methods: {
  51. /**
  52. * @method searchInput 输入后把新的搜索值传给父组件,input模式输入后通知父组件
  53. */
  54. searchInput (value) {
  55. if (value.length > this.maxLen) value = value.slice(0, this.maxLen)
  56. value = value.trim()
  57. this.$emit('input', value)
  58. },
  59. /**
  60. * @method handlecompositionend() 中文输入法
  61. */
  62. handlecompositionstart() {
  63. this.compositioning = true
  64. },
  65. /**
  66. * @method handlecompositionend() 中文输入法输入结束
  67. */
  68. handlecompositionend() {
  69. this.compositioning = false
  70. },
  71. keydownEvent(e) {
  72. //当输入中文按下回车松开时,不会触发keyup事件
  73. if (e.code == 'Enter' || e.code == 'NumpadEnter') {
  74. this.keyStatus = 'keyup'
  75. } else {
  76. this.keyStatus = 'keydown'
  77. }
  78. },
  79. keyupEvent() {
  80. this.keyStatus = 'keyup'
  81. },
  82. /**
  83. * @method handleClickOnTableSearchInput table搜索框点击事件
  84. */
  85. handleClickOnTableSearchInput(e) {
  86. // 如果是点击在搜索图标上,就去查询
  87. if (e && e.target && e.target.className && e.target.className.includes('el-input__icon')) this.handleTreeEnter()
  88. },
  89. /**
  90. * @method handleTreeEnter 搜索方法,search模式输入后通知父组件
  91. */
  92. handleTreeEnter() {
  93. //输入防抖
  94. clearTimeout(this.searchTimer)
  95. //如果还在中文输入
  96. if(this.compositioning) return
  97. this.searchTimer = setTimeout(() => {
  98. //如果键盘还是按下状态,持续输入
  99. if(this.keyStatus == 'keydown') return
  100. if (this.value.length > this.maxLen) this.value = this.value.slice(0, this.maxLen)
  101. this.value = this.value.trim()
  102. //把搜索值传到父组件
  103. this.$emit('search', this.value)
  104. }, 600)
  105. }
  106. }
  107. }
  108. </script>
  109. <style scoped>
  110. .input-box {
  111. width: 100%;
  112. display: inline-block;
  113. }
  114. /deep/ i.tree-search-icon {
  115. top: 6px;
  116. }
  117. /deep/ .el-input--suffix .el-input__inner {
  118. padding-right: 40px;
  119. }
  120. </style>