index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <uni-shadow-root class="weapp-lib-picker-index"><view class="van-picker custom-class">
  3. <include v-if="toolbarPosition === 'top'" src="./toolbar.wxml"></include>
  4. <view v-if="loading" class="van-picker__loading">
  5. <loading color="#1989fa"></loading>
  6. </view>
  7. <view class="van-picker__columns" :style="computed.columnsStyle({ itemHeight, visibleItemCount })" @touchmove.stop.prevent="noop">
  8. <picker-column v-for="(item,index) in (computed.columns(columns))" :key="item.index" class="van-picker__column" :data-index="index" custom-class="column-class" :value-key="valueKey" :initial-options="item.values" :default-index="item.defaultIndex || defaultIndex" :item-height="itemHeight" :visible-item-count="visibleItemCount" active-class="active-class" @change="onChange"></picker-column>
  9. <view class="van-picker__mask" :style="computed.maskStyle({ itemHeight, visibleItemCount })"></view>
  10. <view class="van-picker__frame van-hairline--top-bottom" :style="computed.frameStyle({ itemHeight })"></view>
  11. </view>
  12. <include v-if="toolbarPosition === 'bottom'" src="./toolbar.wxml"></include>
  13. </view></uni-shadow-root>
  14. </template>
  15. <wxs src="./index.wxs" module="computed"></wxs>
  16. <script>
  17. import PickerColumn from '../picker-column/index.vue'
  18. import Loading from '../loading/index.vue'
  19. global['__wxVueOptions'] = {components:{'picker-column': PickerColumn,'loading': Loading}}
  20. global['__wxRoute'] = 'weapp/lib/picker/index'
  21. "use strict";
  22. var __assign = (this && this.__assign) || function () {
  23. __assign = Object.assign || function(t) {
  24. for (var s, i = 1, n = arguments.length; i < n; i++) {
  25. s = arguments[i];
  26. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  27. t[p] = s[p];
  28. }
  29. return t;
  30. };
  31. return __assign.apply(this, arguments);
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. var component_1 = require("../common/component");
  35. var shared_1 = require("./shared");
  36. (0, component_1.VantComponent)({
  37. classes: ['active-class', 'toolbar-class', 'column-class'],
  38. props: __assign(__assign({}, shared_1.pickerProps), { valueKey: {
  39. type: String,
  40. value: 'text',
  41. }, toolbarPosition: {
  42. type: String,
  43. value: 'top',
  44. }, defaultIndex: {
  45. type: Number,
  46. value: 0,
  47. }, columns: {
  48. type: Array,
  49. value: [],
  50. observer: function (columns) {
  51. if (columns === void 0) { columns = []; }
  52. this.simple = columns.length && !columns[0].values;
  53. if (Array.isArray(this.children) && this.children.length) {
  54. this.setColumns().catch(function () { });
  55. }
  56. },
  57. } }),
  58. beforeCreate: function () {
  59. var _this = this;
  60. Object.defineProperty(this, 'children', {
  61. get: function () { return _this.selectAllComponents('.van-picker__column') || []; },
  62. });
  63. },
  64. methods: {
  65. noop: function () { },
  66. setColumns: function () {
  67. var _this = this;
  68. var data = this.data;
  69. var columns = this.simple ? [{ values: data.columns }] : data.columns;
  70. var stack = columns.map(function (column, index) {
  71. return _this.setColumnValues(index, column.values);
  72. });
  73. return Promise.all(stack);
  74. },
  75. emit: function (event) {
  76. var type = event.currentTarget.dataset.type;
  77. if (this.simple) {
  78. this.$emit(type, {
  79. value: this.getColumnValue(0),
  80. index: this.getColumnIndex(0),
  81. });
  82. }
  83. else {
  84. this.$emit(type, {
  85. value: this.getValues(),
  86. index: this.getIndexes(),
  87. });
  88. }
  89. },
  90. onChange: function (event) {
  91. if (this.simple) {
  92. this.$emit('change', {
  93. picker: this,
  94. value: this.getColumnValue(0),
  95. index: this.getColumnIndex(0),
  96. });
  97. }
  98. else {
  99. this.$emit('change', {
  100. picker: this,
  101. value: this.getValues(),
  102. index: event.currentTarget.dataset.index,
  103. });
  104. }
  105. },
  106. // get column instance by index
  107. getColumn: function (index) {
  108. return this.children[index];
  109. },
  110. // get column value by index
  111. getColumnValue: function (index) {
  112. var column = this.getColumn(index);
  113. return column && column.getValue();
  114. },
  115. // set column value by index
  116. setColumnValue: function (index, value) {
  117. var column = this.getColumn(index);
  118. if (column == null) {
  119. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  120. }
  121. return column.setValue(value);
  122. },
  123. // get column option index by column index
  124. getColumnIndex: function (columnIndex) {
  125. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  126. },
  127. // set column option index by column index
  128. setColumnIndex: function (columnIndex, optionIndex) {
  129. var column = this.getColumn(columnIndex);
  130. if (column == null) {
  131. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  132. }
  133. return column.setIndex(optionIndex);
  134. },
  135. // get options of column by index
  136. getColumnValues: function (index) {
  137. return (this.children[index] || {}).data.options;
  138. },
  139. // set options of column by index
  140. setColumnValues: function (index, options, needReset) {
  141. if (needReset === void 0) { needReset = true; }
  142. var column = this.children[index];
  143. if (column == null) {
  144. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  145. }
  146. var isSame = JSON.stringify(column.data.options) === JSON.stringify(options);
  147. if (isSame) {
  148. return Promise.resolve();
  149. }
  150. return column.set({ options: options }).then(function () {
  151. if (needReset) {
  152. column.setIndex(0);
  153. }
  154. });
  155. },
  156. // get values of all columns
  157. getValues: function () {
  158. return this.children.map(function (child) { return child.getValue(); });
  159. },
  160. // set values of all columns
  161. setValues: function (values) {
  162. var _this = this;
  163. var stack = values.map(function (value, index) {
  164. return _this.setColumnValue(index, value);
  165. });
  166. return Promise.all(stack);
  167. },
  168. // get indexes of all columns
  169. getIndexes: function () {
  170. return this.children.map(function (child) { return child.data.currentIndex; });
  171. },
  172. // set indexes of all columns
  173. setIndexes: function (indexes) {
  174. var _this = this;
  175. var stack = indexes.map(function (optionIndex, columnIndex) {
  176. return _this.setColumnIndex(columnIndex, optionIndex);
  177. });
  178. return Promise.all(stack);
  179. },
  180. },
  181. });
  182. export default global['__wxComponents']['weapp/lib/picker/index']
  183. </script>
  184. <style platform="mp-weixin">
  185. @import '../common/index.css';.van-picker{-webkit-text-size-adjust:100%;background-color:var(--picker-background-color,#fff);overflow:hidden;position:relative;-webkit-user-select:none;user-select:none}.van-picker__toolbar{display:flex;height:var(--picker-toolbar-height,44px);justify-content:space-between;line-height:var(--picker-toolbar-height,44px)}.van-picker__cancel,.van-picker__confirm{font-size:var(--picker-action-font-size,14px);padding:var(--picker-action-padding,0 16px)}.van-picker__cancel--hover,.van-picker__confirm--hover{opacity:.7}.van-picker__confirm{color:var(--picker-confirm-action-color,#576b95)}.van-picker__cancel{color:var(--picker-cancel-action-color,#969799)}.van-picker__title{font-size:var(--picker-option-font-size,16px);font-weight:var(--font-weight-bold,500);max-width:50%;text-align:center}.van-picker__columns{display:flex;position:relative}.van-picker__column{flex:1 1;width:0}.van-picker__loading{align-items:center;background-color:var(--picker-loading-mask-color,hsla(0,0%,100%,.9));bottom:0;display:flex;justify-content:center;left:0;position:absolute;right:0;top:0;z-index:4}.van-picker__mask{-webkit-backface-visibility:hidden;backface-visibility:hidden;background-image:linear-gradient(180deg,hsla(0,0%,100%,.9),hsla(0,0%,100%,.4)),linear-gradient(0deg,hsla(0,0%,100%,.9),hsla(0,0%,100%,.4));background-position:top,bottom;background-repeat:no-repeat;height:100%;left:0;top:0;width:100%;z-index:2}.van-picker__frame,.van-picker__mask{pointer-events:none;position:absolute}.van-picker__frame{left:16px;right:16px;top:50%;transform:translateY(-50%);z-index:1}
  186. </style>