uni-cms-article-search-bar.uvue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="search-bar">
  3. <view :style="{ height: `${navBarHeight}px` }"></view>
  4. <view class="search-bar__content" @click="goToSearchPage">
  5. <view class="search-bar__left" v-if="!showPlaceholder">
  6. <view class="back-icon">
  7. <uni-cms-article-icons type="back" :size="26" color="#333" @click="back"></uni-cms-article-icons>
  8. </view>
  9. </view>
  10. <view class="search-bar__center">
  11. <uni-cms-article-icons type="search" :size="18" color="#c0c4cc"></uni-cms-article-icons>
  12. <text class="search-bar__placeholder" v-if="showPlaceholder">请输入搜索内容</text>
  13. <input
  14. v-else
  15. ref="search-input"
  16. class="search-bar__input"
  17. placeholder="请输入搜索内容"
  18. v-model="searchVal"
  19. confirm-type="search"
  20. :focus="focus"
  21. @confirm="confirm"
  22. />
  23. <view class="clear-icon" v-if="hasSearchValue" @click="clear">
  24. <uni-cms-article-icons type="closeempty" :size="12" color="#fff"></uni-cms-article-icons>
  25. </view>
  26. </view>
  27. <view class="search-bar__right" v-if="!showPlaceholder">
  28. <!-- <uni-cms-article-icons type="scan" :size="20" color="#c0c4cc" @click="scan"></uni-cms-article-icons>-->
  29. <text class="search-bar__search-text" @click="confirm">搜索</text>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script lang="uts">
  35. // import parseScanResult from "@/uni_modules/uni-cms-article/common/parse-scan-result.uts";
  36. export default {
  37. name: 'search-bar',
  38. emits: ['update:modelValue', 'clear', 'confirm'],
  39. data() {
  40. return {
  41. navBarHeight: 44,
  42. searchVal: ""
  43. }
  44. },
  45. props: {
  46. showPlaceholder: {
  47. type: Boolean,
  48. default: false
  49. },
  50. focus: {
  51. type: Boolean,
  52. default: false
  53. },
  54. modelValue: {
  55. type: String,
  56. default: ""
  57. }
  58. },
  59. watch: {
  60. searchVal(newValue) {
  61. this.$emit('update:modelValue', newValue)
  62. },
  63. modelValue: {
  64. immediate: true,
  65. handler(newVal) {
  66. this.searchVal = newVal
  67. }
  68. },
  69. },
  70. computed: {
  71. hasSearchValue(): boolean {
  72. return this.searchVal != ""
  73. }
  74. },
  75. methods: {
  76. back() {
  77. // 获取当前页面数量
  78. const pages = getCurrentPages()
  79. // 定义文章列表页的路径
  80. const pageUrl = '/uni_modules/uni-cms-article/pages/list/list'
  81. // 如果当前页面数量大于1,返回上一页
  82. if (pages.length > 1) {
  83. uni.navigateBack({})
  84. } else { // 否则跳转到文章列表页
  85. uni.redirectTo({
  86. url: pageUrl,
  87. fail: (e: RedirectToFail) => {
  88. // 如果跳转失败,说明当前页面是tabbar页面,需要使用switchTab跳转
  89. if (e.errMsg.indexOf('tabbar') !== -1) {
  90. uni.switchTab({
  91. url: pageUrl
  92. })
  93. }
  94. }
  95. })
  96. }
  97. },
  98. clear() {
  99. this.searchVal = '';
  100. (this.$refs['search-input'] as Element).blur()
  101. this.$emit('clear')
  102. },
  103. confirm() {
  104. (this.$refs['search-input'] as Element).blur()
  105. this.$emit('confirm', this.searchVal)
  106. },
  107. scan() {
  108. // 扫码暂不支持
  109. // uni.scanCode({
  110. // onlyFromCamera: true,
  111. // scanType: ["qrCode"],
  112. // success: (e) => parseScanResult(e.result),
  113. // fail: (e) => {
  114. // console.error(e)
  115. // }
  116. // })
  117. },
  118. goToSearchPage() {
  119. if (!this.showPlaceholder) return
  120. uni.navigateTo({
  121. url: '/uni_modules/uni-cms-article/pages/search/search'
  122. })
  123. },
  124. hideKeyboard() {
  125. (this.$refs['search-input'] as Element).blur()
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss">
  131. .search-bar {
  132. background: #fff;
  133. &__content {
  134. display: flex;
  135. flex-direction: row;
  136. align-items: center;
  137. padding: 10rpx 20rpx;
  138. }
  139. &__left {
  140. margin-left: -20rpx;
  141. }
  142. &__center {
  143. display: flex;
  144. flex-direction: row;
  145. align-items: center;
  146. justify-content: center;
  147. background: #F8F8F8;
  148. padding: 20rpx;
  149. margin: 0 20rpx;
  150. margin-left: 0;
  151. border-radius: 40rpx;
  152. flex: 1;
  153. }
  154. &__placeholder {
  155. color: #c0c4cc;
  156. font-size: 28rpx;
  157. margin-left: 10rpx;
  158. }
  159. &__input {
  160. margin-left: 10rpx;
  161. flex: 1;
  162. }
  163. &__search-text {
  164. color: #c0402b;
  165. font-size: 28rpx;
  166. }
  167. }
  168. .back-icon {
  169. padding: 10rpx;
  170. }
  171. .clear-icon {
  172. width: 30rpx;
  173. height: 30rpx;
  174. background: #c0c4cc;
  175. border-radius: 15rpx;
  176. margin-right: 0;
  177. margin-left: 10rpx;
  178. display: flex;
  179. align-items: center;
  180. justify-content: center;
  181. }
  182. </style>