index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view>
  3. <!-- template对应的原始代码,为保证正常显示,已对其进行隐藏。 -->
  4. <block name="toolbar" v-if="false">
  5. <view :wx:if="showToolbar" class="van-picker__toolbar van-hairline--top-bottom toolbar-class">
  6. <view class="van-picker__cancel" hover-class="van-picker__cancel--hover" hover-stay-time="70" data-type="cancel" @tap="emit">{{ cancelButtonText }}</view>
  7. <view :wx: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" @tap="emit">{{ confirmButtonText }}</view>
  9. </view>
  10. </block>
  11. <view class="van-picker custom-class">
  12. <!-- parse <template is="toolbar" :wx:if="toolbarPosition === 'top'" :data="showToolbar, cancelButtonText, title, confirmButtonText"></template> -->
  13. <block name="toolbar" :wx:if="toolbarPosition === 'top'">
  14. <view :wx:if="showToolbar" class="van-picker__toolbar van-hairline--top-bottom toolbar-class">
  15. <view class="van-picker__cancel" hover-class="van-picker__cancel--hover" hover-stay-time="70" data-type="cancel" @tap="emit">{{ cancelButtonText }}</view>
  16. <view :wx:if="title" class="van-picker__title van-ellipsis">{{ title }}</view>
  17. <view class="van-picker__confirm" hover-class="van-picker__confirm--hover" hover-stay-time="70" data-type="confirm" @tap="emit">{{ confirmButtonText }}</view>
  18. </view>
  19. </block>
  20. <view :wx:if="loading" class="van-picker__loading"><loading color="#1989fa" /></view>
  21. <view class="van-picker__columns" :style="'height: ' + itemHeight * visibleItemCount + 'px'" @touchmove.stop.prevent="noop">
  22. <picker-column
  23. class="van-picker__column"
  24. :wx:for="isSimple(columns) ? [columns] : columns"
  25. wx:key="index"
  26. :data-index="index"
  27. custom-class="column-class"
  28. :value-key="valueKey"
  29. :initial-options="isSimple(columns) ? item : item.values"
  30. :default-index="item.defaultIndex || defaultIndex"
  31. :item-height="itemHeight"
  32. :visible-item-count="visibleItemCount"
  33. active-class="active-class"
  34. @change="onChange($event, { index })"
  35. />
  36. <view class="van-picker__frame van-hairline--top-bottom" :style="'height: ' + itemHeight + 'px'" />
  37. </view>
  38. <!-- parse <template is="toolbar" :wx:if="toolbarPosition === 'bottom'" :data="showToolbar, cancelButtonText, title, confirmButtonText"></template> -->
  39. <block name="toolbar" v-if="false" :wx:if="toolbarPosition === 'bottom'">
  40. <view :wx:if="showToolbar" class="van-picker__toolbar van-hairline--top-bottom toolbar-class">
  41. <view class="van-picker__cancel" hover-class="van-picker__cancel--hover" hover-stay-time="70" data-type="cancel" @tap="emit">{{ cancelButtonText }}</view>
  42. <view :wx:if="title" class="van-picker__title van-ellipsis">{{ title }}</view>
  43. <view class="van-picker__confirm" hover-class="van-picker__confirm--hover" hover-stay-time="70" data-type="confirm" @tap="emit">{{ confirmButtonText }}</view>
  44. </view>
  45. </block>
  46. </view>
  47. <sjs module="isSimple">function isSimple(columns) { return columns.length && !columns[0].values; } module.exports = isSimple;</sjs>
  48. </view>
  49. </template>
  50. <script>
  51. import pickerColumn from '../picker-column/index';
  52. import loading from '../loading/index';
  53. import { VantComponent } from '../common/component';
  54. import { pickerProps } from './shared';
  55. export default {
  56. components: {
  57. pickerColumn,
  58. loading
  59. },
  60. data() {
  61. return {
  62. showToolbar: '',
  63. cancelButtonText: '',
  64. title: '',
  65. confirmButtonText: '',
  66. toolbarPosition: '',
  67. loading: '',
  68. visibleItemCount: 0,
  69. columns: '',
  70. valueKey: '',
  71. defaultIndex: ''
  72. };
  73. },
  74. classes: ['active-class', 'toolbar-class', 'column-class'],
  75. props: Object.assign(Object.assign({}, pickerProps), {
  76. valueKey: {
  77. type: String,
  78. value: 'text'
  79. },
  80. toolbarPosition: {
  81. type: String,
  82. value: 'top'
  83. },
  84. defaultIndex: {
  85. type: Number,
  86. value: 0
  87. },
  88. columns: {
  89. type: Array,
  90. value: [],
  91. observer(columns = []) {
  92. this.simple = columns.length && !columns[0].values;
  93. this.children = this.zpSelectAllComponents('.van-picker__column');
  94. if (Array.isArray(this.children) && this.children.length) {
  95. this.setColumns().catch(() => {});
  96. }
  97. }
  98. }
  99. }),
  100. beforeCreate() {
  101. this.children = [];
  102. },
  103. methods: {
  104. noop() {},
  105. setColumns() {
  106. const { data } = this;
  107. const columns = this.simple
  108. ? [
  109. {
  110. values: data.columns
  111. }
  112. ]
  113. : data.columns;
  114. const stack = columns.map((column, index) => this.setColumnValues(index, column.values));
  115. return Promise.all(stack);
  116. },
  117. emit(event) {
  118. const { type } = event.currentTarget.dataset;
  119. if (this.simple) {
  120. this.$emit(type, {
  121. value: this.getColumnValue(0),
  122. index: this.getColumnIndex(0)
  123. });
  124. } else {
  125. this.$emit(type, {
  126. value: this.getValues(),
  127. index: this.getIndexes()
  128. });
  129. }
  130. },
  131. onChange(event, _dataset) {
  132. /* ---处理dataset begin--- */
  133. this.handleDataset(event, _dataset);
  134. /* ---处理dataset end--- */
  135. if (this.simple) {
  136. this.$emit('change', {
  137. picker: this,
  138. value: this.getColumnValue(0),
  139. index: this.getColumnIndex(0)
  140. });
  141. } else {
  142. this.$emit('change', {
  143. picker: this,
  144. value: this.getValues(),
  145. index: event.currentTarget.dataset.index
  146. });
  147. }
  148. },
  149. getColumn(index) {
  150. return this.children[index];
  151. },
  152. getColumnValue(index) {
  153. const column = this.getColumn(index);
  154. return column && column.getValue();
  155. },
  156. setColumnValue(index, value) {
  157. const column = this.getColumn(index);
  158. if (column == null) {
  159. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  160. }
  161. return column.setValue(value);
  162. },
  163. getColumnIndex(columnIndex) {
  164. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  165. },
  166. setColumnIndex(columnIndex, optionIndex) {
  167. const column = this.getColumn(columnIndex);
  168. if (column == null) {
  169. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  170. }
  171. return column.setIndex(optionIndex);
  172. },
  173. getColumnValues(index) {
  174. return (this.children[index] || {}).data.options;
  175. },
  176. setColumnValues(index, options, needReset = true) {
  177. const column = this.children[index];
  178. if (column == null) {
  179. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  180. }
  181. const isSame = JSON.stringify(column.data.options) === JSON.stringify(options);
  182. if (isSame) {
  183. return Promise.resolve();
  184. }
  185. return column
  186. .set({
  187. options
  188. })
  189. .then(() => {
  190. if (needReset) {
  191. column.setIndex(0);
  192. }
  193. });
  194. },
  195. getValues() {
  196. return this.children.map((child) => child.getValue());
  197. },
  198. setValues(values) {
  199. const stack = values.map((value, index) => this.setColumnValue(index, value));
  200. return Promise.all(stack);
  201. },
  202. getIndexes() {
  203. return this.children.map((child) => child.data.currentIndex);
  204. },
  205. setIndexes(indexes) {
  206. const stack = indexes.map((optionIndex, columnIndex) => this.setColumnIndex(columnIndex, optionIndex));
  207. return Promise.all(stack);
  208. }
  209. }
  210. };
  211. </script>
  212. <style>
  213. @import '../common/index.ttss';
  214. .van-picker {
  215. position: relative;
  216. overflow: hidden;
  217. -webkit-text-size-adjust: 100%;
  218. -webkit-user-select: none;
  219. user-select: none;
  220. background-color: #fff;
  221. background-color: var(--picker-background-color, #fff);
  222. }
  223. .van-picker__toolbar {
  224. display: -webkit-flex;
  225. display: flex;
  226. -webkit-justify-content: space-between;
  227. justify-content: space-between;
  228. height: 44px;
  229. height: var(--picker-toolbar-height, 44px);
  230. line-height: 44px;
  231. line-height: var(--picker-toolbar-height, 44px);
  232. }
  233. .van-picker__cancel,
  234. .van-picker__confirm {
  235. padding: 0 16px;
  236. padding: var(--picker-action-padding, 0 16px);
  237. font-size: 14px;
  238. font-size: var(--picker-action-font-size, 14px);
  239. color: #1989fa;
  240. color: var(--picker-action-text-color, #1989fa);
  241. }
  242. .van-picker__cancel--hover,
  243. .van-picker__confirm--hover {
  244. background-color: #f2f3f5;
  245. background-color: var(--picker-action-active-color, #f2f3f5);
  246. }
  247. .van-picker__title {
  248. max-width: 50%;
  249. text-align: center;
  250. font-weight: 500;
  251. font-weight: var(--font-weight-bold, 500);
  252. font-size: 16px;
  253. font-size: var(--picker-option-font-size, 16px);
  254. }
  255. .van-picker__columns {
  256. position: relative;
  257. display: -webkit-flex;
  258. display: flex;
  259. }
  260. .van-picker__column {
  261. -webkit-flex: 1 1;
  262. flex: 1 1;
  263. width: 0;
  264. }
  265. .van-picker__loading {
  266. position: absolute;
  267. top: 0;
  268. right: 0;
  269. bottom: 0;
  270. left: 0;
  271. z-index: 4;
  272. display: -webkit-flex;
  273. display: flex;
  274. -webkit-align-items: center;
  275. align-items: center;
  276. -webkit-justify-content: center;
  277. justify-content: center;
  278. background-color: hsla(0, 0%, 100%, 0.9);
  279. background-color: var(--picker-loading-mask-color, hsla(0, 0%, 100%, 0.9));
  280. }
  281. .van-picker__frame,
  282. .van-picker__loading .van-loading {
  283. position: absolute;
  284. top: 50%;
  285. left: 0;
  286. z-index: 1;
  287. width: 100%;
  288. -webkit-transform: translateY(-50%);
  289. transform: translateY(-50%);
  290. pointer-events: none;
  291. }
  292. @import '@/lib/vant-weapp/picker/index.ttss';
  293. </style>