Gathering detailed insights and metrics for @tynik/react-honey-form
Gathering detailed insights and metrics for @tynik/react-honey-form
Gathering detailed insights and metrics for @tynik/react-honey-form
Gathering detailed insights and metrics for @tynik/react-honey-form
npm install @tynik/react-honey-form
Typescript
Module System
Node Version
NPM Version
64.2
Supply Chain
93.3
Quality
85.6
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last day
166.7%
8
Compared to previous day
Last week
15.4%
45
Compared to previous week
Last month
281.5%
103
Compared to previous month
Last year
-54%
9,347
Compared to previous year
3
26
HoneyForm 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 HoneyForm, 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.
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 the UseHoneyFormFieldConfig
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. The parentField
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 the defaults
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 the defaults
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 callback onChange
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 the onSubmit
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 the onChange
callback. Default is 0
.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. The value
property will not be destructured from field properties and must be set directly in the input field's value
attribute.file
: Represents a file input field.object
: Represents an object field, where onChange
can directly accept any object instead of e.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 to true
, the field must have a non-empty value for the form to be considered valid. Default is false
.min
- The minimum value allowed for numeric fields or minimum length of a string. Only applicable for fields of type number
or string
.max
- The maximum value allowed for numeric fields or maximum length of a string. Only applicable for fields of type number
or string
.decimal
- A boolean value indicating whether the numeric field can accept decimal values. Only applicable for fields of type number
.negative
- A boolean value indicating whether the numeric field can accept negative values. Only applicable for fields of type number
.maxFraction
- The maximum number of decimal places allowed for numeric fields. Only applicable for fields of type number
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 as undefined
. 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 is false
.submitFormattedValue
- A boolean flag indicating when formatted field value should be submitted instead of clean value. Default is false
.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.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 be undefined
.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. The onChange
handler directly accepts any object instead of e.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 when props
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 becomes true
when the form has any error. It remains false
when the form is error-free.isFormDefaultsFetching
- A boolean value that indicates whether form default values are being retrieved from a Promise function. It is false
by default and becomes true
during the retrieval process. It returns to false
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 is false
by default and becomes true
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 is false
by default and becomes true
when any field value is changed. It returns to false
when the form is successfully submitted.isFormValidating
- A boolean value that becomes true
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 becomes true
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 becomes true
when the form has been successfully submitted. It resets to false
when any field value is changed.setFormValues
- A function that allows setting form values. It supports partial field value setting. The clearAll
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.Hook based example
1import React from 'react'; 2import { useHoneyForm } from 'react-honey-form'; 3 4// Define the form fields structure 5type ProfileForm = { 6 name: string; 7 age: number; 8} 9 10const Form = () => { 11 // Use the useHoneyForm hook to manage form state 12 const { formFields, submitForm } = useHoneyForm<ProfileForm>({ 13 fields: { 14 name: { 15 type: 'string', 16 required: true 17 }, 18 age: { 19 type: 'number' 20 }, 21 }, 22 onSubmit: (data) => { 23 // Handle form submission 24 console.log(data); 25 }, 26 }); 27 28 return <form noValidate> 29 <input {...formFields.name.props}/> 30 31 <input {...formFields.age.props}/> 32 33 <button type="button" onClick={submitForm}>Submit</button> 34 </form>; 35}
Component based example
1import React from 'react'; 2import { HoneyForm } from 'react-honey-form'; 3 4// Define the form fields structure 5type ProfileForm = { 6 name: string; 7 age: number; 8} 9 10const Form = () => { 11 const handleSubmit = (data: ProfileForm) => { 12 // Process form data 13 console.log(data); 14 } 15 16 // Render the form using the HoneyForm component 17 return ( 18 <HoneyForm 19 fields={{ 20 name: { 21 type: 'string', 22 required: true, 23 }, 24 age: { 25 type: 'number', 26 }, 27 }} 28 onSubmit={handleSubmit} // Pass the submit handler 29 > 30 {/* Destructure the formFields and submitForm from the render prop function */} 31 {({ formFields }) => ( 32 <> 33 <input {...formFields.name.props} /> 34 <input {...formFields.age.props} /> 35 36 <button type="submit">Submit</button> 37 </> 38 )} 39 </HoneyForm> 40 ); 41}; 42
The react-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.
No vulnerabilities found.
No security vulnerabilities found.