fa-search.vue 2.5 KB

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