| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 | 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 (      <div class={styles.subjects}>        <div class={styles.subjectContainer}>          {this.subjectList.length ? (            this.selectType === 'Checkbox' ? (              <CheckboxGroup v-model={this.checkBox} max={this.max}>                <div class={styles.subjectMaxLength}>                  最多可选择{this.max}个乐器                </div>                {!this.single &&                  this.subjectList.map((item: any) =>                    item.subjects && item.subjects.length > 0 ? (                      <>                        <div class={styles.title}>{item.name}</div>                        <div class={styles['subject-list']}>                          {item.subjects &&                            item.subjects.map((sub: any) => (                              <div                                class={styles['subject-item']}                                onClick={() => this.onSelect(sub.id)}                              >                                <Image                                  src={sub.img || 'xxx'}                                  width="100%"                                  height="100%"                                  fit="cover"                                  v-slots={{                                    loading: () => (                                      <Loading type="spinner" size={20} />                                    )                                  }}                                />                                <div class={styles.topBg}>                                  <Checkbox                                    name={sub.id}                                    class={styles.checkbox}                                    disabled                                    ref={(el: any) =>                                      (this.checkboxRefs[sub.id] = el)                                    }                                    v-slots={{                                      icon: (props: any) => (                                        <Icon                                          name={                                            props.checked                                              ? checkBoxActive                                              : checkBoxDefault                                          }                                          size="20"                                        />                                      )                                    }}                                  />                                  <p class={styles.name}>{sub.name}</p>                                </div>                              </div>                            ))}                        </div>                      </>                    ) : null                  )}                {this.single ? (                  <div class={styles['subject-list']}>                    {this.subjectList.map((item: any) => (                      <div                        class={styles['subject-item']}                        onClick={() => this.onSelect(item.id)}                      >                        <Image                          src={item.img || 'xxx'}                          width="100%"                          height="100%"                          fit="cover"                          v-slots={{                            loading: () => <Loading type="spinner" size={20} />                          }}                        />                        <div class={styles.topBg}>                          <Checkbox                            name={item.id}                            class={styles.checkbox}                            disabled                            ref={(el: any) => (this.checkboxRefs[item.id] = el)}                            v-slots={{                              icon: (props: any) => (                                <Icon                                  name={                                    props.checked                                      ? checkBoxActive                                      : checkBoxDefault                                  }                                  size="20"                                />                              )                            }}                          />                          <p class={styles.name}>{item.name}</p>                        </div>                      </div>                    ))}                  </div>                ) : null}              </CheckboxGroup>            ) : (              <RadioGroup v-model={this.radio}>                {!this.single &&                  this.subjectList.map((item: any) =>                    item.subjects && item.subjects.length > 0 ? (                      <>                        <div class={styles.title}>{item.name}</div>                        <div class={styles['subject-list']}>                          {item.subjects &&                            item.subjects.map((sub: any) => (                              <div                                class={styles['subject-item']}                                onClick={() => this.onSelect(sub.id)}                              >                                <Image                                  src={sub.img || 'xxx'}                                  width="100%"                                  height="100%"                                  fit="cover"                                  v-slots={{                                    loading: () => (                                      <Loading type="spinner" size={20} />                                    )                                  }}                                />                                <div class={styles.topBg}>                                  <Radio                                    name={sub.id}                                    class={styles.checkbox}                                    v-slots={{                                      icon: (props: any) => (                                        <Icon                                          name={                                            props.checked                                              ? checkBoxActive                                              : checkBoxDefault                                          }                                          size="20"                                        />                                      )                                    }}                                  />                                  <p class={styles.name}>{sub.name}</p>                                </div>                              </div>                            ))}                        </div>                      </>                    ) : null                  )}                {this.single ? (                  <div class={styles['subject-list']}>                    {this.subjectList.map((item: any) => (                      <div                        class={styles['subject-item']}                        onClick={() => this.onSelect(item.id)}                      >                        <Image                          src={item.img || 'xxx'}                          width="100%"                          height="100%"                          fit="cover"                          v-slots={{                            loading: () => <Loading type="spinner" size={20} />                          }}                        />                        <div class={styles.topBg}>                          <Radio                            name={item.id}                            class={styles.checkbox}                            v-slots={{                              icon: (props: any) => (                                <Icon                                  name={                                    props.checked                                      ? checkBoxActive                                      : checkBoxDefault                                  }                                  size="20"                                />                              )                            }}                          />                          <p class={styles.name}>{item.name}</p>                        </div>                      </div>                    ))}                  </div>                ) : null}              </RadioGroup>            )          ) : (            <ColResult tips="暂无声部数据" btnStatus={false} />          )}        </div>        {this.subjectList.length > 0 && (          <Sticky offsetBottom={0} position="bottom">            <div class={'btnGroup'}>              <Button                round                block                type="primary"                style={{ width: '96%', margin: '0 auto' }}                onClick={() =>                  this.onChoice(                    this.selectType === 'Checkbox' ? this.checkBox : this.radio                  )                }              >                确定              </Button>            </div>          </Sticky>        )}      </div>    )  }})
 |