lj-searchbar.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!--搜索框组件-->
  2. <!--events-->
  3. <!--搜索按钮点击或键盘回车点击search,参数(value)-->
  4. <template>
  5. <div style="padding:5px;line-height: 20px;height:40px;width: 100%">
  6. <mu-text-field :placeholder="hint" solo v-model="currentValue" class="circle"
  7. @keydown.enter.native="handleTextEnter" style="min-height: 0;height: 100%;" ref="textField">
  8. <template slot="append">
  9. <slot name="serch_append" :handleTextEnter="handleTextEnter">
  10. <mu-button icon @click="handleTextEnter">
  11. <i class="iconfont icon-search" style="font-size: 20px;"></i>
  12. </mu-button>
  13. </slot>
  14. </template>
  15. </mu-text-field>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. name: "lj-searchbar",
  21. props: {
  22. // 输入提示
  23. hint: {
  24. type: String,
  25. default: ""
  26. },
  27. // 输入框的值,支持v-model双向绑定
  28. value: {
  29. type: String
  30. },
  31. // 点击后是否把界面拉回到最顶
  32. actionToTop: {
  33. type: Boolean,
  34. default: true
  35. }
  36. },
  37. data() {
  38. return {
  39. currentValue: this.value
  40. }
  41. },
  42. methods: {
  43. handleTextEnter() {
  44. if (this.actionToTop) {
  45. document.body.scrollTop = 0;
  46. document.documentElement.scrollTop = 0;
  47. }
  48. let input = this.$refs.textField.$el.querySelector('input');
  49. input.blur();
  50. this.$emit('search', this.currentValue);
  51. }
  52. },
  53. watch: {
  54. value(newVal, oldVal) {
  55. if (newVal !== oldVal) {
  56. this.currentValue = newVal;
  57. }
  58. },
  59. currentValue(newVal, oldVal) {
  60. if (newVal !== oldVal) {
  61. this.$emit('input', newVal);
  62. }
  63. }
  64. }
  65. }
  66. </script>
  67. <style scoped lang="less">
  68. @import "~assets/css/lj-base.less";
  69. .circle {
  70. border: 1px solid;
  71. border-radius: 9999px;
  72. padding-top: 0;
  73. padding-bottom: 0;
  74. padding-left: 10px;
  75. padding-right: 10px;
  76. width: 100%;
  77. background-color: @groupCellBackgroundColor;
  78. font-size: 14px;
  79. }
  80. </style>