-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathcustom_value_renderer.html
123 lines (107 loc) · 3.19 KB
/
custom_value_renderer.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JSONEditor | Custom Value Renderer</title>
<style>
body {
font-family: sans-serif;
line-height: 1.5em;
max-width: 800px;
}
code {
background: #f5f5f5;
padding: 2px;
}
#jsoneditor {
width: 100%;
height: 500px;
}
.custom-evaluator-component {
background: greenyellow;
}
</style>
</head>
<body>
<h1>Custom value renderer (password, enum, action)</h1>
<p>Provide a custom <code>onRenderValue</code> method, which demonstrates three things:</p>
<ol>
<li>
It hides the value of all fields with the name "password" using a custom component (action)
<code>ReadonlyPasswordAction</code>.
</li>
<li>
It creates an enum component for the fields with name "gender" using a component
<code>EnumValue</code> provided by <code>svelte-jsoneditor</code>.
</li>
<li>
The creates a custom component for the field named "evaluate" using a custom component
(action) <code>EvaluatorAction</code>, which evaluates the value as an expression containing
an addition of two or more values. This solution can be used when using svelte-jsoneditor in
a Vanilla JS environment.
</li>
</ol>
<div id="jsoneditor"></div>
<script type="module">
import { createJSONEditor, renderValue, EnumValue } from '../../package-vanilla/standalone.js'
import { ReadonlyPasswordAction } from './components/ReadonlyPasswordAction.js'
import { EvaluatorAction } from './components/EvaluatorAction.js'
const genderOptions = [
{ value: null, text: '-' },
{ value: 'male', text: 'Male' },
{ value: 'female', text: 'Female' },
{ value: 'other', text: 'Other' }
]
const content = {
text: undefined, // can be used to pass a stringified JSON document instead
json: {
username: 'John',
password: 'secret...',
gender: 'male',
evaluate: '2 + 3'
}
}
// define a custom rendering function for values
function onRenderValue(props) {
const key = props.path[props.path.length - 1]
if (key === 'password' && !props.isEditing) {
return [
{
action: ReadonlyPasswordAction,
props
}
]
}
if (key === 'gender') {
return [
{
component: EnumValue,
props: {
...props,
options: genderOptions
}
}
]
}
if (key === 'evaluate' && !props.isEditing) {
return [
{
action: EvaluatorAction,
props
}
]
}
// fallback on the default render components
return renderValue(props)
}
// create the editor
const editor = createJSONEditor({
target: document.getElementById('jsoneditor'),
props: {
content,
onRenderValue
}
})
</script>
</body>
</html>