import { Button, Checkbox, CheckboxGroup, Icon, Image, Loading, Radio, RadioGroup, Sticky, Toast } from 'vant' import { defineComponent, PropType } from 'vue' import styles from './index.module.less' import checkBoxActive from '@/teacher/teacher-cert/images/checkbox_active.png' import checkBoxDefault from '@/teacher/teacher-cert/images/checkbox_default.png' import ColResult from '@/components/col-result' export default defineComponent({ name: 'SubjectList', props: { onChoice: { type: Function, default: (item: any) => {} }, choiceSubjectIds: { type: Array, default: [] }, subjectList: { type: Array, default: [] }, max: { // 最多可选数量 type: Number, default: 5 }, selectType: { // 选择类型,Radio:单选,Checkbox:多选 type: String as PropType<'Checkbox' | 'Radio'>, default: 'Checkbox' }, single: { // 单选模式 type: Boolean, default: false } }, data() { return { checkBox: [], checkboxRefs: [] as any, radio: null as any // 单选 } }, async mounted() { if (this.selectType === 'Radio') { this.radio = this.choiceSubjectIds[0] } else { this.checkBox = this.choiceSubjectIds as never[] } }, watch: { choiceSubjectIds(val: any, oldVal) { // 同步更新显示数据 this.checkBox = [...val] as never[] } }, methods: { onSelect(id: number) { if (this.selectType === 'Checkbox') { if ( this.max === this.checkBox.length && !this.checkBox.includes(id as never) ) { Toast(`乐器最多选择${this.max}个`) } this.checkboxRefs[id].toggle() } else if (this.selectType === 'Radio') { this.radio = id } } }, render() { return (
{this.subjectList.length ? ( this.selectType === 'Checkbox' ? (
最多可选择{this.max}个乐器
{!this.single && this.subjectList.map((item: any) => item.subjects && item.subjects.length > 0 ? ( <>
{item.name}
{item.subjects && item.subjects.map((sub: any) => (
this.onSelect(sub.id)} > ( ) }} />
(this.checkboxRefs[sub.id] = el) } v-slots={{ icon: (props: any) => ( ) }} />

{sub.name}

))}
) : null )} {this.single ? (
{this.subjectList.map((item: any) => (
this.onSelect(item.id)} > }} />
(this.checkboxRefs[item.id] = el)} v-slots={{ icon: (props: any) => ( ) }} />

{item.name}

))}
) : null}
) : ( {!this.single && this.subjectList.map((item: any) => item.subjects && item.subjects.length > 0 ? ( <>
{item.name}
{item.subjects && item.subjects.map((sub: any) => (
this.onSelect(sub.id)} > ( ) }} />
( ) }} />

{sub.name}

))}
) : null )} {this.single ? (
{this.subjectList.map((item: any) => (
this.onSelect(item.id)} > }} />
( ) }} />

{item.name}

))}
) : null}
) ) : ( )}
{this.subjectList.length > 0 && (
)}
) } })