index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view>
  3. <view class="u-dropdown-select" :class="{ 'active': showOptions }" @touchstart="touchStart">
  4. <view class="u-dropdown-label">{{ label || placeholder }}</view>
  5. <van-icon name="arrow-down" />
  6. </view>
  7. <view v-if="showOptions" class="u-dropdown-select-mask" :style="{ 'top': offsetTop +'px' }" @click="showOptions = false"></view>
  8. <view v-if="showOptions" class="u-dropdown-panel" :style="{ 'top': offsetTop +'px' }">
  9. <!-- <view v-if="tabs.length" class="u-dropdown-tabs">
  10. <u-tabs :active.sync="tabActive" :tabs="tabs" tab-style="tag" @change="tabChange"/>
  11. </view>
  12. <view class="u-dropdown-options">
  13. <template v-if="list.length">
  14. <template v-for="(item, index) in list">
  15. <view class="u-dropdown-option" :class="{ 'active': item.active }" @click="clickOption(item, list, index)">
  16. <view>{{ item.label }}</view>
  17. <van-icon class="van-icon" name="arrow" />
  18. </view>
  19. </template>
  20. </template>
  21. <van-empty v-else class="van-empty" description="没有数据"/>
  22. </view>
  23. <view class="u-dropdown-action">
  24. <van-button type="info" class="button-info" block @click="reset">重置</van-button>
  25. <van-button type="primary" class="button-primary" block @click="confirm">确定</van-button>
  26. </view>-->
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import UTabs from '@/components/common/u-tabs/index.vue'
  32. export default {
  33. options: {
  34. styleIsolation: 'shared'
  35. },
  36. components: {
  37. UTabs
  38. },
  39. props: {
  40. placeholder: String,
  41. options: Array,
  42. dataApi: Function,
  43. dataParams: Object,
  44. value: [String, Number]
  45. },
  46. watch: {
  47. options() {
  48. this.initComponent()
  49. }
  50. },
  51. data() {
  52. return {
  53. showOptions: false,
  54. offsetTop: 0,
  55. tabActive: 3,
  56. tabs: [],
  57. list: [],
  58. label: ''
  59. }
  60. },
  61. created() {
  62. this.initComponent()
  63. },
  64. mounted() {
  65. },
  66. methods: {
  67. initComponent() {
  68. if (this.options) {
  69. this.setOptions(this.options)
  70. }
  71. if (this.value) {
  72. this.setLabel(this.value)
  73. }
  74. if (this.dataApi) {
  75. this.dataApi(this.dataParams || {}).then(res => {
  76. this.setOptions(res.data)
  77. })
  78. }
  79. },
  80. tabChange(e) {
  81. this.list = this.tabs[e.detail.index].children
  82. },
  83. setOptions(options) {
  84. if (options) {
  85. if (!options.length) {
  86. this.tabs = []
  87. this.list = []
  88. } else if (!options[0].children || options[0].children.length === 0) {
  89. this.tabs = []
  90. this.list = options.map(v => {
  91. if (v.value === this.value) {
  92. v.active = true
  93. } else {
  94. v.active = false
  95. }
  96. return v
  97. })
  98. } else {
  99. this.tabs = options.map(v => {
  100. return {
  101. label: v.label,
  102. value: v.value,
  103. children: v.children || []
  104. }
  105. })
  106. this.list = this.tabs[0].children.map(v => {
  107. if (v.value === this.value) {
  108. v.active = true
  109. } else {
  110. v.active = false
  111. }
  112. return v
  113. })
  114. this.tabActive = this.tabs[0].value
  115. }
  116. }
  117. },
  118. setLabel(value) {
  119. const option = this.getSelectOption(value)
  120. if (option) {
  121. if (option.tab) {
  122. this.label = option.tab.label + ' / ' + option.item.label
  123. } else {
  124. this.label = option.label
  125. }
  126. } else {
  127. this.label = ''
  128. }
  129. },
  130. reset() {
  131. this.cleanListActive()
  132. this.label = ''
  133. this.showOptions = false
  134. this.$emit('input', null)
  135. this.$emit('change', {
  136. detail: {
  137. value: null
  138. }
  139. })
  140. },
  141. confirm() {
  142. this.showOptions = false
  143. let val = null
  144. const option = this.getSelectOption()
  145. if (option) {
  146. this.setLabel()
  147. if (option.tab) {
  148. val = option.item.value
  149. } else {
  150. val = option.value
  151. }
  152. this.$emit('input', val)
  153. }
  154. this.$emit('change', {
  155. detail: {
  156. value: val
  157. }
  158. })
  159. },
  160. getSelectOption(value) {
  161. if (this.tabs.length) {
  162. for (const tab of this.tabs) {
  163. for (const item of tab.children) {
  164. if (item.active || item.value === value) {
  165. return {
  166. tab: tab,
  167. item: item
  168. }
  169. }
  170. }
  171. }
  172. } else {
  173. for (const item of this.list) {
  174. if (item.active || item.value === value) {
  175. return item
  176. }
  177. }
  178. }
  179. },
  180. cleanListActive() {
  181. if (this.tabs && this.tabs.length) {
  182. for (const tab of this.tabs) {
  183. tab.children.forEach(item => {
  184. this.$set(item, 'active', false)
  185. })
  186. }
  187. }
  188. this.list.forEach(item => {
  189. this.$set(item, 'active', false)
  190. })
  191. // this.$emit('change', {
  192. // detail: {
  193. // value: null
  194. // }
  195. // })
  196. // this.$emit('input', null)
  197. },
  198. clickOption(item, list, index) {
  199. this.cleanListActive()
  200. this.$set(item, 'active', true)
  201. this.$set(list, index, item)
  202. },
  203. touchStart() {
  204. this.showOptions = !this.showOptions
  205. const query = uni.createSelectorQuery().in(this)
  206. query.select('.u-dropdown-select').boundingClientRect(data => {
  207. if (data) {
  208. this.offsetTop = data.top + data.height
  209. // data.top 就是元素的offsetTop
  210. }
  211. }).exec()
  212. if (this.showOptions) {
  213. this.$emit('dropdown')
  214. }
  215. },
  216. showDropdown() {
  217. this.showOptions = true
  218. },
  219. hideDropdown() {
  220. this.showOptions = false
  221. }
  222. }
  223. }
  224. </script>
  225. <style lang="scss">
  226. .u-dropdown-tabs{
  227. padding: 10rpx 0;
  228. margin-top: 20rpx;
  229. }
  230. .van-empty{
  231. --image-width: 160rpx;
  232. --image-height: 160rpx;
  233. }
  234. .u-dropdown-select{
  235. @include display-flex-between;
  236. min-width: 211rpx;
  237. height: 50rpx;
  238. padding: 0 10rpx;
  239. border: $borderLight;
  240. border-radius: 8rpx;
  241. background-color: #FFFFFF;
  242. border-radius: 8rpx;
  243. border: 1rpx solid #D9D9D9;
  244. &.active{
  245. background-color: $buttonPrimaryColor;
  246. border-color: $buttonPrimaryColor;
  247. color: white;
  248. }
  249. .van-icon{
  250. font-size: 15rpx;
  251. }
  252. }
  253. .u-dropdown-label{
  254. max-width: 100%;
  255. @include text-ellipsis;
  256. font-size: $fontSize2;
  257. }
  258. .u-dropdown-tabs{
  259. --tabs-line-height: 44px;
  260. --tabs-card-height: 30px;
  261. --tabs-bottom-bar-height: 0rpx;
  262. --tab-active-text-color: #E57519;
  263. }
  264. .u-dropdown-panel{
  265. position: fixed;
  266. left: 0;
  267. top: 0;
  268. display: block;
  269. width: 100%;
  270. z-index: 9999;
  271. background-color: white;
  272. }
  273. .u-dropdown-options{
  274. min-height: 100rpx;
  275. max-height: 400rpx;
  276. padding: 20rpx;
  277. overflow-y: auto;
  278. }
  279. .u-dropdown-option{
  280. line-height: 50rpx;
  281. @include display-flex-between;
  282. font-size: $fontSize3;
  283. &.active{
  284. color: $buttonPrimaryColor;
  285. }
  286. }
  287. .u-dropdown-action{
  288. display: grid;
  289. grid-template-columns: 1fr 1fr;
  290. .van-button{
  291. border-radius: 0rpx;
  292. height: 80rpx;
  293. font-size: $fontSize3;
  294. }
  295. }
  296. .van-empty__description{
  297. font-size: $fontSize3;
  298. margin-top: 0rpx;
  299. }
  300. .button-info{
  301. --button-info-background-color: black;
  302. --button-info-border-color: black;
  303. --button-info-color: white;
  304. }
  305. .u-dropdown-select-mask{
  306. position: fixed;
  307. display: block;
  308. left: 0;
  309. right: 0;
  310. bottom: 0;
  311. z-index: 9998;
  312. background-color: rgba(0, 0, 0, 0.5);
  313. }
  314. </style>