index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  3. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  4. if (ar || !(i in from)) {
  5. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  6. ar[i] = from[i];
  7. }
  8. }
  9. return to.concat(ar || Array.prototype.slice.call(from));
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var component_1 = require("../common/component");
  13. var FieldName;
  14. (function (FieldName) {
  15. FieldName["TEXT"] = "text";
  16. FieldName["VALUE"] = "value";
  17. FieldName["CHILDREN"] = "children";
  18. })(FieldName || (FieldName = {}));
  19. var defaultFieldNames = {
  20. text: FieldName.TEXT,
  21. value: FieldName.VALUE,
  22. children: FieldName.CHILDREN,
  23. };
  24. (0, component_1.VantComponent)({
  25. props: {
  26. title: String,
  27. value: {
  28. type: String,
  29. observer: 'updateValue',
  30. },
  31. placeholder: {
  32. type: String,
  33. value: '请选择',
  34. },
  35. activeColor: {
  36. type: String,
  37. value: '#1989fa',
  38. },
  39. options: {
  40. type: Array,
  41. value: [],
  42. observer: 'updateOptions',
  43. },
  44. swipeable: {
  45. type: Boolean,
  46. value: false,
  47. },
  48. closeable: {
  49. type: Boolean,
  50. value: true,
  51. },
  52. showHeader: {
  53. type: Boolean,
  54. value: true,
  55. },
  56. closeIcon: {
  57. type: String,
  58. value: 'cross',
  59. },
  60. fieldNames: {
  61. type: Object,
  62. value: defaultFieldNames,
  63. observer: 'updateFieldNames',
  64. },
  65. },
  66. data: {
  67. tabs: [],
  68. activeTab: 0,
  69. textKey: FieldName.TEXT,
  70. valueKey: FieldName.VALUE,
  71. childrenKey: FieldName.CHILDREN,
  72. },
  73. created: function () {
  74. this.updateTabs();
  75. },
  76. methods: {
  77. updateOptions: function (val, oldVal) {
  78. var isAsync = !!(val.length && oldVal.length);
  79. this.updateTabs(isAsync);
  80. },
  81. updateValue: function (val) {
  82. var _this = this;
  83. if (val !== undefined) {
  84. var values = this.data.tabs.map(function (tab) { return tab.selected && tab.selected[_this.data.valueKey]; });
  85. if (values.indexOf(val) > -1) {
  86. return;
  87. }
  88. }
  89. this.updateTabs();
  90. },
  91. updateFieldNames: function () {
  92. var _a = this.data.fieldNames || defaultFieldNames, _b = _a.text, text = _b === void 0 ? 'text' : _b, _c = _a.value, value = _c === void 0 ? 'value' : _c, _d = _a.children, children = _d === void 0 ? 'children' : _d;
  93. this.setData({
  94. textKey: text,
  95. valueKey: value,
  96. childrenKey: children,
  97. });
  98. },
  99. getSelectedOptionsByValue: function (options, value) {
  100. for (var i = 0; i < options.length; i++) {
  101. var option = options[i];
  102. if (option[this.data.valueKey] === value) {
  103. return [option];
  104. }
  105. if (option[this.data.childrenKey]) {
  106. var selectedOptions = this.getSelectedOptionsByValue(option[this.data.childrenKey], value);
  107. if (selectedOptions) {
  108. return __spreadArray([option], selectedOptions, true);
  109. }
  110. }
  111. }
  112. },
  113. updateTabs: function (isAsync) {
  114. var _this = this;
  115. if (isAsync === void 0) { isAsync = false; }
  116. var _a = this.data, options = _a.options, value = _a.value;
  117. if (value !== undefined) {
  118. var selectedOptions = this.getSelectedOptionsByValue(options, value);
  119. if (selectedOptions) {
  120. var optionsCursor_1 = options;
  121. var tabs_1 = selectedOptions.map(function (option) {
  122. var tab = {
  123. options: optionsCursor_1,
  124. selected: option,
  125. };
  126. var next = optionsCursor_1.find(function (item) { return item[_this.data.valueKey] === option[_this.data.valueKey]; });
  127. if (next) {
  128. optionsCursor_1 = next[_this.data.childrenKey];
  129. }
  130. return tab;
  131. });
  132. if (optionsCursor_1) {
  133. tabs_1.push({
  134. options: optionsCursor_1,
  135. selected: null,
  136. });
  137. }
  138. this.setData({
  139. tabs: tabs_1,
  140. });
  141. wx.nextTick(function () {
  142. _this.setData({
  143. activeTab: tabs_1.length - 1,
  144. });
  145. });
  146. return;
  147. }
  148. }
  149. // 异步更新
  150. if (isAsync) {
  151. var tabs = this.data.tabs;
  152. tabs[tabs.length - 1].options =
  153. options[options.length - 1][this.data.childrenKey];
  154. this.setData({
  155. tabs: tabs,
  156. });
  157. return;
  158. }
  159. this.setData({
  160. tabs: [
  161. {
  162. options: options,
  163. selected: null,
  164. },
  165. ],
  166. });
  167. },
  168. onClose: function () {
  169. this.$emit('close');
  170. },
  171. onClickTab: function (e) {
  172. var _a = e.detail, tabIndex = _a.index, title = _a.title;
  173. this.$emit('click-tab', { title: title, tabIndex: tabIndex });
  174. },
  175. // 选中
  176. onSelect: function (e) {
  177. var _this = this;
  178. var _a = e.currentTarget.dataset, option = _a.option, tabIndex = _a.tabIndex;
  179. if (option && option.disabled) {
  180. return;
  181. }
  182. var _b = this.data, valueKey = _b.valueKey, childrenKey = _b.childrenKey;
  183. var tabs = this.data.tabs;
  184. tabs[tabIndex].selected = option;
  185. if (tabs.length > tabIndex + 1) {
  186. tabs = tabs.slice(0, tabIndex + 1);
  187. }
  188. if (option[childrenKey]) {
  189. var nextTab = {
  190. options: option[childrenKey],
  191. selected: null,
  192. };
  193. if (tabs[tabIndex + 1]) {
  194. tabs[tabIndex + 1] = nextTab;
  195. }
  196. else {
  197. tabs.push(nextTab);
  198. }
  199. wx.nextTick(function () {
  200. _this.setData({
  201. activeTab: tabIndex + 1,
  202. });
  203. });
  204. }
  205. this.setData({
  206. tabs: tabs,
  207. });
  208. var selectedOptions = tabs.map(function (tab) { return tab.selected; }).filter(Boolean);
  209. var params = {
  210. value: option[valueKey],
  211. tabIndex: tabIndex,
  212. selectedOptions: selectedOptions,
  213. };
  214. this.$emit('change', params);
  215. if (!option[childrenKey]) {
  216. this.$emit('finish', params);
  217. }
  218. },
  219. },
  220. });