picker.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. keys(obj) {
  33. this.setData({
  34. labelAlias: obj.label || 'label',
  35. valueAlias: obj.value || 'value',
  36. });
  37. },
  38. };
  39. this.data = {
  40. prefix,
  41. classPrefix: name,
  42. labelAlias: 'label',
  43. valueAlias: 'value',
  44. };
  45. this.methods = {
  46. updateChildren() {
  47. const { value } = this.properties;
  48. this.$children.forEach((child, index) => {
  49. child.setData({
  50. value: (value === null || value === void 0 ? void 0 : value[index]) || '',
  51. });
  52. child.update();
  53. });
  54. },
  55. getSelectedValue() {
  56. const value = this.$children.map((item) => item._selectedValue);
  57. const label = this.$children.map((item) => item._selectedLabel);
  58. return [value, label];
  59. },
  60. getColumnIndexes() {
  61. const columns = this.$children.map((pickerColumn, columnIndex) => {
  62. return {
  63. column: columnIndex,
  64. index: pickerColumn._selectedIndex,
  65. };
  66. });
  67. return columns;
  68. },
  69. onConfirm() {
  70. const [value, label] = this.getSelectedValue();
  71. const columns = this.getColumnIndexes();
  72. this.close('confirm-btn');
  73. this.triggerEvent('change', { value, label, columns });
  74. this.triggerEvent('confirm', { value, label, columns });
  75. },
  76. triggerColumnChange({ column, index }) {
  77. const [value, label] = this.getSelectedValue();
  78. this.triggerEvent('pick', { value, label, column, index });
  79. },
  80. onCancel() {
  81. this.close('cancel-btn');
  82. this.triggerEvent('cancel');
  83. },
  84. onPopupChange(e) {
  85. const { visible } = e.detail;
  86. this.close('overlay');
  87. this.triggerEvent('visible-change', { visible });
  88. },
  89. close(trigger) {
  90. if (this.data.autoClose) {
  91. this.setData({ visible: false });
  92. }
  93. this.triggerEvent('close', { trigger });
  94. },
  95. };
  96. }
  97. ready() {
  98. this.$children.map((column, index) => (column.columnIndex = index));
  99. }
  100. };
  101. Picker = __decorate([
  102. wxComponent()
  103. ], Picker);
  104. export default Picker;