withSerializer
fun <Value> MutablePreviewLabField<Value>.withSerializer(serializer: KSerializer<Value>): MutablePreviewLabField<Value>
Wraps this field with a custom me.tbsten.compose.preview.lab.PreviewLabField.serializer implementation.
Use this extension function to provide a custom serializer for fields that don't have built-in serialization support, or to override the default serializer with a custom one.
Usage
@Serializable
enum class Theme { Light, Dark, System }
@Preview
@Composable
fun ThemePreview() = PreviewLab {
val theme = fieldValue {
SelectableField(
label = "Theme",
choices = Theme.entries,
choiceLabel = { it.name },
).withSerializer(Theme.serializer())
}
AppTheme(theme = theme)
}Content copied to clipboard
When to use
When using SelectableField with serializable enum or sealed class values
When the field type has a custom serializer that differs from the default
When you need to enable serialization for fields that return null by default
Return
A new field with the custom serializer implementation
Parameters
serializer
The KSerializer to use for this field's value type