Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional propDescriptions #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ The returned object is compatible with [`react-docgen`](https://github.com/react
}
```

If you also need the property `description` (as provided by `react-docgen`) you can add it using the proprietary `propDescriptions` object:

```jsx
MyComponent.propDescriptions = {
name: 'The name'
}
```

## Why not [`react-docgen`](https://github.com/reactjs/react-docgen)?

[`react-docgen`](https://github.com/reactjs/react-docgen) reads file contents in order to find prop types definitions. It has some limitations, such as not allowing computed prop types and, in several situations, not being able to parse file contents correctly.
Expand All @@ -77,11 +85,11 @@ The returned object is compatible with [`react-docgen`](https://github.com/react

**Parameters**

- `$0` **any**
- `$0` **any**
- `$0.propTypes` (optional, default `{}`)
- `$0.defaultProps` (optional, default `{}`)

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**

## License

Expand Down
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,36 @@ Object.keys(T)

const parsePropTypeMethod = (
{ isRequired, ...method }: Object,
value?: any
value?: any,
description?: String
): Object => ({
type: {
name: "custom"
},
required: false,
...(typeof value !== "undefined" ? { defaultValue: { value } } : {}),
...(typeof description !== "undefined" ? { description } : {}),
...method
});

/** */
const parsePropTypes = ({
propTypes = {},
defaultProps = {}
defaultProps = {},
propDescriptions = {}
}: {
propTypes?: Object,
defaultProps?: Object
defaultProps?: Object,
propDescriptions?: Object
}): Object =>
Object.keys(propTypes).reduce(
(parsed, prop) => ({
...parsed,
[prop]: parsePropTypeMethod(propTypes[prop], defaultProps[prop])
[prop]: parsePropTypeMethod(
propTypes[prop],
defaultProps[prop],
propDescriptions[prop]
)
}),
{}
);
Expand Down
19 changes: 19 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ Object {
}
`;

exports[`parsePropTypes works with propDescriptions 1`] = `
Object {
"bar": Object {
"description": "True or false",
"required": false,
"type": Object {
"name": "bool",
},
},
"foo": Object {
"description": "Important string",
"required": true,
"type": Object {
"name": "string",
},
},
}
`;

exports[`parsePropTypes works without defaultProps 1`] = `
Object {
"bar": Object {
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ describe("parsePropTypes", () => {
const Component = {};
expect(parsePropTypes(Component)).toEqual({});
});

it("works with propDescriptions", () => {
const Component = {
propTypes: {
foo: PropTypes.string.isRequired,
bar: PropTypes.bool
},
propDescriptions: {
foo: "Important string",
bar: "True or false"
}
};

expect(parsePropTypes(Component)).toMatchSnapshot();
});
});