@@ -2,6 +2,7 @@ import React, { useRef, useState, useEffect, useCallback } from "react";
2
2
3
3
import api from "@mwdb-web/commons/api" ;
4
4
import { ConfirmationModal } from "@mwdb-web/commons/ui" ;
5
+ import RichAttributeRenderer from "./RichAttribute/RichAttributeRenderer" ;
5
6
6
7
import AceEditor from "react-ace" ;
7
8
@@ -13,8 +14,10 @@ import "ace-builds/src-noconflict/ext-searchbox";
13
14
export default function AttributesAddModal ( { isOpen, onAdd, onRequestClose } ) {
14
15
const [ attributeDefinitions , setAttributeDefinitions ] = useState ( { } ) ;
15
16
const [ attributeKey , setAttributeKey ] = useState ( "" ) ;
17
+ const [ richTemplate , setRichTemplate ] = useState ( "" ) ;
16
18
const [ attributeValue , setAttributeValue ] = useState ( "" ) ;
17
19
const [ attributeType , setAttributeType ] = useState ( "string" ) ;
20
+ const [ invalid , setInvalid ] = useState ( false ) ;
18
21
const [ error , setError ] = useState ( null ) ;
19
22
const attributeForm = useRef ( null ) ;
20
23
@@ -35,6 +38,17 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
35
38
36
39
function handleKeyChange ( ev ) {
37
40
setAttributeKey ( ev . target . value ) ;
41
+ if ( ! ev . target . value . length ) setRichTemplate ( "" ) ;
42
+ else {
43
+ setRichTemplate (
44
+ attributeDefinitions [ ev . target . value ] . rich_template
45
+ ) ;
46
+ setAttributeValue (
47
+ attributeDefinitions [ ev . target . value ] . example_value || ""
48
+ ) ;
49
+ }
50
+ setAttributeType ( "object" ) ;
51
+
38
52
setError ( null ) ;
39
53
}
40
54
@@ -81,7 +95,7 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
81
95
isOpen = { isOpen }
82
96
onRequestClose = { onRequestClose }
83
97
onConfirm = { handleSubmit }
84
- confirmDisabled = { ! attributesAvailable }
98
+ confirmDisabled = { ! attributesAvailable || ( invalid && richTemplate ) }
85
99
>
86
100
{ error ? (
87
101
< div
@@ -123,7 +137,7 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
123
137
</ select >
124
138
{ attributeDefinitions [ attributeKey ] &&
125
139
attributeDefinitions [ attributeKey ] . description ? (
126
- < div className = "form-hint " >
140
+ < div className = "form-group pt-2 " >
127
141
{ attributeDefinitions [ attributeKey ] . description }
128
142
</ div >
129
143
) : (
@@ -183,14 +197,39 @@ export default function AttributesAddModal({ isOpen, onAdd, onRequestClose }) {
183
197
wrapEnabled
184
198
onChange = { ( input ) => setAttributeValue ( input ) }
185
199
value = { attributeValue }
186
- width = "300px "
200
+ width = "500px "
187
201
height = "150px"
188
202
setOptions = { {
189
203
useWorker : false ,
190
204
} }
191
205
/>
192
206
) }
193
207
</ div >
208
+ { richTemplate ? (
209
+ < div className = "form-group" >
210
+ < label > Rich attribute preview</ label >
211
+ < table
212
+ className = "table table-striped table-bordered table-hover data-table"
213
+ style = { {
214
+ width : `500px` ,
215
+ } }
216
+ >
217
+ < tbody >
218
+ < RichAttributeRenderer
219
+ template = { richTemplate }
220
+ value = {
221
+ attributeType === "string"
222
+ ? JSON . stringify ( attributeValue )
223
+ : attributeValue
224
+ }
225
+ setInvalid = { setInvalid }
226
+ />
227
+ </ tbody >
228
+ </ table >
229
+ </ div >
230
+ ) : (
231
+ [ ]
232
+ ) }
194
233
</ form >
195
234
) }
196
235
</ ConfirmationModal >
0 commit comments