-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
91 lines (80 loc) · 2.19 KB
/
page.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
85
86
87
88
89
90
91
import React from 'react'
import Playground from '../common/playground'
import Title from '../common/title'
import Description from '../common/description'
import PropTable from '../common/prop-table'
import Examples from '../common/examples'
import getDefaultProps from '../common/default-props'
/*
prettify that code!
*/
const format = (code, propList) => {
const propString = getPropString(propList)
code = code.replace('{props}', propString)
let printWidth = 50 // default
const editor = document.getElementById('editor')
// printWidth is width of the editor / width of each charachter
if (editor) printWidth = editor.offsetWidth / 10
code = window.prettyFormat(code, { printWidth })
code += '\n'
return code
}
const getPropString = propList => {
return (
propList
// remove props with null values
.filter(prop => prop.value)
// get values
.map(prop => prop.value)
// make one string that can be passed to the component
.join(' ')
)
}
class Page extends React.Component {
constructor(props) {
super(props)
// clone the propList to a new variable
const propList = props.docs.propList.slice(0)
// get props with default:true
const defaultProps = getDefaultProps(propList)
this.state = {
code: format(this.props.docs.template, defaultProps)
}
}
onPropsChanged(props) {
// convert object to array
const propList = Object.entries(props).map(([key, value]) => ({
name: key,
value
}))
// prettify code
let code = format(this.props.docs.template, propList)
this.setState({ code })
}
render() {
return (
<div>
<Title>
{this.props.docs.name}
</Title>
<Description>
{this.props.docs.description}
</Description>
<Playground
main
code={this.state.code}
components={this.props.components}
/>
<PropTable
list={this.props.docs.propList}
onPropsChanged={this.onPropsChanged.bind(this)}
/>
<Examples
examples={this.props.docs.examples}
components={this.props.components}
/>
</div>
)
}
}
export default Page