navBar.vue 7.1 KB

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