This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.js
84 lines (78 loc) · 2.4 KB
/
index.js
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
/** @jsx jsx */
import { useState } from 'react'
import { jsx } from 'theme-ui'
import { ChevronDown, ChevronUp } from '../Icons'
import * as styles from './styles'
export const getDefaultValue = ({ defaultValue, type, flowType }) => {
const propType = flowType ? flowType : type
if (!defaultValue || !defaultValue.value) return null
if (defaultValue.value === "''") {
return '[Empty string]'
}
if (propType && propType.name === 'string') {
return defaultValue.value.replace(/\'/g, '"')
}
if (typeof defaultValue.value === 'object' && defaultValue.value.toString) {
return defaultValue.value.toString()
}
return defaultValue.value
}
export const Prop = ({ propName, prop, getPropType, isToggle }) => {
const [showing, setShowing] = useState(isToggle || false)
if (!prop.type && !prop.flowType) return null
const toggle = () => setShowing(s => !s)
return (
<div sx={styles.line} data-testid="prop">
<div sx={styles.content}>
<div sx={styles.propName} data-testid="prop-name">
{propName}
</div>
<div sx={styles.propType} data-testid="prop-type">
{getPropType(prop)}
</div>
{prop.defaultValue && (
<div sx={styles.defaultValue} data-testid="prop-default-value">
<em>{getDefaultValue(prop)}</em>
</div>
)}
<div sx={styles.right}>
{prop.required && (
<div sx={styles.propRequired} data-testid="prop-required">
<strong>required</strong>
</div>
)}
{prop.description && (
<button
sx={styles.openDescBtn}
onClick={toggle}
data-testid="prop-toggle-description"
>
{showing ? <ChevronUp size={20} /> : <ChevronDown size={20} />}
</button>
)}
</div>
</div>
{showing && prop.description && (
<div sx={styles.description} data-testid="prop-description">
{prop.description}
</div>
)}
</div>
)
}
export const Props = ({ props, getPropType, isToggle }) => {
const entries = Object.entries(props)
return (
<div sx={styles.container} data-testid="props">
{entries.map(([key, prop]) => (
<Prop
key={key}
propName={key}
prop={prop}
getPropType={getPropType}
isToggle={isToggle}
/>
))}
</div>
)
}