-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (155 loc) · 3.7 KB
/
index.js
File metadata and controls
163 lines (155 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { useEffect, useState } from 'react';
import { TextField, FormControl, Autocomplete, Box } from '@mui/material';
import { useUserData } from '@nhost/nextjs';
import * as _ from 'lodash';
const FormBuilder = ({ fields, data, onChange, disabled }) => {
const userData = useUserData();
const [formData, setFormData] = useState({});
useEffect(() => {
setFormData({ ...data });
}, [data]);
const onDataChanged = (key, value) => {
formData[key] = value;
setFormData({ ...formData });
if (onChange) {
onChange(formData);
}
};
const getSelectedItem = (options, value) => {
const item = options.find((opt) => {
if (opt.id === value) return opt;
});
return item || '';
};
const renderField = (field) => {
switch (field.type) {
case 'input':
return (
<TextField
id={field.id}
label={field.plaeholder}
value={formData[field.datafield]}
size='small'
className='w-full'
disabled={disabled}
{...field}
onChange={(e) => {
onDataChanged(field.datafield, e.target.value);
}}
/>
);
case 'select':
return (
<Autocomplete
id={field.datafield}
size='small'
options={field.options || []}
getOptionLabel={(option) =>
option.name || option[field.fieldName] || option
}
value={getSelectedItem(
field.options,
formData[field.datafield]
)}
disabled={disabled}
filterSelectedOptions
multiple={field.multiple}
freeSolo={field.freeSolo}
onChange={(e, newValue) => {
let updatedval = newValue;
onDataChanged(field.datafield, updatedval.id);
}}
renderInput={(params) => (
<TextField
{...params}
size='small'
placeholder={field.placeholder}
/>
)}
/>
);
case 'userlist':
return (
<div>
<Autocomplete
id={field.datafield}
size='small'
options={
field.options
? field.options.filter(
(u) => u.id !== userData.id
)
: []
}
disabled={disabled}
getOptionLabel={(option) =>
option.name || option[field.fieldName] || option
}
filterSelectedOptions
value={getSelectedItem(
field.options,
formData[field.datafield]
)}
multiple={field.multiple}
freeSolo={field.freeSolo}
renderOption={(props, option) => (
<Box
component='li'
sx={{ '& > img': { mr: 2, flexShrink: 0 } }}
{...props}>
<img
loading='lazy'
width='40'
src={option.avatarUrl}
alt=''
className='rounded-full'
/>
{option.displayName}
</Box>
)}
onChange={(e, newValue) => {
let updatedval = newValue;
onDataChanged(field.datafield, updatedval?.id);
}}
renderInput={(params) => (
<div>
<TextField
{...params}
size='small'
placeholder={field.placeholder}
/>
</div>
)}
/>
<div className='text-slate-400 text-xs '>
You are already a member, select another person
[optional] as your partner
</div>
</div>
);
default:
return <></>;
}
};
const renderForm = () => {
return (
<>
<FormControl className='w-full'>
{fields.map((field, field_key) => {
return (
<div className='flex p-2 flex-col' key={field_key}>
<div>
{field.display}
{field.required ? '*' : ''}
</div>
<div>{renderField(field)}</div>
</div>
);
})}
</FormControl>{' '}
</>
);
};
return <>{renderForm()}</>;
};
export default FormBuilder;