1.0.1 • Published 6 months ago
@two-devs-and-a-dog/flexible-react-form v1.0.1
Flexible React Form Generator
A React component that generates dynamic forms based on JSON schemas.
Installation
npm install flexible-react-form
# or
yarn add flexible-react-form
Usage
import { SchemaForm } from 'flexible-react-form';
const schema = {
title: 'Escape Room Location',
fields: {
numberOfRooms: {
type: 'number',
label: 'Number of Escape Rooms',
required: true,
min: 1
},
rooms: {
type: 'array',
label: 'Escape Rooms',
itemSchema: {
difficulty: {
type: 'range',
label: 'Difficulty Level',
min: 1,
max: 10,
required: true
}
}
}
}
};
function App() {
const handleSubmit = (data) => {
console.log(data);
};
return <SchemaForm schema={schema} onSubmit={handleSubmit} />;
}
Schema Structure
Field Types
text
: Text inputnumber
: Number inputrange
: Number input with min/maxenum
: Dropdown selectdate
: Date pickerarray
: Repeatable field group
Field Properties
type
: (Required) Field typelabel
: (Required) Field labelrequired
: Booleanmin
: Minimum value for number/rangemax
: Maximum value for number/rangeoptions
: Array of options for enum typesitemSchema
: Schema for array field items
Examples
Basic Form
const schema = {
title: 'Contact Form',
fields: {
name: {
type: 'text',
label: 'Full Name',
required: true
},
email: {
type: 'text',
label: 'Email',
required: true
}
}
};
Form with Enums
const schema = {
title: 'Store Details',
fields: {
pricePoint: {
type: 'enum',
label: 'Price Range',
options: ['$', '$$', '$$$', '$$$$'],
required: true
}
}
};
Nested Array Fields
const schema = {
title: 'Product Catalog',
fields: {
categories: {
type: 'array',
label: 'Categories',
itemSchema: {
name: {
type: 'text',
label: 'Category Name',
required: true
},
priority: {
type: 'number',
label: 'Priority Level',
min: 1,
max: 5
}
}
}
}
};
Props
Prop | Type | Description |
---|---|---|
schema | Object | Form schema definition |
onSubmit | Function | Callback function receiving form data |
Requirements
- React 16.8+
- Tailwind CSS 3.0+
License
MIT