Skip to content

Commit 6bfb43a

Browse files
committed
Implement onValueChangeFinished for sliders on JS
1 parent b2b4271 commit 6bfb43a

File tree

2 files changed

+16
-3
lines changed
  • demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo
  • material3/src/jsMain/kotlin/com/huanshankeji/compose/material3

2 files changed

+16
-3
lines changed

demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/Material3.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,17 @@ fun Material3(/*modifier: Modifier = Modifier*/
277277
Column {
278278
var sliderPosition by remember { mutableStateOf(0.5f) }
279279
Text("Slider position: $sliderPosition")
280+
var sliderChanging by remember { mutableStateOf(false) }
281+
Text("Slider changing: $sliderChanging")
280282
Slider(
281283
value = sliderPosition,
282-
onValueChange = { sliderPosition = it }
284+
onValueChange = {
285+
sliderChanging = true
286+
sliderPosition = it
287+
},
288+
onValueChangeFinished = {
289+
sliderChanging = false
290+
}
283291
)
284292
var discreteSliderPosition by remember { mutableStateOf(0.5f) }
285293
Text("Discrete slider position: $discreteSliderPosition")

material3/src/jsMain/kotlin/com/huanshankeji/compose/material3/Slider.js.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
44
import com.huanshankeji.compose.html.material3.MdSlider
55
import com.huanshankeji.compose.ui.Modifier
66
import com.huanshankeji.compose.ui.toAttrs
7+
import com.huanshankeji.compose.web.attributes.ext.onChange
78
import com.huanshankeji.compose.web.attributes.ext.onInput
89
import com.huanshankeji.compose.web.attributes.isFalseOrNull
910

@@ -31,6 +32,8 @@ actual fun Slider(
3132
step = calculateStep(steps, valueRange),
3233
disabled = enabled.isFalseOrNull(),
3334
attrs = modifier.toAttrs {
35+
//https://github.com/material-components/material-web/blob/main/docs/components/slider.md#events
36+
3437
onInput { event ->
3538
// MdSlider uses input event for value changes
3639
//console.log("Slider input event: ", event)
@@ -39,8 +42,8 @@ actual fun Slider(
3942
val value = target.value as Float
4043
onValueChange(value)
4144
}
42-
// Note: onValueChangeFinished is not currently supported in the JS implementation
43-
// TODO: Add support for onValueChangeFinished using appropriate event listener
45+
46+
onValueChangeFinished?.let { onChange { it() } }
4447
}
4548
)
4649

@@ -70,4 +73,6 @@ actual fun RangeSlider(
7073
val newEnd = target.valueEnd as Float
7174
onValueChange(newStart..newEnd)
7275
}
76+
77+
onValueChangeFinished?.let { onChange { it() } }
7378
})

0 commit comments

Comments
 (0)