index.vue 8.1 KB

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