|
| 1 | +// @flow |
| 2 | + |
| 3 | +/* eslint-disable no-underscore-dangle */ |
| 4 | + |
| 5 | +/** @jsx jsx */ |
| 6 | +import { jsx, css } from '@emotion/core'; |
| 7 | +import React, { Component, type ComponentType } from 'react'; |
| 8 | + |
| 9 | +import type { Components } from '../components'; |
| 10 | +import type { CommonProps } from '../types'; |
| 11 | +import PropsWrapper from '../Props/Wrapper'; |
| 12 | +import getPropTypes from '../getPropTypes'; |
| 13 | +import renderPropType from '../PropType'; |
| 14 | +import PropEntry from './PropEntry'; |
| 15 | + |
| 16 | +type Obj = { |
| 17 | + kind: 'object', |
| 18 | + members: Array<any> |
| 19 | +}; |
| 20 | + |
| 21 | +type Gen = { |
| 22 | + kind: 'generic', |
| 23 | + value: any |
| 24 | +}; |
| 25 | + |
| 26 | +type Inter = { |
| 27 | + kind: 'intersection', |
| 28 | + types: Array<Obj | Gen> |
| 29 | +}; |
| 30 | + |
| 31 | +type DynamicPropsProps = { |
| 32 | + components?: Components, |
| 33 | + heading?: string, |
| 34 | + shouldCollapseProps?: boolean, |
| 35 | + overrides?: { |
| 36 | + [string]: ComponentType<CommonProps> |
| 37 | + }, |
| 38 | + props?: { |
| 39 | + component?: Obj | Inter |
| 40 | + }, |
| 41 | + component?: ComponentType<any> |
| 42 | +}; |
| 43 | + |
| 44 | +const getProps = props => { |
| 45 | + if (props && props.component) { |
| 46 | + return getPropTypes(props.component); |
| 47 | + } |
| 48 | + return null; |
| 49 | +}; |
| 50 | + |
| 51 | +export default class HybridLayout extends Component<DynamicPropsProps> { |
| 52 | + render() { |
| 53 | + let { props, heading, component, components, ...rest } = this.props; |
| 54 | + if (component) { |
| 55 | + /* $FlowFixMe the component prop is typed as a component because |
| 56 | + that's what people pass to Props and the ___types property shouldn't |
| 57 | + exist in the components types so we're just going to ignore this error */ |
| 58 | + if (component.___types) { |
| 59 | + props = { type: 'program', component: component.___types }; |
| 60 | + } else { |
| 61 | + /* eslint-disable-next-line no-console */ |
| 62 | + console.error( |
| 63 | + 'A component was passed to <Props> but it does not have types attached.\n' + |
| 64 | + 'babel-plugin-extract-react-types may not be correctly installed.\n' + |
| 65 | + '<Props> will fallback to the props prop to display types.' |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!components || !components.Description) { |
| 71 | + components = components || {}; |
| 72 | + components.Description = ({ children }) => ( |
| 73 | + <div |
| 74 | + css={css` |
| 75 | + p:first-of-type { |
| 76 | + margin-top: 0px; |
| 77 | + } |
| 78 | + p:last-of-type { |
| 79 | + margin-bottom: 0px; |
| 80 | + } |
| 81 | + `} |
| 82 | + > |
| 83 | + {children} |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + let propTypes = getProps(props); |
| 89 | + if (!propTypes) return null; |
| 90 | + |
| 91 | + return ( |
| 92 | + <PropsWrapper heading={heading}> |
| 93 | + {propTypes.map(propType => renderPropType(propType, { ...rest, components }, PropEntry))} |
| 94 | + </PropsWrapper> |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments