form-conductor icon indicating copy to clipboard operation
form-conductor copied to clipboard

Way to set default value

Open Demetri0 opened this issue 11 months ago • 0 comments

I wonder if there is a way to set some initial data to fields in compose variant?

I have code like this:

@Composable
fun MySuperComponent() {
  Column {
    MySuperFetch( SomeRequest() ) { data ->

      form(SomeForm::class) {

         field(SomeForm::someField) {

           TextField(
             label = { Text("Cool Field") },
             value = state.value?.value.orEmpty(),
             onValueChange = this::setField
           )

         }

      }

    }
  }
}

I've found this way working, but it doesn't seems good.

@Composable
fun MySuperComponent() {
  Column {
    MySuperFetch( SomeRequest() ) { data ->

      form(SomeForm::class) {
         
         field(SomeForm::someField) {
          
+          val fld = this
+          LaunchedEffect(Unit) {
+             fld.setField(data.someField)
+          }

           TextField(
             label = { Text("Cool Field") },
             value = state.value?.value.orEmpty(),
             onValueChange = this::setField
           )

         }

      }

    }
  }
}

Is there better way? f.e. like this?

@Composable
fun MySuperComponent() {
  Column {
    MySuperFetch( SomeRequest() ) { data ->

      form(SomeForm::class) {

-        field(SomeForm::someField) {         
+        field(SomeForm::someField, defaultValue = data.someField) {

           TextField(
             label = { Text("Cool Field") },
             value = state.value?.value.orEmpty(),
             onValueChange = this::setField
           )

         }

      }

    }
  }
}

Demetri0 avatar Feb 12 '25 08:02 Demetri0