@react-hive/honey-form
v2.0.0
Published
The TS library to build the fully customized forms using React framework
Downloads
14
Readme
React Honey Form
Intro
Honey Form is a React library designed to simplify the process of creating and managing forms. It offers a collection of customizable and extensible components that enable you to effortlessly create forms that seamlessly integrate with your application's visual style.
With Honey Form, you can streamline your form development workflow and handle form state management with ease. The library provides an intuitive API and a range of features to support various form scenarios, including dynamic form fields, form validation, error handling, and form submission.
Key Features
- Customizable and Extensible: Honey Form offers a set of components that can be easily customized to match your application's look and feel. You have full control over the styling and behavior of the form components.
- Effortless Form Creation: With Honey Form, you can quickly create forms by leveraging the provided components and intuitive configuration options. Define your form structure, set validation rules, and handle form submission with simplicity.
- Dynamic Form Fields: Honey Form supports dynamic form fields, allowing you to add or remove fields dynamically as per your application requirements. This feature is particularly useful when dealing with forms that involve variable sets of data.
- Form Validation: Ensure data integrity and user input correctness with Honey Form's built-in form validation capabilities. Define validation rules for each form field, and easily handle validation errors.
- Form Submission: Honey Form simplifies the process of submitting form data. Hook into the form submission event, handle the submission logic, and interact with the form data seamlessly.
Parameters
The useHoneyForm
hook takes an options object as a single argument with the following properties:
fields
- An object that defines the fields of the form. Each field is an object that defines its properties such as type, required, value, min, max, etc. See more in theUseHoneyFormFieldConfig
type.formIndex
- An optional parameter that represents the index of the child form within an array of forms. This parameter is used when working with nested forms.parentField
- An optional parameter that specifies the parent field for a child form. When working with nested forms, this parameter allows you to establish a parent-child relationship between forms. TheparentField
should be a reference to the parent form field where the child form's data will be stored. By linking a child form to a parent field, changes in the child form's fields can be synchronized with the corresponding parent field, enabling hierarchical data management.defaults
- An optional object that defines the default values for the form fields. It allows you to pre-populate the form fields with initial values. If thedefaults
argument is a regular object, it is expected to have keys corresponding to the field names and values representing the default values for those fields. If thedefaults
argument is a Promise function, it should resolve an object with the same structure, allowing for more dynamic default values based on some logic or external data.values
- An optional object with form field values that can be provided to the form to synchronize its values. If provided, the form will stay in sync with these external values. The callbackonChange
will not be called when using this form field values synchronization.resetAfterSubmit
- An optional parameter that specifies whether the form should be reset to its initial state after a successful submission. The form will be reset only when theonSubmit
callback completes without returning any errors.context
- An optional object that you can pass to the form for use in field validators or other custom logic. This context can contain additional data or functions that are needed for validation or other form-related operations.onSubmit
- A callback function that will be called when the form is submitted. The function receives the form data as a parameter.onChange
- An optional callback function that will be called when any field value is changed.onChangeDebounce
- An optional number that specifies the debounce time in milliseconds for theonChange
callback. Default is0
.
Field Configuration
defaultValue
- The default value of the form field.type
- The type of the form field. It can be one of the following values:string
: Represents a general string input field.numeric
: Represents a field that accepts only numeric values without decimal places.number
: Represents a field that accepts numeric values, including decimals and optional negative numbers.email
: Represents an email input field.checkbox
: Represents a checkbox input field.radio
: Represents a radio input field. Thevalue
property will not be destructured from field properties and must be set directly in the input field'svalue
attribute.file
: Represents a file input field.object
: Represents an object field, whereonChange
can directly accept any object instead ofe.target.value
.nestedForms
: Represents the array type of field to work with nested forms.
required
- A boolean value indicating whether the form field is required or not. If set totrue
, the field must have a non-empty value for the form to be considered valid. Default isfalse
.min
- The minimum value allowed for numeric fields or minimum length of a string. Only applicable for fields of typenumber
orstring
.max
- The maximum value allowed for numeric fields or maximum length of a string. Only applicable for fields of typenumber
orstring
.decimal
- A boolean value indicating whether the numeric field can accept decimal values. Only applicable for fields of typenumber
.negative
- A boolean value indicating whether the numeric field can accept negative values. Only applicable for fields of typenumber
.maxFraction
- The maximum number of decimal places allowed for numeric fields. Only applicable for fields of typenumber
and when decimal is set to true.dependsOn
- Specifies one or more fields that the current field depends on. When any of the dependent fields change, the current field's value will be set asundefined
. This property can be a single field name (as a string), an array of field names, or a function that must return a boolean value indicating whether the field is dependent on the specified condition.errorMessages
- An object that specifies custom error messages for different validation errors. The keys of the object correspond to validation error types, and the values are the corresponding error messages. This allows you to customize the error messages displayed for specific validation errors.validator
- A custom validation function for the field. It should accept the field value as an argument and return either true (indicating the value is valid) or an error message (indicating the value is invalid). The validator function can also be asynchronous and return a Promise that resolves to the same response.filter
- A function that can be used to remove or modify certain characters from the field value. The function takes the current value as input and should return the modified value.formatter
- A function that can be used to transform the field value into a different format. The function takes the current value as input and should return the transformed value.formatOnBlur
- A boolean flag indicating whether the formatter function should be applied to the field's value when the focus is removed from the input (on blur). Default isfalse
.submitFormattedValue
- A boolean flag indicating when formatted field value should be submitted instead of clean value. Default isfalse
.props
- Additional properties for configuring the field's HTML input element.skip
- A function that determines whether the field should not be validated and skipped (not included) in the form submission. The function takes the complete form fields object as input and should return a boolean value indicating whether the field should be skipped.onChange
: A callback function that will be called whenever the field value changes. This can be used to perform additional actions or side effects when the field value changes.
Return value
The useHoneyForm
hook returns an object with the following properties:
formFields
- An object that contains the state of the form fields. Each field has the following properties:defaultValue
: The default value initially set for the field.rawValue
: The unprocessed value, before any filtering or formatting.cleanValue
: The processed value after filtering and formatting. If there are errors, this may beundefined
.value
: The final, formatted value ready to be displayed to the user.errors
: An array of error messages if the field is invalid.props
: An object with the necessary props for interactive elements (inputs where a user can type any text) to bind to the corresponding input element in the form.passiveProps
: Properties for non-interactive fields (e.g., checkbox, radio, file).objectProps
: Properties for object fields, enabling direct handling of object values. TheonChange
handler directly accepts any object instead ofe.target.value
.config
: The original configuration object of the field.getChildFormsValues
: A function to retrieve child forms' values if the field is a parent field.setValue
: A function to set the value of the field.pushValue
: A function to add a new value to a parent field that can have child forms.removeValue
: A function to remove a value from a parent field by its index.resetValue
: A function to reset a value to the initial.addError
: A function to add an error to the field's error array.clearErrors
: A function to clear all errors associated with this field.validate
: A function to validate this field.focus
: A function to focus on this field. Note: Can only be used whenprops
are destructured within a component.__meta__
: Internal metadata used by the library.
formValues
- Provides quick access to the current values of all form fields.formDefaultValues
- Provides quick access to the default values of all form fields.formErrors
- An object that includes all field errors. It is{}
by default. When a field has any error, the field appears in this object as a key, and the value is an array of field errors.isFormErred
- A boolean value that becomestrue
when the form has any error. It remainsfalse
when the form is error-free.isFormDefaultsFetching
- A boolean value that indicates whether form default values are being retrieved from a Promise function. It isfalse
by default and becomestrue
during the retrieval process. It returns tofalse
when default values are successfully retrieved or an error occurs.isFormDefaultsFetchingErred
- A boolean value that indicates whether there was an error retrieving form default values. It isfalse
by default and becomestrue
if the default values cannot be retrieved from the Promise function.isFormDirty
- A boolean value that indicates whether any field value in the form has changed. It isfalse
by default and becomestrue
when any field value is changed. It returns tofalse
when the form is successfully submitted.isFormValidating
- A boolean value that becomestrue
when the form is in the process of validation. It indicates that the validation of the form's fields is currently underway.isFormValid
- A boolean value that becomestrue
when the process of form validation has successfully finished, and no errors have been detected in any of the form's fields.isFormSubmitting
- A boolean value that indicates whether the form is currently submitting.isFormSubmitted
- A boolean value that becomestrue
when the form has been successfully submitted. It resets tofalse
when any field value is changed.setFormValues
- A function that allows setting form values. It supports partial field value setting. TheclearAll
option can be used to clear other fields that were not mentioned.addFormField
- A function that dynamically allows adding a new form field.removeFormField
- A function to remove a form field. You can only remove optional form fields.addFormFieldError
- A function to add a new error related to a specific field. All previous field errors remain present.clearFormErrors
- A function to clear all form field errors.validateForm
- A function to validate the form.submitForm
- A function to submit the form. It can accept an async function that will be called with the clean form data.resetForm
- A function to reset all form field values to their initial state.
Examples
Hook based example
import React from 'react';
import { useHoneyForm } from '@react-hive/honey-form';
// Define the form fields structure
type ProfileForm = {
name: string;
age: number;
}
const Form = () => {
// Use the useHoneyForm hook to manage form state
const { formFields, submitForm } = useHoneyForm<ProfileForm>({
fields: {
name: {
type: 'string',
required: true
},
age: {
type: 'number'
},
},
onSubmit: (data) => {
// Handle form submission
console.log(data);
},
});
return <form noValidate>
<input {...formFields.name.props}/>
<input {...formFields.age.props}/>
<button type="button" onClick={submitForm}>Submit</button>
</form>;
}
Component based example
import React from 'react';
import { HoneyForm } from '@react-hive/honey-form';
// Define the form fields structure
type ProfileForm = {
name: string;
age: number;
}
const Form = () => {
const handleSubmit = (data: ProfileForm) => {
// Process form data
console.log(data);
}
// Render the form using the HoneyForm component
return (
<HoneyForm
fields={{
name: {
type: 'string',
required: true,
},
age: {
type: 'number',
},
}}
onSubmit={handleSubmit} // Pass the submit handler
>
{/* Destructure the formFields and submitForm from the render prop function */}
{({ formFields }) => (
<>
<input {...formFields.name.props} />
<input {...formFields.age.props} />
<button type="submit">Submit</button>
</>
)}
</HoneyForm>
);
};
Conclusion
The @react-hive/honey-form
is a powerful and customizable library for creating and managing forms in React. With its comprehensive set of components and hooks, you can effortlessly create forms that seamlessly blend with your application's design and behavior. The library provides convenient features for handling form submission and validation, simplifying the development process. Whether you're building simple or complex forms, react-honey-form
empowers you to create delightful user experiences with ease.