@bitkai/dynamic-fields
v0.1.0
Published
A TypeScript library for handling dynamic form fields with validation.
Downloads
11
Readme
Dynamic Fields
A TypeScript library for handling dynamic form fields with validation.
Installation
npm install @bitkai/dynamic-fieldsUsage
For complete examples, check out the examples folder:
- Form Schema Example - Shows how to define a complex form schema
- Usage Example - Demonstrates validation and default values
Basic usage:
import { Field, validateField, getDefaultValue, InferFieldType } from '@bitkai/dynamic-fields';
// Define a field schema
const field = {
type: 'text',
key: 'name',
label: 'Name',
minLength: 2,
maxLength: 50,
optional: false
} as const satisfies Field;
// Infer the type of the field value
type FieldValue = InferFieldType<typeof field>; // string
// Get default value
const defaultValue = getDefaultValue(field); // returns ''
// Validate a value
try {
const value: FieldValue = 'John';
validateField(field, value);
console.log('Valid!');
} catch (error) {
console.error(error.message);
}The InferFieldType helper provides automatic type inference for your field values based on the field definition. This ensures type safety when working with form values.
Field Types
The library supports the following field types:
text: Text input with optional length constraintsnumber: Numeric inputboolean: Boolean/checkbox inputarray: Array of fieldsgroup: Group of fieldsselect: Single selection from optionsmultiselect: Multiple selection from optionsimage: Image inputcolor: Color inputdate: Date inputtime: Time input
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT
