forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameForm.tsx.template
124 lines (115 loc) · 4.13 KB
/
NameForm.tsx.template
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
import {
Form,
FormError,
FieldError,
Label,
${fieldsToImport.join(',\n ')},
Submit,
} from '@redwoodjs/forms'
import type { Edit${singularPascalName}ById, Update${singularPascalName}Input } from 'types/graphql'
import type { RWGqlError } from '@redwoodjs/forms'
<% if (fieldsToImport.includes('DatetimeLocalField')) { %>
const formatDatetime = (value) => {
if (value) {
return value.replace(/:\d{2}\.\d{3}\w/, '')
}
}
<% } %>
type Form${singularPascalName} = NonNullable<Edit${singularPascalName}ById['${singularCamelName}']>
interface ${singularPascalName}FormProps {
${singularCamelName}?: Edit${singularPascalName}ById['${singularCamelName}']
onSave: (data: Update${singularPascalName}Input, id?: Form${singularPascalName}['id']) => void
error: RWGqlError
loading: boolean
}
const ${singularPascalName}Form = (props: ${singularPascalName}FormProps) => {
const onSubmit = (data: Form${singularPascalName}) => {
<% editableColumns.forEach(column => { %>
<% if (column.isEnum && !column.isList && !column.isRequired) { %>
if (data.${column.name} === '') {
data.${column.name} = null
}
<% } %>
<% if (column.isEnum && column.isList) { %>
if (data.${column.name}) {
data.${column.name} = data.${column.name}.filter((value) => !!value)
}
<% } %>
<% }) %>
props.onSave(data, props?.${singularCamelName}?.id)
}
return (
<div className="rw-form-wrapper">
<Form<Form${singularPascalName}> onSubmit={onSubmit} error={props.error}>
<FormError
error={props.error}
wrapperClassName="rw-form-error-wrapper"
titleClassName="rw-form-error-title"
listClassName="rw-form-error-list"
/>
<% editableColumns.forEach(column => { %>
<Label
name="${column.name}"
className="rw-label"
errorClassName="rw-label rw-label-error"
>
${column.label}
</Label>
<% if (column.isEnum) { %>
<% if (!column.isRequired) { %>
<div className="rw-check-radio-items">
<${column.component}
id="${singularCamelName}-${column.name}-none"
name="${column.name}"
defaultValue=""
${column.defaultProp}={!props.spot?.spotType}
className="rw-input"
errorClassName="rw-input rw-input-error"
/>
<div className="rw-check-radio-item-none">
None
</div>
</div>
<% } %>
<% column.values?.forEach((value, i) => {
const columnComponentName = column.isList ? `${column.name}[${i}]` : column.name
%>
<div className="rw-check-radio-items">
<${column.component}
id="${singularCamelName}-${column.name}-${i}"
name="${columnComponentName}"
defaultValue="${value.name}"
${column.defaultProp}={props.${singularCamelName}?.${column.name}?.includes('${value.name}')}
className="rw-input"
errorClassName="rw-input rw-input-error"
/>
<div>
${value.name.replace('_', ' ').toLowerCase().replace(/\b\w/g, l => l.toUpperCase())}
</div>
</div>
<% }) %>
<% } else { %>
<${column.component}
name="${column.name}"
${column.defaultProp}={${column.deserializeFunction && column.deserializeFunction + '('}props.${singularCamelName}?.<%= column.name %>${column.deserializeFunction && ')'}}
className="rw-input"
errorClassName="rw-input rw-input-error"
<%= !column.validation ? '' : `validation=${column.validation}` %>
<%= column.isRelationalField && !column.isRequired ? `emptyAs={'undefined'}` : '' %>
/>
<% } %>
<FieldError name="${column.name}" className="rw-field-error" />
<% }) %>
<div className="rw-button-group">
<Submit
disabled={props.loading}
className="rw-button rw-button-blue"
>
Save
</Submit>
</div>
</Form>
</div>
)
}
export default ${singularPascalName}Form