index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <!--
  3. ss-search-button 搜索按钮组件
  4. 基于原JSP中的mobileSearch-button样式封装
  5. 支持多选项popup功能
  6. -->
  7. <view class="ss-search-button-container" :class="{ open: showOptionsMenu }">
  8. <button
  9. class="ss-search-button"
  10. :style="buttonStyle"
  11. @click="handleClick"
  12. :disabled="disabled"
  13. >
  14. <!-- 前置图标插槽 -->
  15. <view v-if="preIcon || $slots.preIcon" class="ss-search-button__pre-icon">
  16. <slot name="preIcon">
  17. <Icon v-if="preIcon" :name="preIcon" :size="iconSize" :color="iconColor" />
  18. </slot>
  19. </view>
  20. <!-- 按钮文本 -->
  21. <text class="ss-search-button__text">{{ text }}</text>
  22. <!-- 后置图标插槽 -->
  23. <view v-if="suffixIcon || $slots.suffixIcon" class="ss-search-button__suffix-icon">
  24. <slot name="suffixIcon">
  25. <Icon v-if="suffixIcon" :name="suffixIcon" :size="iconSize" :color="iconColor" />
  26. </slot>
  27. </view>
  28. </button>
  29. <!-- 选项弹窗菜单 - 参考ss-select样式 -->
  30. <view
  31. v-if="showOptionsMenu && hasMultipleOptions"
  32. class="options-menu"
  33. @click.stop
  34. >
  35. <view
  36. v-for="(option, index) in options"
  37. :key="index"
  38. class="option-item"
  39. @click="handleOptionClick(option, index)"
  40. >
  41. <text v-if="option.icon" :class="['iconfont', option.icon]"></text>
  42. <text class="option-text">{{ option.text }}</text>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script setup>
  48. import { computed, ref, onMounted, onUnmounted } from 'vue'
  49. import Icon from '@/components/icon/index.vue'
  50. const props = defineProps({
  51. // 按钮文本
  52. text: {
  53. type: String,
  54. default: '增加'
  55. },
  56. // 是否禁用
  57. disabled: {
  58. type: Boolean,
  59. default: false
  60. },
  61. // 按钮高度
  62. height: {
  63. type: [String, Number],
  64. default: '36px'
  65. },
  66. // 前置图标名称
  67. preIcon: {
  68. type: String,
  69. default: ''
  70. },
  71. // 后置图标名称
  72. suffixIcon: {
  73. type: String,
  74. default: ''
  75. },
  76. // 图标大小
  77. iconSize: {
  78. type: [String, Number],
  79. default: '32'
  80. },
  81. // 图标颜色
  82. iconColor: {
  83. type: String,
  84. default: '#585d6e'
  85. },
  86. // 自定义按钮样式
  87. customStyle: {
  88. type: Object,
  89. default: () => ({})
  90. },
  91. // 跳转链接(兼容原JSP用法)
  92. href: {
  93. type: String,
  94. default: ''
  95. },
  96. // 选项列表 - 新增
  97. options: {
  98. type: Array,
  99. default: () => []
  100. }
  101. })
  102. const emit = defineEmits(['click', 'optionClick'])
  103. // 状态
  104. const showOptionsMenu = ref(false)
  105. // 计算属性
  106. const hasOptions = computed(() => {
  107. return props.options && Array.isArray(props.options) && props.options.length > 0
  108. })
  109. const hasMultipleOptions = computed(() => {
  110. return props.options && props.options.length > 1
  111. })
  112. const isSingleOption = computed(() => {
  113. return props.options && props.options.length === 1
  114. })
  115. // 按钮样式
  116. const buttonStyle = computed(() => ({
  117. height: typeof props.height === 'number' ? `${props.height}px` : props.height,
  118. ...props.customStyle
  119. }))
  120. /**
  121. * 处理按钮点击
  122. */
  123. const handleClick = () => {
  124. if (!hasOptions.value) {
  125. // 没有选项,直接触发点击事件
  126. emit('click')
  127. } else if (isSingleOption.value) {
  128. // 单个选项,直接执行
  129. handleOptionClick(props.options[0], 0)
  130. } else if (hasMultipleOptions.value) {
  131. // 先记录当前状态
  132. const wasOpen = showOptionsMenu.value
  133. // 关闭其他按钮的菜单
  134. uni.$emit('closeAllButtonMenus')
  135. // 切换当前菜单状态
  136. showOptionsMenu.value = !wasOpen
  137. }
  138. }
  139. /**
  140. * 处理选项点击
  141. */
  142. const handleOptionClick = (option, index) => {
  143. showOptionsMenu.value = false
  144. // 执行选项的回调
  145. if (option.onclick && typeof option.onclick === 'function') {
  146. option.onclick()
  147. }
  148. // 触发组件事件
  149. emit('optionClick', { option, index })
  150. // 兼容原来的click事件
  151. emit('click', { option, index })
  152. }
  153. /**
  154. * 关闭菜单
  155. */
  156. const closeMenu = () => {
  157. showOptionsMenu.value = false
  158. }
  159. /**
  160. * 监听全局关闭事件
  161. */
  162. onMounted(() => {
  163. uni.$on('closeAllButtonMenus', closeMenu)
  164. })
  165. onUnmounted(() => {
  166. uni.$off('closeAllButtonMenus', closeMenu)
  167. })
  168. </script>
  169. <style lang="scss" scoped>
  170. .ss-search-button-container {
  171. position: relative;
  172. display: inline-block;
  173. }
  174. .ss-search-button {
  175. height: auto;
  176. padding: 0 1rem;
  177. border: 1px solid #eceded;
  178. outline: none;
  179. background-color: #fff;
  180. text-align: center;
  181. font-size: 1rem;
  182. color: #585d6e;
  183. letter-spacing: 0.1rem;
  184. border-radius: 4px;
  185. display: flex;
  186. align-items: center;
  187. justify-content: center;
  188. -webkit-appearance: none;
  189. appearance: none;
  190. box-sizing: border-box;
  191. transition: all 0.2s ease;
  192. &:active {
  193. background-color: #eceded;
  194. color: #fff;
  195. border-color: #eceded;
  196. transform: scale(0.98);
  197. transition: all 0.1s ease;
  198. // active 状态下图标样式
  199. .ss-search-button__pre-icon,
  200. .ss-search-button__suffix-icon {
  201. :deep(.iconfont) {
  202. color: #fff !important;
  203. }
  204. :deep(text) {
  205. color: #fff !important;
  206. }
  207. }
  208. }
  209. &:disabled {
  210. opacity: 0.6;
  211. cursor: not-allowed;
  212. }
  213. &__pre-icon {
  214. margin-right: 0.3rem;
  215. display: flex;
  216. align-items: center;
  217. }
  218. &__suffix-icon {
  219. margin-left: 0.3rem;
  220. display: flex;
  221. align-items: center;
  222. }
  223. &__text {
  224. font-size: inherit;
  225. color: inherit;
  226. }
  227. }
  228. // 选项弹窗菜单 - 参考ss-select样式
  229. .options-menu {
  230. position: absolute;
  231. top: 100%;
  232. left: 0;
  233. width: 100%;
  234. background-color: #393D51;
  235. z-index: 1003; // 比卡片popup稍高
  236. color: #fff;
  237. border: 2rpx solid #393D51;
  238. box-sizing: border-box;
  239. border-radius: 10rpx;
  240. overflow: hidden;
  241. max-height: 600rpx;
  242. overflow: auto;
  243. min-width: 200rpx;
  244. .option-item {
  245. padding: 20rpx 20rpx 20rpx 32rpx;
  246. cursor: pointer;
  247. position: relative;
  248. display: flex;
  249. align-items: center;
  250. // 分隔线
  251. &::after {
  252. content: "";
  253. position: absolute;
  254. bottom: 0%;
  255. left: 50%;
  256. transform: translateX(-50%);
  257. width: 80%;
  258. height: 2rpx;
  259. background-color: #303445;
  260. }
  261. &:last-child::after {
  262. display: none;
  263. }
  264. &:active {
  265. background-color: #fff;
  266. color: #393D51;
  267. &::after {
  268. display: none;
  269. }
  270. }
  271. .iconfont {
  272. font-size: 28rpx;
  273. color: inherit;
  274. margin-right: 16rpx;
  275. }
  276. .option-text {
  277. font-size: 28rpx;
  278. color: inherit;
  279. flex: 1;
  280. }
  281. }
  282. }
  283. // 响应式适配
  284. @media screen and (max-width: 750px) {
  285. .ss-search-button {
  286. font-size: 14px;
  287. padding: 0 0.8rem;
  288. }
  289. }
  290. </style>