|
| 1 | +<template> |
| 2 | + <div class="var-checkbox-group__wrap"> |
| 3 | + <div class="var-checkbox-group" :class="[`var-checkbox-group--${direction}`]"> |
| 4 | + <slot /> |
| 5 | + </div> |
| 6 | + <var-form-details :error-message="errorMessage" /> |
| 7 | + </div> |
| 8 | +</template> |
| 9 | + |
| 10 | +<script> |
| 11 | +import VarFormDetails from '../form-details' |
| 12 | +import { defineComponent } from '../utils/create' |
| 13 | +import { props } from './props' |
| 14 | +import { uniq } from '../utils/shared' |
| 15 | +import { ValidationMixin } from '../utils/mixins/validation' |
| 16 | +import { createParentMixin, createChildrenMixin } from '../utils/mixins/relation' |
| 17 | +
|
| 18 | +export default defineComponent({ |
| 19 | + name: 'VarCheckboxGroup', |
| 20 | +
|
| 21 | + components: { VarFormDetails }, |
| 22 | +
|
| 23 | + mixins: [ |
| 24 | + ValidationMixin, |
| 25 | + createParentMixin('checkboxGroup', { childrenKey: 'checkboxes' }), |
| 26 | + createChildrenMixin('form', { parentKey: 'form', childrenKey: 'formItems' }), |
| 27 | + ], |
| 28 | +
|
| 29 | + props, |
| 30 | +
|
| 31 | + computed: { |
| 32 | + checkedCount() { |
| 33 | + return this.value.length |
| 34 | + }, |
| 35 | +
|
| 36 | + checkboxGroupErrorMessage() { |
| 37 | + return this.errorMessage |
| 38 | + }, |
| 39 | + }, |
| 40 | +
|
| 41 | + watch: { |
| 42 | + value: { |
| 43 | + handler() { |
| 44 | + this.syncCheckboxes() |
| 45 | + }, |
| 46 | + deep: true, |
| 47 | + }, |
| 48 | +
|
| 49 | + checkboxes() { |
| 50 | + this.syncCheckboxes() |
| 51 | + }, |
| 52 | + }, |
| 53 | +
|
| 54 | + methods: { |
| 55 | + // expose |
| 56 | + checkAll() { |
| 57 | + const checkedValues = this.checkboxes.map(({ checkedValue }) => checkedValue) |
| 58 | + this.resetWithAnimation() |
| 59 | + const changedModelValue = uniq(checkedValues) |
| 60 | +
|
| 61 | + this.getListeners().onInput?.(changedModelValue) |
| 62 | +
|
| 63 | + return changedModelValue |
| 64 | + }, |
| 65 | +
|
| 66 | + // expose |
| 67 | + inverseAll() { |
| 68 | + const checkedValues = this.checkboxes?.filter(({ checked }) => !checked).map(({ checkedValue }) => checkedValue) |
| 69 | + this.resetWithAnimation() |
| 70 | + const changedModelValue = uniq(checkedValues) |
| 71 | +
|
| 72 | + this.getListeners().onInput?.(changedModelValue) |
| 73 | +
|
| 74 | + return changedModelValue |
| 75 | + }, |
| 76 | +
|
| 77 | + // expose |
| 78 | + reset() { |
| 79 | + this.getListeners().onInput?.([]) |
| 80 | + this.resetValidation() |
| 81 | + }, |
| 82 | +
|
| 83 | + // expose |
| 84 | + validate() { |
| 85 | + this._validate(this.rules, this.value) |
| 86 | + }, |
| 87 | +
|
| 88 | + resetWithAnimation() { |
| 89 | + this.checkboxes.forEach((checkbox) => { |
| 90 | + checkbox.withAnimation = false |
| 91 | + }) |
| 92 | + }, |
| 93 | +
|
| 94 | + validateWithTrigger(trigger) { |
| 95 | + this.$nextTick(() => { |
| 96 | + const { validateTrigger, rules, value } = this |
| 97 | + this._validateWithTrigger(validateTrigger, trigger, rules, value) |
| 98 | + }) |
| 99 | + }, |
| 100 | +
|
| 101 | + change(changedValue) { |
| 102 | + this.getListeners().onInput?.(changedValue) |
| 103 | + this.getListeners().onChange?.(changedValue) |
| 104 | + this.validateWithTrigger('onChange') |
| 105 | + }, |
| 106 | +
|
| 107 | + onChecked(changedValue) { |
| 108 | + const { value } = this |
| 109 | +
|
| 110 | + if (!value.includes(changedValue)) { |
| 111 | + this.change([...value, changedValue]) |
| 112 | + } |
| 113 | + }, |
| 114 | +
|
| 115 | + onUnchecked(changedValue) { |
| 116 | + if (!this.value.includes(changedValue)) { |
| 117 | + return |
| 118 | + } |
| 119 | +
|
| 120 | + this.change(this.value?.filter((value) => value !== changedValue)) |
| 121 | + }, |
| 122 | +
|
| 123 | + syncCheckboxes() { |
| 124 | + this.checkboxes?.forEach(({ sync }) => { |
| 125 | + sync(this.value) |
| 126 | + }) |
| 127 | + }, |
| 128 | + }, |
| 129 | +}) |
| 130 | +</script> |
| 131 | +
|
| 132 | +<style lang="less"> |
| 133 | +@import '../styles/common'; |
| 134 | +@import '../form-details/formDetails'; |
| 135 | +@import './checkboxGroup'; |
| 136 | +</style> |
0 commit comments