s-search-block.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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" @input="onInput" 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', 'input']);
  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. // 添加 input 事件处理
  115. const onInput = (value) => {
  116. emits('input', value);
  117. };
  118. </script>
  119. <style lang="scss" scoped>
  120. .border-content {
  121. border: 2rpx solid #eee;
  122. }
  123. .search-content {
  124. flex: 1;
  125. // height: 80rpx;
  126. position: relative;
  127. .search-icon {
  128. font-size: 38rpx;
  129. margin-right: 20rpx;
  130. }
  131. .keyword-link {
  132. position: absolute;
  133. right: 16rpx;
  134. top: 18rpx;
  135. }
  136. .search-input {
  137. font-size: 28rpx;
  138. }
  139. }
  140. </style>