react-form-dto 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/components/Field.d.ts +34 -0
- package/dist/components/FormBuilder.d.ts +45 -0
- package/dist/components/Section.d.ts +30 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/renderers/AutoComplete.d.ts +10 -0
- package/dist/components/renderers/CheckBoxInput.d.ts +16 -0
- package/dist/components/renderers/MultiAutoComplete.d.ts +10 -0
- package/dist/components/renderers/RadioInput.d.ts +10 -0
- package/dist/components/renderers/SelectInput.d.ts +16 -0
- package/dist/components/renderers/TextInput.d.ts +16 -0
- package/dist/components/renderers/TextareaInput.d.ts +10 -0
- package/dist/components/renderers/index.d.ts +7 -0
- package/dist/examples/profileForm.d.ts +2 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/useFormBuilder.d.ts +29 -0
- package/dist/index.d.ts +4 -1
- package/dist/react-form-dto.es.js +406 -20469
- package/dist/react-form-dto.umd.js +2 -162
- package/dist/types/dto.d.ts +50 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/layout.d.ts +18 -0
- package/dist/utils/validation.d.ts +24 -0
- package/package.json +17 -31
package/README.md
CHANGED
|
@@ -18,6 +18,14 @@ This library lets you define forms declaratively using JSON DTOs (`FormDTO`, `Se
|
|
|
18
18
|
|
|
19
19
|
---
|
|
20
20
|
|
|
21
|
+
📘 Documentation
|
|
22
|
+
|
|
23
|
+
Comprehensive documentation is available here
|
|
24
|
+
|
|
25
|
+
👉 [Documentation](https://shakir-afridi.github.io/react-form-dto/)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
21
29
|
## 📦 Installation
|
|
22
30
|
|
|
23
31
|
Clone the repo and install dependencies:
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FieldDTO } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Props passed to field renderer components.
|
|
5
|
+
*/
|
|
6
|
+
export interface FieldRendererProps {
|
|
7
|
+
/** The field definition containing type, label, validation rules, etc. */
|
|
8
|
+
field: FieldDTO;
|
|
9
|
+
/** The current value of the field */
|
|
10
|
+
value: any;
|
|
11
|
+
/** Callback function to update the field value */
|
|
12
|
+
onChange: (val: any) => void;
|
|
13
|
+
error?: string | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Props for the Field component.
|
|
17
|
+
*/
|
|
18
|
+
interface FieldProps extends FieldRendererProps {
|
|
19
|
+
renderers?: Record<string, React.ComponentType<FieldRendererProps>>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Field component that renders the appropriate input component based on field type.
|
|
23
|
+
* Supports both default renderers and custom renderers provided via props.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* <Field
|
|
27
|
+
* field={fieldDTO}
|
|
28
|
+
* value={currentValue}
|
|
29
|
+
* onChange={handleChange}
|
|
30
|
+
* renderers={{ customType: CustomRenderer }}
|
|
31
|
+
* />
|
|
32
|
+
*/
|
|
33
|
+
export declare const Field: React.FC<FieldProps>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FormDTO } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Handle interface exposed by FormBuilder via ref.
|
|
5
|
+
* Provides methods to interact with the form programmatically.
|
|
6
|
+
*/
|
|
7
|
+
export type FormBuilderHandle = {
|
|
8
|
+
/** Returns all current form values */
|
|
9
|
+
getValues: () => Record<string, any>;
|
|
10
|
+
/** Returns all current form errors */
|
|
11
|
+
getErrors: () => Record<string, string | null>;
|
|
12
|
+
/** Validates all fields and returns errors */
|
|
13
|
+
validateAll: () => Record<string, string[]>;
|
|
14
|
+
/** Validates a specific field by ID and returns errors */
|
|
15
|
+
validateField: (id: string) => string[];
|
|
16
|
+
handleChange?: (id: string, val: any) => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Props for the FormBuilder component.
|
|
20
|
+
*/
|
|
21
|
+
type FormBuilderProps = {
|
|
22
|
+
/** The form DTO definition containing all sections and fields */
|
|
23
|
+
dto: FormDTO;
|
|
24
|
+
/** Optional custom renderers for specific field types */
|
|
25
|
+
renderers?: Record<string, React.ComponentType<any>>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* FormBuilder component that dynamically renders a form based on a FormDTO definition.
|
|
29
|
+
* Supports custom field renderers, validation, and programmatic access via ref.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const formRef = useRef<FormBuilderHandle>(null);
|
|
33
|
+
*
|
|
34
|
+
* const handleSubmit = () => {
|
|
35
|
+
* const errors = formRef.current?.validateAll();
|
|
36
|
+
* if (Object.keys(errors || {}).length === 0) {
|
|
37
|
+
* const values = formRef.current?.getValues();
|
|
38
|
+
* // Submit form values
|
|
39
|
+
* }
|
|
40
|
+
* };
|
|
41
|
+
*
|
|
42
|
+
* <FormBuilder ref={formRef} dto={myFormDTO} renderers={customRenderers} />
|
|
43
|
+
*/
|
|
44
|
+
export declare const FormBuilder: React.ForwardRefExoticComponent<FormBuilderProps & React.RefAttributes<FormBuilderHandle>>;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { SectionDTO } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Props for the Section component.
|
|
5
|
+
*/
|
|
6
|
+
type SectionProps = {
|
|
7
|
+
/** The section definition containing heading, description, and fields */
|
|
8
|
+
section: SectionDTO;
|
|
9
|
+
/** Current form values keyed by field ID */
|
|
10
|
+
values: Record<string, any>;
|
|
11
|
+
/** Callback function to handle field value changes */
|
|
12
|
+
onChange: (id: string, val: any) => void;
|
|
13
|
+
/** Optional custom renderers for specific field types */
|
|
14
|
+
renderers?: Record<string, React.ComponentType<any>>;
|
|
15
|
+
validateField: (id: string) => string[];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Section component that renders a form section with its heading, description, and fields.
|
|
19
|
+
* Automatically arranges fields in a responsive grid layout based on their column span configuration.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* <Section
|
|
23
|
+
* section={sectionDTO}
|
|
24
|
+
* values={formValues}
|
|
25
|
+
* onChange={handleFieldChange}
|
|
26
|
+
* renderers={customRenderers}
|
|
27
|
+
* />
|
|
28
|
+
*/
|
|
29
|
+
export declare const Section: React.FC<SectionProps>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FieldDTO } from '../../types';
|
|
3
|
+
type AutoCompleteFieldProps = {
|
|
4
|
+
field: FieldDTO;
|
|
5
|
+
value: any;
|
|
6
|
+
onChange: (val: any) => void;
|
|
7
|
+
error?: string | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const AutoCompleteField: React.FC<AutoCompleteFieldProps>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FieldRendererProps } from '../../components/Field';
|
|
2
|
+
/**
|
|
3
|
+
* Checkbox input renderer for boolean/checkbox field types.
|
|
4
|
+
* Renders a Material-UI Checkbox wrapped in a FormControlLabel.
|
|
5
|
+
*
|
|
6
|
+
* @param props - Field renderer props containing field definition, value, and onChange handler
|
|
7
|
+
* @returns A configured Checkbox component with label
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* <CheckBoxInput
|
|
11
|
+
* field={fieldDTO}
|
|
12
|
+
* value={isChecked}
|
|
13
|
+
* onChange={handleChange}
|
|
14
|
+
* />
|
|
15
|
+
*/
|
|
16
|
+
export declare function CheckBoxInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FieldDTO } from '../../types';
|
|
3
|
+
type MultiAutoCompleteFieldProps = {
|
|
4
|
+
field: FieldDTO;
|
|
5
|
+
value: any[];
|
|
6
|
+
onChange: (val: any) => void;
|
|
7
|
+
error?: string | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const MultiAutoCompleteField: React.FC<MultiAutoCompleteFieldProps>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FieldDTO } from '../../types';
|
|
3
|
+
type RadioInputProps = {
|
|
4
|
+
field: FieldDTO;
|
|
5
|
+
value: any;
|
|
6
|
+
onChange: (val: any) => void;
|
|
7
|
+
error?: string | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const RadioInput: React.FC<RadioInputProps>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FieldRendererProps } from '../../components/Field';
|
|
2
|
+
/**
|
|
3
|
+
* Select input renderer for dropdown/select field types.
|
|
4
|
+
* Renders a Material-UI Select component with options from the field definition.
|
|
5
|
+
*
|
|
6
|
+
* @param props - Field renderer props containing field definition, value, and onChange handler
|
|
7
|
+
* @returns A configured Select component with MenuItems for each option
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* <SelectInput
|
|
11
|
+
* field={fieldDTO}
|
|
12
|
+
* value={selectedValue}
|
|
13
|
+
* onChange={handleChange}
|
|
14
|
+
* />
|
|
15
|
+
*/
|
|
16
|
+
export declare function SelectInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FieldRendererProps } from '../../components/Field';
|
|
2
|
+
/**
|
|
3
|
+
* Text input renderer for text, number, and date field types.
|
|
4
|
+
* Renders a Material-UI TextField with appropriate configuration based on field properties.
|
|
5
|
+
*
|
|
6
|
+
* @param props - Field renderer props containing field definition, value, and onChange handler
|
|
7
|
+
* @returns A configured TextField component
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* <TextInput
|
|
11
|
+
* field={fieldDTO}
|
|
12
|
+
* value={currentValue}
|
|
13
|
+
* onChange={handleChange}
|
|
14
|
+
* />
|
|
15
|
+
*/
|
|
16
|
+
export declare function TextInput({ field, value, onChange, error, }: FieldRendererProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { FieldDTO } from '../../types';
|
|
3
|
+
type TextAreaInputProps = {
|
|
4
|
+
field: FieldDTO;
|
|
5
|
+
value: any;
|
|
6
|
+
onChange: (val: any) => void;
|
|
7
|
+
error?: string | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const TextAreaInput: React.FC<TextAreaInputProps>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useFormBuilder';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FormDTO } from '../types';
|
|
2
|
+
type Errors = Record<string, string | null>;
|
|
3
|
+
/**
|
|
4
|
+
* A custom React hook for building and managing form state with validation.
|
|
5
|
+
* Provides form values, errors, and validation utilities for a given FormDTO.
|
|
6
|
+
*
|
|
7
|
+
* @param dto - The form DTO definition containing sections and fields
|
|
8
|
+
* @returns An object containing form state and utility functions
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const form = useFormBuilder(myFormDTO);
|
|
12
|
+
* form.handleChange('email', 'user@example.com');
|
|
13
|
+
* const errors = form.validateAll();
|
|
14
|
+
*/
|
|
15
|
+
export declare function useFormBuilder(dto: FormDTO): {
|
|
16
|
+
/** Current form values for all fields */
|
|
17
|
+
values: Record<string, any>;
|
|
18
|
+
/** Function to update a field value */
|
|
19
|
+
handleChange: (id: string, val: any) => void;
|
|
20
|
+
/** Function to validate all fields */
|
|
21
|
+
validateAll: () => Record<string, string[]>;
|
|
22
|
+
/** Function to get all current form values */
|
|
23
|
+
getValues: () => Record<string, any>;
|
|
24
|
+
/** Function to get all current form errors */
|
|
25
|
+
getErrors: () => Errors;
|
|
26
|
+
/** Function to validate a specific field */
|
|
27
|
+
validateField: (id: string) => string[];
|
|
28
|
+
};
|
|
29
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export {}
|
|
1
|
+
export { FormBuilder, Section, Field, TextAreaInput, AutoCompleteField, MultiAutoCompleteField, RadioInput, SelectInput, TextInput, CheckBoxInput, } from './components';
|
|
2
|
+
export { validateAll, validateField, validationRules } from './utils';
|
|
3
|
+
export type { FieldDTO, SectionDTO, FormDTO, InputType, Validations, } from './types';
|
|
4
|
+
export { useFormBuilder } from './hooks';
|