Skip to content

Commit 08d546d

Browse files
authored
Forms: Update readme (#305)
1 parent 7bcd70c commit 08d546d

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

toolkit/featureforms/README.md

+68-42
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,96 @@
1+
2+
13
# FeatureForm
24

35
## Description
46

5-
The Forms toolkit component enables users to edit field values of features in a layer using forms that have been configured externally (using either in the the Web Map Viewer or the Fields Maps web app).
7+
The FeatureForm toolkit component enables users to edit field values of features in a layer using the `FeatureForm` API that has been configured externally (using either in the Web Map Viewer or the Fields Maps web app).
68

79
## Behavior
810

911
To see it in action, check out the [microapp](../../microapps/FeatureFormsApp).
1012

11-
## Usage
13+
## Features
1214

13-
The `FeatureForm` composable is provided with its `FeatureFormState` and its default implementation `FeatureFormStateImpl` which is also available through the Factory function `FeatureFormState()`.
14-
They can be used either as a simple state class or within a ViewModel.
15+
The `FeatureForm` is a Composable that can render a `FeatureForm` object with a `FeatureFormDefinition` using Jetpack Compose.
16+
- It can be integrated into any custom layout or container. The [microapp](../../microapps/FeatureFormsApp) integrates it into a `BottomSheet`.
17+
- All expressions are initially evaluated with a progress indicator before the FeatureForm is available for editing.
18+
- Provides automatic saving and restoring of form data after device configuration changes.
19+
- Shows validation errors for any fields with errors.
20+
- Visibility behavior of validation errors can be customized.
21+
- Supports all `FormInput` types except Attachments, Relationships and Barcodes.
22+
- Provides a DateTime picker and a picker for coded-value field types.
23+
- Follows material 3 design system.
1524

1625

17-
#### Creating the state using the Factory function
26+
## Usage
1827

19-
```kotlin
20-
val formState = FeatureFormState()
21-
```
28+
A `FeatureForm` composable can be created using a `FeatureForm` object as follows.
2229

23-
#### Using ViewModels
30+
#### Creating a FeatureForm object
2431

2532
```kotlin
26-
class MyViewModel : FeatureFormState by FeatureFormState() {
27-
...
28-
}
33+
// get the feature layer
34+
val layer = feature.featureTable.layer as FeatureLayer
35+
// grab the FeatureFormDefinition from the layer
36+
val featureFormDefinition = layer.featureFormDefinition
37+
// create the FeatureForm from the Feature and the FeatureFormDefinition
38+
val featureForm = FeatureForm(feature, featureFormDefinition)
2939
```
3040

31-
#### Creating the FeatureForm
41+
#### Creating a FeatureForm in Compose
3242

33-
The FeatureForm should be displayed in a Container and passed the `FeatureFormState`.
34-
It's visibility and the container are external and should be controlled by the calling Composable.
43+
A `FeatureForm` can be created within a composition by simply calling the `FeatureForm` composable with the `FeaureForm` object. The FeatureForm should be displayed in a container. It's visibility and the container are external and should be controlled by the calling Composable.
3544

36-
```kotlin
37-
@Composable
38-
fun MyComposable() {
39-
// use the inEditingMode to control the visibility of the FeatureForm
40-
val inEditingMode by formState.inEditingMode.collectAsState()
41-
// a container
45+
```kotlin
46+
@Composable
47+
fun MyComposable(featureForm : FeatureForm) {
48+
// a container
49+
MyContainer(modifier = Modifier) {
50+
// create a FeatureForm Composable
51+
FeatureForm(
52+
// pass in the FeatureForm object
53+
featureForm = featureForm,
54+
// control the layout using the modifier property
55+
modifier = Modifier.fillMaxSize()
56+
)
57+
}
58+
}
59+
```
60+
61+
#### Updating the `FeatureForm`
62+
63+
To display a new `FeatureForm` object, simply trigger a recomposition with the new `FeatureForm` object.
64+
65+
```kotlin
66+
@Composable
67+
fun MyComposable(viewModel : MyViewModel) {
68+
// use a state object that will recompose this composable when the featureForm changes
69+
// in this example, the FeatureForm object is hoisted in the ViewModel
70+
val featureForm : State by viewModel.featureForm
71+
// a container
4272
MyContainer(modifier = Modifier) {
43-
if (inEditingMode) {
44-
// show the FeatureForm only when inEditingMode is true
45-
FeatureForm(
46-
// pass in our FeatureFormState
47-
featureFormState = formState,
48-
// control the layout using the modifier property
49-
modifier = Modifier.fillMaxSize()
50-
)
51-
}
52-
}
73+
FeatureForm(
74+
featureForm = featureForm,
75+
modifier = Modifier.fillMaxSize()
76+
)
77+
}
5378
}
54-
```
79+
```
80+
81+
#### Changing the Validation Error Visibility policy
5582

56-
#### Updating the `FeatureFormState`
83+
By default validation errors for any fields are only visible after the fields gain focus. But this can be customized using the `validationErrorVisibility` parameter of the `FeatureForm`. This property can be changed at any time to show all the errors. It supports two modes of visibility.
5784

58-
Changes to the `FeatureFormState` will cause a recomposition.
85+
- **ValidationErrorVisibility.Automatic** : *Indicates that the validation errors are only visible for editable fields that have received focus.*
86+
- **ValidationErrorVisibility.Visible** : *Indicates the validation is run for all the editable fields, and errors are displayed regardless of the focus state.*
5987

6088
```kotlin
6189
@Composable
62-
fun MyComposable() {
63-
....
64-
// set the feature, this causes recomposition
65-
formState.setFeature(feature)
66-
// set formViewModel to editing state
67-
formState.setEditingActive(true)
68-
....
69-
}
90+
FeatureForm(
91+
featureForm = featureForm,
92+
modifier = Modifier.fillMaxSize(),
93+
validationErrorVisibility = ValidationErrorVisibility.Visible
94+
)
7095
```
96+
*Note* : Once the `validationErrorVisibility` is set to `Visible`, changing it back to `Automatic` will have no effect since all the fields have now gained focus to show any errors.

0 commit comments

Comments
 (0)