Skip to content

Commit 13653c3

Browse files
committed
feat: switch 增加点击控制能力
1 parent 9688077 commit 13653c3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

packages/mpx-cube-ui/lib/components/switch/switch.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ createComponent({
2626
disabled: {
2727
type: Boolean,
2828
value: false
29+
},
30+
/**
31+
* @description 点击后是否需要更改 value
32+
* @optional true/false
33+
*/
34+
changeOnClick: {
35+
type: Boolean,
36+
value: true
2937
}
3038
},
3139
data: {
@@ -68,10 +76,18 @@ createComponent({
6876
},
6977
methods: {
7078
toggleSwitch() {
71-
if (this.disabled)
79+
if (this.disabled) {
80+
this.triggerEvent('click', { value: this.isOn });
81+
return;
82+
}
83+
if (!this.changeOnClick) {
84+
this.triggerEvent('click', { value: this.isOn });
7285
return;
86+
}
7387
const newValue = !this.isOn;
7488
this.isOn = newValue;
89+
// 当开关有点击时触发
90+
this.triggerEvent('click', { value: newValue });
7591
// 当开关状态变化时触发
7692
this.triggerEvent('change', { value: newValue });
7793
// 当开关状态变化时触发

packages/mpx-cube-ui/src/components/switch/switch.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ createComponent({
2727
disabled: {
2828
type: Boolean,
2929
value: false
30+
},
31+
/**
32+
* @description 点击后是否需要更改 value
33+
* @optional true/false
34+
*/
35+
changeOnClick: {
36+
type: Boolean,
37+
value: true
3038
}
3139
},
3240
data: {
@@ -67,9 +75,20 @@ createComponent({
6775
},
6876
methods: {
6977
toggleSwitch() {
70-
if (this.disabled) return
78+
if (this.disabled) {
79+
this.triggerEvent('click', { value: this.isOn })
80+
return
81+
}
82+
83+
if (!this.changeOnClick) {
84+
this.triggerEvent('click', { value: this.isOn })
85+
return
86+
}
87+
7188
const newValue = !this.isOn
7289
this.isOn = newValue
90+
// 当开关有点击时触发
91+
this.triggerEvent('click', { value: newValue })
7392
// 当开关状态变化时触发
7493
this.triggerEvent('change', { value: newValue })
7594
// 当开关状态变化时触发

0 commit comments

Comments
 (0)