mui-custom-form 0.1.0 → 0.1.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/dist/CustomForm.js +1 -0
- package/dist/CustomForm.js.map +1 -0
- package/package.json +4 -4
- package/src/CustomForm.tsx +0 -171
- package/tsconfig.json +0 -13
package/dist/CustomForm.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CustomForm.js","sourceRoot":"","sources":["../src/CustomForm.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAWsB;AACtB,sDAAgD;AAChD,mDAA4C;AAE5C,gDAAyB;AA0BlB,IAAM,UAAU,GAA0B,UAAC,EAMjD;QALC,YAAY,kBAAA,EACZ,QAAQ,cAAA,EACR,WAAW,iBAAA,EACX,oBAAmB,EAAnB,YAAY,mBAAG,IAAI,KAAA,EACnB,UAAU,gBAAA;IAEF,IAAA,OAAO,GAAe,WAAW,QAA1B,EAAE,QAAQ,GAAK,WAAW,SAAhB,CAAgB;IAEzC,IAAM,WAAW,GAAG,UAAC,KAA2B;QAC9C,QAAQ,KAAK,CAAC,IAAI,EAAE;YAClB,KAAK,MAAM,CAAC;YACZ,KAAK,QAAQ;gBACX,OAAO,CACL,8BAAC,4BAAU,IACT,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,UAAC,EAA8C;4BAArC,YAAY,WAAA,EAAgB,KAAK,sBAAA;wBAAS,OAAA,CAC1D,8BAAC,oBAAS,eACJ,YAAY,EACZ,KAAK,CAAC,UAAU,IACpB,KAAK,EAAE,KAAK,CAAC,KAAK,EAClB,IAAI,EAAE,KAAK,CAAC,IAAI,EAChB,SAAS,QACT,QAAQ,EAAE,KAAK,CAAC,QAAQ,EACxB,KAAK,EAAE,CAAC,CAAC,KAAK,EACd,UAAU,EAAE,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,IAC1B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K,OAAA,CACjC,8BAAC,eAAI,IAAC,IAAI,QAAC,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,KAAK,CAAC,IAAI,IAAI,IAAI,IAC/C,WAAW,CAAC,KAAwC,CAAC,CACjD,CACR,EAJkC,CAIlC,CAAC,CACG,CACR,EARuC,CAQvC,CAAC,CACG;QACN,YAAY,IAAI,CACf,8BAAC,iBAAM,IAAC,IAAI,EAAC,QAAQ,EAAC,OAAO,EAAC,WAAW,aAEhC,CACV,CACK,CACT,CAAA;AACH,CAAC,CAAA;AAjIY,QAAA,UAAU,cAiItB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mui-custom-form",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A versatile React form component utilizing MUI components and react-hook-form.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/DevOsamaIslam/
|
|
13
|
+
"url": "git+https://github.com/DevOsamaIslam/mui-custom-form.git"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
16
|
"react",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"author": "Your Name",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/
|
|
25
|
+
"url": "https://github.com/DevOsamaIslam/mui-custom-form/issues"
|
|
26
26
|
},
|
|
27
|
-
"homepage": "https://github.com/
|
|
27
|
+
"homepage": "https://github.com/DevOsamaIslam/mui-custom-form#readme",
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@mui/material": "5.14.2",
|
|
30
30
|
"@mui/x-date-pickers": "^6.10.2",
|
package/src/CustomForm.tsx
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Button,
|
|
3
|
-
FormControl,
|
|
4
|
-
FormHelperText,
|
|
5
|
-
FormLabel,
|
|
6
|
-
Grid,
|
|
7
|
-
InputLabel,
|
|
8
|
-
MenuItem,
|
|
9
|
-
Select,
|
|
10
|
-
Stack,
|
|
11
|
-
TextField,
|
|
12
|
-
} from "@mui/material"
|
|
13
|
-
import { DatePicker } from "@mui/x-date-pickers"
|
|
14
|
-
import { Controller } from "react-hook-form"
|
|
15
|
-
import { useForm } from "react-hook-form"
|
|
16
|
-
import React from "react"
|
|
17
|
-
|
|
18
|
-
export type $option<T = any> = {
|
|
19
|
-
icon?: React.ReactNode
|
|
20
|
-
label: string
|
|
21
|
-
value: T
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ICustomField<T = string> {
|
|
25
|
-
label: string
|
|
26
|
-
name: T extends string ? string : keyof T
|
|
27
|
-
type: "text" | "number" | "single-select" | "multi-select" | "date" | "file"
|
|
28
|
-
list?: $option[]
|
|
29
|
-
required?: boolean // This can be further refined to your validation rules
|
|
30
|
-
otherProps?: any
|
|
31
|
-
span?: number // A number between 1 and 12, representing MUI's grid system
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface ICustomForm {
|
|
35
|
-
fieldsGroups: ICustomField<any>[][]
|
|
36
|
-
onSubmit: ReturnType<this["formControl"]["handleSubmit"]>
|
|
37
|
-
formControl: ReturnType<typeof useForm>
|
|
38
|
-
submitButton?: boolean
|
|
39
|
-
otherProps?: any
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const CustomForm: React.FC<ICustomForm> = ({
|
|
43
|
-
fieldsGroups,
|
|
44
|
-
onSubmit,
|
|
45
|
-
formControl,
|
|
46
|
-
submitButton = true,
|
|
47
|
-
otherProps,
|
|
48
|
-
}) => {
|
|
49
|
-
const { control, setValue } = formControl
|
|
50
|
-
|
|
51
|
-
const renderField = (field: ICustomField<string>) => {
|
|
52
|
-
switch (field.type) {
|
|
53
|
-
case "text":
|
|
54
|
-
case "number":
|
|
55
|
-
return (
|
|
56
|
-
<Controller
|
|
57
|
-
name={field.name}
|
|
58
|
-
control={control}
|
|
59
|
-
render={({ field: controlField, fieldState: { error } }) => (
|
|
60
|
-
<TextField
|
|
61
|
-
{...controlField}
|
|
62
|
-
{...field.otherProps}
|
|
63
|
-
label={field.label}
|
|
64
|
-
type={field.type}
|
|
65
|
-
fullWidth
|
|
66
|
-
required={field.required}
|
|
67
|
-
error={!!error}
|
|
68
|
-
helperText={error?.message}
|
|
69
|
-
/>
|
|
70
|
-
)}
|
|
71
|
-
/>
|
|
72
|
-
)
|
|
73
|
-
case "single-select":
|
|
74
|
-
case "multi-select":
|
|
75
|
-
return (
|
|
76
|
-
<Controller
|
|
77
|
-
name={field.name}
|
|
78
|
-
control={control}
|
|
79
|
-
render={({ field: controlField, fieldState: { error } }) => (
|
|
80
|
-
<FormControl fullWidth error={!!error}>
|
|
81
|
-
<InputLabel required={field.required}>{field.label}</InputLabel>
|
|
82
|
-
<Select
|
|
83
|
-
{...controlField}
|
|
84
|
-
{...field.otherProps}
|
|
85
|
-
multiple={field.type === "multi-select"}>
|
|
86
|
-
{field.list?.map((item) => (
|
|
87
|
-
<MenuItem key={item.value} value={item.value}>
|
|
88
|
-
{item.label}
|
|
89
|
-
</MenuItem>
|
|
90
|
-
))}
|
|
91
|
-
</Select>
|
|
92
|
-
{error && <FormHelperText>{error.message}</FormHelperText>}
|
|
93
|
-
</FormControl>
|
|
94
|
-
)}
|
|
95
|
-
/>
|
|
96
|
-
)
|
|
97
|
-
case "date":
|
|
98
|
-
return (
|
|
99
|
-
<Controller
|
|
100
|
-
name={field.name}
|
|
101
|
-
control={control}
|
|
102
|
-
render={({ field: controlField, fieldState: { error } }) => (
|
|
103
|
-
<DatePicker
|
|
104
|
-
{...controlField}
|
|
105
|
-
{...field.otherProps}
|
|
106
|
-
label={field.label}
|
|
107
|
-
onChange={controlField.onChange}
|
|
108
|
-
slotProps={{
|
|
109
|
-
textField: {
|
|
110
|
-
required: field.required,
|
|
111
|
-
error: !!error,
|
|
112
|
-
helperText: error?.message,
|
|
113
|
-
fullWidth: true,
|
|
114
|
-
},
|
|
115
|
-
}}
|
|
116
|
-
/>
|
|
117
|
-
)}
|
|
118
|
-
/>
|
|
119
|
-
)
|
|
120
|
-
case "file":
|
|
121
|
-
return (
|
|
122
|
-
<>
|
|
123
|
-
<FormLabel component="legend">{field.label}</FormLabel>
|
|
124
|
-
<Button variant="contained" component="label">
|
|
125
|
-
Upload File
|
|
126
|
-
<input
|
|
127
|
-
type="file"
|
|
128
|
-
hidden
|
|
129
|
-
onChange={(e) => {
|
|
130
|
-
// Set the value to either the File instance or undefined
|
|
131
|
-
const fileValue =
|
|
132
|
-
e.target.files && e.target.files.length > 0
|
|
133
|
-
? e.target.files[0]
|
|
134
|
-
: undefined
|
|
135
|
-
setValue(field.name, fileValue)
|
|
136
|
-
}}
|
|
137
|
-
/>
|
|
138
|
-
</Button>
|
|
139
|
-
</>
|
|
140
|
-
)
|
|
141
|
-
default:
|
|
142
|
-
return null
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<Stack
|
|
148
|
-
component="form"
|
|
149
|
-
onSubmit={onSubmit}
|
|
150
|
-
noValidate
|
|
151
|
-
{...otherProps}
|
|
152
|
-
spacing={3}>
|
|
153
|
-
<Grid container spacing={1}>
|
|
154
|
-
{fieldsGroups.map((fields, rowIndex) => (
|
|
155
|
-
<Grid container item key={rowIndex} spacing={2}>
|
|
156
|
-
{fields.map((field, fieldIndex) => (
|
|
157
|
-
<Grid item key={fieldIndex} xs={field.span || true}>
|
|
158
|
-
{renderField(field as unknown as ICustomField<string>)}
|
|
159
|
-
</Grid>
|
|
160
|
-
))}
|
|
161
|
-
</Grid>
|
|
162
|
-
))}
|
|
163
|
-
</Grid>
|
|
164
|
-
{submitButton && (
|
|
165
|
-
<Button type="submit" variant="contained">
|
|
166
|
-
Submit
|
|
167
|
-
</Button>
|
|
168
|
-
)}
|
|
169
|
-
</Stack>
|
|
170
|
-
)
|
|
171
|
-
}
|
package/tsconfig.json
DELETED