index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <uni-shadow-root class="weapp-dist-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/dist/cascader/index'
  24. import { VantComponent } from '../common/component';
  25. var FieldName;
  26. (function (FieldName) {
  27. FieldName["TEXT"] = "text";
  28. FieldName["VALUE"] = "value";
  29. FieldName["CHILDREN"] = "children";
  30. })(FieldName || (FieldName = {}));
  31. const defaultFieldNames = {
  32. text: FieldName.TEXT,
  33. value: FieldName.VALUE,
  34. children: FieldName.CHILDREN,
  35. };
  36. VantComponent({
  37. props: {
  38. title: String,
  39. value: {
  40. type: String,
  41. observer: 'updateValue',
  42. },
  43. placeholder: {
  44. type: String,
  45. value: '请选择',
  46. },
  47. activeColor: {
  48. type: String,
  49. value: '#1989fa',
  50. },
  51. options: {
  52. type: Array,
  53. value: [],
  54. observer: 'updateOptions',
  55. },
  56. swipeable: {
  57. type: Boolean,
  58. value: false,
  59. },
  60. closeable: {
  61. type: Boolean,
  62. value: true,
  63. },
  64. showHeader: {
  65. type: Boolean,
  66. value: true,
  67. },
  68. closeIcon: {
  69. type: String,
  70. value: 'cross',
  71. },
  72. fieldNames: {
  73. type: Object,
  74. value: defaultFieldNames,
  75. observer: 'updateFieldNames',
  76. },
  77. },
  78. data: {
  79. tabs: [],
  80. activeTab: 0,
  81. textKey: FieldName.TEXT,
  82. valueKey: FieldName.VALUE,
  83. childrenKey: FieldName.CHILDREN,
  84. },
  85. created() {
  86. this.updateTabs();
  87. },
  88. methods: {
  89. updateOptions(val, oldVal) {
  90. const isAsync = !!(val.length && oldVal.length);
  91. this.updateTabs(isAsync);
  92. },
  93. updateValue(val) {
  94. if (val !== undefined) {
  95. const values = this.data.tabs.map((tab) => tab.selected && tab.selected[this.data.valueKey]);
  96. if (values.indexOf(val) > -1) {
  97. return;
  98. }
  99. }
  100. this.updateTabs();
  101. },
  102. updateFieldNames() {
  103. const { text = 'text', value = 'value', children = 'children', } = this.data.fieldNames || defaultFieldNames;
  104. this.setData({
  105. textKey: text,
  106. valueKey: value,
  107. childrenKey: children,
  108. });
  109. },
  110. getSelectedOptionsByValue(options, value) {
  111. for (let i = 0; i < options.length; i++) {
  112. const option = options[i];
  113. if (option[this.data.valueKey] === value) {
  114. return [option];
  115. }
  116. if (option[this.data.childrenKey]) {
  117. const selectedOptions = this.getSelectedOptionsByValue(option[this.data.childrenKey], value);
  118. if (selectedOptions) {
  119. return [option, ...selectedOptions];
  120. }
  121. }
  122. }
  123. },
  124. updateTabs(isAsync = false) {
  125. const { options, value } = this.data;
  126. if (value !== undefined) {
  127. const selectedOptions = this.getSelectedOptionsByValue(options, value);
  128. if (selectedOptions) {
  129. let optionsCursor = options;
  130. const tabs = selectedOptions.map((option) => {
  131. const tab = {
  132. options: optionsCursor,
  133. selected: option,
  134. };
  135. const next = optionsCursor.find((item) => item[this.data.valueKey] === option[this.data.valueKey]);
  136. if (next) {
  137. optionsCursor = next[this.data.childrenKey];
  138. }
  139. return tab;
  140. });
  141. if (optionsCursor) {
  142. tabs.push({
  143. options: optionsCursor,
  144. selected: null,
  145. });
  146. }
  147. this.setData({
  148. tabs,
  149. });
  150. wx.nextTick(() => {
  151. this.setData({
  152. activeTab: tabs.length - 1,
  153. });
  154. });
  155. return;
  156. }
  157. }
  158. // 异步更新
  159. if (isAsync) {
  160. const { tabs } = this.data;
  161. tabs[tabs.length - 1].options =
  162. options[options.length - 1][this.data.childrenKey];
  163. this.setData({
  164. tabs,
  165. });
  166. return;
  167. }
  168. this.setData({
  169. tabs: [
  170. {
  171. options,
  172. selected: null,
  173. },
  174. ],
  175. });
  176. },
  177. onClose() {
  178. this.$emit('close');
  179. },
  180. onClickTab(e) {
  181. const { index: tabIndex, title } = e.detail;
  182. this.$emit('click-tab', { title, tabIndex });
  183. this.setData({
  184. activeTab: tabIndex,
  185. });
  186. },
  187. // 选中
  188. onSelect(e) {
  189. const { option, tabIndex } = e.currentTarget.dataset;
  190. if (option && option.disabled) {
  191. return;
  192. }
  193. const { valueKey, childrenKey } = this.data;
  194. let { tabs } = this.data;
  195. tabs[tabIndex].selected = option;
  196. if (tabs.length > tabIndex + 1) {
  197. tabs = tabs.slice(0, tabIndex + 1);
  198. }
  199. if (option[childrenKey]) {
  200. const nextTab = {
  201. options: option[childrenKey],
  202. selected: null,
  203. };
  204. if (tabs[tabIndex + 1]) {
  205. tabs[tabIndex + 1] = nextTab;
  206. }
  207. else {
  208. tabs.push(nextTab);
  209. }
  210. wx.nextTick(() => {
  211. this.setData({
  212. activeTab: tabIndex + 1,
  213. });
  214. });
  215. }
  216. this.setData({
  217. tabs,
  218. });
  219. const selectedOptions = tabs.map((tab) => tab.selected).filter(Boolean);
  220. const params = {
  221. value: option[valueKey],
  222. tabIndex,
  223. selectedOptions,
  224. };
  225. this.$emit('change', params);
  226. if (!option[childrenKey]) {
  227. this.$emit('finish', params);
  228. }
  229. },
  230. },
  231. });
  232. export default global['__wxComponents']['weapp/dist/cascader/index']
  233. </script>
  234. <style platform="mp-weixin">
  235. @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}
  236. </style>