index.vue 7.6KB

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