1 | # react-docgen-typescript
|
2 |
|
3 | [](https://github.com/styleguidist/react-docgen-typescript/actions/workflows/nodejs.yml)
|
4 |
|
5 | 
|
6 |
|
7 | A simple parser for React properties defined in TypeScript instead of propTypes.
|
8 |
|
9 | It can be used with [React Styleguidist](https://github.com/styleguidist/react-styleguidist).
|
10 |
|
11 | ## Installation
|
12 |
|
13 | ```bash
|
14 | npm install --save-dev react-docgen-typescript
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | To parse a file for docgen information use the `parse` function.
|
20 |
|
21 | ```ts
|
22 | const docgen = require("react-docgen-typescript");
|
23 |
|
24 | const options = {
|
25 | savePropValueAsString: true,
|
26 | };
|
27 |
|
28 | // Parse a file for docgen info
|
29 | docgen.parse("./path/to/component", options);
|
30 | ```
|
31 |
|
32 | If you want to customize the typescript configuration or docgen options, this package exports a variety of ways to create custom parsers.
|
33 |
|
34 | ```ts
|
35 | const docgen = require("react-docgen-typescript");
|
36 |
|
37 | // Create a parser with the default typescript config and custom docgen options
|
38 | const customParser = docgen.withDefaultConfig(options);
|
39 |
|
40 | const docs = customParser.parse("./path/to/component");
|
41 |
|
42 | // Create a parser with the custom typescript and custom docgen options
|
43 | const customCompilerOptionsParser = docgen.withCompilerOptions(
|
44 | { esModuleInterop: true },
|
45 | options
|
46 | );
|
47 |
|
48 | // Create a parser with using your typescript config
|
49 | const tsConfigParser = docgen.withCustomConfig("./tsconfig.json", {
|
50 | savePropValueAsString: true,
|
51 | });
|
52 | ```
|
53 |
|
54 | ### React Styleguidist integration
|
55 |
|
56 | Include following line in your `styleguide.config.js`:
|
57 |
|
58 | ```javascript
|
59 | module.exports = {
|
60 | propsParser: require("react-docgen-typescript").withDefaultConfig([
|
61 | parserOptions,
|
62 | ]).parse,
|
63 | };
|
64 | ```
|
65 |
|
66 | or if you want to use custom tsconfig file
|
67 |
|
68 | ```javascript
|
69 | module.exports = {
|
70 | propsParser: require("react-docgen-typescript").withCustomConfig(
|
71 | "./tsconfig.json",
|
72 | [parserOptions]
|
73 | ).parse,
|
74 | };
|
75 | ```
|
76 |
|
77 | ## Options
|
78 |
|
79 | ### `propFilter`
|
80 |
|
81 | The `propFilter` option allows you to omit certain props from documentation generation.
|
82 |
|
83 | You can either provide and object with some of our pre-configured filters:
|
84 |
|
85 | ```typescript
|
86 | interface FilterOptions {
|
87 | skipPropsWithName?: string[] | string;
|
88 | skipPropsWithoutDoc?: boolean;
|
89 | }
|
90 |
|
91 | const options = {
|
92 | propFilter: {
|
93 | skipPropsWithName: ['as', 'id'];
|
94 | skipPropsWithoutDoc: true;
|
95 | }
|
96 | }
|
97 | ```
|
98 |
|
99 | If you do not want to print out all the HTML attributes of a component typed like the following:
|
100 |
|
101 | ```typescript
|
102 | const MyComponent: React.FC<React.HTMLAttributes<HTMLDivElement>> = ()...
|
103 | ```
|
104 |
|
105 | you can provide a `propFilter` function and do the filtering logic yourself.
|
106 |
|
107 | ```typescript
|
108 | type PropFilter = (prop: PropItem, component: Component) => boolean;
|
109 |
|
110 | const 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 |
|
125 | Note: `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 |
|
133 | If 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 |
|
137 | If 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 |
|
141 | If set to true, every unions will be converted to docgen enum format.
|
142 |
|
143 | ### `skipChildrenPropWithoutDoc`: boolean (default: `true`)
|
144 |
|
145 | If set to false the docs for the `children` prop will be generated even without an explicit description.
|
146 |
|
147 | ### `shouldRemoveUndefinedFromOptional`: boolean
|
148 |
|
149 | If set to true, types that are optional will not display " | undefined" in the type.
|
150 |
|
151 | ### `savePropValueAsString`: boolean
|
152 |
|
153 | If set to true, defaultValue to props will be string.
|
154 | Example:
|
155 |
|
156 | ```javascript
|
157 | Component.defaultProps = {
|
158 | counter: 123,
|
159 | disabled: false,
|
160 | };
|
161 | ```
|
162 |
|
163 | Will 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
|
181 | componentNameResolver: (exp, source) =>
|
182 | exp.getName() === "StyledComponentClass" && getDefaultExportForFile(source);
|
183 | ```
|
184 |
|
185 | > The parser exports `getDefaultExportForFile` helper through its public API.
|
186 |
|
187 | ## Example
|
188 |
|
189 | In 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 |
|
193 | The component [`Column.tsx`](./examples/react-styleguidist-example/components/Column.tsx)
|
194 |
|
195 | ```javascript
|
196 | import * as React from "react";
|
197 | import { Component } from "react";
|
198 |
|
199 | /**
|
200 | * Column properties.
|
201 | */
|
202 | export 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 | */
|
218 | export class Column extends Component<IColumnProps, {}> {
|
219 | render() {
|
220 | return <div>Test</div>;
|
221 | }
|
222 | }
|
223 | ```
|
224 |
|
225 | Will generate the following stylesheet:
|
226 |
|
227 | 
|
228 |
|
229 | The functional component [`Grid.tsx`](./examples/react-styleguidist-example/components/Grid.tsx)
|
230 |
|
231 | ```javascript
|
232 | import * as React from "react";
|
233 |
|
234 | /**
|
235 | * Grid properties.
|
236 | */
|
237 | export 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 | */
|
253 | export const Grid = (props: IGridProps) => {
|
254 | const smaller = () => {
|
255 | return;
|
256 | };
|
257 | return <div>Grid</div>;
|
258 | };
|
259 | ```
|
260 |
|
261 | Will generate the following stylesheet:
|
262 |
|
263 | 
|
264 |
|
265 | ## Contributions
|
266 |
|
267 | The typescript is pretty complex and there are many different ways how
|
268 | to define components and their props so it's realy hard to support all
|
269 | these use cases. That means only one thing, contributions are highly
|
270 | welcome. Just keep in mind that each PR should also include tests for
|
271 | the part it's fixing.
|
272 |
|
273 | Thanks to all contributors without their help there wouldn't be a single
|
274 | bug fixed or feature implemented. Check the [**contributors**](https://github.com/styleguidist/react-docgen-typescript/graphs/contributors) tab to find out
|
275 | more. All those people supported this project. **THANK YOU!**
|
276 |
|
277 | ## Thanks to others
|
278 |
|
279 | The 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.
|