index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { defineComponent, onMounted, reactive, watch } from "vue";
  2. import styles from "./index.module.less";
  3. import { Button, showToast } from "vant";
  4. import { getQuery } from "/src/utils/queryString";
  5. export default defineComponent({
  6. name: "change-subject",
  7. props: {
  8. subjectList: {
  9. type: Array,
  10. default: () => [],
  11. },
  12. subject: {
  13. type: String,
  14. default: "",
  15. },
  16. },
  17. emits: ["close", "confirm"],
  18. setup(props, { emit }) {
  19. const query = getQuery();
  20. const state = reactive({
  21. subjectValue: null as any,
  22. instrumentCode: null as any,
  23. selectList: [] as any,
  24. });
  25. // subject 变化时候 重新刷新
  26. watch(
  27. () => props.subject,
  28. () => {
  29. selectItem();
  30. }
  31. );
  32. //
  33. const selectItem = () => {
  34. // const i: any = props.subjectList.find((item: any) => item.value === props.subject);
  35. // if (i) {
  36. // state.subjectValue = i.id;
  37. // state.instrumentCode = i.value;
  38. // state.selectList = [];
  39. // }
  40. // const i: any = props.subjectList.find((item: any) => item.value === props.subject);
  41. let i: any = {};
  42. props.subjectList.forEach((item: any) => {
  43. if (Array.isArray(item.children)) {
  44. item.children.forEach((child: any) => {
  45. if (child.value === props.subject) {
  46. i = {
  47. ...child,
  48. parentId: item.id,
  49. };
  50. state.instrumentCode = child.value;
  51. state.subjectValue = item.id;
  52. state.selectList = item.children || [];
  53. }
  54. });
  55. }
  56. });
  57. if (!i) {
  58. props.subjectList.forEach((item: any) => {
  59. if (item.children && item.children.length > 0) {
  60. item.children.forEach((child: any) => {
  61. if (child.value === props.subject) {
  62. state.instrumentCode = child.value;
  63. state.subjectValue = item.id;
  64. state.selectList = item.children;
  65. }
  66. });
  67. }
  68. });
  69. }
  70. };
  71. onMounted(() => {
  72. console.log(props.subjectList, "subjectList", props.subject, query);
  73. selectItem();
  74. });
  75. return () => (
  76. <div class={[styles.changeSubject, query.platform === "pc" && styles.changeSubjectPc]}>
  77. {/* <div class={styles.changeSubjectContainer}>
  78. <div class={styles.title}>乐器</div>
  79. <div class={styles.subjectContainer}>
  80. {props.subjectList.map((item: any) => (
  81. <div
  82. class={[styles.subjectItem, item.id === state.subjectValue && styles.active]}
  83. onClick={() => {
  84. // if (item.children.length <= 0) {
  85. // state.instrumentCode = "";
  86. // }
  87. state.subjectValue = item.id;
  88. state.instrumentCode = item.value;
  89. // state.selectList = item.children;
  90. }}
  91. >
  92. {item.text}
  93. </div>
  94. ))}
  95. </div>
  96. </div> */}
  97. <div class={[styles.changeSubjectContainer, "changeSubjectContainer_pc"]}>
  98. <div class={styles.title}>声部</div>
  99. <div class={styles.subjectContainer}>
  100. {props.subjectList.map((item: any) => (
  101. <div
  102. class={[styles.subjectItem, item.children.length > 0 && styles.arrow, item.id === state.subjectValue && styles.active]}
  103. onClick={() => {
  104. if (item.children.length <= 0) {
  105. state.instrumentCode = "";
  106. }
  107. state.subjectValue = item.id;
  108. state.selectList = item.children;
  109. if (state.selectList.length > 0) {
  110. state.instrumentCode = state.selectList[0].value;
  111. }
  112. }}
  113. >
  114. {item.text}
  115. </div>
  116. ))}
  117. </div>
  118. {state.selectList.length > 0 && (
  119. <>
  120. <div class={styles.title}>乐器</div>
  121. <div class={styles.subjectContainer}>
  122. {state.selectList.map((item: any) => (
  123. <div
  124. class={[styles.subjectItem, item.value === state.instrumentCode && styles.active]}
  125. onClick={() => {
  126. state.instrumentCode = item.value;
  127. }}
  128. >
  129. {item.text}
  130. </div>
  131. ))}
  132. </div>
  133. </>
  134. )}
  135. </div>
  136. <div class={[styles.btnGroups, "btnGroups_pc"]}>
  137. <div
  138. class={[styles.btn, styles.resetBtn]}
  139. onClick={() => {
  140. emit("close");
  141. selectItem();
  142. }}
  143. ></div>
  144. <div
  145. class={[styles.btn, styles.confirmBtn]}
  146. onClick={() => {
  147. if (state.selectList.length > 0 && !state.instrumentCode) {
  148. showToast("请选择乐器");
  149. return;
  150. }
  151. emit("confirm", state.instrumentCode || state.subjectValue);
  152. }}
  153. ></div>
  154. {/* <Button
  155. round
  156. block
  157. class={styles.resetBtn}
  158. onClick={() => {
  159. emit("close");
  160. selectItem();
  161. }}
  162. >
  163. 重置
  164. </Button>
  165. <Button
  166. type="primary"
  167. block
  168. round
  169. class={styles.confirmBtn}
  170. onClick={() => {
  171. if (state.selectList.length > 0 && !state.instrumentCode) {
  172. showToast("请选择乐器");
  173. return;
  174. }
  175. emit("confirm", state.instrumentCode || state.subjectValue);
  176. }}
  177. >
  178. 确认
  179. </Button> */}
  180. </div>
  181. </div>
  182. );
  183. },
  184. });