mehaotian-search.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="search" :style="{ backgroundColor: backgroundColor }">
  3. <view class="content" :style="{ 'border-radius': radius + 'px', border: border }">
  4. <view class="content-box" :class="{ center: mode === 2 }">
  5. <view class="icon icon-search"><uni-icons type="search" size="30" color="#9c9c9c"></uni-icons></view>
  6. <input class="input" :class="{ center: !active && mode === 2 }" :focus="isFocus" :placeholder="placeholder" v-model="inputVal" @focus="focus" @blur="blur" @confirm="search"/>
  7. <!-- <view v-if="!active && mode === 2" class="input sub" @click="getFocus">请输入搜索内容</view> -->
  8. <view v-if="isDelShow" class="icon icon-del" @click="clear"><uni-icons type="close" size="20" color="#9c9c9c"></uni-icons></view>
  9. </view>
  10. <view v-show="(active && show && button === 'inside') || (isDelShow && button === 'inside')" class="searchBtn" @click="search" @confirm="search">搜索</view>
  11. </view>
  12. <view v-if="button === 'outside'" class="button" :class="{ active: show || active }" @click="search">
  13. <view class="button-item">{{ !show ? searchName : '搜索' }}</view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import uniIcons from "@/components/uni-icons/uni-icons.vue";
  19. export default {
  20. components: {uniIcons},
  21. props: {
  22. mode: {
  23. type: Number,
  24. default: 1
  25. },
  26. button: {
  27. type: String,
  28. default: 'outside'
  29. },
  30. show: {
  31. type: Boolean,
  32. default: true
  33. },
  34. radius: {
  35. type: String,
  36. default: '60'
  37. },
  38. placeholder: {
  39. type: String,
  40. default: '请输入搜索内容'
  41. },
  42. backgroundColor: {
  43. type: String,
  44. default: '#fff'
  45. },
  46. border: { type: String, default: '1px #f5f5f5 solid' }
  47. },
  48. data() {
  49. return {
  50. active: false,
  51. inputVal: '',
  52. searchName: '取消',
  53. isDelShow: false,
  54. isFocus: false
  55. };
  56. },
  57. methods: {
  58. focus() {
  59. this.active = true;
  60. },
  61. blur() {
  62. this.isFocus = false;
  63. if (!this.inputVal) {
  64. this.active = false;
  65. }
  66. },
  67. clear() {
  68. this.inputVal = '';
  69. this.active = false;
  70. this.$emit('search', '');
  71. },
  72. getFocus() {
  73. this.isFocus = true;
  74. },
  75. search() {
  76. if (!this.inputVal) return;
  77. console.log(this.inputVal);
  78. this.$emit('search', this.inputVal);
  79. uni.hideKeyboard()
  80. }
  81. },
  82. watch: {
  83. inputVal(newVal) {
  84. if (newVal) {
  85. this.searchName = '搜索';
  86. this.isDelShow = true;
  87. } else {
  88. this.searchName = '取消';
  89. this.isDelShow = false;
  90. }
  91. }
  92. }
  93. };
  94. </script>
  95. <style lang="scss" scoped>
  96. .search {
  97. display: flex;
  98. width: 100%;
  99. border-bottom: 1px #f5f5f5 solid;
  100. box-sizing: border-box;
  101. padding: 15upx;
  102. font-size: $uni-font-size-base;
  103. background: #fff;
  104. .content {
  105. display: flex;
  106. align-items: center;
  107. width: 100%;
  108. height: 60upx;
  109. border: 1px #ccc solid;
  110. background: #fff;
  111. overflow: hidden;
  112. transition: all 0.2s linear;
  113. border-radius: 30px;
  114. .content-box {
  115. background-color: #d7d7d7;
  116. width: 100%;
  117. display: flex;
  118. align-items: center;
  119. &.center {
  120. justify-content: center;
  121. }
  122. .icon {
  123. padding: 0 15upx;
  124. &.icon-del {
  125. font-size: 38upx;
  126. }
  127. }
  128. .input {
  129. background-color: #d7d7d7;
  130. width: 100%;
  131. max-width: 100%;
  132. line-height: 60upx;
  133. height: 60upx;
  134. transition: all 0.2s linear;
  135. &.center {
  136. width: 200upx;
  137. }
  138. &.sub {
  139. // position: absolute;
  140. width: auto;
  141. color: grey;
  142. }
  143. }
  144. }
  145. .searchBtn {
  146. height: 100%;
  147. flex-shrink: 0;
  148. padding: 0 30upx;
  149. background: $uni-color-success;
  150. line-height: 60upx;
  151. color: #fff;
  152. border-left: 1px #ccc solid;
  153. transition: all 0.3s;
  154. }
  155. }
  156. .button {
  157. display: flex;
  158. align-items: center;
  159. justify-content: center;
  160. position: relative;
  161. flex-shrink: 0;
  162. width: 0;
  163. transition: all 0.2s linear;
  164. white-space: nowrap;
  165. overflow: hidden;
  166. &.active {
  167. padding-left: 15upx;
  168. width: 100upx;
  169. }
  170. }
  171. }
  172. @font-face {
  173. font-family: 'iconfont';
  174. src: url('https://at.alicdn.com/t/font_989023_efq0mtli526.ttf') format('truetype');
  175. }
  176. .icon {
  177. font-family: iconfont;
  178. font-size: 32upx;
  179. font-style: normal;
  180. color: #999;
  181. }
  182. </style>