picker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = [`${prefix}-class`, `${prefix}-class-confirm`, `${prefix}-class-cancel`, `${prefix}-class-title`];
  17. this.options = {
  18. multipleSlots: true,
  19. };
  20. this.relations = {
  21. '../picker-item/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. prefix,
  35. classPrefix: name,
  36. };
  37. this.methods = {
  38. updateChildren() {
  39. const { value } = this.properties;
  40. this.$children.forEach((child, index) => {
  41. child.setData({
  42. value: (value === null || value === void 0 ? void 0 : value[index]) || '',
  43. siblingCount: this.$children.length,
  44. });
  45. child.update();
  46. });
  47. },
  48. getSelectedValue() {
  49. const value = this.$children.map((item) => item._selectedValue);
  50. const label = this.$children.map((item) => item._selectedLabel);
  51. return [value, label];
  52. },
  53. getColumnIndexes() {
  54. const columns = this.$children.map((pickerColumn, columnIndex) => {
  55. return {
  56. column: columnIndex,
  57. index: pickerColumn._selectedIndex,
  58. };
  59. });
  60. return columns;
  61. },
  62. onConfirm() {
  63. const [value, label] = this.getSelectedValue();
  64. const columns = this.getColumnIndexes();
  65. this.close();
  66. this.triggerEvent('change', { value, label, columns });
  67. this.triggerEvent('confirm', { value, label, columns });
  68. },
  69. triggerColumnChange({ column, index }) {
  70. const [value, label] = this.getSelectedValue();
  71. this.triggerEvent('pick', { value, label, column, index });
  72. },
  73. onCancel() {
  74. this.close();
  75. this.triggerEvent('cancel');
  76. },
  77. onPopupChange(e) {
  78. const { visible } = e.detail;
  79. this.close();
  80. this.triggerEvent('visible-change', { visible });
  81. },
  82. close() {
  83. if (this.data.autoClose) {
  84. this.setData({ visible: false });
  85. }
  86. },
  87. };
  88. }
  89. ready() {
  90. this.$children.map((column, index) => (column.columnIndex = index));
  91. }
  92. };
  93. Picker = __decorate([
  94. wxComponent()
  95. ], Picker);
  96. export default Picker;