|
| 1 | + |
| 2 | + |
1 | 3 | # FeatureForm
|
2 | 4 |
|
3 | 5 | ## Description
|
4 | 6 |
|
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). |
6 | 8 |
|
7 | 9 | ## Behavior
|
8 | 10 |
|
9 | 11 | To see it in action, check out the [microapp](../../microapps/FeatureFormsApp).
|
10 | 12 |
|
11 |
| -## Usage |
| 13 | +## Features |
12 | 14 |
|
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. |
15 | 24 |
|
16 | 25 |
|
17 |
| -#### Creating the state using the Factory function |
| 26 | +## Usage |
18 | 27 |
|
19 |
| -```kotlin |
20 |
| -val formState = FeatureFormState() |
21 |
| -``` |
| 28 | +A `FeatureForm` composable can be created using a `FeatureForm` object as follows. |
22 | 29 |
|
23 |
| -#### Using ViewModels |
| 30 | +#### Creating a FeatureForm object |
24 | 31 |
|
25 | 32 | ```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) |
29 | 39 | ```
|
30 | 40 |
|
31 |
| -#### Creating the FeatureForm |
| 41 | +#### Creating a FeatureForm in Compose |
32 | 42 |
|
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. |
35 | 44 |
|
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 |
42 | 72 | 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 | + } |
53 | 78 | }
|
54 |
| -``` |
| 79 | +``` |
| 80 | + |
| 81 | +#### Changing the Validation Error Visibility policy |
55 | 82 |
|
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. |
57 | 84 |
|
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.* |
59 | 87 |
|
60 | 88 | ```kotlin
|
61 | 89 | @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 | +) |
70 | 95 | ```
|
| 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