import React from 'react';
import { renderMethod } from './APISectionMethods';
import { InlineCode } from '~/components/base/code';
import { B, P } from '~/components/base/paragraph';
import { H2, H3Code, H4 } from '~/components/plugins/Headings';
import {
CommentData,
CommentTagData,
InterfaceDefinitionData,
MethodSignatureData,
PropData,
} from '~/components/plugins/api/APIDataTypes';
import {
CommentTextBlock,
getTagData,
mdInlineComponents,
renderFlags,
renderParamRow,
renderTableHeadRow,
resolveTypeName,
STYLES_APIBOX,
STYLES_NESTED_SECTION_HEADER,
} from '~/components/plugins/api/APISectionUtils';
import { Cell, Row, Table } from '~/ui/components/Table';
export type APISectionInterfacesProps = {
data: InterfaceDefinitionData[];
};
const renderDefaultValue = (defaultValue?: CommentTagData) =>
defaultValue && (
<>
Default: {defaultValue.text}
>
);
const renderInterfaceComment = (comment?: CommentData, signatures?: MethodSignatureData[]) => {
if (signatures && signatures.length) {
const { type, parameters, comment: signatureComment } = signatures[0];
const defaultValue = getTagData('default', signatureComment);
return (
<>
{parameters?.length ? parameters.map(param => renderParamRow(param)) : null}
Returns:
{resolveTypeName(type)}
{signatureComment && (
<>
>
)}
>
);
} else {
const defaultValue = getTagData('default', comment);
return (
);
}
};
const renderInterfacePropertyRow = ({
name,
flags,
type,
comment,
signatures,
}: PropData): JSX.Element => {
return (
|
{name}
{renderFlags(flags)}
|
{resolveTypeName(type)}
|
{renderInterfaceComment(comment, signatures)} |
);
};
const renderInterface = ({
name,
children,
comment,
extendedTypes,
}: InterfaceDefinitionData): JSX.Element | null => {
const interfaceChildren = children?.filter(child => !child?.inheritedFrom) || [];
if (!interfaceChildren.length) return null;
const interfaceMethods = interfaceChildren.filter(child => child.signatures);
const interfaceFields = interfaceChildren.filter(child => !child.signatures);
return (
{name}
{extendedTypes?.length ? (
Extends:
{extendedTypes.map(extendedType => (
{resolveTypeName(extendedType)}
))}
) : null}
{interfaceMethods.length ? (
<>
{name} Methods
{interfaceMethods.map(method => renderMethod(method))}
>
) : undefined}
{interfaceFields.length ? (
<>
{name} Properties
{renderTableHeadRow()}
{interfaceFields.map(renderInterfacePropertyRow)}
>
) : undefined}
);
};
const APISectionInterfaces = ({ data }: APISectionInterfacesProps) =>
data?.length ? (
<>
Interfaces
{data.map(renderInterface)}
>
) : null;
export default APISectionInterfaces;