import React from 'react'; import { Prop } from '../typings/prop'; import Markdown from '../Markdown'; import styles from './styles.scss'; const wrap = name => children => ( {name} [{children}] ); const failSafe = type => () => ( Unable to parse propType:
{JSON.stringify(type, null, 2)}
); const buildLinkToType = (tags?: Prop['tags']): string | undefined => { const { description } = tags?.find(({ title }) => title === 'linkTypeTo') || {}; if (description) { const baseUrl = window.parent.location.href; const isExternalLink = description.substring(0, 7) === 'http://' || description.substring(0, 8) === 'https://'; if (isExternalLink) { return description; } return ( baseUrl.substring(0, baseUrl.indexOf('/story/') + 7) + description.toLowerCase() ); } }; const PropTypeName: React.FunctionComponent<{ name: string; link?: string; }> = ({ name, link }) => { if (link) { return ( {name} ); } return {name}; }; const renderPropType: ( type: Prop['type'], tags?: Prop['tags'], ) => React.ReactNode = (type = {}, tags) => { const typeHandlers = { custom: () => wrap('custom')(''), enum: () => wrap('oneOf')( Array.isArray(type.value) ? type.value.map((v, i, allValues) => ( {(v as any).value} {allValues[i + 1] && ', '} )) : JSON.stringify(type.value, null, 2), ), union: () => wrap('oneOfType')( type.value.map((v, i, allValues) => ( {renderPropType(v as any)} {allValues[i + 1] && ', '} )), ), shape: () => (type as any).computed ? type.value : wrap('shape')( , ), arrayOf: () => wrap('arrayOf')(renderPropType(type.value as any)), }; const propTypeName = type.value ? (typeHandlers[type.name] || failSafe(type))() : type.name; const propTypeNameOverride = (tags || []).find( ({ title }) => title === 'setTypeName', )?.description; return ( ); }; const prepareProps = props => { const asList = Object.keys(props).map(key => ({ ...props[key], name: key, })); const lexical = (a, b) => a.name.localeCompare(b.name); const required = asList.filter(prop => prop.required).sort(lexical); const notRequired = asList.filter(prop => !prop.required).sort(lexical); // required props go first return required.concat(notRequired); }; export interface PropertiesTableProps { props: { [s: string]: Prop }; } export const PropsTable: React.FunctionComponent = ({ props, }) => ( {prepareProps(props).map(prop => ( ))}
Name Type Default Value Description
{prop.name || '-'} {renderPropType(prop.type, prop.tags)} {prop.defaultValue && prop.defaultValue.value && ( )} {prop.description && }
);