fa-search.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="search">
  3. <view class="content" :style="{ 'border-radius': radius + 'px' }">
  4. <view class="content-box" :class="{ center: mode === 2 }" @click.self="getFocus">
  5. <u-icon name="search" color="#808080" size="30"></u-icon>
  6. <input
  7. class="input u-m-l-10"
  8. :class="{ center: !active && mode === 2 }"
  9. :focus="isFocus"
  10. :placeholder="placeholder"
  11. v-model="inputVal"
  12. @focus="focus"
  13. @blur="blur"
  14. @confirm="search"
  15. />
  16. <u-icon v-if="isDelShow" @click="clear" name="close-circle-fill" color="#b4b4b4" size="35"></u-icon>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name:'fa-search',
  24. props: {
  25. mode: {
  26. type: Number,
  27. default: 1
  28. },
  29. radius: {
  30. type: String,
  31. default: '60'
  32. },
  33. placeholder: {
  34. type: String,
  35. default: '请输入关键词搜索'
  36. },
  37. custom:{
  38. type:Boolean,
  39. default:false
  40. }
  41. },
  42. data() {
  43. return {
  44. active: false,
  45. inputVal: '',
  46. isDelShow: false,
  47. isFocus: false
  48. };
  49. },
  50. watch: {
  51. inputVal(newVal) {
  52. if (newVal) {
  53. this.isDelShow = true;
  54. } else {
  55. this.isDelShow = false;
  56. }
  57. }
  58. },
  59. methods: {
  60. focus() {
  61. this.active = true;
  62. },
  63. blur() {
  64. this.isFocus = false;
  65. if (!this.inputVal) {
  66. this.active = false;
  67. }
  68. },
  69. clear() {
  70. this.inputVal = '';
  71. this.active = false;
  72. },
  73. getFocus() {
  74. this.isFocus = true;
  75. },
  76. search(e) {
  77. if(this.custom){
  78. this.$emit('change',e.detail.value);
  79. return;
  80. }
  81. this.$Router.push({
  82. path: '/pages/search/search',
  83. query: { keyword: e.detail.value }
  84. });
  85. }
  86. }
  87. };
  88. </script>
  89. <style lang="scss" scoped>
  90. .search {
  91. display: flex;
  92. width: 100%;
  93. box-sizing: border-box;
  94. font-size: $uni-font-size-base;
  95. background: #fff;
  96. .content {
  97. display: flex;
  98. align-items: center;
  99. width: 100%;
  100. height: 70rpx;
  101. background-color: #f2f2f2;
  102. overflow: hidden;
  103. transition: all 0.2s linear;
  104. border-radius: 30px;
  105. padding: 0 30rpx;
  106. .content-box {
  107. width: 100%;
  108. display: flex;
  109. align-items: center;
  110. &.center {
  111. justify-content: center;
  112. }
  113. .input {
  114. width: 100%;
  115. max-width: 100%;
  116. line-height: 60rpx;
  117. font-size: 30rpx;
  118. height: 60rpx;
  119. background-color: #f2f2f2;
  120. transition: all 0.2s linear;
  121. &.center {
  122. width: 250rpx;
  123. }
  124. &.sub {
  125. width: auto;
  126. color: grey;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. </style>