SetField

open class SetField<Value>(label: String, initialValue: Set<Value>, elementField: SetField.ElementFieldScope.() -> MutablePreviewLabField<Value>, defaultValue: () -> Value = { initialValue.firstOrNull() ?: error("SetField requires a non-empty initialValue or an explicit defaultValue") }) : MutablePreviewLabField<Set<Value>>

A field for editing Set values with support for dynamic element insertion, deletion, and duplicate detection.

SetField provides a UI for editing set collections where each element is represented by its own field. Elements can be inserted at any position using insert buttons. Unlike ListField, SetField automatically detects and highlights duplicate values, displaying them with an error indicator in the edit modal.

Usage

PreviewLab {
val fruits by fieldValue {
SetField(
label = "fruits",
initialValue = setOf("Apple", "Banana", "Cherry"),
elementField = { StringField(label, initialValue) },
)
}
Text(fruits.joinToString(", "))
}

Duplicate Detection

When editing, if two or more elements have the same value, they will be highlighted with an error color and a message indicating which values are duplicated. The summary view shows only unique values.

Parameters

Value

The type of elements in the set.

label

Label displayed in the UI for this field.

initialValue

Initial set value.

elementField

Factory function to create a field for each element. Receives ElementFieldScope with the element's label (index) and initial value.

defaultValue

Factory function to create a default value when inserting new elements. Defaults to the first element of initialValue if available.

Constructors

Link copied to clipboard
constructor(label: String, initialValue: Set<Value>, elementField: SetField.ElementFieldScope.() -> MutablePreviewLabField<Value>, defaultValue: () -> Value = { initialValue.firstOrNull() ?: error("SetField requires a non-empty initialValue or an explicit defaultValue") })

Types

Link copied to clipboard
inner class ElementFieldScope

Scope provided to elementField factory function for creating element fields.

Properties

Link copied to clipboard
open override val coroutineScope: CoroutineScope
Link copied to clipboard
open override val initialValue: Set<Value>

Default value for this field.

Link copied to clipboard
open override var label: String

The label for this field. This is not used in any of the program logic, but only for display purposes, so it is best to set it in a language that is easy for your team members to read.

Link copied to clipboard
open override var value: Set<Value>

Current value of this PreviewLabField.

Link copied to clipboard
open override val valueFlow: Flow<Set<Value>>

Functions

Link copied to clipboard
open operator override fun component1(): Set<Value>
Link copied to clipboard
open operator override fun component2(): (Set<Value>) -> Unit
Link copied to clipboard
open override fun Content()

Composable, which displays the main UI for this Field. If you want to customize the UI, you can override this method in your PreviewLabField to customize the UI.

Link copied to clipboard
fun <Value> PreviewLabField<Value>.DefaultFieldView(modifier: Modifier = Modifier, menuItems: List<PreviewLabField.ViewMenuItem<Value>> = ViewMenuItem.defaults<Value>(this), content: @Composable () -> Unit = { Content() })

Default UI implementation of me.tbsten.compose.preview.lab.PreviewLabField.View. Display a label and draw the content below it.

Link copied to clipboard
fun <Value> PreviewLabField<Value>.FieldLabelHeader(menuItems: List<PreviewLabField.ViewMenuItem<Value>> = ViewMenuItem.defaults<Value>(this))

Display the label of PreviewLabField.

Link copied to clipboard
fun <Value : Any> PreviewLabField<Value>.nullable(initialValue: Value? = this.initialValue): NullableField<Value>

Create a PreviewLabField that makes the receiver's PreviewLabField nullable.

Link copied to clipboard
open fun onCleared()
Link copied to clipboard
open override fun serializer(): KSerializer<Set<Value>>?

Returns a KSerializer for this field's value type, or null if serialization is not supported.

Link copied to clipboard
open fun testValues(): List<Set<Value>>

Returns a list of representative values for this field to be used in automated testing.

Link copied to clipboard
fun <Value> MutablePreviewLabField<Value>.TextFieldContent(toString: (Value) -> String, toValue: (String) -> Result<Value>, modifier: Modifier = Modifier, prefix: @Composable () -> Unit? = null, suffix: @Composable () -> Unit? = null, placeholder: @Composable () -> Unit? = null)

Helper for UI of Fields that can be input with TextField.

Link copied to clipboard

Transforms a MutablePreviewLabField to work with a different value type.

Link copied to clipboard
open override fun valueCode(): String

Returns a Kotlin code string representing the current value of this field.

Link copied to clipboard

Composable, which displays the entire UI for this Field. If you want to customize the UI, you can override this method in your PreviewLabField to customize the UI. However, in many cases where the UI is customized, overriding the content method is more appropriate.

Link copied to clipboard

Adds an "Empty List" hint to a ListField, allowing quick selection of an empty list value.

Adds an "Empty Set" hint to a SetField, allowing quick selection of an empty set value.

Link copied to clipboard

Adds hint choices to a MutablePreviewLabField, allowing users to quickly select from predefined values.

Adds hint choices to a MutablePreviewLabField from a Map.

Adds a single action hint to a MutablePreviewLabField.

Link copied to clipboard

Adds hint actions to a MutablePreviewLabField, allowing users to execute custom actions.

Link copied to clipboard
fun MutablePreviewLabField<String>.withImageUrlHint(includeDummyImages: Boolean = true, includeInvalidImages: Boolean = true): MutablePreviewLabField<String>
Link copied to clipboard
Link copied to clipboard

Adds predefined color hints to a Color field for quick selection.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun <Value> MutablePreviewLabField<Value>.wrap(wrapRange: WrapRange = WrapRange.OnlyContent, content: @Composable (@Composable () -> Unit) -> Unit): WrapField<Value>

Wraps this field with additional composable content.