index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <uni-shadow-root class="weapp-lib-cascader-index"><view v-if="showHeader" class="van-cascader__header">
  3. <text class="van-cascader__title"><slot name="title"></slot>{{ title }}</text>
  4. <van-icon v-if="closeable" :name="closeIcon" class="van-cascader__close-icon" @click.native="onClose"></van-icon>
  5. </view>
  6. <van-tabs :active="activeTab" custom-class="van-cascader__tabs" wrap-class="van-cascader__tabs-wrap" tab-class="van-cascader__tab" :color="activeColor" :border="false" :swipeable="swipeable" @click="onClickTab">
  7. <van-tab v-for="(tab,tabIndex) in (tabs)" :key="tab.tabIndex" :title="tab.selected ? tab.selected[textKey] : placeholder" style="width: 100%;" :title-style="(!tab.selected ? 'color: #969799;font-weight:normal;' : '')">
  8. <view class="van-cascader__options">
  9. <view v-for="(option,index) in (tab.options)" :key="option.index" :class="(option.className)+' '+(utils.optionClass(tab, textKey, option))" :style="utils.optionStyle({ tab, textKey, option, activeColor })" :data-option="option" :data-tab-index="tabIndex" @click="onSelect">
  10. <text>{{ option[textKey] }}</text>
  11. <van-icon v-if="utils.isSelected(tab, textKey, option)" name="success" size="18"></van-icon>
  12. </view>
  13. </view>
  14. </van-tab>
  15. </van-tabs></uni-shadow-root>
  16. </template>
  17. <wxs src="./index.wxs" module="utils"></wxs>
  18. <script>
  19. import VanIcon from '../icon/index.vue'
  20. import VanTab from '../tab/index.vue'
  21. import VanTabs from '../tabs/index.vue'
  22. global['__wxVueOptions'] = {components:{'van-icon': VanIcon,'van-tab': VanTab,'van-tabs': VanTabs}}
  23. global['__wxRoute'] = 'weapp/lib/cascader/index'
  24. "use strict";
  25. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  26. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  27. if (ar || !(i in from)) {
  28. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  29. ar[i] = from[i];
  30. }
  31. }
  32. return to.concat(ar || Array.prototype.slice.call(from));
  33. };
  34. Object.defineProperty(exports, "__esModule", { value: true });
  35. var component_1 = require("../common/component");
  36. var FieldName;
  37. (function (FieldName) {
  38. FieldName["TEXT"] = "text";
  39. FieldName["VALUE"] = "value";
  40. FieldName["CHILDREN"] = "children";
  41. })(FieldName || (FieldName = {}));
  42. var defaultFieldNames = {
  43. text: FieldName.TEXT,
  44. value: FieldName.VALUE,
  45. children: FieldName.CHILDREN,
  46. };
  47. (0, component_1.VantComponent)({
  48. props: {
  49. title: String,
  50. value: {
  51. type: String,
  52. observer: 'updateValue',
  53. },
  54. placeholder: {
  55. type: String,
  56. value: '请选择',
  57. },
  58. activeColor: {
  59. type: String,
  60. value: '#1989fa',
  61. },
  62. options: {
  63. type: Array,
  64. value: [],
  65. observer: 'updateOptions',
  66. },
  67. swipeable: {
  68. type: Boolean,
  69. value: false,
  70. },
  71. closeable: {
  72. type: Boolean,
  73. value: true,
  74. },
  75. showHeader: {
  76. type: Boolean,
  77. value: true,
  78. },
  79. closeIcon: {
  80. type: String,
  81. value: 'cross',
  82. },
  83. fieldNames: {
  84. type: Object,
  85. value: defaultFieldNames,
  86. observer: 'updateFieldNames',
  87. },
  88. },
  89. data: {
  90. tabs: [],
  91. activeTab: 0,
  92. textKey: FieldName.TEXT,
  93. valueKey: FieldName.VALUE,
  94. childrenKey: FieldName.CHILDREN,
  95. },
  96. created: function () {
  97. this.updateTabs();
  98. },
  99. methods: {
  100. updateOptions: function (val, oldVal) {
  101. var isAsync = !!(val.length && oldVal.length);
  102. this.updateTabs(isAsync);
  103. },
  104. updateValue: function (val) {
  105. var _this = this;
  106. if (val !== undefined) {
  107. var values = this.data.tabs.map(function (tab) { return tab.selected && tab.selected[_this.data.valueKey]; });
  108. if (values.indexOf(val) > -1) {
  109. return;
  110. }
  111. }
  112. this.updateTabs();
  113. },
  114. updateFieldNames: function () {
  115. 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;
  116. this.setData({
  117. textKey: text,
  118. valueKey: value,
  119. childrenKey: children,
  120. });
  121. },
  122. getSelectedOptionsByValue: function (options, value) {
  123. for (var i = 0; i < options.length; i++) {
  124. var option = options[i];
  125. if (option[this.data.valueKey] === value) {
  126. return [option];
  127. }
  128. if (option[this.data.childrenKey]) {
  129. var selectedOptions = this.getSelectedOptionsByValue(option[this.data.childrenKey], value);
  130. if (selectedOptions) {
  131. return __spreadArray([option], selectedOptions, true);
  132. }
  133. }
  134. }
  135. },
  136. updateTabs: function (isAsync) {
  137. var _this = this;
  138. if (isAsync === void 0) { isAsync = false; }
  139. var _a = this.data, options = _a.options, value = _a.value;
  140. if (value !== undefined) {
  141. var selectedOptions = this.getSelectedOptionsByValue(options, value);
  142. if (selectedOptions) {
  143. var optionsCursor_1 = options;
  144. var tabs_1 = selectedOptions.map(function (option) {
  145. var tab = {
  146. options: optionsCursor_1,
  147. selected: option,
  148. };
  149. var next = optionsCursor_1.find(function (item) { return item[_this.data.valueKey] === option[_this.data.valueKey]; });
  150. if (next) {
  151. optionsCursor_1 = next[_this.data.childrenKey];
  152. }
  153. return tab;
  154. });
  155. if (optionsCursor_1) {
  156. tabs_1.push({
  157. options: optionsCursor_1,
  158. selected: null,
  159. });
  160. }
  161. this.setData({
  162. tabs: tabs_1,
  163. });
  164. wx.nextTick(function () {
  165. _this.setData({
  166. activeTab: tabs_1.length - 1,
  167. });
  168. });
  169. return;
  170. }
  171. }
  172. // 异步更新
  173. if (isAsync) {
  174. var tabs = this.data.tabs;
  175. tabs[tabs.length - 1].options =
  176. options[options.length - 1][this.data.childrenKey];
  177. this.setData({
  178. tabs: tabs,
  179. });
  180. return;
  181. }
  182. this.setData({
  183. tabs: [
  184. {
  185. options: options,
  186. selected: null,
  187. },
  188. ],
  189. });
  190. },
  191. onClose: function () {
  192. this.$emit('close');
  193. },
  194. onClickTab: function (e) {
  195. var _a = e.detail, tabIndex = _a.index, title = _a.title;
  196. this.$emit('click-tab', { title: title, tabIndex: tabIndex });
  197. this.setData({
  198. activeTab: tabIndex,
  199. });
  200. },
  201. // 选中
  202. onSelect: function (e) {
  203. var _this = this;
  204. var _a = e.currentTarget.dataset, option = _a.option, tabIndex = _a.tabIndex;
  205. if (option && option.disabled) {
  206. return;
  207. }
  208. var _b = this.data, valueKey = _b.valueKey, childrenKey = _b.childrenKey;
  209. var tabs = this.data.tabs;
  210. tabs[tabIndex].selected = option;
  211. if (tabs.length > tabIndex + 1) {
  212. tabs = tabs.slice(0, tabIndex + 1);
  213. }
  214. if (option[childrenKey]) {
  215. var nextTab = {
  216. options: option[childrenKey],
  217. selected: null,
  218. };
  219. if (tabs[tabIndex + 1]) {
  220. tabs[tabIndex + 1] = nextTab;
  221. }
  222. else {
  223. tabs.push(nextTab);
  224. }
  225. wx.nextTick(function () {
  226. _this.setData({
  227. activeTab: tabIndex + 1,
  228. });
  229. });
  230. }
  231. this.setData({
  232. tabs: tabs,
  233. });
  234. var selectedOptions = tabs.map(function (tab) { return tab.selected; }).filter(Boolean);
  235. var params = {
  236. value: option[valueKey],
  237. tabIndex: tabIndex,
  238. selectedOptions: selectedOptions,
  239. };
  240. this.$emit('change', params);
  241. if (!option[childrenKey]) {
  242. this.$emit('finish', params);
  243. }
  244. },
  245. },
  246. });
  247. export default global['__wxComponents']['weapp/lib/cascader/index']
  248. </script>
  249. <style platform="mp-weixin">
  250. @import '../common/index.css';.van-cascader__header{align-items:center;display:flex;height:48px;justify-content:space-between;padding:0 16px}.van-cascader__title{font-size:16px;font-weight:600;line-height:20px}.van-cascader__close-icon{color:#c8c9cc;font-size:22px;height:22px}.van-cascader__tabs-wrap{height:48px!important;padding:0 8px}.van-cascader__tab{color:#323233!important;flex:none!important;font-weight:600!important;padding:0 8px!important}.van-cascader__tab--unselected{color:#969799!important;font-weight:400!important}.van-cascader__option{align-items:center;cursor:pointer;display:flex;font-size:14px;justify-content:space-between;line-height:20px;padding:10px 16px}.van-cascader__option:active{background-color:#f2f3f5}.van-cascader__option--selected{color:#1989fa;font-weight:600}.van-cascader__option--disabled{color:#c8c9cc;cursor:not-allowed}.van-cascader__option--disabled:active{background-color:initial}.van-cascader__options{-webkit-overflow-scrolling:touch;box-sizing:border-box;height:384px;overflow-y:auto;padding-top:6px}
  251. </style>