Skip to content

Commit

Permalink
fix(checkbox-group): dynamic modification of disabled attribute is in…
Browse files Browse the repository at this point in the history
…valid (#2647)

* fix(checkbox-group): dynamic modification of disabled attribute is invalid

* perf(checkbox): optimize tap event
  • Loading branch information
betavs authored Mar 29, 2024
1 parent be7f8e4 commit b582fa5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/checkbox-group/checkbox-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export default class CheckBoxGroup extends SuperComponent {
options() {
this.initWithOptions();
},
disabled(v) {
if (this.data.options?.length) {
this.initWithOptions();
return;
}
this.getChildren().forEach((item) => {
item.setDisabled(v);
});
},
};

lifetimes = {
Expand Down
26 changes: 17 additions & 9 deletions src/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class CheckBox extends SuperComponent {
const valueSet = new Set(value);
const checkedFromParent = valueSet.has(this.data.value);
const data: any = {
disabled: this.data.disabled == null ? disabled : this.data.disabled,
_disabled: this.data.disabled == null ? disabled : this.data.disabled,
};

if (borderless) {
Expand Down Expand Up @@ -62,6 +62,13 @@ export default class CheckBox extends SuperComponent {
data = {
prefix,
classPrefix: name,
_disabled: false,
};

observers = {
disabled(v) {
this.setData({ _disabled: v });
},
};

controlledProps = [
Expand All @@ -73,16 +80,11 @@ export default class CheckBox extends SuperComponent {

methods = {
handleTap(e: WechatMiniprogram.TouchEvent) {
const { disabled, readonly } = this.data;

if (disabled || readonly) return;

const { _disabled, readonly, contentDisabled } = this.data;
const { target } = e.currentTarget.dataset;
const { contentDisabled } = this.data;

if (target === 'text' && contentDisabled) {
return;
}
if (_disabled || readonly || (target === 'text' && contentDisabled)) return;

const { value, label } = this.data;
const checked = !this.data.checked;
const parent = this.$parent;
Expand All @@ -93,5 +95,11 @@ export default class CheckBox extends SuperComponent {
this._trigger('change', { context: { value, label }, checked });
}
},

setDisabled(disabled: Boolean) {
this.setData({
_disabled: this.data.disabled || disabled,
});
},
};
}
10 changes: 5 additions & 5 deletions src/checkbox/checkbox.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
class="{{_.cls(classPrefix, [placement, theme, ['checked', checked], ['block', block]])}} class {{prefix}}-class"
aria-role="checkbox"
aria-checked="{{checked ? (indeterminate ? 'mixed' : true) : false}}"
aria-disabled="{{disabled ? true : false}}"
aria-disabled="{{_disabled ? true : false}}"
mut-bind:tap="handleTap"
tabindex="{{tabindex}}"
>
<view
wx:if="{{theme == 'default'}}"
class="{{_.cls(classPrefix + '__icon', [placement, ['checked', checked], ['disabled', disabled]])}} {{prefix}}-class-icon"
class="{{_.cls(classPrefix + '__icon', [placement, ['checked', checked], ['disabled', _disabled]])}} {{prefix}}-class-icon"
>
<slot name="icon" wx:if="{{icon === 'slot'}}" />
<view wx:elif="{{_.isArray(icon)}}" class="{{classPrefix}}__icon">
Expand All @@ -34,22 +34,22 @@
/>
<view
wx:elif="{{!checked && (icon == 'circle' || icon == 'rectangle')}}"
class="{{_.cls(classPrefix + '__icon-' + icon, [['disabled', disabled]])}}"
class="{{_.cls(classPrefix + '__icon-' + icon, [['disabled', _disabled]])}}"
/>
<view wx:if="{{!checked && icon == 'line'}}" class="placeholder"></view>
</block>
</view>
<view class="{{classPrefix}}__content" data-target="text" mut-bind:tap="handleTap">
<view
class="{{_.cls(classPrefix + '__title', [['disabled', disabled], ['checked', checked]])}} {{prefix}}-class-label"
class="{{_.cls(classPrefix + '__title', [['disabled', _disabled], ['checked', checked]])}} {{prefix}}-class-label"
style="-webkit-line-clamp:{{maxLabelRow}}"
>
{{label}}
<slot />
<slot name="label" />
</view>
<view
class="{{_.cls(classPrefix + '__description', [['disabled', disabled]])}} {{prefix}}-class-content "
class="{{_.cls(classPrefix + '__description', [['disabled', _disabled]])}} {{prefix}}-class-content "
style="-webkit-line-clamp:{{maxContentRow}}"
>{{content}}<slot name="content"
/></view>
Expand Down

0 comments on commit b582fa5

Please sign in to comment.