fix(Checkbox): support set disabled dynamic (#2748)

fix #2742
This commit is contained in:
rex 2020-02-12 12:04:43 +08:00 committed by GitHub
parent 46375658c2
commit bfa648b13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 11 deletions

View File

@ -1,5 +1,7 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
type TrivialInstance = WechatMiniprogram.Component.TrivialInstance;
VantComponent({ VantComponent({
field: true, field: true,
@ -13,7 +15,7 @@ VantComponent({
}, },
unlinked(target) { unlinked(target) {
this.children = this.children.filter( this.children = this.children.filter(
(child: WechatMiniprogram.Component.TrivialInstance) => child !== target (child: TrivialInstance) => child !== target
); );
} }
}, },
@ -32,16 +34,16 @@ VantComponent({
methods: { methods: {
updateChildren() { updateChildren() {
(this.children || []).forEach((child: WechatMiniprogram.Component.TrivialInstance) => (this.children || []).forEach((child: TrivialInstance) =>
this.updateChild(child) this.updateChild(child)
); );
}, },
updateChild(child: WechatMiniprogram.Component.TrivialInstance) { updateChild(child: TrivialInstance) {
const { value, disabled } = this.data; const { value, disabled } = this.data;
child.setData({ child.setData({
value: value.indexOf(child.data.name) !== -1, value: value.indexOf(child.data.name) !== -1,
disabled: disabled || child.data.disabled parentDisabled: disabled
}); });
} }
} }

View File

@ -38,6 +38,10 @@ VantComponent({
} }
}, },
data: {
parentDisabled: false
},
methods: { methods: {
emitChange(value: boolean) { emitChange(value: boolean) {
if (this.parent) { if (this.parent) {
@ -48,15 +52,15 @@ VantComponent({
}, },
toggle() { toggle() {
const { disabled, value } = this.data; const { parentDisabled, disabled, value } = this.data;
if (!disabled) { if (!disabled && !parentDisabled) {
this.emitChange(!value); this.emitChange(!value);
} }
}, },
onClickLabel() { onClickLabel() {
const { labelDisabled, disabled, value } = this.data; const { labelDisabled, parentDisabled, disabled, value } = this.data;
if (!disabled && !labelDisabled) { if (!disabled && !labelDisabled && !parentDisabled) {
this.emitChange(!value); this.emitChange(!value);
} }
}, },

View File

@ -1,4 +1,5 @@
<wxs src="../wxs/utils.wxs" module="utils" /> <wxs src="../wxs/utils.wxs" module="utils" />
<wxs src="./index.wxs" module="computed" />
<view class="van-checkbox custom-class"> <view class="van-checkbox custom-class">
<view class="van-checkbox__icon-wrap" bindtap="toggle"> <view class="van-checkbox__icon-wrap" bindtap="toggle">
@ -7,13 +8,13 @@
wx:else wx:else
name="success" name="success"
size="0.8em" size="0.8em"
class="{{ utils.bem('checkbox__icon', [shape, { disabled, checked: value }]) }}" class="{{ utils.bem('checkbox__icon', [shape, { disabled: disabled || parentDisabled, checked: value }]) }}"
style="font-size: {{ utils.addUnit(iconSize) }};{{ checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : '' }}" style="{{ computed.iconStyle(checkedColor, value, disabled, parentDisabled, iconSize) }}"
custom-class="icon-class" custom-class="icon-class"
custom-style="line-height: 1.25em;" custom-style="line-height: 1.25em;"
/> />
</view> </view>
<view class="label-class {{ utils.bem('checkbox__label', [labelPosition, { disabled }]) }}" bindtap="onClickLabel"> <view class="label-class {{ utils.bem('checkbox__label', [labelPosition, { disabled: disabled || parentDisabled }]) }}" bindtap="onClickLabel">
<slot /> <slot />
</view> </view>
</view> </view>

View File

@ -0,0 +1,20 @@
/* eslint-disable */
var utils = require('../wxs/utils.wxs');
function iconStyle(checkedColor, value, disabled, parentDisabled, iconSize) {
var styles = [['font-size', utils.addUnit(iconSize)]];
if (checkedColor && value && !disabled && !parentDisabled) {
styles.push(['border-color', checkedColor]);
styles.push(['background-color', checkedColor]);
}
return styles
.map(function(item) {
return item.join(':');
})
.join(';');
}
module.exports = {
iconStyle: iconStyle
};