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')(
| Name | Type | Default Value | Description |
|---|---|---|---|
| {prop.name || '-'} | {renderPropType(prop.type, prop.tags)} |
{prop.defaultValue && prop.defaultValue.value && (
|
{prop.description && |