UNPKG

7.4 kBMarkdownView Raw
1# react-docgen-typescript
2
3[![Build Status](https://travis-ci.org/styleguidist/react-docgen-typescript.svg)](https://travis-ci.org/styleguidist/react-docgen-typescript)
4
5![](https://nodei.co/npm/react-docgen-typescript.png?downloadRank=true&downloads=true)
6
7A simple parser for React properties defined in TypeScript instead of propTypes.
8
9It can be used with [React Styleguidist](https://github.com/styleguidist/react-styleguidist).
10
11## Installation
12
13```bash
14npm install --save-dev react-docgen-typescript
15```
16
17## Usage
18
19To parse a file for docgen information use the `parse` function.
20
21```ts
22const docgen = require("react-docgen-typescript");
23
24const options = {
25 savePropValueAsString: true,
26};
27
28// Parse a file for docgen info
29docgen.parse("./path/to/component", options);
30```
31
32If you want to customize the typescript configuration or docgen options, this package exports a variety of ways to create custom parsers.
33
34```ts
35const docgen = require("react-docgen-typescript");
36
37// Create a parser with the default typescript config and custom docgen options
38const customParser = docgen.withDefaultConfig(options);
39
40const docs = customParser.parse("./path/to/component");
41
42// Create a parser with the custom typescript and custom docgen options
43const customCompilerOptionsParser = docgen.withCompilerOptions(
44 { esModuleInterop: true },
45 options
46);
47
48// Create a parser with using your typescript config
49const tsConfigParser = docgen.withCustomConfig("./tsconfig.json", {
50 savePropValueAsString: true,
51});
52```
53
54### React Styleguidist integration
55
56Include following line in your `styleguide.config.js`:
57
58```javascript
59module.exports = {
60 propsParser: require("react-docgen-typescript").withDefaultConfig([
61 parserOptions,
62 ]).parse,
63};
64```
65
66or if you want to use custom tsconfig file
67
68```javascript
69module.exports = {
70 propsParser: require("react-docgen-typescript").withCustomConfig(
71 "./tsconfig.json",
72 [parserOptions]
73 ).parse,
74};
75```
76
77## Options
78
79### `propFilter`
80
81The `propFilter` option allows you to omit certain props from documentation generation.
82
83You can either provide and object with some of our pre-configured filters:
84
85```typescript
86interface FilterOptions {
87 skipPropsWithName?: string[] | string;
88 skipPropsWithoutDoc?: boolean;
89}
90
91const options = {
92 propFilter: {
93 skipPropsWithName: ['as', 'id'];
94 skipPropsWithoutDoc: true;
95 }
96}
97```
98
99If you do not want to print out all the HTML attributes of a component typed like the following:
100
101```typescript
102const MyComponent: React.FC<React.HTMLAttributes<HTMLDivElement>> = ()...
103```
104
105you can provide a `propFilter` function and do the filtering logic yourself.
106
107```typescript
108type PropFilter = (prop: PropItem, component: Component) => boolean;
109
110const options = {
111 propFilter: (prop: PropItem, component: Component) => {
112 if (prop.declarations !== undefined && prop.declarations.length > 0) {
113 const hasPropAdditionalDescription = prop.declarations.find((declaration) => {
114 return !declaration.fileName.includes("node_modules");
115 });
116
117 return Boolean(hasPropAdditionalDescription);
118 }
119
120 return true;
121 },
122};
123```
124
125Note: `children` without a doc comment will not be documented.
126
127### `componentNameResolver`
128
129```typescript
130(exp: ts.Symbol, source: ts.SourceFile) => string | undefined | null | false;
131```
132
133If a string is returned, then the component will use that name. Else it will fallback to the default logic of parser.
134
135### `shouldExtractLiteralValuesFromEnum`: boolean
136
137If set to true, string enums and unions will be converted to docgen enum format. Useful if you use Storybook and want to generate knobs automatically using [addon-smart-knobs](https://github.com/storybookjs/addon-smart-knobs).
138
139### `shouldExtractValuesFromUnion`: boolean
140
141If set to true, every unions will be converted to docgen enum format.
142
143### `skipChildrenPropWithoutDoc`: boolean (default: `true`)
144
145If set to false the docs for the `children` prop will be generated even without an explicit description.
146
147### `shouldRemoveUndefinedFromOptional`: boolean
148
149If set to true, types that are optional will not display " | undefined" in the type.
150
151### `savePropValueAsString`: boolean
152
153If set to true, defaultValue to props will be string.
154Example:
155
156```javascript
157Component.defaultProps = {
158 counter: 123,
159 disabled: false,
160};
161```
162
163Will return:
164
165```javascript
166 counter: {
167 defaultValue: '123',
168 required: true,
169 type: 'number'
170 },
171 disabled: {
172 defaultValue: 'false',
173 required: true,
174 type: 'boolean'
175 }
176```
177
178**Styled components example:**
179
180```typescript
181componentNameResolver: (exp, source) =>
182 exp.getName() === "StyledComponentClass" && getDefaultExportForFile(source);
183```
184
185> The parser exports `getDefaultExportForFile` helper through its public API.
186
187## Example
188
189In the example folder you can see React Styleguidist integration.
190
191**Warning:** only named exports are supported. If your project uses default exports, you still need to include named exports for `react-docgen-typescript`.
192
193The component [`Column.tsx`](./examples/react-styleguidist-example/components/Column.tsx)
194
195```javascript
196import * as React from "react";
197import { Component } from "react";
198
199/**
200 * Column properties.
201 */
202export interface IColumnProps {
203 /** prop1 description */
204 prop1?: string;
205 /** prop2 description */
206 prop2: number;
207 /**
208 * prop3 description
209 */
210 prop3: () => void;
211 /** prop4 description */
212 prop4: "option1" | "option2" | "option3";
213}
214
215/**
216 * Form column.
217 */
218export class Column extends Component<IColumnProps, {}> {
219 render() {
220 return <div>Test</div>;
221 }
222}
223```
224
225Will generate the following stylesheet:
226
227![Stylesheet example](https://github.com/styleguidist/react-docgen-typescript/raw/master/stylesheet-example-column.png "Stylesheet example")
228
229The functional component [`Grid.tsx`](./examples/react-styleguidist-example/components/Grid.tsx)
230
231```javascript
232import * as React from "react";
233
234/**
235 * Grid properties.
236 */
237export interface IGridProps {
238 /** prop1 description */
239 prop1?: string;
240 /** prop2 description */
241 prop2: number;
242 /**
243 * prop3 description
244 */
245 prop3: () => void;
246 /** Working grid description */
247 prop4: "option1" | "option2" | "option3";
248}
249
250/**
251 * Form Grid.
252 */
253export const Grid = (props: IGridProps) => {
254 const smaller = () => {
255 return;
256 };
257 return <div>Grid</div>;
258};
259```
260
261Will generate the following stylesheet:
262
263![Stylesheet example](https://github.com/styleguidist/react-docgen-typescript/raw/master/stylesheet-example-grid.png "Stylesheet example")
264
265## Contributions
266
267The typescript is pretty complex and there are many different ways how
268to define components and their props so it's realy hard to support all
269these use cases. That means only one thing, contributions are highly
270welcome. Just keep in mind that each PR should also include tests for
271the part it's fixing.
272
273Thanks to all contributors without their help there wouldn't be a single
274bug fixed or feature implemented. Check the [**contributors**](https://github.com/styleguidist/react-docgen-typescript/graphs/contributors) tab to find out
275more. All those people supported this project. **THANK YOU!**
276
277## Thanks to others
278
279The integration with React Styleguidist wouldn't be possible without [Vyacheslav Slinko](https://github.com/vslinko) pull request [#118](https://github.com/styleguidist/react-styleguidist/pull/118) at React Styleguidist.