研发图纸文件管理系统-前端项目
iczer
2020-06-16 066ab7ec229fc71415285d993d883dca4e3225f7
src/components/checkbox/ColorCheckbox.vue
@@ -42,10 +42,19 @@
    }
  },
  watch: {
    values: function (newVal, oldVal) {
      // 此条件是为解决单选时,触发两次chang事件问题
      if (!(newVal.length === 1 && oldVal.length === 1 && newVal[0] === oldVal[0]) || this.multiple) {
    values() {
        this.$emit('change', this.values, this.colors)
    },
    defaultValues(value) {
      if (this.multiple) {
        this.options.forEach(item => {
          item.sChecked = value.indexOf(item.value) > -1
        })
      } else {
        this.options.forEach(item => {
          let first = value[0]
          item.sChecked = first && first == item.value
        })
      }
    }
  },
@@ -55,9 +64,8 @@
        this.values = this.values.filter(item => item !== option.value)
      } else {
        if (!this.multiple) {
          this.values = [option.value]
          this.options.forEach(item => {
            if (item.value !== option.value) {
            if (item.value != option.value) {
              item.sChecked = false
            }
          })
@@ -97,8 +105,10 @@
  },
  data () {
    return {
      sChecked: this.checked
      sChecked: this.initChecked()
    }
  },
  computed: {
  },
  inject: ['groupContext'],
  watch: {
@@ -118,13 +128,22 @@
  created () {
    const groupContext = this.groupContext
    if (groupContext) {
      this.sChecked = groupContext.defaultValues.indexOf(this.value) >= 0
      groupContext.options.push(this)
    }
  },
  methods: {
    toggle () {
      this.sChecked = !this.sChecked
    },
    initChecked() {
      let groupContext = this.groupContext
      if (!groupContext) {
        return this.check
      }else if (groupContext.multiple) {
        return groupContext.defaultValues.indexOf(this.value) > -1
      } else {
        return groupContext.defaultValues[0] == this.value
      }
    }
  }
}