|
| 1 | + |
| 2 | +import React from 'react'; |
| 3 | +import transform from 'lodash/transform'; |
| 4 | + |
| 5 | + |
| 6 | +function displayObj(obj){ |
| 7 | + return JSON.stringify(obj, null, 2).replace(/"|'/g, '') |
| 8 | +} |
| 9 | + |
| 10 | +let capitalize = str => str[0].toUpperCase() + str.substr(1); |
| 11 | +let cleanDocletValue = str => str.trim().replace(/^\{/, '').replace(/\}$/, ''); |
| 12 | + |
| 13 | +const extractMarkdown = ({ description }) => ( |
| 14 | + description && |
| 15 | + description.childMarkdownRemark && |
| 16 | + description.childMarkdownRemark.html |
| 17 | +); |
| 18 | + |
| 19 | +class ComponentPage extends React.Component { |
| 20 | + render() { |
| 21 | + const { metadata, ...props } = this.props; |
| 22 | + |
| 23 | + return ( |
| 24 | + <div {...this.props}> |
| 25 | + <h2 id={metadata.displayName}><a href={`#${metadata.displayName}`}> |
| 26 | + {metadata.displayName}</a> |
| 27 | + </h2> |
| 28 | + <p dangerouslySetInnerHTML={{ __html: extractMarkdown(metadata) }} /> |
| 29 | + |
| 30 | + <h3>Props</h3> |
| 31 | + {metadata.props.map(p => |
| 32 | + this.renderProp(p, metadata.displayName |
| 33 | + ))} |
| 34 | + </div> |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + renderProp = (prop, componentName) => { |
| 39 | + const { defaultValue, name, required } = prop |
| 40 | + let typeInfo = this.renderType(prop); |
| 41 | + let id = `${componentName}-prop-${name}`; |
| 42 | + |
| 43 | + return ( |
| 44 | + <section key={name}> |
| 45 | + <h4 id={id}> |
| 46 | + <a href={`#${id}`}><code>{name}</code></a> |
| 47 | + {required && ( |
| 48 | + <strong>{' required'}</strong> |
| 49 | + )} |
| 50 | + </h4> |
| 51 | + <div dangerouslySetInnerHTML={{ __html: extractMarkdown(prop) }} /> |
| 52 | + |
| 53 | + <div style={{ paddingLeft: 0 }}> |
| 54 | + <div> |
| 55 | + {'type: '} |
| 56 | + { typeInfo && typeInfo.type === 'pre' ? typeInfo : <code>{typeInfo}</code> } |
| 57 | + </div> |
| 58 | + {defaultValue && |
| 59 | + <div>default: <code>{defaultValue.value.trim()}</code></div> |
| 60 | + } |
| 61 | + </div> |
| 62 | + </section> |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + renderType(prop) { |
| 67 | + let type = prop.type || {}; |
| 68 | + let name = getDisplayTypeName(type.name); |
| 69 | + let doclets = prop.doclets || {}; |
| 70 | + |
| 71 | + switch (name) { |
| 72 | + case 'node': |
| 73 | + return 'any'; |
| 74 | + case 'function': |
| 75 | + return 'Function'; |
| 76 | + case 'elementType': |
| 77 | + return 'ReactClass<any>'; |
| 78 | + case 'dateFormat': |
| 79 | + return 'string | (date: Date, culture: ?string, localizer: Localizer) => string'; |
| 80 | + case 'dateRangeFormat': |
| 81 | + return '(range: { start: Date, end: Date }, culture: ?string, localizer: Localizer) => string'; |
| 82 | + case 'object': |
| 83 | + case 'Object': |
| 84 | + if (type.value) |
| 85 | + return ( |
| 86 | + <pre className='shape-prop'> |
| 87 | + {displayObj(renderObject(type.value))} |
| 88 | + </pre> |
| 89 | + ) |
| 90 | + |
| 91 | + return name; |
| 92 | + case 'union': |
| 93 | + return type.value.reduce((current, val, i, list) => { |
| 94 | + val = typeof val === 'string' ? { name: val } : val; |
| 95 | + let item = this.renderType({ type: val }); |
| 96 | + |
| 97 | + if (React.isValidElement(item)) { |
| 98 | + item = React.cloneElement(item, {key: i}); |
| 99 | + } |
| 100 | + |
| 101 | + current = current.concat(item); |
| 102 | + |
| 103 | + return i === (list.length - 1) ? current : current.concat(' | '); |
| 104 | + }, []); |
| 105 | + case 'array': |
| 106 | + case 'Array': { |
| 107 | + let child = this.renderType({ type: type.value }); |
| 108 | + |
| 109 | + return <span>{'Array<'}{ child }{'>'}</span>; |
| 110 | + } |
| 111 | + case 'enum': |
| 112 | + return this.renderEnum(type); |
| 113 | + case 'custom': |
| 114 | + return cleanDocletValue(doclets.type || name); |
| 115 | + default: |
| 116 | + return name; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + renderEnum(enumType) { |
| 121 | + const enumValues = enumType.value || []; |
| 122 | + return <code>{enumValues.join(' | ')}</code>; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function getDisplayTypeName(typeName) { |
| 127 | + if (typeName === 'func') { |
| 128 | + return 'function'; |
| 129 | + } else if (typeName === 'bool') { |
| 130 | + return 'boolean'; |
| 131 | + } else if (typeName === 'object') { |
| 132 | + return 'Object'; |
| 133 | + } |
| 134 | + |
| 135 | + return typeName; |
| 136 | +} |
| 137 | + |
| 138 | +function renderObject(props){ |
| 139 | + return transform(props, (obj, val, key) => { |
| 140 | + obj[val.required ? key : key + '?'] = simpleType(val) |
| 141 | + |
| 142 | + }, {}) |
| 143 | +} |
| 144 | + |
| 145 | +function simpleType(prop) { |
| 146 | + let type = prop.type || {}; |
| 147 | + let name = getDisplayTypeName(type.name); |
| 148 | + let doclets = prop.doclets || {}; |
| 149 | + |
| 150 | + switch (name) { |
| 151 | + case 'node': |
| 152 | + return 'any'; |
| 153 | + case 'function': |
| 154 | + return 'Function'; |
| 155 | + case 'elementType': |
| 156 | + return 'ReactClass<any>'; |
| 157 | + case 'object': |
| 158 | + case 'Object': |
| 159 | + if (type.value) |
| 160 | + return renderObject(type.value) |
| 161 | + return name; |
| 162 | + case 'array': |
| 163 | + case 'Array': { |
| 164 | + let child = simpleType({ type: type.value }); |
| 165 | + |
| 166 | + return 'Array<' + child + '>'; |
| 167 | + } |
| 168 | + case 'custom': |
| 169 | + return cleanDocletValue(doclets.type || name); |
| 170 | + default: |
| 171 | + return name; |
| 172 | + } |
| 173 | +} |
| 174 | +export default ComponentPage; |
| 175 | + |
| 176 | +export const descFragment = graphql` |
| 177 | + fragment ComponentPage_desc on ComponentDescription { |
| 178 | + childMarkdownRemark { |
| 179 | + html |
| 180 | + } |
| 181 | + } |
| 182 | +`; |
| 183 | + |
| 184 | +export const propsFragment = graphql` |
| 185 | + fragment ComponentPage_prop on ComponentProp { |
| 186 | + name |
| 187 | + required |
| 188 | + type { |
| 189 | + name |
| 190 | + value |
| 191 | + raw |
| 192 | + } |
| 193 | + defaultValue { |
| 194 | + value |
| 195 | + computed |
| 196 | + } |
| 197 | + description { |
| 198 | + ...ComponentPage_desc |
| 199 | + } |
| 200 | + doclets { type } |
| 201 | + } |
| 202 | +`; |
| 203 | + |
| 204 | +export const query = graphql` |
| 205 | + fragment ComponentPage_metadata on ComponentMetadata { |
| 206 | + displayName |
| 207 | + composes |
| 208 | + description { |
| 209 | + ...ComponentPage_desc |
| 210 | + } |
| 211 | + props { |
| 212 | + ...ComponentPage_prop |
| 213 | + } |
| 214 | + } |
| 215 | +`; |
0 commit comments