index.taro.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view ref="collapseDom">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script lang="ts">
  7. import { onMounted, provide, ref, watch, getCurrentInstance } from 'vue';
  8. import { createComponent } from '@/packages/utils/create';
  9. import { nextTick } from '@tarojs/taro';
  10. const { create } = createComponent('collapse');
  11. export default create({
  12. props: {
  13. active: {
  14. type: [String, Number, Array]
  15. },
  16. accordion: {
  17. type: Boolean
  18. },
  19. // expandIconPosition: {
  20. // type: String,
  21. // default: 'right'
  22. // },
  23. titleIcon: {
  24. type: String,
  25. default: ''
  26. },
  27. titleIconSize: {
  28. type: String,
  29. default: '16px'
  30. },
  31. titleIconColor: {
  32. type: String,
  33. default: ''
  34. },
  35. titleIconPosition: {
  36. type: String,
  37. default: 'left'
  38. },
  39. icon: {
  40. type: String,
  41. default: ''
  42. },
  43. iconSize: {
  44. type: String,
  45. default: '16px'
  46. },
  47. iconColor: {
  48. type: String,
  49. default: ''
  50. },
  51. rotate: {
  52. type: [String, Number],
  53. default: 180
  54. }
  55. },
  56. emits: ['update:active', 'change'],
  57. setup(props, { emit, slots }) {
  58. const collapseDom: any = ref(null);
  59. const changeVal = (val: string | number | Array<string | number>) => {
  60. emit('update:active', val);
  61. emit('change', val);
  62. };
  63. const changeValAry = (name: string) => {
  64. const activeItem: any = props.active instanceof Object ? Object.values(props.active) : props.active;
  65. let index = -1;
  66. activeItem.forEach((item: string | number, idx: number) => {
  67. if (String(item) == String(name)) {
  68. index = idx;
  69. }
  70. });
  71. index > -1 ? activeItem.splice(index, 1) : activeItem.push(name);
  72. changeVal(activeItem);
  73. };
  74. const isExpanded = (name: string | number | Array<string | number>) => {
  75. const { accordion, active } = props;
  76. if (accordion) {
  77. return typeof active === 'number' || typeof active === 'string' ? active == name : false;
  78. }
  79. };
  80. const activeIndex = () => {
  81. const activeCollapse: any = props.active;
  82. const childrenList: any = slots.default?.();
  83. let act: any = [];
  84. childrenList.forEach((item: any, index: number) => {
  85. if (typeof activeCollapse == 'number' || typeof activeCollapse == 'string') {
  86. if (item.props.name == activeCollapse) {
  87. act.push(item.flag);
  88. return act;
  89. }
  90. } else {
  91. let ary = Array.from(activeCollapse);
  92. if (ary.includes(String(item.props.name)) || ary.includes(Number(item.props.name))) {
  93. act.push(item.flag);
  94. }
  95. }
  96. });
  97. return act;
  98. };
  99. const childrenDom = ref(null);
  100. onMounted(() => {
  101. childrenDom.value = (getCurrentInstance() as any).provides.collapseParent.children;
  102. });
  103. watch(
  104. () => props.active,
  105. (newval: any, oldval) => {
  106. nextTick(() => {
  107. let domsProps: any = slots?.default?.();
  108. let doms: any = childrenDom.value;
  109. Array.from(doms).forEach((item: any, index: number) => {
  110. if (typeof newval == 'number' || typeof newval == 'string') {
  111. if (newval == domsProps[index].props.name) {
  112. item.changeOpen(true);
  113. } else {
  114. item.changeOpen(false);
  115. }
  116. } else if (Object.values(newval) instanceof Array) {
  117. if (
  118. newval.indexOf(Number(domsProps[index].props.name)) > -1 ||
  119. newval.indexOf(String(domsProps[index].props.name)) > -1
  120. ) {
  121. item.changeOpen(true);
  122. } else {
  123. item.changeOpen(false);
  124. }
  125. }
  126. item.animation();
  127. });
  128. });
  129. }
  130. );
  131. const getParentChildren = () => {
  132. return slots.default?.();
  133. };
  134. provide('collapseParent', {
  135. children: [],
  136. props,
  137. changeValAry,
  138. changeVal,
  139. isExpanded,
  140. activeIndex,
  141. getParentChildren
  142. });
  143. return { collapseDom };
  144. }
  145. });
  146. </script>