This repository was archived by the owner on Mar 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-admin.jinja
More file actions
199 lines (186 loc) · 8.21 KB
/
react-admin.jinja
File metadata and controls
199 lines (186 loc) · 8.21 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{%- import "macros.jinja" as macros -%}
{%- macro get_input_type(name, property, required=false) -%}
{%- filter trim -%}
{%- set map = [
{"type": "string", "format": "uuid", "result": "TextInput"},
{"type": "string", "format": "date-time", "result": "DateTimeInput"},
{"type": "string", "format": "date", "result": "DateInput"},
{"type": "string", "format": "time", "result": "DateInput"},
{"type": "string", "result": "TextInput"},
{"type": "string", "enum": property.enum, "result": "SelectInput"},
{"type": "number", "enum": property.enum, "result": "SelectInput"},
{"type": "integer", "enum": property.enum, "result": "SelectInput"},
{"type": "boolean", "result": "BooleanInput"},
{"type": "integer", "min": 0, "max": 255, "result": "NumberInput"},
{"type": "integer", "min": 0, "max": 65535, "result": "NumberInput"},
{"type": "integer", "min": 0, "max": 4294967295, "result": "NumberInput"},
{"type": "integer", "min": 0, "max": None, "result": "NumberInput"},
{"type": "integer", "min": None, "max": 127, "result": "NumberInput"},
{"type": "integer", "min": None, "max": 32767, "result": "NumberInput"},
{"type": "integer", "min": None, "max": 2147483647, "result": "NumberInput"},
{"type": "integer", "min": None, "max": None, "result": "NumberInput"},
{"type": "number", "min": -3.40282347, "max": 3.40282347, "result": "NumberInput"},
{"type": "number", "min": None, "max": None, "result": "NumberInput"},
{"type": "object", "x-relationship": "many-to-one", "result": "ReferenceInput"},
{"type": "array", "x-relationship": "one-to-many", "result": "ReferenceArrayInput"},
{"type": "array","x-relationship": "many-to-many", "result": "ReferenceArrayInput"}
] -%}
{%- set type = map
| selectattr('type', 'equalto', property.type)
| selectattr('format', 'equalto', property.format)
| selectattr('min', 'equalto', property.min)
| selectattr('enum', 'equalto', property.enum)
| selectattr('max', 'equalto', property.max)
| selectattr('x-relationship', 'equalto', property['x-relationship'])
| map(attribute='result')
| first
| default('TextInput')
-%}
{{ type }}
{%- endfilter -%}
{%- endmacro -%}
{%- macro get_choices(object) -%}
{%- filter trim -%}
{%- if 'enum' in object -%}
choices={[
{%- for enum in object.enum -%}{ id: '{{ enum }}', name: '{{ enum }}' }{{ ',' if not loop.last }}{%- endfor -%}]}
{% endif -%}
{%- endfilter -%}
{%- endmacro -%}
{#- Macro to get the input element for a property -#}
{%- macro get_input_element(name, property) -%}
{%- filter trim -%}
<{{ get_input_type(name, property, name in property.required) }} {{ 'readOnly' if 'readOnly' in property }} source="{{ name | camel_case }}"
{%- set is_relation = macros.relation_is_many_to_many(property)=='true' or macros.relation_is_many_to_one(property=property)=='true' -%}
{%- if is_relation -%}
{%- set relation = macros.get_relation(property) -%}
{{' '}}reference="{{ relation | plural | kebab_case }}" label="{{ relation | pascal_case }}" defaultValue={[]} {{- ' validate={[required()]}' if 'required' in entity and name in entity.required }}/>
{% else -%}
{{- ' validate={[required()]}' if 'required' in entity and name in entity.required }} {{ get_choices(entity) }} {{ get_choices(property) }}/>
{% endif -%}
{%- endfilter -%}
{%- endmacro -%}
{%- macro get_field(property) -%}
{% filter trim %}
{% if property.type and property.type == "string" -%}
{% if property.format and property.format == "uuid" -%}
TextField
{% elif property.format and property.format == "date-time" -%}
DateField
{% elif property.format and property.format == "date" -%}
DateField
{% elif property.format and property.format == "time" -%}
TextField
{% elif property.format and (property.format == "uri" or property.format == "url") -%}
UrlField
{% else -%}
TextField
{% endif -%}
{% elif property.type and property.type == "boolean" -%}
TextField
{% elif property.type and property.type == "integer" -%}
NumberField
{% elif property.type and property.type == "number" -%}
NumberField
{% elif property.enum %}
TextField
{% elif macros.relation_is_one_to_many(property)=='true' or macros.relation_is_many_to_many(property)=='true' -%}
{% set relation = macros.get_relation(property) | plural | snake_case -%}
TextField
{% elif macros.relation_is_many_to_one(property)=='true' -%}
{% set relation = macros.get_relation(property) -%}
ReferenceField reference="{{ relation | plural | kebab_case }}" label="{{ relation | pascal_case }}"
{% elif macros.relation_is_many_to_many(property)=='true' -%}
{% set relation = macros.get_relation(property) -%}
ReferenceArrayField reference="{{ relation | plural | kebab_case }}" label="{{ relation | pascal_case }}"
{% else -%}
TextField
{% endif -%}
{% endfilter %}
{%- endmacro -%}
{%- macro is_read_only(property) -%}
{% if property and property.readOnly -%}
readOnly
{% endif -%}
{%- endmacro -%}
{%- macro get_input_field(property) -%}
{% filter trim %}
{% if macros.relation_is_many_to_one(property)=='true' or macros.relation_is_one_to_one(property)=='true' -%}
ReferenceInput
{% elif macros.relation_is_many_to_many(property)=='true' or macros.relation_is_one_to_many(property)=='true' -%}
ReferenceArrayInput
{% elif 'type' in property and property.type == "string" -%}
{% if property.format and property.format == "uuid" -%}
TextInput
{% elif property.format and property.format == "date-time" -%}
DateTimeInput
{% elif property.format and property.format == "date" -%}
DateInput
{% elif property.format and property.format == "time" -%}
TextInput
{% else -%}
TextInput
{% endif -%}
{% elif 'type' in property and property.type == "boolean" -%}
BooleanInput
{% elif 'type' in property and property.type == "integer" -%}
NumberInput
{% elif 'type' in property and property.type == "number" -%}
NumberInput
{% elif 'enum' in property %}
SelectInput
{% else -%}
TextInput
{% endif -%}
{% endfilter %}
{%- endmacro -%}
{%- macro get_reference(property) -%}
{% filter trim %}
{% if macros.is_relation(property) == 'true'-%}
reference="{{ macros.get_relation(property) | plural | snake_case }}"
{% endif -%}
{% endfilter %}
{%- endmacro -%}
{%- macro get_all_properties_by_name(entity) -%}
{%- set properties = [] -%}
{% for name,property in entity.properties | items -%}
{% if macros.relation_is_one_to_many(property)=='true' or macros.relation_is_many_to_many(property)=='true' -%}
{% continue -%}
{% endif -%}
{% if macros.relation_is_many_to_one(property)=='true' -%}
{% set relation = macros.get_relation(property) | camel_case | trim -%}
{% set name = relation ~ " { id }" -%}
{% endif -%}
{%- set properties = properties | concat(name) -%}
{% endfor -%}
{{ properties | join(" ") }}
{%- endmacro -%}
{% macro get_all_properties_by_name(entity) %}
{% set properties = [] %}
{% for name, property in entity.properties | items %}
{% if macros.relation_is_one_to_many(property)=='true' or macros.relation_is_many_to_many(property)=='true' %}
{% continue %}
{% endif %}
{% if macros.relation_is_many_to_one(property)=='true' %}
{% set relation = get_relation(property) | camel_case | trim %}
{% set name = relation ~ " { id }" %}
{% endif %}
{% set properties = properties + [name] %}
{% endfor %}
{{ properties | join(' ') }}
{% endmacro %}
{%- macro source(name,property) -%}
{% filter trim %}
{{name|camel_case}}
{%- if macros.relation_is_many_to_one(property)=='true' -%}
.id
{%- endif -%}
{%- endfilter -%}
{%- endmacro -%}
{%- macro validation(entity,name,property) -%}
{%- filter trim -%}
{%- if 'required' in entity and 'name' in entity.required -%}
validate={[required()]}
{%- endif -%}
{%- endfilter -%}
{%- endmacro -%}