navBar.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <!-- 头部 -->
  3. <uni-nav-bar :class="{'custom_uni_nav_bar':!onlyTitle,'custom_navbar_only_title':onlyTitle,'custom_only_title_back_icon':!showHeadSearchInput}"
  4. :height="`${height}rpx`" :backgroundColor="backgroundColor" :title="title" color="#333333" :fixed="true"
  5. :border="showHeaderborder" :statusBar="true" :leftWidth="leftWidth" :rightWidth="rightWidth" left-icon="left"
  6. @clickLeft="GoBack()">
  7. <!-- 表头内容 -->
  8. <view :class="['nav_head',{'only_title':!showHeadSearchInput}]" v-if="!onlyTitle">
  9. <text class="nav_title">{{title}}</text>
  10. <template v-if="showHeadSearchInput">
  11. <uni-easyinput v-if="rightType=='searchBox'" class="search_box" v-model="state.searchWord" trim="all" :styles="state.searchInputStyles"
  12. :placeholderStyle="state.searchInputPlaceholderStyle" placeholder="请输入关键字搜索" @input="HandleSearchInput">
  13. <template #left>
  14. <image src="@/static/image/overview/search.png" class="search_icon"></image>
  15. </template>
  16. </uni-easyinput>
  17. <template v-else>
  18. <slot name="headRightIcon" />
  19. </template>
  20. </template>
  21. </view>
  22. <!-- 条件选择器 -->
  23. <view class="filter_picker" v-if="showFilterPicker">
  24. <template v-for="(item,index) in filterPickerData" :key="index">
  25. <picker class="item_filter_picker" mode="selector" v-if="item?.list?.length > 0" :range="item.list" :value="item.valueIndex" range-key="label" style="width: calc((100% - 32rpx * 2) / 3);" @change="(e)=>ChangeFilterPicker(e,index)">
  26. <view class="filter_item">
  27. <text class="filter_text">{{ item?.list?.[item.valueIndex]?.label || '' }}</text>
  28. <uni-icons type="down" size="16" color="#999999"></uni-icons>
  29. </view>
  30. </picker>
  31. </template>
  32. </view>
  33. <slot name="tabs_top_content" />
  34. <!-- tabs -->
  35. <view class="scroll_tabs" v-if="showTabs && tabList.length > 0">
  36. <scroll-view class="scroll_tabs_view" scroll-x="true" scroll-left="0" :show-scrollbar="false">
  37. <view :class="['tab_item',{'selected':item.value===state.tabsSelected}]" v-for="item in tabList" :key="item.value" @click="SelectTabsValue(item.value)">
  38. {{item.label}}
  39. </view>
  40. </scroll-view>
  41. <slot name="tabs_right_content" />
  42. </view>
  43. <!-- tabs 按钮 -->
  44. <scroll-view v-if="showTabBtns && tabBtns.length > 0" class="scroll_tab_btn" scroll-x="true" scroll-left="0" :show-scrollbar="false">
  45. <slot name="tab_btns_lef" />
  46. <view :class="['tab_item',{'selected':item.value===state.tabBtnSelected}]" v-for="item in tabBtns" :key="item.value" @click="SelectTabBtnsValue(item.value)">
  47. {{item.label}}
  48. </view>
  49. </scroll-view>
  50. </uni-nav-bar>
  51. </template>
  52. <script setup>
  53. import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue';
  54. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  55. import uniEasyinput from '@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue';
  56. import {
  57. reactive,
  58. onMounted,
  59. watch
  60. } from 'vue';
  61. const emit = defineEmits(['GoBack','CommonFilterData']);
  62. const props = defineProps({
  63. height: { //高度
  64. type: String,
  65. default: () => ''
  66. },
  67. leftWidth: { //导航栏左侧插槽宽度
  68. type: String,
  69. default: () => '96rpx'
  70. },
  71. rightWidth: { //导航栏右侧插槽宽度
  72. type: String,
  73. default: () => '96rpx'
  74. },
  75. title: { //标题
  76. type: String,
  77. default: () => ''
  78. },
  79. showHeaderborder: { //是否显示边框 默认不显示
  80. type: Boolean,
  81. default: false
  82. },
  83. backgroundColor: { //背景色
  84. type: String,
  85. default: () => '#FFFFFF'
  86. },
  87. backPath: { //返回路径
  88. type: String,
  89. default: () => ''
  90. },
  91. onlyTitle: { //是否只显示标题
  92. type: Boolean,
  93. default: true
  94. },
  95. filterPickerData: { //条件选择器
  96. type: Array,
  97. default: () => []
  98. },
  99. tabList: { //tabs
  100. type: Array,
  101. default: () => []
  102. },
  103. defaultTabValue:{
  104. type: [String,Number],
  105. default: () => ''
  106. },//默认值
  107. tabBtns: { //tabBtns
  108. type: Array,
  109. default: () => []
  110. },
  111. showHeadSearchInput:{//是否显示头部搜索框
  112. type: Boolean,
  113. default: true
  114. },
  115. rightType:{//头部右侧内容
  116. type: String,
  117. default: 'searchBox'
  118. },
  119. showFilterPicker: { //是否显示条件选择器
  120. type: Boolean,
  121. default: false
  122. },
  123. showTabs: { //是否显示条件选择器tabs
  124. type: Boolean,
  125. default: false
  126. },
  127. showTabBtns: { //是否显示条件选择器tabBtns
  128. type: Boolean,
  129. default: false
  130. }
  131. });
  132. const state = reactive({
  133. searchWord: '', //关键字
  134. filterOpts:{
  135. valueIndex:0,
  136. index:0,
  137. filterPickerValue: []
  138. },//条件选择器选中的值
  139. tabsSelected: '', //tabs 默认选中
  140. tabBtnSelected: '',//tabBtns 默认选中
  141. searchInputStyles: { //搜索框样式
  142. borderColor: '#DCDFE6',
  143. color: '#333333'
  144. },
  145. searchInputPlaceholderStyle: 'color: #C0C4CC;fontSize: 28rpx' //搜索框样式
  146. })
  147. watch(()=>props.tabList,(newVal, oldVal)=>{
  148. if(newVal){
  149. const firstValue = props?.tabList?.[0]?.value ?? '';
  150. state.tabsSelected = props.defaultTabValue!==''?props.defaultTabValue : firstValue; //tab 默认值
  151. }
  152. })
  153. watch(()=>props.tabBtns,(newVal, oldVal)=>{
  154. if(newVal){
  155. state.tabBtnSelected = props?.tabBtns?.[0]?.value ?? ''; //tab 默认值
  156. }
  157. })
  158. //关键字搜索
  159. const HandleSearchInput = (e) => {
  160. state.searchWord = e;
  161. CommonFilterData('search');
  162. }
  163. //切换条件选择器 index:数组filterPickerData索引
  164. const ChangeFilterPicker = (e, index) => {
  165. const valueIndex = e.detail.value; //下拉框选中值的索引
  166. const selectedValue = props.filterPickerData?.[index]?.list?.[valueIndex]?.value ?? '';
  167. state.filterOpts.filterPickerValue[index] = selectedValue;
  168. //处理下一级默认值
  169. for(let i = 0; i < props.filterPickerData.length; i++){
  170. if(i > index){
  171. const list = props.filterPickerData?.[i]?.list || [];
  172. state.filterOpts.filterPickerValue[i] = list.length > 0 ? list[0].value : '';
  173. }
  174. }
  175. state.filterOpts.valueIndex = valueIndex;
  176. state.filterOpts.index = index;
  177. CommonFilterData('filterPicker');
  178. }
  179. //tabs切换
  180. const SelectTabsValue = (value) => {
  181. state.tabsSelected = value;
  182. CommonFilterData('tabs');
  183. }
  184. //tabBtns切换
  185. const SelectTabBtnsValue = (value) => {
  186. state.tabBtnSelected = value;
  187. CommonFilterData('tabBtns');
  188. }
  189. //条件发生改变进行数据查询
  190. const CommonFilterData = (type) => {
  191. emit('CommonFilterData',{
  192. searchWord:state.searchWord,
  193. filterOpts:state.filterOpts,
  194. tabsSelected:state.tabsSelected,
  195. tabBtnSelected:state.tabBtnSelected
  196. },type);
  197. }
  198. const GoBack = () => {
  199. if (props.backPath == 'myIndex') {
  200. uni.redirectTo({
  201. url: '/pages/my/index'
  202. });
  203. } else if (props.backPath == 'workspace') { //跳转工作台
  204. uni.redirectTo({
  205. url: '/pages/workspace/index'
  206. });
  207. } else {
  208. emit('GoBack');
  209. }
  210. }
  211. onMounted(() => {
  212. const firstValue = props?.tabList?.[0]?.value ?? '';
  213. state.tabsSelected = props.defaultTabValue!==''?props.defaultTabValue : firstValue; //tab 默认值
  214. state.tabBtnSelected = props?.tabBtns?.[0]?.value || ''; //tabBtns 默认值
  215. state.filterOpts.filterPickerValue = props.filterPickerData.map(item => item?.defaultValue ?? '');
  216. })
  217. </script>
  218. <style scoped lang="scss">
  219. :deep(.uni-navbar__header) {
  220. padding: 0;
  221. position: relative;
  222. z-index: 999;
  223. }
  224. :deep(.uni-navbar__header-container) {
  225. padding: 0;
  226. }
  227. </style>