withValueCode

Wraps this field with a custom me.tbsten.compose.preview.lab.PreviewLabField.valueCode implementation.

Use this extension function to customize how the field's current value is converted to Kotlin code in the Inspector's Code tab, without creating a new field class.

Usage

@Preview
@Composable
fun FontWeightPreview() = PreviewLab {
val fontWeight = fieldValue {
SelectableField(
label = "Font Weight",
choices = listOf(FontWeight.Normal, FontWeight.Bold, FontWeight.W500),
choiceLabel = { it.toString() },
).withValueCode { weight ->
when (weight) {
FontWeight.Normal -> "FontWeight.Normal"
FontWeight.Bold -> "FontWeight.Bold"
else -> "FontWeight(${weight.weight})"
}
}
}

Text("Sample", fontWeight = fontWeight)
}

When to use

  • When using SelectableField with enum or sealed class values

  • When the default toString() representation isn't valid Kotlin code

  • When you want to use named constants (e.g., Color.Red) instead of constructor calls

Return

A new field with the custom valueCode implementation

Parameters

valueCode

A function that converts the current value to a Kotlin code string

See also