Skip to content

Commit 9789ae5

Browse files
committed
Clarify documentation for creating custom form elements and add note on JSON-serializable values
1 parent 1b66945 commit 9789ae5

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

docs/designing-studies/custom-form-elements.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This page will walk you through how to create and use custom form elements in yo
66

77
## Creating Custom Form Elements
88

9-
To create a custom form element, you need to define a React component that follows the structure defined by the `CustomResponseParams` type. This component receives the current response value, metadata about the response itself, optional custom parameters from the study config, validation state, and a `field` helper for updating and blurring the value. You can then use this component in your study configuration to collect responses from participants. Here's a basic example of a custom form element that collects a color input from participants:
9+
To create a custom form element, you need to define a React component that follows the structure defined by the `CustomResponseParams` type. This component receives the current response value, metadata about the response itself, optional custom parameters from the study config, the current validation error message if one exists, and a `field` helper for updating and blurring the value. You can then use this component in your study configuration to collect responses from participants. Here's a basic example of a custom form element that collects a color input from participants:
1010

1111
```tsx title="ColorPicker.tsx"
1212
import { CustomResponseParams } from '../../../store/types';
@@ -65,6 +65,10 @@ To use your custom form element in your study config, you need to define a respo
6565
The path here is relative to the `src/public` directory of the ReVISit app, not the `public` directory used for most other study assets. You can place your custom form element component anywhere under `src/public`. We recommend that you follow the same folder structure that we suggest in the [react stimulus docs](./react-stimulus.md) for your custom form elements.
6666
:::
6767

68+
::note[JSON-serializable values]
69+
Custom response values must be JSON-serializable. This applies both to the values you store through `field.setValue(...)` and to any `default` value you define in the study config. Strings, numbers, booleans, `null`, arrays, and plain objects are supported.
70+
:::
71+
6872
## Custom Validation Logic for Custom Form Elements
6973

7074
Custom validation logic can be added to your custom form elements by defining a validation function that checks the response value and returns an error message if the value is invalid. The validation controls whether the participant can submit their response or not. The `validate` function receives the current value, the full set of response values for the component, and the current response config, so it can validate against sibling answers or `response.parameters`. Here's an example of how you can add custom validation logic to the `ColorPicker` component:
@@ -114,4 +118,4 @@ When you include this custom form element in your study config, the validation l
114118

115119
## Provenance tracking for custom form elements
116120

117-
Since custom form elements are hooked into the same response system as built-in form elements, they will automatically have provenance tracking enabled. For proper rehydration of the response values, make sure that your custom form element component updates values through `field.setValue(...)` and marks interaction with `field.onBlur()`. This ensures that the response values are properly tracked and can be analyzed.
121+
Since custom form elements are hooked into the same response system as built-in form elements, their answers participate in the standard saving and provenance flow. To keep the stored answer in sync, update values through `field.setValue(...)`. Call `field.onBlur()` when the user finishes interacting with the element so validation and touched-state behavior work as expected.

0 commit comments

Comments
 (0)