Custom
Overview
CustomField is a component that allows you to define custom form fields that can be used in your react application. You can use it to render any type of form field that you want, based on the type specified in the field configuration.
CustomField can be used either globally, by specifying it in the fieldComponents object passed to the FormProvider component, or locally, by specifying the Field prop in the field configuration when creating a form.
Example
import { Field, FieldConfig } from '@tutim/types';
export const CustomField: Field = ({ inputProps, fieldConfig }) => {
const { value, onChange } = inputProps;
const onClick = () => onChange(value + 2);
return (
<button type="button" onClick={onClick}>
{fieldConfig.label}: {value}
</button>
);
};Interface
Field
This is an interface representing a custom form field. It includes the following properties:
props: The propertiesYourCustomFieldgets, return any JSX field
FieldProps
This is an interface representing field props. It includes the following properties:
``
inputProps: The control properties of the input, use it to control your field.``
fieldConfig: The properties defined onFieldConfig, use it to customize your field (static).
InputProps
This is an interface representing a form of field props. It includes the following properties:
value: The current value of the fieldonChange: The update method. Use it to updatevalue.
FieldConfig
This is an interface representing the configuration for a single form field. It includes the following properties:
key: A unique identifier for the field.type: The type of input to use for the field, as specified by theInputTypeenumeration.``
Field: A Field Component, use in order to replace the default (global) field set for thistype, can only be used withInputType.Custom.label: (optional) The label to display for the field.isDisabled: (optional) A boolean value indicating whether the field should be disabled.isRequired: (optional) A boolean value indicating whether the field is required.options: (optional) An array ofOptionobjects for use withInputType.SelectandInputType.Radiofields.defaultValue: (optional) The default value for the field.
InputState
This is an interface representing a form of field state props. It includes the following properties:
invalid: A boolean representing the validity of the fielderror: Error object withmessage, usually a validation message
More examples
Basic example
To use CustomField, you will need to define the custom field component and specify the type that you want to use to reference it. You can do this by creating a new component that implements the Field interface from the @tutim/types library, and specifying the type in the field configuration when creating a form.
Here is an example of how to use CustomField to create a custom form field that renders a button that increments a number when clicked:
You can then use the customFieldConfig object to create a form with a custom field that increments its value by 2 every time the button is clicked:
Advanced example
You can use CustomField globally by specifying it in the fieldComponents object passed to the FormProvider component. This allows you to use your custom field in any form within your application, without having to specify the Field prop in the field configuration for each form.
To use CustomField globally, you will need to define the custom field component and add it to the fieldComponents object, using the type as the key and the field component as the value.
For example, you can use CustomField to create a custom form field that renders a button that increments a number when clicked, and use it in multiple forms by specifying it in the fieldComponents object passed to FormProvider:
In this example, the CustomField component is added to the fieldComponents object with the key 'custom-field'. This means that any form field with an type of 'custom-field' will use the CustomField component to render the field.
Multiple custom fields
You can use multiple custom fields in your application by adding them to the fieldComponents object in the same way. For example, you can define a second custom field like this:
You can then add the CustomField2 component to the fieldComponents object, using the type 'custom-field-2' as the key:
In this example, the CustomField and CustomField2 components are added to the fieldComponents object with the keys 'custom-field' and 'custom-field-2', respectively. This means that any form field with an type of 'custom-field' will use the CustomField component to render the field, and any form field with an type of 'custom-field-2' will use the CustomField2 component to render the field.
Last updated
Was this helpful?