mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-29 01:39:15 +08:00
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:
parent
5ec43156d0
commit
13ce11f608
@ -3,6 +3,8 @@ import { mount } from '../../../test';
|
||||
import { Checkbox } from '../../checkbox';
|
||||
import { CheckboxGroup, CheckboxGroupToggleAllOptions } from '..';
|
||||
|
||||
const disabledClass = 'van-checkbox--disabled';
|
||||
|
||||
test('should emit "update:modelValue" event when checkbox is clicked', async () => {
|
||||
const wrapper = mount({
|
||||
setup() {
|
||||
@ -49,6 +51,39 @@ test('should change icon size when using icon-size prop', () => {
|
||||
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', () => {
|
||||
const wrapper = mount({
|
||||
render() {
|
||||
|
@ -20,6 +20,8 @@ export type CheckerParent = {
|
||||
iconSize?: Numeric;
|
||||
direction?: CheckerDirection;
|
||||
checkedColor?: string;
|
||||
modelValue?: unknown | unknown[];
|
||||
max?: Numeric;
|
||||
};
|
||||
};
|
||||
|
||||
@ -54,9 +56,24 @@ export default defineComponent({
|
||||
}
|
||||
};
|
||||
|
||||
const disabled = computed(
|
||||
() => getParentProp('disabled') || props.disabled
|
||||
);
|
||||
const disabled = computed(() => {
|
||||
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'));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user