index.tsx 5.6 KB

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