Sin descripción

xp-picker.vue 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="xp-h-full">
  3. <view @tap="show" class="xp-h-full">
  4. <slot>
  5. <view class="picker-label xp-h-full" :class="{'is-placeholder':label===placeholder}">{{label}}
  6. </view>
  7. </slot>
  8. </view>
  9. <view class="xp-picker" :style="{'visibility':pickerVisible?'visible':'hidden'}">
  10. <view class="xp-picker-mask" :class="{'xp-picker-animation':animation}"
  11. :style="{'opacity':pickerVisible?0.6:0}" @tap="_cancel"></view>
  12. <view class="xp-picker-container"
  13. :class="{'xp-picker-container--show':pickerVisible,'xp-picker-animation':animation}">
  14. <view v-if="actionPosition==='top'" class="xp-picker-action">
  15. <view class="xp-picker-action--cancel" @tap="_cancel">取消</view>
  16. <view class="xp-picker-action--confirm" @tap="_confirm">确定</view>
  17. </view>
  18. <view v-if="isError" class="xp-picker-error" :style="{'height':height+'vh'}">
  19. <text>(请检查你的配置 或 查看控制台错误信息)</text>
  20. </view>
  21. <!-- #ifdef VUE3 -->
  22. <picker-view v-else style="margin-top: 40rpx;" :style="{'height':height+'vh'}"
  23. indicator-style="height:40px;" :value="selected" @change="_change">
  24. <picker-view-column v-for="(k,i) in modeArr" :key="cols[i].length" class="xp-picker-column">
  25. <view class="xp-picker-list-item" v-for="(item,index) in cols[i]" :key="index">
  26. {{item+units[i]}}
  27. </view>
  28. </picker-view-column>
  29. </picker-view>
  30. <!-- #endif -->
  31. <!-- #ifdef VUE2 -->
  32. <picker-view v-else style="margin-top: 40rpx;" :style="{'height':height+'vh'}"
  33. indicator-style="height:40px;" :value="selected" @change="_change">
  34. <picker-view-column v-for="(k,i) in modeArr" :key="k" class="xp-picker-column">
  35. <view class="xp-picker-list-item" v-for="(item,index) in cols[i]" :key="index">
  36. {{item+units[i]}}
  37. </view>
  38. </picker-view-column>
  39. </picker-view>
  40. <!-- #endif -->
  41. <view v-if="actionPosition==='bottom'" class="xp-picker-btns">
  42. <button class="xp-button xp-button--cancel" @tap="_cancel">取消</button>
  43. <button class="xp-button xp-button--confirm" @tap="_confirm">确定</button>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import {
  51. templateFactory,
  52. getLocalTime,
  53. fmtNumber,
  54. time2Timestamp,
  55. getDate,
  56. getForm
  57. } from "./util.js"
  58. import assert from './assert.js'
  59. export default {
  60. name: 'XpPicker',
  61. data() {
  62. return {
  63. isError: false,
  64. pickerVisible: false,
  65. cols: [],
  66. selected: []
  67. }
  68. },
  69. props: {
  70. mode: {
  71. type: String,
  72. default: "ymd"
  73. },
  74. animation: {
  75. type: Boolean,
  76. default: true
  77. },
  78. height: {
  79. type: [Number, String],
  80. default: 35
  81. },
  82. actionPosition: {
  83. type: String,
  84. default: "bottom"
  85. },
  86. yearRange: {
  87. type: Array,
  88. default: () => [2010, null]
  89. },
  90. value: String,
  91. // #ifdef VUE3
  92. modelValue: String,
  93. // #endif
  94. history: Boolean,
  95. placeholder: {
  96. type: String,
  97. default: '请选择'
  98. }
  99. },
  100. watch: {
  101. mode() {
  102. this.render()
  103. }
  104. },
  105. // #ifdef VUE3
  106. emits: ['confirm', 'update:modelValue'],
  107. // #endif
  108. computed: {
  109. modeArr() {
  110. return this.mode.split("")
  111. },
  112. units() {
  113. const arr = []
  114. for (const k in this.template) {
  115. if (this.mode.indexOf(k) !== -1) arr.push(this.template[k].text)
  116. }
  117. return arr
  118. },
  119. label() {
  120. const val = this.value || this.modelValue
  121. if (val) return val
  122. return this.placeholder
  123. }
  124. },
  125. created() {
  126. this.bindForm()
  127. this.isConfirm = false
  128. this.template = {}
  129. this.render()
  130. // #ifdef VUE2
  131. if (this.value) {
  132. this.setSelected(this.value)
  133. }
  134. // #endif
  135. // #ifdef VUE3
  136. if (this.modelValue) {
  137. this.setSelected(this.modelValue)
  138. }
  139. // #endif
  140. },
  141. methods: {
  142. bindForm() {
  143. this.form = getForm.call(this, 'uniForms')
  144. this.formItem = getForm.call(this, 'uniFormsItem')
  145. if (this.form && this.formItem) {
  146. if (this.formItem.name) {
  147. if (!this.is_reset) {
  148. this.is_reset = false
  149. // #ifdef VUE2
  150. this.formItem.setValue(this.value)
  151. // #endif
  152. // #ifdef VUE3
  153. this.formItem.setValue(this.modelValue)
  154. // #endif
  155. }
  156. this.form.inputChildrens.push(this)
  157. }
  158. }
  159. },
  160. render() {
  161. try {
  162. assert(this)
  163. this.template = templateFactory(this) //生成所需列 默认模板
  164. this.initCols() //根据模板 初始化列
  165. this.initSelected() //设置默认值
  166. } catch (e) {
  167. console.error(e)
  168. this.isError = true
  169. }
  170. },
  171. initCols() {
  172. for (const k of this.mode) {
  173. const range = this.template[k].range
  174. this.fillCol(k, ...range)
  175. }
  176. },
  177. initSelected() {
  178. let dt
  179. // #ifdef VUE2
  180. dt = this.value
  181. // #endif
  182. // #ifdef VUE3
  183. dt = this.modelValue
  184. // #endif
  185. if (!dt) dt = getLocalTime(this.mode)
  186. if (!dt) return
  187. this.setSelected(dt)
  188. },
  189. fillCol(k, s, e) {
  190. const index = this.mode.indexOf(k)
  191. let arr = []
  192. for (let i = s; i <= e; i++)
  193. arr.push(fmtNumber(i))
  194. // #ifdef VUE2
  195. this.$set(this.cols, index, arr)
  196. // #endif
  197. // #ifdef VUE3
  198. this.cols[index] = arr
  199. // #endif
  200. },
  201. //dt 时间字符串 如 '2020-02-16'
  202. setSelected(dt) {
  203. const arr = dt.split(/-|:|\s/)
  204. const a = this.cols
  205. for (let i = 0; i < a.length; i++)
  206. this.$set(this.selected, i, a[i].indexOf(arr[i]))
  207. },
  208. show() {
  209. if ((!this.value && !this.modelValue) || (!this.history) || (this.history && !this.isConfirm)) {
  210. this.initSelected()
  211. }
  212. this.pickerVisible = true
  213. },
  214. _resolveCurrentDt() {
  215. let str = ""
  216. for (let i = 0; i < this.selected.length; i++)
  217. str += this.cols[i][this.selected[i]] + this.units[i]
  218. let dt = str
  219. .replace('年', '-')
  220. .replace('月', '-')
  221. .replace('日', ' ')
  222. .replace('时', ':')
  223. .replace('分', ':')
  224. .replace('秒', '')
  225. if (!this.mode.endsWith('s'))
  226. dt = dt.substring(0, dt.length - 1)
  227. return dt
  228. },
  229. _confirm() {
  230. const result = this._getResult()
  231. const val = result.value
  232. if (!this.isError) {
  233. // #ifdef VUE2
  234. this.$emit('input', val)
  235. // #endif
  236. // #ifdef VUE3
  237. this.$emit('update:modelValue', val)
  238. // #endif
  239. this.$emit('confirm', result)
  240. this.isConfirm = true
  241. if (this.formItem) this.formItem.setValue(val)
  242. }
  243. this.pickerVisible = false
  244. },
  245. _getResult() {
  246. const value = this._resolveCurrentDt()
  247. const detail = {
  248. value
  249. }
  250. const tp = time2Timestamp(value)
  251. if (!isNaN(tp)) detail.timestamp = tp
  252. return detail
  253. },
  254. _cancel() {
  255. this.pickerVisible = false
  256. },
  257. _change(e) {
  258. let col;
  259. const newValue = e.detail.value
  260. for (let i = 0; i < newValue.length; i++) {
  261. if (newValue[i] !== this.selected[i]) {
  262. col = this.modeArr[i]
  263. break
  264. }
  265. }
  266. this.selected = newValue
  267. const index = this.mode.indexOf("d")
  268. if (index !== -1 && (col === 'y' || col === 'm')) {
  269. const currentDt = this._resolveCurrentDt()
  270. this.fillCol("d", 1, getDate(currentDt))
  271. }
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped lang="scss">
  277. .xp-h-full {
  278. height: 100%;
  279. }
  280. .picker-label {
  281. height: 100%;
  282. display: flex;
  283. align-items: center;
  284. padding-left: 10px;
  285. font-size: 13px;
  286. }
  287. .is-placeholder {
  288. color: #999;
  289. }
  290. .xp-picker {
  291. position: fixed;
  292. top: 0;
  293. bottom: 0;
  294. left: 0;
  295. right: 0;
  296. z-index: 999;
  297. font-size: 30rpx;
  298. }
  299. .xp-picker-container {
  300. position: fixed;
  301. bottom: 0;
  302. transform: translateY(100%);
  303. z-index: 999;
  304. width: 100%;
  305. background-color: #fff;
  306. visibility: hidden;
  307. border-radius: 16px 16px 0 0;
  308. }
  309. .xp-picker-container--show {
  310. transform: translateY(0);
  311. visibility: visible;
  312. }
  313. .xp-picker-mask {
  314. z-index: 998;
  315. width: 100%;
  316. height: 100%;
  317. background-color: rgb(0, 0, 0);
  318. }
  319. .xp-picker-animation {
  320. transition: all 0.25s;
  321. }
  322. .xp-picker-error {
  323. width: 100%;
  324. display: flex;
  325. flex-direction: column;
  326. justify-content: center;
  327. align-items: center;
  328. color: #ff0000
  329. }
  330. .xp-picker-action {
  331. width: 100%;
  332. display: flex;
  333. justify-content: space-between;
  334. align-items: center;
  335. height: 90rpx;
  336. padding: 0 28rpx;
  337. box-sizing: border-box;
  338. position: relative;
  339. font-size: 34rpx;
  340. border-bottom: 0.5px solid #e5e5e5
  341. }
  342. .xp-picker-btns {
  343. width: 100%;
  344. display: flex;
  345. justify-content: space-around;
  346. align-items: center;
  347. padding: 40rpx 30rpx;
  348. box-sizing: border-box;
  349. position: relative;
  350. }
  351. .xp-button {
  352. line-height: 2.3;
  353. font-size: 32rpx;
  354. margin: 0;
  355. padding: 0 80rpx;
  356. transform: translate(0upx, 0upx);
  357. }
  358. .xp-button:active:not([disabled]) {
  359. transform: translate(1upx, 1upx);
  360. }
  361. .xp-button:after {
  362. border: none;
  363. }
  364. .xp-button--cancel {
  365. background-color: #f5f5f5;
  366. color: #47a16e;
  367. }
  368. .xp-button--confirm {
  369. background-color: #47a16e;
  370. color: #fff;
  371. }
  372. .xp-picker-action--cancel {
  373. opacity: .7;
  374. }
  375. .xp-picker-action--confirm {
  376. color: #007aff;
  377. }
  378. .xp-picker-column {
  379. text-align: center;
  380. border: none;
  381. font-size: 32rpx;
  382. }
  383. .xp-picker-list-item {
  384. display: flex;
  385. justify-content: center;
  386. align-items: center;
  387. height: 40px;
  388. }
  389. </style>