mui-custom-form 0.1.5 → 0.1.7

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.
@@ -17,7 +17,7 @@ export interface ICustomField<T = string> {
17
17
  interface ICustomForm {
18
18
  fieldsGroups: ICustomField<any>[][];
19
19
  onSubmit: ReturnType<this["formControl"]["handleSubmit"]>;
20
- formControl: ReturnType<typeof useForm>;
20
+ formControl: ReturnType<typeof useForm<any>>;
21
21
  submitButton?: boolean;
22
22
  otherProps?: any;
23
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mui-custom-form",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "A versatile React form component utilizing MUI components and react-hook-form.",
5
5
  "main": "dist/CustomForm.js",
6
6
  "types": "dist/CustomForm.d.ts",
@@ -36,5 +36,10 @@
36
36
  "@types/node": "^20.5.1",
37
37
  "@types/react": "^18.2.20",
38
38
  "typescript": "^5.1.6"
39
+ },
40
+ "dependencies": {
41
+ "date-fns": "^2.30.0",
42
+ "dayjs": "^1.11.9",
43
+ "moment": "^2.29.4"
39
44
  }
40
45
  }
package/readme.md CHANGED
@@ -36,6 +36,8 @@ Ensure that you have the required peer dependencies installed:
36
36
 
37
37
  #### Props
38
38
 
39
+ - CustomForm component
40
+
39
41
  | Name | Description |
40
42
  | -------------- | -------------------------------------------------------- |
41
43
  | `fieldsGroups` | 2D array representing groups of fields in the form. |
@@ -44,18 +46,39 @@ Ensure that you have the required peer dependencies installed:
44
46
  | `submitButton` | Boolean to toggle the visibility of the submit button. |
45
47
  | `otherProps` | Any additional props to pass down to the form component. |
46
48
 
49
+ - CustomField 2D array
50
+
51
+ The `ICustomField` interface is used to define each field in the form. Below are its properties:
52
+
53
+ | Name | Description |
54
+ | ------------ | ----------------------------------------------------------------------------------------- |
55
+ | `label` | The display label of the field. |
56
+ | `name` | The name attribute of the field, used for form data identification. |
57
+ | `type` | The type of the field, determining its behavior and appearance. |
58
+ | `list` | An array of options for select type fields. Each option has a label and a value. |
59
+ | `required` | Indicates whether the field is mandatory or not. |
60
+ | `otherProps` | Any additional props to pass to the underlying MUI component. |
61
+ | `span` | A number between 1 and 12, representing the grid span for the field in MUI's grid system. |
62
+
63
+ The `list` array, has the following structure:
64
+
65
+ | Name | Description |
66
+ | ------- | ---------------------------------------------------------- |
67
+ | `icon` | An optional icon to display alongside the option. |
68
+ | `label` | The display label of the option. |
69
+ | `value` | The value of the option, used when the option is selected. |
70
+
71
+ ---
72
+
47
73
  ### Example
48
74
 
49
75
  Here's a simple example showcasing how to implement a `CustomForm`:
50
76
 
51
77
  ```typescript
52
- import React from "react"
53
- import { CustomForm } from "your-package-name"
54
-
55
78
  const MyComponent = () => {
56
79
  const formControl = useForm()
57
80
 
58
- const fieldsGroups = [
81
+ const fieldsGroups: ICustomField[][] = [
59
82
  [
60
83
  {
61
84
  label: "Username",
@@ -71,20 +94,66 @@ const MyComponent = () => {
71
94
  ],
72
95
  ]
73
96
 
74
- const handleSubmit = (data) => {
75
- console.log(data)
97
+ const handleSubmit = (data: unknown) => {
98
+ console.log({ success: data })
76
99
  }
77
100
 
78
101
  return (
79
102
  <CustomForm
80
103
  fieldsGroups={fieldsGroups}
81
- onSubmit={handleSubmit}
104
+ onSubmit={formControl.handleSubmit(handleSubmit)}
82
105
  formControl={formControl}
83
106
  />
84
107
  )
85
108
  }
109
+ ```
110
+
111
+ Usage with zod
112
+
113
+ ```typescript
114
+ const Fields = z.object({
115
+ username: z.string(),
116
+ age: z.string(),
117
+ })
118
+
119
+ type FieldTypes = z.infer<typeof Fields>
120
+
121
+ function MyComponent() {
122
+ const formControl = useForm<FieldTypes>({
123
+ resolver: zodResolver(Fields),
124
+ })
125
+
126
+ const fieldsGroups: ICustomField<FieldTypes>[][] = [
127
+ [
128
+ {
129
+ label: "Username",
130
+ name: "username",
131
+ type: "text",
132
+ required: true,
133
+ },
134
+ {
135
+ label: "Age",
136
+ name: "age",
137
+ type: "number",
138
+ required: true,
139
+ },
140
+ ],
141
+ ]
142
+
143
+ const onSubmit = (data: FieldTypes) => {
144
+ console.log({ success: data })
145
+ }
146
+
147
+ return (
148
+ <CustomForm
149
+ fieldsGroups={fieldsGroups}
150
+ onSubmit={formControl.handleSubmit(onSubmit, (fail) =>
151
+ console.log({ fail })
152
+ )}
153
+ formControl={formControl}
154
+ />
155
+ )
86
156
 
87
- export default MyComponent
88
157
  ```
89
158
 
90
159
  ### Contribution