aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-docs/src/components/signature_block.tsx
blob: 1ea0ea28ca98224544b0609446eda51d0519bfca (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
import { AnchorTitle, colors, HeaderSizes, Styles } from '@0xproject/react-shared';
import {
    Parameter,
    SolidityMethod,
    TypeDefinitionByName,
    TypescriptFunction,
    TypescriptMethod,
} from '@0xproject/types';
import * as _ from 'lodash';
import * as React from 'react';

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

import { Comment } from './comment';
import { Signature } from './signature';
import { SourceLink } from './source_link';

export interface SignatureBlockProps {
    method: SolidityMethod | TypescriptFunction | TypescriptMethod;
    sectionName: string;
    libraryVersion: string;
    typeDefinitionByName: TypeDefinitionByName;
    docsInfo: DocsInfo;
    sourceUrl: string;
}

export interface SignatureBlockState {
    shouldShowAnchor: boolean;
}

const styles: Styles = {
    chip: {
        fontSize: 13,
        backgroundColor: colors.lightBlueA700,
        color: colors.white,
        height: 11,
        borderRadius: 14,
        lineHeight: 0.9,
    },
};

export class SignatureBlock extends React.Component<SignatureBlockProps, SignatureBlockState> {
    constructor(props: SignatureBlockProps) {
        super(props);
        this.state = {
            shouldShowAnchor: false,
        };
    }
    public render(): React.ReactNode {
        const method = this.props.method;

        return (
            <div
                id={`${this.props.sectionName}-${method.name}`}
                style={{ overflow: 'hidden', width: '100%' }}
                className="pb4"
                onMouseOver={this._setAnchorVisibility.bind(this, true)}
                onMouseOut={this._setAnchorVisibility.bind(this, false)}
            >
                {!(method as TypescriptMethod).isConstructor && (
                    <div className="flex pb2 pt2">
                        {(method as TypescriptMethod).isStatic && this._renderChip('Static')}
                        {(method as SolidityMethod).isConstant && this._renderChip('Constant')}
                        {(method as SolidityMethod).isPayable && this._renderChip('Payable')}
                        <div style={{ lineHeight: 1.3 }}>
                            <AnchorTitle
                                headerSize={HeaderSizes.H3}
                                title={method.name}
                                id={`${this.props.sectionName}-${method.name}`}
                                shouldShowAnchor={this.state.shouldShowAnchor}
                            />
                        </div>
                    </div>
                )}
                <code className={`hljs ${constants.TYPE_TO_SYNTAX[this.props.docsInfo.type]}`}>
                    <Signature
                        name={method.name}
                        returnType={method.returnType}
                        parameters={method.parameters}
                        typeParameter={(method as TypescriptMethod).typeParameter}
                        callPath={(method as TypescriptMethod).callPath}
                        sectionName={this.props.sectionName}
                        typeDefinitionByName={this.props.typeDefinitionByName}
                        docsInfo={this.props.docsInfo}
                        isInPopover={false}
                    />
                </code>
                {(method as TypescriptMethod).source && (
                    <SourceLink
                        version={this.props.libraryVersion}
                        source={(method as TypescriptMethod).source}
                        sourceUrl={this.props.sourceUrl}
                    />
                )}
                {method.comment && <Comment comment={method.comment} className="py2" />}
                {method.parameters &&
                    !_.isEmpty(method.parameters) && (
                        <div>
                            <h4 className="pb1 thin" style={{ borderBottom: '1px solid #e1e8ed' }}>
                                ARGUMENTS
                            </h4>
                            {this._renderParameterDescriptions(method.parameters)}
                        </div>
                    )}
                {method.returnComment && (
                    <div className="pt1 comment">
                        <h4 className="pb1 thin" style={{ borderBottom: '1px solid #e1e8ed' }}>
                            RETURNS
                        </h4>
                        <Comment comment={method.returnComment} />
                    </div>
                )}
            </div>
        );
    }
    private _renderChip(text: string): React.ReactNode {
        return (
            <div className="p1 mr1" style={styles.chip}>
                {text}
            </div>
        );
    }
    private _renderParameterDescriptions(parameters: Parameter[]): React.ReactNode {
        const descriptions = _.map(parameters, parameter => {
            const isOptional = parameter.isOptional;
            return (
                <div
                    key={`param-description-${parameter.name}`}
                    className="flex pb1 mb2"
                    style={{ borderBottom: '1px solid #f0f4f7' }}
                >
                    <div className="pl2 col lg-col-4 md-col-4 sm-col-12 col-12">
                        <div
                            className="bold"
                            style={{ overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' }}
                        >
                            {parameter.name}
                        </div>
                        <div className="pt1" style={{ color: colors.grey, fontSize: 14 }}>
                            {isOptional && 'optional'}
                        </div>
                    </div>
                    <div className="col lg-col-8 md-col-8 sm-col-12 col-12" style={{ paddingLeft: 5 }}>
                        {parameter.comment && <Comment comment={parameter.comment} />}
                    </div>
                </div>
            );
        });
        return descriptions;
    }
    private _setAnchorVisibility(shouldShowAnchor: boolean): void {
        this.setState({
            shouldShowAnchor,
        });
    }
}