import { css } from '@emotion/react';
import React from 'react';
import { InlineCode } from '~/components/base/code';
import { H4 } from '~/components/base/headings';
import { LI, UL } from '~/components/base/list';
import { P } from '~/components/base/paragraph';
import { H2, H3, H3Code } from '~/components/plugins/Headings';
import {
DefaultPropsDefinitionData,
PropData,
PropsDefinitionData,
TypeDefinitionData,
} from '~/components/plugins/api/APIDataTypes';
import {
CommentTextBlock,
getCommentOrSignatureComment,
getTagData,
renderTypeOrSignatureType,
resolveTypeName,
STYLES_SECONDARY,
} from '~/components/plugins/api/APISectionUtils';
export type APISectionPropsProps = {
data: PropsDefinitionData[];
defaultProps?: DefaultPropsDefinitionData;
header?: string;
};
const UNKNOWN_VALUE = '...';
const PROP_LIST_ELEMENT_STYLE = css`
padding: 0;
`;
const extractDefaultPropValue = (
{ comment, name }: PropData,
defaultProps?: DefaultPropsDefinitionData
): string | undefined => {
const annotationDefault = getTagData('default', comment);
if (annotationDefault) {
return annotationDefault.text;
}
return defaultProps?.type?.declaration?.children?.filter(
(defaultProp: PropData) => defaultProp.name === name
)[0]?.defaultValue;
};
const renderInheritedProp = (ip: TypeDefinitionData) => {
return (
{resolveTypeName(ip)}
);
};
const renderInheritedProps = (
data: TypeDefinitionData[] | undefined,
exposeInSidebar?: boolean
): JSX.Element | undefined => {
const inheritedProps = data?.filter((ip: TypeDefinitionData) => ip.type === 'reference') ?? [];
if (inheritedProps.length) {
return (
<>
{exposeInSidebar ? Inherited Props
: Inherited Props
}
{inheritedProps.map(renderInheritedProp)}
>
);
}
return undefined;
};
const renderProps = (
{ name, type }: PropsDefinitionData,
defaultValues?: DefaultPropsDefinitionData,
exposeInSidebar?: boolean
): JSX.Element => {
const baseTypes = type.types
? type.types?.filter((t: TypeDefinitionData) => t.declaration)
: [type];
const propsDeclarations = baseTypes
.map(def => def?.declaration?.children)
.flat()
.filter((dec, i, arr) => arr.findIndex(t => t?.name === dec?.name) === i);
return (
{propsDeclarations?.map(prop =>
prop
? renderProp(prop, extractDefaultPropValue(prop, defaultValues), exposeInSidebar)
: null
)}
{renderInheritedProps(type.types, exposeInSidebar)}
);
};
const renderProp = (
{ comment, name, type, flags, signatures }: PropData,
defaultValue?: string,
exposeInSidebar?: boolean
) => (
{exposeInSidebar ? {name}
: {name}
}
{flags?.isOptional && Optional • }
Type: {renderTypeOrSignatureType(type, signatures, true)}
{defaultValue && defaultValue !== UNKNOWN_VALUE ? (
• Default:{' '}
{defaultValue}
) : null}
);
const APISectionProps = ({ data, defaultProps, header = 'Props' }: APISectionPropsProps) =>
data?.length ? (
<>
{header === 'Props' ? (
{header}
) : (
<>
{header}
>
)}
{data.map((propsDefinition: PropsDefinitionData) =>
renderProps(propsDefinition, defaultProps, header === 'Props')
)}
>
) : null;
export default APISectionProps;