import { defineComponent, onMounted, reactive } from "vue"; import styles from "./index.module.less"; import { Button, showToast } from "vant"; export default defineComponent({ name: "change-subject", props: { subjectList: { type: Array, default: () => [], }, subject: { type: String, default: "", }, }, emits: ["close", "confirm"], setup(props, { emit }) { const state = reactive({ subjectValue: null as any, instrumentCode: null as any, selectList: [] as any, }); // const selectItem = () => { // const i: any = props.subjectList.find((item: any) => item.value === props.subject); // if (i) { // state.subjectValue = i.id; // state.instrumentCode = i.value; // state.selectList = []; // } // const i: any = props.subjectList.find((item: any) => item.value === props.subject); let i: any = {}; props.subjectList.forEach((item: any) => { if (Array.isArray(item.children)) { item.children.forEach((child: any) => { if (child.value === props.subject) { i = { ...child, parentId: item.id, }; state.instrumentCode = child.value; state.subjectValue = item.id; state.selectList = item.children || []; } }); } }); if (!i) { props.subjectList.forEach((item: any) => { if (item.children && item.children.length > 0) { item.children.forEach((child: any) => { if (child.value === props.subject) { state.instrumentCode = child.value; state.subjectValue = item.id; state.selectList = item.children; } }); } }); } }; onMounted(() => { console.log(props.subjectList, "subjectList", props.subject); selectItem(); }); return () => (
{/*
乐器
{props.subjectList.map((item: any) => (
{ // if (item.children.length <= 0) { // state.instrumentCode = ""; // } state.subjectValue = item.id; state.instrumentCode = item.value; // state.selectList = item.children; }} > {item.text}
))}
*/}
声部
{props.subjectList.map((item: any) => (
0 && styles.arrow, item.id === state.subjectValue && styles.active]} onClick={() => { if (item.children.length <= 0) { state.instrumentCode = ""; } state.subjectValue = item.id; state.selectList = item.children; if (state.selectList.length > 0) { state.instrumentCode = state.selectList[0].value; } }} > {item.text}
))}
{state.selectList.length > 0 && ( <>
乐器
{state.selectList.map((item: any) => (
{ state.instrumentCode = item.value; }} > {item.text}
))}
)}
{ emit("close"); selectItem(); }} >
{ if (state.selectList.length > 0 && !state.instrumentCode) { showToast("请选择乐器"); return; } emit("confirm", state.instrumentCode || state.subjectValue); }} >
{/* */}
); }, });