s-search-block.vue 3.2 KB

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