picker.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-picker`;
  12. let Picker = class Picker extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.properties = props;
  16. this.externalClasses = ['t-class', 't-class-confirm', 't-class-cancel', 't-class-title'];
  17. this.options = {
  18. multipleSlots: true,
  19. };
  20. this.relations = {
  21. './picker-item': {
  22. type: 'child',
  23. linked() {
  24. this.updateChildren();
  25. },
  26. },
  27. };
  28. this.observers = {
  29. value() {
  30. this.updateChildren();
  31. },
  32. };
  33. this.data = {
  34. classPrefix: name,
  35. };
  36. this.methods = {
  37. getPickerColumns() {
  38. const pickerColumns = this.getRelationNodes('./picker-item');
  39. if (Array.isArray(pickerColumns)) {
  40. return pickerColumns;
  41. }
  42. return [];
  43. },
  44. updateChildren() {
  45. const { value } = this.properties;
  46. const pickerColumns = this.getPickerColumns();
  47. if (!(pickerColumns === null || pickerColumns === void 0 ? void 0 : pickerColumns.length)) {
  48. return;
  49. }
  50. pickerColumns.forEach((child, index) => {
  51. child.setData({
  52. value: (value === null || value === void 0 ? void 0 : value[index]) || '',
  53. });
  54. child.update();
  55. });
  56. },
  57. getSelectedValue() {
  58. const pickerColumns = this.getPickerColumns();
  59. const value = pickerColumns.map((item) => item._selectedValue);
  60. const label = pickerColumns.map((item) => item._selectedLabel);
  61. return [value, label];
  62. },
  63. getColumnIndexes() {
  64. const pickerColumns = this.getPickerColumns();
  65. const columns = pickerColumns.map((pickerColumn, columnIndex) => {
  66. return {
  67. column: columnIndex,
  68. index: pickerColumn._selectedIndex,
  69. };
  70. });
  71. return columns;
  72. },
  73. onConfirm() {
  74. const [value, label] = this.getSelectedValue();
  75. const columns = this.getColumnIndexes();
  76. this.close();
  77. this.triggerEvent('change', { value, label, columns });
  78. this.triggerEvent('confirm', { value, label, columns });
  79. },
  80. triggerColumnChange({ column, index }) {
  81. const [value, label] = this.getSelectedValue();
  82. this.triggerEvent('pick', { value, label, column, index });
  83. },
  84. onCancel() {
  85. this.close();
  86. this.triggerEvent('cancel');
  87. },
  88. onPopupChange(e) {
  89. const { visible } = e.detail;
  90. this.close();
  91. this.triggerEvent('visible-change', { visible });
  92. },
  93. close() {
  94. if (this.data.autoClose) {
  95. this.setData({ visible: false });
  96. }
  97. },
  98. };
  99. }
  100. ready() {
  101. const columns = this.getPickerColumns();
  102. columns.map((column, index) => (column.columnIndex = index));
  103. }
  104. };
  105. Picker = __decorate([
  106. wxComponent()
  107. ], Picker);
  108. export default Picker;