forked from canpolatoral/boa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.js
213 lines (191 loc) · 6.58 KB
/
preview.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React from 'react';
import { action } from '@storybook/addon-actions';
import reactElementToJSXString from 'react-element-to-jsx-string';
import { ComponentBase } from '@kuveytturk/boa-base';
import { DocViewer } from '@kuveytturk/boa-components/DocViewer';
import { DocCode } from '@kuveytturk/boa-components/DocCode';
import PropsPanel from './props-panel';
import * as Utils from './utils';
// import BaseProps from './doc.json';
export default class Preview extends ComponentBase {
constructor(props, context) {
super(props, context);
this.componentDefaultProps = {};
this.onPropertyChanged = this.onPropertyChanged.bind(this);
this.componentRef = React.createRef();
}
state = {
availableProperties: [],
currentProperties: {},
selectedTheme: 'violet',
selectedLanguage: this.props.context.language,
};
componentWillReceiveProps(nextProps) {
if (nextProps.defaultProps !== this.props.defaultProps) {
this.setState(
prevState => {
const props = Object.assign(prevState.currentProperties, nextProps.defaultProps);
return { currentProperties: props };
},
() => {
const code = this.getComponentString();
this.setState({ code });
},
);
}
}
componentWillMount() {
this.prepareData();
}
prepareData() {
const self = this;
const propMetaData = self.props.doc.props;
const composeMetaData = self.props.doc.composeProps;
const availableProperties = [];
const availableComposedProperties = [];
const currentProperties = {};
const createPropMeta = (metaData, available, current) => {
Object.keys(metaData)
.sort()
.forEach(key => {
const prop = metaData[key];
if (prop.description && prop.description.includes('@ignore')) return;
const property = {
name: key,
type: Utils.getPropType(prop),
value: Utils.getPropValue(prop),
values: Utils.getAvailableValues(prop),
default: Utils.getDefaultValue(prop),
};
available.push(property);
const defaultValue = Utils.getDefaultValue(prop);
if (defaultValue) {
current[key] = defaultValue;
self.componentDefaultProps[key] = defaultValue;
}
});
if (available && available.length > 0) {
available.sort((a, b) => {
if (a.type < b.type) return -1;
if (a.type > b.type) return 1;
if (a.name > b.name) return 1;
if (a.name < b.name) return -1;
return 0;
});
}
};
if (composeMetaData) {
Object.keys(composeMetaData).forEach(composeName => {
createPropMeta(
composeMetaData[composeName],
availableComposedProperties,
currentProperties,
);
});
}
createPropMeta(propMetaData, availableProperties, currentProperties);
Object.assign(currentProperties, this.props.defaultProps);
this.setState({ availableProperties, availableComposedProperties, currentProperties }, () => {
const code = self.getComponentString();
this.setState({ code });
});
}
getName() {
return this.props.doc.displayName;
}
propertyChanged(property, val) {
let value = val;
/*
* If component propType is oneOf([..]) and values are not string
* The NativeSelect component (on the props-panel.js) returns selected value as string,
* So we are find the correct value from doc.json
*/
if (typeof value === 'string') {
const values = Utils.getAvailableValues(this.props.doc.props[property]);
if (values && values.length > 0) {
value = values.find(x => x.toString() === val);
}
}
this.setState(
prevState => {
const newCurrentProperties = Object.assign({}, prevState.currentProperties);
newCurrentProperties[property] = value;
return { currentProperties: newCurrentProperties };
},
() => {
const code = this.getComponentString();
this.setState({ code });
},
);
}
onPropertyChanged(property, value) {
delete this.componentDefaultProps[property];
this.propertyChanged(property, value);
}
render() {
const self = this;
const RenderedComponent = this.props.component;
const { availableProperties, availableComposedProperties, currentProperties } = this.state;
const changedProps = {};
if (currentProperties) {
const keys = Object.keys(currentProperties);
if (keys.length > 0) {
keys.forEach((key) => {
if (!self.componentDefaultProps[key]) {
changedProps[key] = currentProperties[key];
}
});
}
}
if (!availableProperties || availableProperties.length === 0) {
return null;
}
return (
<div>
<div style={{ display: 'flex', flexDirection: 'row' }}>
<div style={{ width: '100%' }}>
<DocViewer content={'## Props'} editorType="github" />
<PropsPanel
availableProperties={availableProperties}
availableComposedProperties={availableComposedProperties}
currentProperties={currentProperties}
onPropertyChanged={this.onPropertyChanged}
{...this.props}
/>
</div>
<div style={{ marginLeft: 100, width: '100%' }}>
<DocViewer content="## Preview" editorType="github" />
{this.props.sample}
{!this.props.sample && (
<RenderedComponent
{...currentProperties}
{...changedProps}
ref={this.componentRef}
onChange={action(`${self.getName()}-onChange`)}
onClick={action(`${self.getName()}-onClick`)}
context={this.props.context}
/>
)}
</div>
</div>
{this.state.code && (
<DocCode content={this.state.code} highlight lang={'js'} editorType="github" />
)}
</div>
);
}
getComponentString() {
const RenderedComponent = this.props.component;
const RenderedComponentString = reactElementToJSXString(
<RenderedComponent {...this.state.currentProperties} />,
{
displayName: this.getName.bind(this),
filterProps: ['context', 'doc', 'componentSize', 'newLine', ...[this.props.ignoreProps]],
},
);
const strings = RenderedComponentString.split('\n');
strings.splice(1, 0, ' context={context}');
return `import ${this.getName()} from '@kuveytturk/boa-components/${this.getName()}';
${strings.join('\n')}`;
}
}