index.vue 8.7 KB

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