Skip to content

Commit 5657aa6

Browse files
fix: 🐛 修复 Segmented 选项点击时无论是否改变选中值都会触发 change 的问题 (#1326)
✅ Closes: #1323
1 parent c3c6eb1 commit 5657aa6

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎src/uni_modules/wot-design-uni/components/wd-segmented/wd-segmented.vue‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232

3333
<script setup lang="ts">
3434
import { computed, getCurrentInstance, onMounted, reactive, watch } from 'vue'
35-
import { getRect, isObj, objToStyle, addUnit, pause } from '../common/util'
35+
import { getRect, isObj, objToStyle, addUnit, pause, isEqual } from '../common/util'
3636
import type { CSSProperties } from 'vue'
3737
import { segmentedProps, type SegmentedExpose, type SegmentedOption } from './types'
3838
const $item = '.wd-segmented__item'
@@ -94,20 +94,29 @@ function updateActiveStyle(animation: boolean = true) {
9494
})
9595
}
9696
97+
/**
98+
* 更新值
99+
*/
100+
function updateValue(newValue: string | number, option: string | number | SegmentedOption) {
101+
if (!isEqual(newValue, props.value)) {
102+
emit('update:value', newValue)
103+
emit('change', isObj(option) ? option : { value: newValue })
104+
}
105+
}
106+
97107
/**
98108
* 更新当前下标
99109
*/
100110
function updateCurrentIndex() {
101111
const index = props.options.findIndex((option: string | number | SegmentedOption) => {
102112
const value = isObj(option) ? option.value : option
103-
return value == props.value
113+
return isEqual(value, props.value)
104114
})
105115
if (index >= 0) {
106116
state.activeIndex = index
107117
} else {
108118
const value = isObj(props.options[0]) ? props.options[0].value : props.options[0]
109-
emit('update:value', value)
110-
emit('change', isObj(props.options[0]) ? props.options[0] : { value })
119+
updateValue(value, props.options[0])
111120
}
112121
}
113122
@@ -117,10 +126,10 @@ function handleClick(option: string | number | SegmentedOption, index: number) {
117126
return
118127
}
119128
const value = isObj(option) ? option.value : option
129+
120130
state.activeIndex = index
121131
updateActiveStyle()
122-
emit('update:value', value)
123-
emit('change', isObj(option) ? option : { value })
132+
updateValue(value, option)
124133
emit('click', isObj(option) ? option : { value })
125134
}
126135

0 commit comments

Comments
 (0)