-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKeyValue.js
More file actions
42 lines (40 loc) · 1.14 KB
/
KeyValue.js
File metadata and controls
42 lines (40 loc) · 1.14 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
import React from 'react'
const KeyValue = React.memo(({ data, sort, nowrap, serializer }) => {
const keys = Object.keys(data)
if (sort) {
keys.sort()
}
const wrapClass = nowrap ? 'kv-nowrap' : 'kv-wrap'
return (
<dl className="kv">
{keys.map(key => {
const cssKey = key.replace(/ /g, ' ')
const value = data[key]
const type = typeof value
const valueStr = serializer != null ? serializer(value) : '' + value
const title = `${key}: ${valueStr}`
return (
<React.Fragment key={key}>
<dt
className={`kv-${cssKey}-key kv-type-${type}-key ${wrapClass}`}
title={title}
data-key={key}
data-value={valueStr}
>
<span>{key}</span>
</dt>
<dd
className={`kv-${cssKey}-value kv-type-${type}-value ${wrapClass}`}
title={title}
data-key={key}
data-value={valueStr}
>
<span>{valueStr}</span>
</dd>
</React.Fragment>
)
})}
</dl>
)
})
export default KeyValue