Skip to content

Commit fff9c5f

Browse files
masongzhiFeng
authored andcommitted
Checkbox: Using the max attribute can limit the number of items that can be checked (ElemeFE#14742) (ElemeFE#15585)
1 parent c3df0d4 commit fff9c5f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

packages/checkbox/src/checkbox-button.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
@focus="focus = true"
3636
@blur="focus = false">
3737

38-
<span class="el-checkbox-button__inner"
38+
<span class="el-checkbox-button__inner"
3939
v-if="$slots.default || label"
4040
:style="isChecked ? activeStyle : null">
4141
<slot>{{label}}</slot>
@@ -150,9 +150,17 @@
150150
return this._checkboxGroup.checkboxGroupSize || this._elFormItemSize || (this.$ELEMENT || {}).size;
151151
},
152152
153+
/* used to make the isDisabled judgment under max/min props */
154+
isLimitDisabled() {
155+
const { max, min } = this._checkboxGroup;
156+
return !!(max || min) &&
157+
(this.model.length >= max && !this.isChecked) ||
158+
(this.model.length <= min && this.isChecked);
159+
},
160+
153161
isDisabled() {
154162
return this._checkboxGroup
155-
? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled
163+
? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled || this.isLimitDisabled
156164
: this.disabled || (this.elForm || {}).disabled;
157165
}
158166
},

packages/checkbox/src/checkbox.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,17 @@
136136
return this._checkboxGroup ? this._checkboxGroup.value : this.value;
137137
},
138138
139+
/* used to make the isDisabled judgment under max/min props */
140+
isLimitDisabled() {
141+
const { max, min } = this._checkboxGroup;
142+
return !!(max || min) &&
143+
(this.model.length >= max && !this.isChecked) ||
144+
(this.model.length <= min && this.isChecked);
145+
},
146+
139147
isDisabled() {
140148
return this.isGroup
141-
? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled
149+
? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled || this.isLimitDisabled
142150
: this.disabled || (this.elForm || {}).disabled;
143151
},
144152

test/unit/specs/checkbox.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('Checkbox', () => {
147147
}
148148
}, true);
149149
expect(vm.checkList.length === 1).to.be.true;
150+
expect(vm.$refs.a.isDisabled).to.be.true;
150151
vm.$refs.a.$el.click();
151152
vm.$nextTick(() => {
152153
expect(vm.checkList.indexOf('a') !== -1).to.be.true;
@@ -158,6 +159,8 @@ describe('Checkbox', () => {
158159
vm.$nextTick(() => {
159160
expect(vm.checkList.indexOf('c') !== -1).to.be.false;
160161
expect(vm.checkList.indexOf('d') !== -1).to.be.false;
162+
expect(vm.$refs.c.isDisabled).to.be.true;
163+
expect(vm.$refs.d.isDisabled).to.be.true;
161164
done();
162165
});
163166
});

0 commit comments

Comments
 (0)