From 4bea1fafa6f2f16d8c5478cf601e0a0edc43f669 Mon Sep 17 00:00:00 2001 From: Diego Landa Date: Mon, 1 May 2017 23:59:54 -0400 Subject: [PATCH] Added PureComponent do docgenConverter and examples --- .../components/ConstExport.tsx | 48 +++++++++++++++++++ .../components/PureRow.tsx | 29 +++++++++++ src/__tests__/docgenConverter.spec.ts | 26 ++++++++++ src/docgenConverter.ts | 2 +- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 examples/react-styleguidist-example/components/ConstExport.tsx create mode 100644 examples/react-styleguidist-example/components/PureRow.tsx diff --git a/examples/react-styleguidist-example/components/ConstExport.tsx b/examples/react-styleguidist-example/components/ConstExport.tsx new file mode 100644 index 00000000..d11225f1 --- /dev/null +++ b/examples/react-styleguidist-example/components/ConstExport.tsx @@ -0,0 +1,48 @@ +import * as React from 'react'; + +/** + * Row properties. + */ +export interface IRowProps { + /** prop1 description */ + prop1?: string; + /** prop2 description */ + prop2: number; + /** + * prop3 description + */ + prop3: () => void; + /** prop4 description */ + prop4: 'option1' | 'option2' | "option3"; +} + +/** + * test + * + */ +export const test = (one: number) => { + return one; +} + +export const myObj = { + foo: 'bar', +} + +/** + * Form row. + */ +export const ConstExportRow = (props: IRowProps) => { + const innerFunc = (props: IRowProps) => { + return Inner Func + }; + const innerNonExportedFunc = (props: IRowProps) => { + return Inner Func + }; + return
Test
; +}; + +const nonExportedFunc = (props: IRowProps) => { + return
No Export
+}; + +export default ConstExportRow; diff --git a/examples/react-styleguidist-example/components/PureRow.tsx b/examples/react-styleguidist-example/components/PureRow.tsx new file mode 100644 index 00000000..03ae4ea5 --- /dev/null +++ b/examples/react-styleguidist-example/components/PureRow.tsx @@ -0,0 +1,29 @@ +import * as React from 'react'; + +/** + * Row properties. + */ +export interface IRowProps { + /** prop1 description */ + prop1?: string; + /** prop2 description */ + prop2: number; + /** + * prop3 description + */ + prop3: () => void; + /** prop4 description */ + prop4: 'option1' | 'option2' | "option3"; +} + +/** + * Form row. + */ +export class PureRow extends React.PureComponent { + + render() { + return
Test
; + } +}; + +export default PureRow; \ No newline at end of file diff --git a/src/__tests__/docgenConverter.spec.ts b/src/__tests__/docgenConverter.spec.ts index e4cc568f..2622ec8a 100644 --- a/src/__tests__/docgenConverter.spec.ts +++ b/src/__tests__/docgenConverter.spec.ts @@ -99,4 +99,30 @@ describe('docgenConverter', () => { assert.equal('name1', result.displayName); assert.equal('comment1', result.description); }); + + it('Should work with class PureComponent', () => { + let result: StyleguidistComponent = null; + const originalWarn = console.warn; + let warnCallCount = 0; + console.warn = () => warnCallCount++; + try { + result = convertToDocgen({ + classes: [ + { + name: 'name1', + comment: 'comment1', + extends: 'PureComponent', + propInterface: null, + } + ], + interfaces: [] + }); + } finally { + console.warn = originalWarn; + } + + assert.equal(1, warnCallCount); + assert.equal('name1', result.displayName); + assert.equal('comment1', result.description); + }); }); \ No newline at end of file diff --git a/src/docgenConverter.ts b/src/docgenConverter.ts index 60a03088..7c026a97 100644 --- a/src/docgenConverter.ts +++ b/src/docgenConverter.ts @@ -11,7 +11,7 @@ export interface StyleguidistComponent { } export function convertToDocgen(doc: FileDoc): StyleguidistComponent { - const reactClasses = doc.classes.filter(i => i.extends === 'Component' || i.extends === 'StatelessComponent'); + const reactClasses = doc.classes.filter(i => i.extends === 'Component' || i.extends === 'StatelessComponent' || i.extends === 'PureComponent'); if (reactClasses.length === 0) { return null;