
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@firecms/formex
Advanced tools
Formex is a lightweight, flexible library designed to simplify form handling within React applications. By leveraging React's powerful context and hooks features, Formex allows for efficient form state management with minimal boilerplate code.
To install Formex, you can use either npm or yarn:
npm install @firecms/formex
# or if you're using yarn
yarn add @firecms/formex
To get started with Formex, you first need to create your form context and form controller using the useCreateFormex
hook. Then, you can structure your form using the <Field />
components provided by Formex.
import React from 'react';
import { useCreateFormex } from 'formex-library';
const MyForm = () => {
const formController = useCreateFormex({
initialValues: {
name: '',
email: '',
},
// Optionally add a validation function
// validation: values => {
// const errors = {};
// if (!values.name) errors.name = 'Name is required';
// return errors;
// },
onSubmit: (values) => {
console.log('Form Submitted:', values);
},
});
return (
<form onSubmit={formController.handleSubmit}>
{/* Field components go here */}
</form>
);
};
<Field />
componentimport { Field } from 'formex-library';
// Inside your form component
<Field name="name">
{({ field }) => (
<input
{...field}
placeholder="Your name"
/>
)}
</Field>
<Field name="email">
{({ field }) => (
<input
{...field}
type="email"
placeholder="Your email"
/>
)}
</Field>
<button type="submit">Submit</button>
Wrap your form inputs and submit button within a form element and pass the submitForm
method from your form controller to the form's onSubmit
event:
<form onSubmit={formController.handleSubmit}>
{/* Fields and submit button */}
</form>
useCreateFormex
Hook to create a form controller.
Parameters
initialValues
: An object with your form's initial values.initialErrors
(optional): An object for any initial validation errors.validation
(optional): A function for validating form data.validateOnChange
(optional): If true
, validates fields whenever they change.onSubmit
: A function that fires when the form is submitted.<Field />
A component used to render individual form fields.
Props
name
: The name of the form field.as
(optional): The component or HTML tag that should be rendered. Defaults to "input"
.children
: A function that returns the field input component. Receives field props as its parameter.Example
<Field name="username">
{({ field }) => <input {...field} />}
</Field>
Formex is designed to be flexible. You can create custom field components, use any validation library, or integrate with UI component libraries.
import { Field } from 'formex-library';
import { TextField } from 'some-ui-library';
<Field name="username">
{({ field }) => (
<TextField {...field} label="Username" />
)}
</Field>
Leverage the validation
function in useCreateFormex
to integrate any validation logic or library.
const validate = values => {
const errors = {};
if (!values.email.includes('@')) {
errors.email = 'Invalid email';
}
return errors;
};
Formex provides a simple yet powerful way to manage forms in React applications. It reduces the amount of boilerplate code needed and offers flexibility to work with custom components and validation strategies. Whether you are building simple or complex forms, Formex can help streamline your form management process.
For further examples and advanced usage, refer to the Formex documentation or source code.
FAQs
Unknown package
The npm package @firecms/formex receives a total of 230 weekly downloads. As such, @firecms/formex popularity was classified as not popular.
We found that @firecms/formex demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.