index.vue 7.8 KB

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