-
Notifications
You must be signed in to change notification settings - Fork 12
/
CustomAttributesFormatted.jsx
63 lines (58 loc) · 1.91 KB
/
CustomAttributesFormatted.jsx
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
import React from 'react';
import { map, isEmpty, isBoolean, isArray, isObject, find } from 'lodash';
const None = () => {
return <div style={{margin: '5px', fontWeight: '300'}}>None</div>
}
const CustomAttributesFormatted = ({attributes}) => {
const shouldBeNested = value => {
return isArray(value) && Boolean(find(value, isObject))
}
const getNestedValueDom = value => {
return isObject(value) ?
<details>
<summary>{`${JSON.stringify(value).slice(0, 50)}...`}</summary>
<p>
<pre style={{fontSize: '12px'}}>{JSON.stringify(value, undefined, 2)}</pre>
</p>
</details> :
<code>{JSON.stringify(value)}</code>
}
return (
<div className="col-md-12 no-side-padding">
{
isEmpty(attributes) ?
None() :
map(attributes, (value, name) => {
const isBool = isBoolean(value)
const needNesting = !isBool && shouldBeNested(value)
const isArr = isArray(value)
return (
<div className='col-md-12' style={{marginBottom: '5px'}} key={name}>
<div style={{fontWeight: '300'}} className='col-md-3 no-left-padding'>
{name}
</div>
<div className='col-md-9 no-right-padding ellipsis-text' style={{maxWidth: '100%'}}>
{
isBool && value.toString()
}
{
needNesting &&
map(value, val => getNestedValueDom(val))
}
{
isArr && !needNesting &&
<pre style={{margin: '0'}}>{JSON.stringify(value)}</pre>
}
{
!isBool && !needNesting && !isArr &&
value
}
</div>
</div>
)
})
}
</div>
)
}
export default CustomAttributesFormatted;