s-search-block.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view
  3. class="search-content ss-flex ss-col-center ss-row-between"
  4. @tap="click"
  5. :style="[
  6. {
  7. borderRadius: radius + 'px',
  8. background: elBackground,
  9. height: height + 'px',
  10. width: width,
  11. },
  12. ]"
  13. :class="[{ 'border-content': navbar }]"
  14. >
  15. <view class="ss-flex ss-col-center" v-if="navbar">
  16. <view class="search-icon _icon-search ss-m-l-10" :style="[{ color: props.iconColor }]"></view>
  17. <view class="search-input ss-flex-1 ss-line-1" :style="[{ color: fontColor, width: width }]">
  18. {{ placeholder }}
  19. </view>
  20. </view>
  21. <uni-search-bar
  22. v-if="!navbar"
  23. class="ss-flex-1"
  24. :radius="data.borderRadius"
  25. :placeholder="data.placeholder"
  26. cancelButton="none"
  27. clearButton="none"
  28. @confirm="onSearch"
  29. v-model="state.searchVal"
  30. />
  31. <view class="keyword-link ss-flex">
  32. <view v-for="(item, index) in data.hotKeywords" :key="index">
  33. <view
  34. class="ss-m-r-16"
  35. :style="[{ color: data.textColor }]"
  36. @tap.stop="sheep.$router.go('/pages/goods/list', { keyword: item })"
  37. >{{ item }}</view
  38. >
  39. </view>
  40. </view>
  41. <view v-if="data.hotKeywords && data.hotKeywords.length && navbar" class="ss-flex">
  42. <button
  43. class="ss-reset-button keyword-btn"
  44. v-for="(item, index) in data.hotKeywords"
  45. :key="index"
  46. :style="[{ color: data.textColor, marginRight: '10rpx' }]"
  47. >
  48. {{ item }}
  49. </button>
  50. </view>
  51. </view>
  52. </template>
  53. <script setup>
  54. /**
  55. * 基础组件 - 搜索栏
  56. *
  57. * @property {String} elBackground - 输入框背景色
  58. * @property {String} iconColor - 图标颜色
  59. * @property {String} fontColor - 字体颜色
  60. * @property {Number} placeholder - 默认placeholder
  61. * @property {Number} topRadius - 组件上圆角
  62. * @property {Number} bottomRadius - 组件下圆角
  63. *
  64. * @slot keywords - 关键字
  65. * @event {Function} click - 点击组件时触发
  66. */
  67. import { computed, reactive } from 'vue';
  68. import sheep from '@/sheep';
  69. // 组件数据
  70. const state = reactive({
  71. searchVal: '',
  72. });
  73. // 事件页面
  74. const emits = defineEmits(['click']);
  75. // 接收参数
  76. const props = defineProps({
  77. data: {
  78. type: Object,
  79. default: () => ({}),
  80. },
  81. // 输入框背景色
  82. elBackground: {
  83. type: String,
  84. default: '',
  85. },
  86. height: {
  87. type: Number,
  88. default: 36,
  89. },
  90. // 图标颜色
  91. iconColor: {
  92. type: String,
  93. default: '#b0b3bf',
  94. },
  95. // 字体颜色
  96. fontColor: {
  97. type: String,
  98. default: '#b0b3bf',
  99. },
  100. // placeholder
  101. placeholder: {
  102. type: String,
  103. default: '这是一个搜索框',
  104. },
  105. radius: {
  106. type: Number,
  107. default: 10,
  108. },
  109. width: {
  110. type: String,
  111. default: '100%',
  112. },
  113. navbar: {
  114. type: Boolean,
  115. default: true,
  116. },
  117. });
  118. // 点击
  119. const click = () => {
  120. emits('click');
  121. };
  122. function onSearch(e) {
  123. if (e.value) {
  124. sheep.$router.go('/pages/goods/list', { keyword: e.value });
  125. setTimeout(() => {
  126. state.searchVal = '';
  127. }, 100);
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .border-content {
  133. border: 2rpx solid #eee;
  134. }
  135. .search-content {
  136. flex: 1;
  137. // height: 80rpx;
  138. position: relative;
  139. .search-icon {
  140. font-size: 38rpx;
  141. margin-right: 20rpx;
  142. }
  143. .keyword-link {
  144. position: absolute;
  145. right: 16rpx;
  146. top: 18rpx;
  147. }
  148. .search-input {
  149. font-size: 28rpx;
  150. }
  151. }
  152. </style>