index.vue 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <uni-shadow-root class="vant-dist-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'] = 'vant/dist/picker/index'
  21. import { VantComponent } from '../common/component';
  22. import { pickerProps } from './shared';
  23. VantComponent({
  24. classes: ['active-class', 'toolbar-class', 'column-class'],
  25. props: Object.assign(Object.assign({}, pickerProps), { valueKey: {
  26. type: String,
  27. value: 'text',
  28. }, toolbarPosition: {
  29. type: String,
  30. value: 'top',
  31. }, defaultIndex: {
  32. type: Number,
  33. value: 0,
  34. }, columns: {
  35. type: Array,
  36. value: [],
  37. observer(columns = []) {
  38. this.simple = columns.length && !columns[0].values;
  39. if (Array.isArray(this.children) && this.children.length) {
  40. this.setColumns().catch(() => { });
  41. }
  42. },
  43. } }),
  44. beforeCreate() {
  45. Object.defineProperty(this, 'children', {
  46. get: () => this.selectAllComponents('.van-picker__column') || [],
  47. });
  48. },
  49. methods: {
  50. noop() { },
  51. setColumns() {
  52. const { data } = this;
  53. const columns = this.simple ? [{ values: data.columns }] : data.columns;
  54. const stack = columns.map((column, index) => this.setColumnValues(index, column.values));
  55. return Promise.all(stack);
  56. },
  57. emit(event) {
  58. const { type } = event.currentTarget.dataset;
  59. if (this.simple) {
  60. this.$emit(type, {
  61. value: this.getColumnValue(0),
  62. index: this.getColumnIndex(0),
  63. });
  64. }
  65. else {
  66. this.$emit(type, {
  67. value: this.getValues(),
  68. index: this.getIndexes(),
  69. });
  70. }
  71. },
  72. onChange(event) {
  73. if (this.simple) {
  74. this.$emit('change', {
  75. picker: this,
  76. value: this.getColumnValue(0),
  77. index: this.getColumnIndex(0),
  78. });
  79. }
  80. else {
  81. this.$emit('change', {
  82. picker: this,
  83. value: this.getValues(),
  84. index: event.currentTarget.dataset.index,
  85. });
  86. }
  87. },
  88. // get column instance by index
  89. getColumn(index) {
  90. return this.children[index];
  91. },
  92. // get column value by index
  93. getColumnValue(index) {
  94. const column = this.getColumn(index);
  95. return column && column.getValue();
  96. },
  97. // set column value by index
  98. setColumnValue(index, value) {
  99. const column = this.getColumn(index);
  100. if (column == null) {
  101. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  102. }
  103. return column.setValue(value);
  104. },
  105. // get column option index by column index
  106. getColumnIndex(columnIndex) {
  107. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  108. },
  109. // set column option index by column index
  110. setColumnIndex(columnIndex, optionIndex) {
  111. const column = this.getColumn(columnIndex);
  112. if (column == null) {
  113. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  114. }
  115. return column.setIndex(optionIndex);
  116. },
  117. // get options of column by index
  118. getColumnValues(index) {
  119. return (this.children[index] || {}).data.options;
  120. },
  121. // set options of column by index
  122. setColumnValues(index, options, needReset = true) {
  123. const column = this.children[index];
  124. if (column == null) {
  125. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  126. }
  127. const isSame = JSON.stringify(column.data.options) === JSON.stringify(options);
  128. if (isSame) {
  129. return Promise.resolve();
  130. }
  131. return column.set({ options }).then(() => {
  132. if (needReset) {
  133. column.setIndex(0);
  134. }
  135. });
  136. },
  137. // get values of all columns
  138. getValues() {
  139. return this.children.map((child) => child.getValue());
  140. },
  141. // set values of all columns
  142. setValues(values) {
  143. const stack = values.map((value, index) => this.setColumnValue(index, value));
  144. return Promise.all(stack);
  145. },
  146. // get indexes of all columns
  147. getIndexes() {
  148. return this.children.map((child) => child.data.currentIndex);
  149. },
  150. // set indexes of all columns
  151. setIndexes(indexes) {
  152. const stack = indexes.map((optionIndex, columnIndex) => this.setColumnIndex(columnIndex, optionIndex));
  153. return Promise.all(stack);
  154. },
  155. },
  156. });
  157. export default global['__wxComponents']['vant/dist/picker/index']
  158. </script>
  159. <style platform="mp-weixin">
  160. @import '../common/index.css';.van-picker{position:relative;overflow:hidden;-webkit-text-size-adjust:100%;-webkit-user-select:none;user-select:none;background-color:#fff;background-color:var(--picker-background-color,#fff)}.van-picker__toolbar{display:flex;justify-content:space-between;height:44px;height:var(--picker-toolbar-height,44px);line-height:44px;line-height:var(--picker-toolbar-height,44px)}.van-picker__cancel,.van-picker__confirm{padding:0 16px;padding:var(--picker-action-padding,0 16px);font-size:14px;font-size:var(--picker-action-font-size,14px)}.van-picker__cancel--hover,.van-picker__confirm--hover{opacity:.7}.van-picker__confirm{color:#576b95;color:var(--picker-confirm-action-color,#576b95)}.van-picker__cancel{color:#969799;color:var(--picker-cancel-action-color,#969799)}.van-picker__title{max-width:50%;text-align:center;font-weight:500;font-weight:var(--font-weight-bold,500);font-size:16px;font-size:var(--picker-option-font-size,16px)}.van-picker__columns{position:relative;display:flex}.van-picker__column{flex:1 1;width:0}.van-picker__loading{position:absolute;top:0;right:0;bottom:0;left:0;z-index:4;display:flex;align-items:center;justify-content:center;background-color:hsla(0,0%,100%,.9);background-color:var(--picker-loading-mask-color,hsla(0,0%,100%,.9))}.van-picker__mask{top:0;left:0;z-index:2;width:100%;height:100%;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-repeat:no-repeat;background-position:top,bottom;-webkit-backface-visibility:hidden;backface-visibility:hidden}.van-picker__frame,.van-picker__mask{position:absolute;pointer-events:none}.van-picker__frame{top:50%;right:16px;left:16px;z-index:1;transform:translateY(-50%)}
  161. </style>