diff options
author | Fabio Berger <me@fabioberger.com> | 2018-04-02 20:23:07 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-04-02 20:23:07 +0800 |
commit | 8162394797342cef268cc8072fc860326974e269 (patch) | |
tree | 2826b02715a8cb794571be6c7dccdb395329361c /packages/react-docs/src/components/signature.tsx | |
parent | fd001186dd281a11920246c6b9afcefe1d55bc23 (diff) | |
parent | 695b697cdf6c73bb4b5f920869ce128f9a9e7523 (diff) | |
download | dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar.gz dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar.bz2 dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar.lz dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar.xz dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.tar.zst dexon-0x-contracts-8162394797342cef268cc8072fc860326974e269.zip |
Merge branch 'development'
* development: (175 commits)
small README fixes
Update docs list in README
Add manual postpublish command to all public packages and update CHANGELOG.json
Fix postpublish util to ignore namespace
Fix release notes bug
Should print out `lerna publish` stdout so we can see if anything went wrong
Publish
Updated CHANGELOGS
Generate CHANGELOG.json files
Fix hasty find/replace
Default to 4sp
Update moment, no longer need separate moment types
Move prettify command to utils and also call it on CHANGELOG.md
Add prettier run on generated CHANGELOG.json and fix scripts
Remove semi-colons from monorepo-scripts package.json
Get rid of ; in top-level package.json
Fix TSLint error
Make dry-run configurable from top-level package.json
Improve naming
Run prettier, update deployer CHANGELOG
...
Diffstat (limited to 'packages/react-docs/src/components/signature.tsx')
-rw-r--r-- | packages/react-docs/src/components/signature.tsx | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/packages/react-docs/src/components/signature.tsx b/packages/react-docs/src/components/signature.tsx new file mode 100644 index 000000000..83fb1e246 --- /dev/null +++ b/packages/react-docs/src/components/signature.tsx @@ -0,0 +1,131 @@ +import * as _ from 'lodash'; +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; + +import { DocsInfo } from '../docs_info'; +import { Parameter, Type as TypeDef, TypeDefinitionByName, TypeParameter } from '../types'; +import { constants } from '../utils/constants'; + +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; +} + +const defaultProps = { + shouldHideMethodName: false, + shouldUseArrowSyntax: false, + callPath: '', +}; + +export const Signature: React.SFC<SignatureProps> = (props: SignatureProps) => { + const sectionName = constants.TYPES_SECTION_NAME; + const parameters = renderParameters(props.parameters, props.docsInfo, sectionName, 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.typeDefinitionByName); + return ( + <span style={{ fontSize: 15 }}> + {props.callPath} + {methodName} + {typeParameterIfExists}({hasMoreThenTwoParams && <br />} + {paramStringArray}) + {props.returnType && ( + <span> + {props.shouldUseArrowSyntax ? ' => ' : ': '}{' '} + <Type + type={props.returnType} + sectionName={sectionName} + typeDefinitionByName={props.typeDefinitionByName} + docsInfo={props.docsInfo} + /> + </span> + )} + </span> + ); +}; + +Signature.defaultProps = defaultProps; + +function renderParameters( + parameters: Parameter[], + docsInfo: DocsInfo, + sectionName: string, + typeDefinitionByName?: TypeDefinitionByName, +) { + const params = _.map(parameters, (p: Parameter) => { + const isOptional = p.isOptional; + const type = ( + <Type + type={p.type} + sectionName={sectionName} + typeDefinitionByName={typeDefinitionByName} + docsInfo={docsInfo} + /> + ); + return ( + <span key={`param-${p.type}-${p.name}`}> + {p.name} + {isOptional && '?'}: {type} + </span> + ); + }); + return params; +} + +function renderTypeParameter( + typeParameter: TypeParameter, + docsInfo: DocsInfo, + sectionName: string, + typeDefinitionByName?: TypeDefinitionByName, +) { + const typeParam = ( + <span> + {`<${typeParameter.name} extends `} + <Type + type={typeParameter.type} + sectionName={sectionName} + typeDefinitionByName={typeDefinitionByName} + docsInfo={docsInfo} + /> + {`>`} + </span> + ); + return typeParam; +} |