w-picker.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template name="w-picker">
  2. <view class="w-picker" :key="createKey" :data-key="createKey">
  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.stop.prevent="onCancel">取消</text>
  7. <slot></slot>
  8. <text :style="{'color':themeColor}" @tap.stop.prevent="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. name:"w-picker",
  125. components:{
  126. datePicker,
  127. rangePicker,
  128. halfPicker,
  129. timePicker,
  130. selectorPicker,
  131. shorttermPicker,
  132. regionPicker,
  133. linkagePicker
  134. },
  135. props:{
  136. mode:{
  137. type:String,
  138. default:"date"
  139. },
  140. value:{//默认值
  141. type:[String,Array,Number],
  142. default:""
  143. },
  144. current:{//是否默认显示当前时间,如果是,传的默认值将失效
  145. type:Boolean,
  146. default:false
  147. },
  148. themeColor:{//确认按钮主题颜色
  149. type:String,
  150. default:"#f5a200"
  151. },
  152. fields:{//日期颗粒度:year、month、day、hour、minute、second
  153. type:String,
  154. default:"date"
  155. },
  156. disabledAfter:{//是否禁用当前之后的日期
  157. type:Boolean,
  158. default:false
  159. },
  160. second:{//time-picker是否显示秒
  161. type:Boolean,
  162. default:true
  163. },
  164. options:{//selector,region数据源
  165. type:[Array,Object],
  166. default(){
  167. return []
  168. }
  169. },
  170. defaultProps:{//selector,linkagle字段转换配置
  171. type:Object,
  172. default(){
  173. return{
  174. label:"label",
  175. value:"value",
  176. children:"children"
  177. }
  178. }
  179. },
  180. defaultType:{
  181. type:String,
  182. default:"label"
  183. },
  184. hideArea:{//mode=region时,是否隐藏区县列
  185. type:Boolean,
  186. default:false
  187. },
  188. level:{
  189. //多级联动层级,表示几级联动,区间2-4;
  190. type:[Number,String],
  191. default:2
  192. },
  193. timeout:{//是否开启点击延迟,当快速滚动 还没有滚动完毕点击关闭时得到的值是不准确的
  194. type:Boolean,
  195. default:false
  196. },
  197. expand:{//mode=shortterm 默认往后拓展天数
  198. type:[Number,String],
  199. default:30
  200. },
  201. startYear:{
  202. type:[String,Number],
  203. default:1970
  204. },
  205. endYear:{
  206. type:[String,Number],
  207. default:new Date().getFullYear()
  208. },
  209. visible:{
  210. type:Boolean,
  211. default:false
  212. }
  213. },
  214. created() {
  215. this.createKey=Math.random()*1000;
  216. },
  217. data() {
  218. return {
  219. itemHeight:`height: ${uni.upx2px(88)}px;`,
  220. result:{},
  221. confirmFlag:true
  222. };
  223. },
  224. methods:{
  225. touchStart(){
  226. if(this.timeout){
  227. this.confirmFlag=false;
  228. }
  229. },
  230. touchEnd(){
  231. if(this.timeout){
  232. setTimeout(()=>{
  233. this.confirmFlag=true;
  234. },500)
  235. }
  236. },
  237. handlerChange(res){
  238. let _this=this;
  239. this.result={...res};
  240. },
  241. show(){
  242. this.$emit("update:visible",true);
  243. },
  244. hide(){
  245. this.$emit("update:visible",false);
  246. },
  247. onCancel(res){
  248. this.$emit("update:visible",false);
  249. this.$emit("cancel");
  250. },
  251. pickerConfirm(){
  252. if(!this.confirmFlag){
  253. return;
  254. };
  255. this.$emit("confirm",this.result);
  256. this.$emit("update:visible",false);
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss">
  262. .w-picker-item {
  263. text-align: center;
  264. width: 100%;
  265. height: 88upx;
  266. line-height: 88upx;
  267. text-overflow: ellipsis;
  268. white-space: nowrap;
  269. font-size: 30upx;
  270. }
  271. .w-picker{
  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: 0;
  292. left: 0;
  293. width: 100%;
  294. transition: all 0.3s ease;
  295. transform: translateY(100%);
  296. z-index: 3000;
  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. }
  330. </style>