mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[new feature] Checkbox: support config the maximum amount of checked options (#631)
This commit is contained in:
parent
f658204cc5
commit
e0664d7f82
@ -30,6 +30,16 @@
|
|||||||
</van-cell-group>
|
</van-cell-group>
|
||||||
</van-checkbox-group>
|
</van-checkbox-group>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
|
|
||||||
|
<demo-block :title="$t('title5')">
|
||||||
|
<van-checkbox-group v-model="result" :max="max">
|
||||||
|
<van-cell-group>
|
||||||
|
<van-cell v-for="(item, index) in list" :key="index">
|
||||||
|
<van-checkbox :name="item">{{ $t('checkbox') }} {{ item }}</van-checkbox>
|
||||||
|
</van-cell>
|
||||||
|
</van-cell-group>
|
||||||
|
</van-checkbox-group>
|
||||||
|
</demo-block>
|
||||||
</demo-section>
|
</demo-section>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@ -39,12 +49,14 @@ export default {
|
|||||||
'zh-CN': {
|
'zh-CN': {
|
||||||
checkbox: '复选框',
|
checkbox: '复选框',
|
||||||
title3: 'Checkbox 组',
|
title3: 'Checkbox 组',
|
||||||
title4: '与 Cell 组件一起使用'
|
title4: '与 Cell 组件一起使用',
|
||||||
|
title5: '设置最大可选数',
|
||||||
},
|
},
|
||||||
'en-US': {
|
'en-US': {
|
||||||
checkbox: 'Checkbox',
|
checkbox: 'Checkbox',
|
||||||
title3: 'Checkbox Group',
|
title3: 'Checkbox Group',
|
||||||
title4: 'Inside a Cell'
|
title4: 'Inside a Cell',
|
||||||
|
title5: 'Configure the maximum amount of checked options'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -57,7 +69,8 @@ export default {
|
|||||||
'b',
|
'b',
|
||||||
'c'
|
'c'
|
||||||
],
|
],
|
||||||
result: ['a', 'b']
|
result: ['a', 'b'],
|
||||||
|
max: 2
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -69,6 +69,30 @@ export default {
|
|||||||
</van-checkbox-group>
|
</van-checkbox-group>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Configure the maximum amount of checked options
|
||||||
|
|
||||||
|
```html
|
||||||
|
<van-checkbox-group v-model="result" :max="max">
|
||||||
|
<van-cell-group>
|
||||||
|
<van-cell v-for="(item, index) in list" :key="index">
|
||||||
|
<van-checkbox :name="item">Checkbox {{ item }}</van-checkbox>
|
||||||
|
</van-cell>
|
||||||
|
</van-cell-group>
|
||||||
|
</van-checkbox-group>
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
list: ['a', 'b', 'c'],
|
||||||
|
result: ['a', 'b'],
|
||||||
|
max: 2
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
### Checkbox API
|
### Checkbox API
|
||||||
|
|
||||||
| Attribute | Description | Type | Default | Accepted Values |
|
| Attribute | Description | Type | Default | Accepted Values |
|
||||||
@ -82,6 +106,7 @@ export default {
|
|||||||
| Attribute | Description | Type | Default | Accepted Values |
|
| Attribute | Description | Type | Default | Accepted Values |
|
||||||
|-----------|-----------|-----------|-------------|-------------|
|
|-----------|-----------|-----------|-------------|-------------|
|
||||||
| disabled | Disable all checkboxes | `Boolean` | `false` | - |
|
| disabled | Disable all checkboxes | `Boolean` | `false` | - |
|
||||||
|
| max | the maximum amount of checked options | `Number` | `0`(Unlimited) | - |
|
||||||
|
|
||||||
### Checkbox Event
|
### Checkbox Event
|
||||||
|
|
||||||
|
@ -73,6 +73,33 @@ export default {
|
|||||||
</van-checkbox-group>
|
</van-checkbox-group>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 设置最大可选数
|
||||||
|
|
||||||
|
此时你需要引入`CellGroup`组件,`Cell`组件非必须
|
||||||
|
|
||||||
|
```html
|
||||||
|
<van-checkbox-group v-model="result" :max="max">
|
||||||
|
<van-cell-group>
|
||||||
|
<van-cell v-for="(item, index) in list" :key="index">
|
||||||
|
<van-checkbox :name="item">复选框 {{ item }}</van-checkbox>
|
||||||
|
</van-cell>
|
||||||
|
</van-cell-group>
|
||||||
|
</van-checkbox-group>
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
list: ['a', 'b', 'c'],
|
||||||
|
result: ['a', 'b'],
|
||||||
|
max: 2
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Checkbox API
|
### Checkbox API
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||||
@ -86,6 +113,7 @@ export default {
|
|||||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||||
|-----------|-----------|-----------|-------------|-------------|
|
|-----------|-----------|-----------|-------------|-------------|
|
||||||
| disabled | 是否禁用所有单选框 | `Boolean` | `false` | - |
|
| disabled | 是否禁用所有单选框 | `Boolean` | `false` | - |
|
||||||
|
| max | 设置最大可选数 | `Number` | `0`(无限制) | - |
|
||||||
|
|
||||||
### Checkbox Event
|
### Checkbox Event
|
||||||
|
|
||||||
|
@ -12,7 +12,11 @@ export default create({
|
|||||||
|
|
||||||
props: {
|
props: {
|
||||||
value: {},
|
value: {},
|
||||||
disabled: Boolean
|
disabled: Boolean,
|
||||||
|
max: {
|
||||||
|
default: 0,
|
||||||
|
type: Number
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
class="van-checkbox"
|
class="van-checkbox"
|
||||||
:class="[
|
:class="[
|
||||||
`van-checkbox--${shape}`,
|
`van-checkbox--${shape}`,
|
||||||
{ 'van-checkbox--disabled': isDisabled }
|
{ 'van-checkbox--disabled': isDisabled || isLimit }
|
||||||
]"
|
]"
|
||||||
>
|
>
|
||||||
<span class="van-checkbox__input">
|
<span class="van-checkbox__input">
|
||||||
@ -90,7 +90,12 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
isDisabled() {
|
isDisabled() {
|
||||||
return (this.isGroup && this.parentGroup && this.parentGroup.disabled) || this.disabled;
|
return (this.isGroup && this.parentGroup && this.parentGroup.disabled) || this.disabled ||
|
||||||
|
(this.isGroup && this.parentGroup && this.parentGroup.max > 0 && this.parentGroup.max <= this.parentGroup.value.length && !this.isChecked);
|
||||||
|
},
|
||||||
|
|
||||||
|
isLimit() {
|
||||||
|
return this.isGroup && this.parentGroup && this.parentGroup.max > 0 && this.parentGroup.max <= this.parentGroup.value.length;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<van-checkbox-group v-model="result">
|
<van-checkbox-group v-model="result" :max="max">
|
||||||
<van-checkbox v-for="(item, index) in list" :key="index" :name="item" :disabled="index === 2">复选框{{item}}</van-checkbox>
|
<van-checkbox v-for="(item, index) in list" :key="index" :name="item" :disabled="index === 2">复选框{{item}}</van-checkbox>
|
||||||
</van-checkbox-group>
|
</van-checkbox-group>
|
||||||
</template>
|
</template>
|
||||||
@ -22,7 +22,8 @@ export default {
|
|||||||
'c',
|
'c',
|
||||||
'd'
|
'd'
|
||||||
],
|
],
|
||||||
result: ['a', 'b']
|
result: ['a', 'b'],
|
||||||
|
max: 0
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -67,6 +67,23 @@ describe('CheckboxGroup', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('click on unchecked item and checked options num beyond max', (done) => {
|
||||||
|
wrapper = mount(CheckboxTestComponent);
|
||||||
|
|
||||||
|
wrapper.setData({
|
||||||
|
'max': 2
|
||||||
|
});
|
||||||
|
|
||||||
|
const lastCheckboxLabel = wrapper.find('.van-checkbox')[3].find('.van-checkbox__label')[0];
|
||||||
|
lastCheckboxLabel.trigger('click');
|
||||||
|
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(wrapper.vm.result.indexOf('d')).to.equal(-1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('click on disabled item', (done) => {
|
it('click on disabled item', (done) => {
|
||||||
wrapper = mount(CheckboxTestComponent);
|
wrapper = mount(CheckboxTestComponent);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user