aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-docs/src/components/signature.tsx
blob: c229999b12e05fbecbeef5381b726820e2b52071 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as _ from 'lodash';
import * as React from 'react';

import { Parameter, Type as TypeDef, TypeDefinitionByName, TypeParameter } from '@0x/types';

import { DocsInfo } from '../docs_info';

import { Type } from './type';

export interface SignatureProps {
    name: string;
    returnType: TypeDef;
    parameters: Parameter[];
    sectionName: string;
    shouldHideMethodName?: boolean;
    shouldUseArrowSyntax?: boolean;
    typeDefinitionByName?: TypeDefinitionByName;
    typeParameter?: TypeParameter;
    callPath?: string;
    docsInfo: DocsInfo;
    isInPopover: boolean;
    isFallback?: boolean;
}

const defaultProps = {
    shouldHideMethodName: false,
    shouldUseArrowSyntax: false,
    callPath: '',
    isFallback: false,
};

export const Signature: React.SFC<SignatureProps> = (props: SignatureProps) => {
    const sectionName = props.sectionName;
    const parameters = renderParameters(
        props.parameters,
        props.docsInfo,
        sectionName,
        props.isInPopover,
        props.name,
        props.typeDefinitionByName,
    );
    const paramStringArray: any[] = [];
    // HACK: For now we don't put params on newlines if there are less then 2 of them.
    // Ideally we would check the character length of the resulting method signature and
    // if it exceeds the available space, put params on their own lines.
    const hasMoreThenTwoParams = parameters.length > 2;
    _.each(parameters, (param: React.ReactNode, i: number) => {
        const finalParam = hasMoreThenTwoParams ? (
            <span className="pl2" key={`param-${i}`}>
                {param}
            </span>
        ) : (
            param
        );
        paramStringArray.push(finalParam);
        const comma = hasMoreThenTwoParams ? (
            <span key={`param-comma-${i}`}>
                , <br />
            </span>
        ) : (
            ', '
        );
        paramStringArray.push(comma);
    });
    if (!hasMoreThenTwoParams) {
        paramStringArray.pop();
    }
    const methodName = props.shouldHideMethodName ? '' : props.name;
    const typeParameterIfExists = _.isUndefined(props.typeParameter)
        ? undefined
        : renderTypeParameter(
              props.typeParameter,
              props.docsInfo,
              sectionName,
              props.isInPopover,
              props.typeDefinitionByName,
          );
    return (
        <span style={{ fontSize: 15 }}>
            {props.callPath}
            {props.isFallback ? '' : methodName}
            {typeParameterIfExists}({hasMoreThenTwoParams && <br />}
            {paramStringArray})
            {props.returnType && (
                <span>
                    {props.shouldUseArrowSyntax ? ' => ' : ': '}{' '}
                    <Type
                        type={props.returnType}
                        sectionName={sectionName}
                        typeDefinitionByName={props.typeDefinitionByName}
                        docsInfo={props.docsInfo}
                        isInPopover={props.isInPopover}
                    />
                </span>
            )}
        </span>
    );
};

Signature.defaultProps = defaultProps;

function renderParameters(
    parameters: Parameter[],
    docsInfo: DocsInfo,
    sectionName: string,
    isInPopover: boolean,
    name: string,
    typeDefinitionByName?: TypeDefinitionByName,
): React.ReactNode[] {
    const params = _.map(parameters, (p: Parameter, i: number) => {
        const isOptional = p.isOptional;
        const hasDefaultValue = !_.isUndefined(p.defaultValue);
        const type = (
            <Type
                type={p.type}
                sectionName={sectionName}
                typeDefinitionByName={typeDefinitionByName}
                docsInfo={docsInfo}
                isInPopover={isInPopover}
            />
        );
        return (
            <span key={`param-${JSON.stringify(p.type)}-${name}-${i}`}>
                {!_.isEmpty(p.name) && (
                    <span>
                        {p.name}
                        {isOptional && '?'}:{' '}
                    </span>
                )}
                {type}
                {hasDefaultValue && ` = ${p.defaultValue}`}
            </span>
        );
    });
    return params;
}

function renderTypeParameter(
    typeParameter: TypeParameter,
    docsInfo: DocsInfo,
    sectionName: string,
    isInPopover: boolean,
    typeDefinitionByName?: TypeDefinitionByName,
): React.ReactNode {
    const typeParam = (
        <span>
            {`<${typeParameter.name}`}
            {!_.isUndefined(typeParameter.type) && (
                <span>
                    {' extends '}
                    <Type
                        type={typeParameter.type}
                        sectionName={sectionName}
                        typeDefinitionByName={typeDefinitionByName}
                        docsInfo={docsInfo}
                        isInPopover={isInPopover}
                    />
                </span>
            )}
            {`>`}
        </span>
    );
    return typeParam;
}