Gathering detailed insights and metrics for mobx-zod-form-store
Gathering detailed insights and metrics for mobx-zod-form-store
Gathering detailed insights and metrics for mobx-zod-form-store
Gathering detailed insights and metrics for mobx-zod-form-store
A MobX-based form store and component with Zod validation (WIP)
npm install mobx-zod-form-store
Typescript
Module System
Node Version
NPM Version
TypeScript (97.76%)
CSS (1.48%)
JavaScript (0.76%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
70 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 21, 2024
Latest Version
0.4.5
Package Id
mobx-zod-form-store@0.4.5
Unpacked Size
93.64 kB
Size
20.12 kB
File Count
97
NPM Version
10.2.3
Node Version
18.19.0
Published on
Feb 07, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
A MobX-based form store with Zod validation, featuring a dynamic Form component for React applications.
mobx-zod-form-store is a comprehensive utility package designed to streamline the process of creating, managing, and validating forms in React applications. It utilizes MobX for state management and Zod for robust schema validation. This package also includes a dynamic Form component, making form creation more declarative and less error-prone.
To install the package, use npm:
1npm i mobx-zod-form-store
Here's a basic example of how to use the FormStore:
import FormStore from 'mobx-form-store';
import { z } from 'zod';
// Define your schema
const formSchema = z.object({
name: z.string().min(1, 'Name is required'),
bio: z.string().min(1, 'Bio is required'),
});
// Create a store
const formStore = new FormStore({ name: '', bio: '' }, formSchema);
// Update fields and validate
formStore.setField('name', 'John Doe');
formStore.validate();
You can also use the useFormStore hook for managing form state in functional components:
import React from 'react';
import useFormStore from 'path-to-useFormStore';
import { z } from 'zod';
// Define your form schema
const formSchema = z.object({
name: z.string().min(1, 'Name is required'),
email: z.string().email('Invalid email address'),
});
const MyForm = () => {
const { formStore, handleChange, handleSubmit } = useFormStore({
initialFields: { name: '', email: '' },
validationSchema: formSchema,
onSubmit: async (fields) => {
// Handle form submission
console.log('Form Data:', fields);
},
});
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">Name</label>
<input
id="name"
type="text"
value={formStore.fields.name}
onChange={(e) => handleChange('name', e.target.value)}
/>
{formStore.errors.name && <span>{formStore.errors.name}</span>}
</div>
<div>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
value={formStore.fields.email}
onChange={(e) => handleChange('email', e.target.value)}
/>
{formStore.errors.email && <span>{formStore.errors.email}</span>}
</div>
<button type="submit">Submit</button>
</form>
);
};
export default MyForm;
The Form component allows you to create forms dynamically based on a configuration array. This array defines the fields, their types, placeholders, and other properties.
Form Field Types The package supports the following field types:
1import React from 'react'; 2import Form, { FieldType } from 'path-to-Form'; 3import { z } from 'zod'; 4 5// Form fields configuration 6const fieldsConfig = [ 7 { name: 'name', type: FieldType.Text, placeholder: 'Name', className: 'mb-4 p-2 border rounded' }, 8 { name: 'age', type: FieldType.Number, placeholder: 'Age', className: 'mb-4 p-2 border rounded' }, 9 // More fields with Tailwind classes... 10]; 11 12// Form validation schema 13const formSchema = z.object({ 14 name: z.string().min(1, 'Name is required'), 15 age: z.number().min(0, 'Age must be a positive number'), 16 // Validation for other fields... 17}); 18 19const MyDynamicForm = () => { 20 const handleSubmit = async (fields) => { 21 // Handle form submission 22 console.log('Form Data:', fields); 23 }; 24 25 return ( 26 <Form 27 fieldsConfig={fieldsConfig} 28 initialFields={{ name: '', age: 0 }} 29 validationSchema={formSchema} 30 onSubmit={handleSubmit} 31 className={'border p-2'} 32 /> 33 ); 34}; 35 36export default MyDynamicForm; 37 38
This package is designed with Tailwind CSS in mind, offering seamless integration for projects using the Tailwind CSS framework. You can easily apply Tailwind utility classes to enhance the styling and responsiveness of your forms.
The ImageUpload component in mobx-zod-form-store
provides an easy way to upload images to IPFS via NFT.storage, a free service for storing off-chain data like images and metadata for NFTs.
To use the ImageUpload component, you need to:
Set Up NFT.storage Account: First, sign up for an account at NFT.storage and get your API token.
Install Dependencies: Make sure mobx-zod-form-store
and its dependencies are installed in your project.
Use the Component: Import and use the ImageUpload component in your forms.
1 2import React from 'react'; 3import { useFormStore, ImageUpload } from 'mobx-zod-form-store'; 4import { z } from 'zod'; 5 6const formSchema = z.object({ 7 imageUri: z.array(z.string()), 8}); 9 10const MyForm = () => { 11 const { formStore, handleSubmit } = useFormStore({ 12 initialFields: { imageUri: [] }, 13 validationSchema: formSchema, 14 onSubmit: (fields) => { 15 console.log('Form Data:', fields); 16 }, 17 }); 18 19 return ( 20 <form onSubmit={handleSubmit}> 21 <ImageUpload name="imageUri" formStore={formStore} /> 22 <button type="submit">Submit</button> 23 </form> 24 ); 25}; 26 27export default MyForm; 28 29
Remember to provide the NFT.storage API token in your projects .env
to ensure the upload functionality works as expected.
Contributions, issues, and feature requests are welcome!
MIT
@taayyohh
No vulnerabilities found.
No security vulnerabilities found.