fix(Checkbox): disabled unchecked options when the limit is exceeded (#11814)

* fix(Checkbox): disabled unchecked options when the limit is exceeded

* fix: ts def
This commit is contained in:
inottn 2023-05-05 10:04:36 +08:00 committed by GitHub
parent 5ec43156d0
commit 13ce11f608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View File

@ -3,6 +3,8 @@ import { mount } from '../../../test';
import { Checkbox } from '../../checkbox'; import { Checkbox } from '../../checkbox';
import { CheckboxGroup, CheckboxGroupToggleAllOptions } from '..'; import { CheckboxGroup, CheckboxGroupToggleAllOptions } from '..';
const disabledClass = 'van-checkbox--disabled';
test('should emit "update:modelValue" event when checkbox is clicked', async () => { test('should emit "update:modelValue" event when checkbox is clicked', async () => {
const wrapper = mount({ const wrapper = mount({
setup() { setup() {
@ -49,6 +51,39 @@ test('should change icon size when using icon-size prop', () => {
expect(icons[1].style.fontSize).toEqual('5rem'); expect(icons[1].style.fontSize).toEqual('5rem');
}); });
test('should limit the number of checked items when using max prop', async () => {
const wrapper = mount({
setup() {
const groupValue = ref(['a']);
return {
groupValue,
};
},
render() {
return (
<CheckboxGroup v-model={this.groupValue} max={2}>
<Checkbox name="a" />
<Checkbox name="b" />
<Checkbox name="c" />
<Checkbox name="d" />
</CheckboxGroup>
);
},
});
const items = wrapper.findAll('.van-checkbox');
await items[1].trigger('click');
expect(wrapper.vm.groupValue).toEqual(['a', 'b']);
expect(items[2].classes()).toContain(disabledClass);
await items[2].trigger('click');
expect(wrapper.vm.groupValue).toEqual(['a', 'b']);
await items[1].trigger('click');
expect(items[2].classes()).not.toContain(disabledClass);
});
test('should change checked color when using checked-color prop', () => { test('should change checked color when using checked-color prop', () => {
const wrapper = mount({ const wrapper = mount({
render() { render() {

View File

@ -20,6 +20,8 @@ export type CheckerParent = {
iconSize?: Numeric; iconSize?: Numeric;
direction?: CheckerDirection; direction?: CheckerDirection;
checkedColor?: string; checkedColor?: string;
modelValue?: unknown | unknown[];
max?: Numeric;
}; };
}; };
@ -54,9 +56,24 @@ export default defineComponent({
} }
}; };
const disabled = computed( const disabled = computed(() => {
() => getParentProp('disabled') || props.disabled if (props.parent && props.bindGroup) {
); const disabled = getParentProp('disabled') || props.disabled;
if (props.role === 'checkbox') {
const checkedCount = (getParentProp('modelValue') as unknown[])
.length;
const max = getParentProp('max');
const overlimit = max && checkedCount >= +max;
return disabled || (overlimit && !props.checked);
}
return disabled;
}
return props.disabled;
});
const direction = computed(() => getParentProp('direction')); const direction = computed(() => getParentProp('direction'));