@@ -32,7 +32,7 @@ export default {
3232
3333<script setup lang="ts">
3434import { 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'
3636import type { CSSProperties } from ' vue'
3737import { segmentedProps , type SegmentedExpose , type SegmentedOption } from ' ./types'
3838const $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 */
100110function 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