react-smart-fields 1.0.0 → 1.0.1
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/package.json +1 -1
- package/src/components/DynamicField.tsx +62 -32
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-smart-fields",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "> A flexible, customizable, and developer-friendly component to generate dynamic form fields in React. Supports all HTML inputs, validation, and styling out of the box.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Pratik Panchal",
|
|
@@ -22,7 +22,7 @@ export interface FieldConfig {
|
|
|
22
22
|
required?: boolean;
|
|
23
23
|
defaultValue?: any;
|
|
24
24
|
customValidation?: (value: any) => string | null;
|
|
25
|
-
options?: FieldOption[];
|
|
25
|
+
options?: FieldOption[];
|
|
26
26
|
style?: React.CSSProperties;
|
|
27
27
|
className?: string;
|
|
28
28
|
}
|
|
@@ -75,12 +75,20 @@ export const DynamicFields: React.FC<DynamicFieldsProps> = ({
|
|
|
75
75
|
if (onChange) onChange(newFormData, newErrors);
|
|
76
76
|
};
|
|
77
77
|
|
|
78
|
+
const defaultInputClass =
|
|
79
|
+
"padding: 0.6rem 1rem; border: 1px solid #ccc; border-radius: 8px; width: 100%; font-size: 1rem; transition: border-color 0.2s ease-in-out; outline: none;";
|
|
80
|
+
const defaultInputFocus =
|
|
81
|
+
"focus: border-color: #007BFF;";
|
|
82
|
+
|
|
78
83
|
return (
|
|
79
84
|
<>
|
|
80
85
|
{fields.map((field) => (
|
|
81
|
-
<div key={field.name} style={{ marginBottom: "
|
|
86
|
+
<div key={field.name} style={{ marginBottom: "1.25rem" }}>
|
|
82
87
|
{field.label && (
|
|
83
|
-
<label
|
|
88
|
+
<label
|
|
89
|
+
htmlFor={field.name}
|
|
90
|
+
style={{ display: "block", marginBottom: "0.5rem", fontWeight: 600 }}
|
|
91
|
+
>
|
|
84
92
|
{field.label}
|
|
85
93
|
</label>
|
|
86
94
|
)}
|
|
@@ -90,7 +98,16 @@ export const DynamicFields: React.FC<DynamicFieldsProps> = ({
|
|
|
90
98
|
<select
|
|
91
99
|
name={field.name}
|
|
92
100
|
className={field.className}
|
|
93
|
-
style={
|
|
101
|
+
style={{
|
|
102
|
+
...field.style,
|
|
103
|
+
...{
|
|
104
|
+
padding: "0.6rem 1rem",
|
|
105
|
+
border: "1px solid #ccc",
|
|
106
|
+
borderRadius: "8px",
|
|
107
|
+
width: "100%",
|
|
108
|
+
fontSize: "1rem",
|
|
109
|
+
},
|
|
110
|
+
}}
|
|
94
111
|
value={formData[field.name]}
|
|
95
112
|
onChange={(e) =>
|
|
96
113
|
handleChange(field.name, e.target.value, field)
|
|
@@ -106,44 +123,57 @@ export const DynamicFields: React.FC<DynamicFieldsProps> = ({
|
|
|
106
123
|
)}
|
|
107
124
|
|
|
108
125
|
{/* Radio Buttons */}
|
|
109
|
-
{field.type === "radio" &&
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
<
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
handleChange(field.name, opt.value, field)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
126
|
+
{field.type === "radio" && (
|
|
127
|
+
<div style={{ display: "flex", gap: "1rem" }}>
|
|
128
|
+
{field.options?.map((opt) => (
|
|
129
|
+
<label key={String(opt.value)} style={{ display: "flex", alignItems: "center", gap: "0.4rem" }}>
|
|
130
|
+
<input
|
|
131
|
+
type="radio"
|
|
132
|
+
name={field.name}
|
|
133
|
+
value={String(opt.value)}
|
|
134
|
+
checked={formData[field.name] === opt.value}
|
|
135
|
+
onChange={() => handleChange(field.name, opt.value, field)}
|
|
136
|
+
/>
|
|
137
|
+
{opt.label}
|
|
138
|
+
</label>
|
|
139
|
+
))}
|
|
140
|
+
</div>
|
|
141
|
+
)}
|
|
124
142
|
|
|
125
143
|
{/* Checkbox */}
|
|
126
144
|
{field.type === "checkbox" && (
|
|
127
|
-
<
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
145
|
+
<label style={{ display: "flex", alignItems: "center", gap: "0.5rem" }}>
|
|
146
|
+
<input
|
|
147
|
+
type="checkbox"
|
|
148
|
+
name={field.name}
|
|
149
|
+
checked={formData[field.name]}
|
|
150
|
+
className={field.className}
|
|
151
|
+
style={field.style}
|
|
152
|
+
onChange={(e) =>
|
|
153
|
+
handleChange(field.name, e.target.checked, field)
|
|
154
|
+
}
|
|
155
|
+
/>
|
|
156
|
+
{field.label}
|
|
157
|
+
</label>
|
|
137
158
|
)}
|
|
138
159
|
|
|
139
|
-
{/*
|
|
160
|
+
{/* Text, Email, Number, etc. */}
|
|
140
161
|
{!["select", "radio", "checkbox"].includes(field.type) && (
|
|
141
162
|
<input
|
|
142
163
|
type={field.type}
|
|
143
164
|
name={field.name}
|
|
144
165
|
value={formData[field.name]}
|
|
145
166
|
className={field.className}
|
|
146
|
-
style={
|
|
167
|
+
style={{
|
|
168
|
+
...{
|
|
169
|
+
padding: "0.6rem 1rem",
|
|
170
|
+
border: "1px solid #ccc",
|
|
171
|
+
borderRadius: "8px",
|
|
172
|
+
width: "100%",
|
|
173
|
+
fontSize: "1rem",
|
|
174
|
+
},
|
|
175
|
+
...field.style,
|
|
176
|
+
}}
|
|
147
177
|
onChange={(e) =>
|
|
148
178
|
handleChange(field.name, e.target.value, field)
|
|
149
179
|
}
|
|
@@ -152,7 +182,7 @@ export const DynamicFields: React.FC<DynamicFieldsProps> = ({
|
|
|
152
182
|
|
|
153
183
|
{/* Error Display */}
|
|
154
184
|
{errors[field.name] && (
|
|
155
|
-
<p style={{ color: "red", marginTop: "0.
|
|
185
|
+
<p style={{ color: "red", marginTop: "0.4rem", fontSize: "0.9rem" }}>
|
|
156
186
|
{errors[field.name]}
|
|
157
187
|
</p>
|
|
158
188
|
)}
|