toField

fun <Value> PreviewParameterProvider<Value>.toField(label: String, choiceLabel: (Value) -> String = { it.toString() }, type: SelectableField.Type = DROPDOWN): SelectableField<Value>

Converts a PreviewParameterProvider to a SelectableField.

Usage

class MyButtonProperty(val text: String, val backgroundColor: Color, val contentColor: Color)
class MyButtonPreviewParameterProvider() : PreviewParameterProvider<MyButtonProperty> {
override val values: Sequence<MyButtonProperty> = sequenceOf(...)
}

@Preview
@Composable
private fun MyButtonPreview() = PreviewLab {
val properties = fieldValue {
MyButtonPreviewParameterProvider().toField("properties")
}

MyButton(
text = properties.text,
backgroundColor = properties.backgroundColor,
contentColor = properties.contentColor,
)
}

See also


fun <Value> List<Value>.toField(label: String, choiceLabel: (Value) -> String = { it.toString() }, type: SelectableField.Type = DROPDOWN, initialValue: Value = this[0]): SelectableField<Value>

Extension function to convert a List to a SelectableField.

Usage

@Preview
@Composable
fun ColorPreview() = PreviewLab {
val color: Color = fieldValue {
listOf(Color.Red, Color.Green, Color.Blue).toField(
label = "Color",
choiceLabel = { it.toString() }
)
}
Box(Modifier.background(color).size(100.dp))
}

// With custom type
@Preview
@Composable
fun SizePreview() = PreviewLab {
val size: Int = fieldValue {
listOf(8, 16, 24, 32).toField(
label = "Spacing",
choiceLabel = { "${it}dp" },
type = SelectableField.Type.CHIPS
)
}
Spacer(Modifier.height(size.dp))
}