w-picker.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view class="w-picker">
  3. <view class="mask" :class="{'visible':visible}" @tap="onCancel" @touchmove.stop.prevent catchtouchmove="true"></view>
  4. <view class="w-picker-cnt" :class="{'visible':visible}">
  5. <view class="w-picker-header" @touchmove.stop.prevent catchtouchmove="true">
  6. <text @tap="onCancel">取消</text>
  7. <slot></slot>
  8. <text class="queidng" :style="{'color':themeColor}" @tap="pickerConfirm">确定</text>
  9. </view>
  10. <date-picker
  11. v-if="mode=='date'"
  12. class="w-picker-wrapper"
  13. :startYear="startYear"
  14. :endYear="endYear"
  15. :value="value"
  16. :fields="fields"
  17. :item-height="itemHeight"
  18. :current="current"
  19. :disabled-after="disabledAfter"
  20. @change="handlerChange"
  21. @touchstart="touchStart"
  22. @touchend="touchEnd">
  23. </date-picker>
  24. <range-picker
  25. v-if="mode=='range'"
  26. class="w-picker-wrapper"
  27. :startYear="startYear"
  28. :endYear="endYear"
  29. :value="value"
  30. :item-height="itemHeight"
  31. :current="current"
  32. @change="handlerChange"
  33. @touchstart="touchStart"
  34. @touchend="touchEnd">
  35. </range-picker>
  36. <half-picker
  37. v-if="mode=='half'"
  38. class="w-picker-wrapper"
  39. :startYear="startYear"
  40. :endYear="endYear"
  41. :value="value"
  42. :item-height="itemHeight"
  43. :current="current"
  44. :disabled-after="disabledAfter"
  45. @change="handlerChange"
  46. @touchstart="touchStart"
  47. @touchend="touchEnd">
  48. </half-picker>
  49. <shortterm-picker
  50. v-if="mode=='shortTerm'"
  51. class="w-picker-wrapper"
  52. :startYear="startYear"
  53. :endYear="endYear"
  54. :value="value"
  55. :item-height="itemHeight"
  56. :current="current"
  57. expand="60"
  58. :disabled-after="disabledAfter"
  59. @change="handlerChange"
  60. @touchstart="touchStart"
  61. @touchend="touchEnd">
  62. </shortterm-picker>
  63. <time-picker
  64. v-if="mode=='time'"
  65. class="w-picker-wrapper"
  66. :value="value"
  67. :item-height="itemHeight"
  68. :current="current"
  69. :disabled-after="disabledAfter"
  70. :second="second"
  71. @change="handlerChange"
  72. @touchstart="touchStart"
  73. @touchend="touchEnd">
  74. </time-picker>
  75. <selector-picker
  76. v-if="mode=='selector'"
  77. class="w-picker-wrapper"
  78. :value="value"
  79. :item-height="itemHeight"
  80. :options="options"
  81. :default-type="defaultType"
  82. :default-props="defaultProps"
  83. @change="handlerChange"
  84. @touchstart="touchStart"
  85. @touchend="touchEnd">
  86. </selector-picker>
  87. <region-picker
  88. v-if="mode=='region'"
  89. class="w-picker-wrapper"
  90. :value="value"
  91. :hide-area="hideArea"
  92. :default-type="defaultType"
  93. :item-height="itemHeight"
  94. @change="handlerChange"
  95. @touchstart="touchStart"
  96. @touchend="touchEnd">
  97. </region-picker>
  98. <linkage-picker
  99. v-if="mode=='linkage'"
  100. class="w-picker-wrapper"
  101. :value="value"
  102. :options="options"
  103. :level="level"
  104. :default-type="defaultType"
  105. :default-props="defaultProps"
  106. :item-height="itemHeight"
  107. @change="handlerChange"
  108. @touchstart="touchStart"
  109. @touchend="touchEnd">
  110. </linkage-picker>
  111. </view>
  112. </view>
  113. </template>
  114. <script>
  115. import datePicker from "./date-picker.vue"
  116. import rangePicker from "./range-picker.vue"
  117. import halfPicker from "./half-picker.vue"
  118. import shorttermPicker from "./shortterm-picker.vue"
  119. import timePicker from "./time-picker.vue"
  120. import selectorPicker from "./selector-picker.vue"
  121. import regionPicker from "./region-picker.vue"
  122. import linkagePicker from "./linkage-picker.vue"
  123. export default {
  124. components:{
  125. datePicker,
  126. rangePicker,
  127. halfPicker,
  128. timePicker,
  129. selectorPicker,
  130. shorttermPicker,
  131. regionPicker,
  132. linkagePicker
  133. },
  134. props:{
  135. mode:{
  136. type:String,
  137. default:"date"
  138. },
  139. value:{//默认值
  140. type:[String,Array,Number],
  141. default:""
  142. },
  143. current:{//是否默认显示当前时间,如果是,传的默认值将失效
  144. type:Boolean,
  145. default:false
  146. },
  147. themeColor:{//确认按钮主题颜色
  148. type:String,
  149. default:"#f5a200"
  150. },
  151. fields:{//日期颗粒度:year、month、day、hour、minute、second
  152. type:String,
  153. default:"date"
  154. },
  155. disabledAfter:{//是否禁用当前之后的日期
  156. type:Boolean,
  157. default:false
  158. },
  159. second:{//time-picker是否显示秒
  160. type:Boolean,
  161. default:true
  162. },
  163. options:{//selector,region数据源
  164. type:[Array,Object],
  165. default(){
  166. return []
  167. }
  168. },
  169. defaultProps:{//selector,linkagle字段转换配置
  170. type:Object,
  171. default(){
  172. return{
  173. lable:"label",
  174. value:"value",
  175. children:"children"
  176. }
  177. }
  178. },
  179. defaultType:{
  180. type:String,
  181. default:"label"
  182. },
  183. hideArea:{//mode=region时,是否隐藏区县列
  184. type:Boolean,
  185. default:false
  186. },
  187. level:{
  188. //多级联动层级,表示几级联动,区间2-4;
  189. type:[Number,String],
  190. default:2
  191. },
  192. timeout:{//是否开启点击延迟,当快速滚动 还没有滚动完毕点击关闭时得到的值是不准确的
  193. type:Boolean,
  194. default:false
  195. },
  196. expand:{//mode=shortterm 默认往后拓展天数
  197. type:[Number,String],
  198. default:30
  199. },
  200. startYear:{
  201. type:[String,Number],
  202. default:1970
  203. },
  204. endYear:{
  205. type:[String,Number],
  206. default:new Date().getFullYear()
  207. }
  208. },
  209. data() {
  210. return {
  211. itemHeight:`height: ${uni.upx2px(88)}px;`,
  212. visible:false,
  213. result:{},
  214. confirmFlag:true
  215. };
  216. },
  217. methods:{
  218. touchStart(){
  219. if(this.timeout){
  220. this.confirmFlag=false;
  221. }
  222. },
  223. touchEnd(){
  224. if(this.timeout){
  225. setTimeout(()=>{
  226. this.confirmFlag=true;
  227. },500)
  228. }
  229. },
  230. handlerChange(res){
  231. this.result=res;
  232. this.$emit("confirm",this.result);
  233. },
  234. show(){
  235. this.visible=true;
  236. },
  237. hide(){
  238. this.visible=false;
  239. },
  240. onCancel(res){
  241. this.visible=false;
  242. this.$emit("cancel");
  243. },
  244. pickerConfirm(){
  245. // if(!this.confirmFlag){
  246. // return;
  247. // }
  248. // this.$emit("confirm",this.result);
  249. this.visible=false;
  250. }
  251. }
  252. }
  253. </script>
  254. <style lang="scss">
  255. .w-picker-item {
  256. text-align: center;
  257. width: 100%;
  258. height: 88upx;
  259. line-height: 88upx;
  260. text-overflow: ellipsis;
  261. white-space: nowrap;
  262. font-size: 30upx;
  263. }
  264. .queidng{
  265. width: 250px;
  266. height: 80rpx;
  267. line-height: 80rpx;
  268. text-align: right;
  269. }
  270. .w-picker{
  271. position: relative;
  272. z-index: 888;
  273. .mask {
  274. position: fixed;
  275. z-index: 1000;
  276. top: 0;
  277. right: 0;
  278. left: 0;
  279. bottom: 0;
  280. background: rgba(0, 0, 0, 0.6);
  281. visibility: hidden;
  282. opacity: 0;
  283. transition: all 0.3s ease;
  284. }
  285. .mask.visible{
  286. visibility: visible;
  287. opacity: 1;
  288. }
  289. .w-picker-cnt {
  290. position: fixed;
  291. bottom: 38px;
  292. left: 0;
  293. width: 100%;
  294. transition: all 0.3s ease;
  295. transform: translateY(100%);
  296. z-index: 9999;
  297. background-color: #fff;
  298. }
  299. .w-picker-cnt.visible {
  300. transform: translateY(0);
  301. }
  302. .w-picker-header{
  303. display: flex;
  304. align-items: center;
  305. padding: 0 30upx;
  306. height: 88upx;
  307. background-color: #fff;
  308. position: relative;
  309. text-align: center;
  310. font-size: 32upx;
  311. justify-content: space-between;
  312. border-bottom: solid 1px #eee;
  313. .w-picker-btn{
  314. font-size: 30upx;
  315. }
  316. }
  317. .w-picker-hd:after {
  318. content: ' ';
  319. position: absolute;
  320. left: 0;
  321. bottom: 0;
  322. right: 0;
  323. height: 1px;
  324. border-bottom: 1px solid #e5e5e5;
  325. color: #e5e5e5;
  326. transform-origin: 0 100%;
  327. transform: scaleY(0.5);
  328. }
  329. .w-picker-wrapper {
  330. width: 100%;
  331. height: 476upx;
  332. overflow: hidden;
  333. background-color: rgba(255, 255, 255, 1);
  334. z-index: 666;
  335. }
  336. }
  337. </style>