picker.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { rpx2px } from '../common/utils';
  9. import config from '../common/config';
  10. import props from './props';
  11. import useCustomNavbar from '../mixins/using-custom-navbar';
  12. const { prefix } = config;
  13. const name = `${prefix}-picker`;
  14. let Picker = class Picker extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.behaviors = [useCustomNavbar];
  18. this.properties = props;
  19. this.externalClasses = [`${prefix}-class`, `${prefix}-class-confirm`, `${prefix}-class-cancel`, `${prefix}-class-title`];
  20. this.options = {
  21. multipleSlots: true,
  22. };
  23. this.relations = {
  24. '../picker-item/picker-item': {
  25. type: 'child',
  26. linked() {
  27. this.updateChildren();
  28. },
  29. },
  30. };
  31. this.observers = {
  32. 'value, visible'() {
  33. this.updateChildren();
  34. },
  35. keys(obj) {
  36. this.setData({
  37. labelAlias: (obj === null || obj === void 0 ? void 0 : obj.label) || 'label',
  38. valueAlias: (obj === null || obj === void 0 ? void 0 : obj.value) || 'value',
  39. });
  40. },
  41. };
  42. this.lifetimes = {
  43. attached() {
  44. this.setData({
  45. pickItemHeight: rpx2px(this.properties.itemHeight),
  46. });
  47. },
  48. };
  49. this.data = {
  50. prefix,
  51. classPrefix: name,
  52. labelAlias: 'label',
  53. valueAlias: 'value',
  54. defaultPopUpProps: {},
  55. defaultPopUpzIndex: 11500,
  56. pickItemHeight: 0,
  57. };
  58. this.methods = {
  59. updateChildren() {
  60. const { pickItemHeight } = this.data;
  61. const { value, defaultValue } = this.properties;
  62. this.$children.forEach((child, index) => {
  63. var _a, _b;
  64. child.setData({
  65. value: (_b = (_a = value === null || value === void 0 ? void 0 : value[index]) !== null && _a !== void 0 ? _a : defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[index]) !== null && _b !== void 0 ? _b : '',
  66. columnIndex: index,
  67. pickItemHeight,
  68. });
  69. child.update();
  70. });
  71. },
  72. getSelectedValue() {
  73. const value = this.$children.map((item) => item._selectedValue);
  74. const label = this.$children.map((item) => item._selectedLabel);
  75. return [value, label];
  76. },
  77. getColumnIndexes() {
  78. const columns = this.$children.map((pickerColumn, columnIndex) => {
  79. return {
  80. column: columnIndex,
  81. index: pickerColumn._selectedIndex,
  82. };
  83. });
  84. return columns;
  85. },
  86. onConfirm() {
  87. const [value, label] = this.getSelectedValue();
  88. const columns = this.getColumnIndexes();
  89. this.close('confirm-btn');
  90. this.triggerEvent('change', { value, label, columns });
  91. this.triggerEvent('confirm', { value, label, columns });
  92. },
  93. triggerColumnChange({ column, index }) {
  94. const [value, label] = this.getSelectedValue();
  95. this.triggerEvent('pick', { value, label, column, index });
  96. },
  97. onCancel() {
  98. this.close('cancel-btn');
  99. this.triggerEvent('cancel');
  100. },
  101. onPopupChange(e) {
  102. const { visible } = e.detail;
  103. this.close('overlay');
  104. this.triggerEvent('visible-change', { visible });
  105. },
  106. close(trigger) {
  107. if (this.data.autoClose) {
  108. this.setData({ visible: false });
  109. }
  110. this.triggerEvent('close', { trigger });
  111. },
  112. };
  113. }
  114. ready() {
  115. this.$children.map((column, index) => (column.columnIndex = index));
  116. }
  117. };
  118. Picker = __decorate([
  119. wxComponent()
  120. ], Picker);
  121. export default Picker;