index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { VantComponent } from '../common/component';
  2. import { pickerProps } from './shared';
  3. VantComponent({
  4. classes: ['active-class', 'toolbar-class', 'column-class'],
  5. props: Object.assign(Object.assign({}, pickerProps), {
  6. valueKey: {
  7. type: String,
  8. value: 'text',
  9. },
  10. toolbarPosition: {
  11. type: String,
  12. value: 'top',
  13. },
  14. showToolbar: {
  15. type: Boolean,
  16. value: true,
  17. },
  18. defaultIndex: {
  19. type: Number,
  20. value: 0,
  21. },
  22. columns: {
  23. type: Array,
  24. value: [],
  25. observer(columns = []) {
  26. this.simple = columns.length && !columns[0].values;
  27. if (Array.isArray(this.children) && this.children.length) {
  28. this.setColumns().catch(() => {});
  29. }
  30. },
  31. },
  32. }),
  33. beforeCreate() {
  34. Object.defineProperty(this, 'children', {
  35. get: () => this.selectAllComponents('.van-picker__column') || [],
  36. });
  37. },
  38. methods: {
  39. noop() {},
  40. setColumns() {
  41. const { data } = this;
  42. const columns = this.simple ? [{ values: data.columns }] : data.columns;
  43. const stack = columns.map((column, index) =>
  44. this.setColumnValues(index, column.values)
  45. );
  46. return Promise.all(stack);
  47. },
  48. emit(event) {
  49. const { type } = event.currentTarget.dataset;
  50. if (this.simple) {
  51. this.$emit(type, {
  52. value: this.getColumnValue(0),
  53. index: this.getColumnIndex(0),
  54. });
  55. } else {
  56. this.$emit(type, {
  57. value: this.getValues(),
  58. index: this.getIndexes(),
  59. });
  60. }
  61. },
  62. onChange(event) {
  63. if (this.simple) {
  64. this.$emit('change', {
  65. picker: this,
  66. value: this.getColumnValue(0),
  67. index: this.getColumnIndex(0),
  68. });
  69. } else {
  70. this.$emit('change', {
  71. picker: this,
  72. value: this.getValues(),
  73. index: event.currentTarget.dataset.index,
  74. });
  75. }
  76. },
  77. // get column instance by index
  78. getColumn(index) {
  79. return this.children[index];
  80. },
  81. // get column value by index
  82. getColumnValue(index) {
  83. const column = this.getColumn(index);
  84. return column && column.getValue();
  85. },
  86. // set column value by index
  87. setColumnValue(index, value) {
  88. const column = this.getColumn(index);
  89. if (column == null) {
  90. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  91. }
  92. return column.setValue(value);
  93. },
  94. // get column option index by column index
  95. getColumnIndex(columnIndex) {
  96. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  97. },
  98. // set column option index by column index
  99. setColumnIndex(columnIndex, optionIndex) {
  100. const column = this.getColumn(columnIndex);
  101. if (column == null) {
  102. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  103. }
  104. return column.setIndex(optionIndex);
  105. },
  106. // get options of column by index
  107. getColumnValues(index) {
  108. return (this.children[index] || {}).data.options;
  109. },
  110. // set options of column by index
  111. setColumnValues(index, options, needReset = true) {
  112. const column = this.children[index];
  113. if (column == null) {
  114. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  115. }
  116. const isSame =
  117. JSON.stringify(column.data.options) === JSON.stringify(options);
  118. if (isSame) {
  119. return Promise.resolve();
  120. }
  121. return column.set({ options }).then(() => {
  122. if (needReset) {
  123. column.setIndex(0);
  124. }
  125. });
  126. },
  127. // get values of all columns
  128. getValues() {
  129. return this.children.map((child) => child.getValue());
  130. },
  131. // set values of all columns
  132. setValues(values) {
  133. const stack = values.map((value, index) =>
  134. this.setColumnValue(index, value)
  135. );
  136. return Promise.all(stack);
  137. },
  138. // get indexes of all columns
  139. getIndexes() {
  140. return this.children.map((child) => child.data.currentIndex);
  141. },
  142. // set indexes of all columns
  143. setIndexes(indexes) {
  144. const stack = indexes.map((optionIndex, columnIndex) =>
  145. this.setColumnIndex(columnIndex, optionIndex)
  146. );
  147. return Promise.all(stack);
  148. },
  149. },
  150. });