mui-custom-form 0.1.6 → 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.
Files changed (2) hide show
  1. package/package.json +6 -5
  2. package/readme.md +52 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mui-custom-form",
3
- "version": "0.1.6",
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",
@@ -28,9 +28,6 @@
28
28
  "peerDependencies": {
29
29
  "@mui/material": "5.14.2",
30
30
  "@mui/x-date-pickers": "^6.10.2",
31
- "date-fns": "^2.30.0",
32
- "dayjs": "^1.11.9",
33
- "moment": "^2.29.4",
34
31
  "react-hook-form": "^7.45.2"
35
32
  },
36
33
  "devDependencies": {
@@ -40,5 +37,9 @@
40
37
  "@types/react": "^18.2.20",
41
38
  "typescript": "^5.1.6"
42
39
  },
43
- "dependencies": {}
40
+ "dependencies": {
41
+ "date-fns": "^2.30.0",
42
+ "dayjs": "^1.11.9",
43
+ "moment": "^2.29.4"
44
+ }
44
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,14 +46,35 @@ 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
 
@@ -72,7 +95,7 @@ const MyComponent = () => {
72
95
  ]
73
96
 
74
97
  const handleSubmit = (data: unknown) => {
75
- console.log(data)
98
+ console.log({ success: data })
76
99
  }
77
100
 
78
101
  return (
@@ -83,17 +106,24 @@ const MyComponent = () => {
83
106
  />
84
107
  )
85
108
  }
86
-
87
- export default MyComponent
88
109
  ```
89
110
 
90
111
  Usage with zod
91
112
 
92
113
  ```typescript
93
- const MyComponent = () => {
94
- const formControl = useForm()
114
+ const Fields = z.object({
115
+ username: z.string(),
116
+ age: z.string(),
117
+ })
95
118
 
96
- const fieldsGroups: ICustomField[][] = [
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>[][] = [
97
127
  [
98
128
  {
99
129
  label: "Username",
@@ -102,27 +132,28 @@ const MyComponent = () => {
102
132
  required: true,
103
133
  },
104
134
  {
105
- label: "Birthdate",
106
- name: "birthdate",
107
- type: "date",
135
+ label: "Age",
136
+ name: "age",
137
+ type: "number",
138
+ required: true,
108
139
  },
109
140
  ],
110
141
  ]
111
142
 
112
- const handleSubmit = (data: unknown) => {
113
- console.log(data)
143
+ const onSubmit = (data: FieldTypes) => {
144
+ console.log({ success: data })
114
145
  }
115
146
 
116
147
  return (
117
- <CustomForm
118
- fieldsGroups={fieldsGroups}
119
- onSubmit={formControl.handleSubmit(handleSubmit)}
120
- formControl={formControl}
121
- />
148
+ <CustomForm
149
+ fieldsGroups={fieldsGroups}
150
+ onSubmit={formControl.handleSubmit(onSubmit, (fail) =>
151
+ console.log({ fail })
152
+ )}
153
+ formControl={formControl}
154
+ />
122
155
  )
123
- }
124
156
 
125
- export default MyComponent
126
157
  ```
127
158
 
128
159
  ### Contribution