I love the idea, @kentcdodds!
I'm currently working on a project that doesn't use prop-types
and is not statically typed either π¬. Having the ability to manually add prop definitions is definitely a great addition to Docz!
I started working on this idea, and made some very good progress!
Here are the results of what I have so far...
Given this markup:
<PropsTable
props={[
{
property: "variant",
type: {
name: "enum",
value: [
{ value: "primary" },
{ value: "secondary" },
{ value: "outline" }
]
},
required: false,
defaultValue: {
value: "primary"
},
description: 'Can also specify "secondary"'
},
{
property: "disabled",
type: {
name: "boolean"
},
required: false,
defaultValue: {
value: "false"
}
}
]}
/>
The following is rendered:
<img width="849" alt="screen shot 2018-10-09 at 1 49 15 am" src="https://user-images.githubusercontent.com/2100222/46649243-5c461680-cb66-11e8-8a2d-6904dbafed9b.png">
It's worth mentioning that Docz has some expectations around the component's docgen info, so I started experimenting with some schema validation mechanism that I think would go hand in hand with this feature. This will:
- prevent the rendering logic from throwing cryptic errors in case the schema passed via
props
doesn't match what Docz expects (a valid react-docgen data structure, that is).
- help developers debugging the invalid parts of the schema.
For example, when defining a prop of type enum
, its value should be an array of objects that have a key value
, like in the example above. The following will not be considered valid:
<PropsTable
props={[{
property: "variant",
type: {
name: "enum",
value: ['primary', 'secondary']
},
// ...
In case it was entered, an error like this will be thrown :
<img width="695" alt="screen shot 2018-10-09 at 2 00 32 am" src="https://user-images.githubusercontent.com/2100222/46649647-d4610c00-cb67-11e8-8052-d791c9089905.png">
Another example:
<img width="688" alt="screen shot 2018-10-09 at 2 06 37 am" src="https://user-images.githubusercontent.com/2100222/46649691-fc506f80-cb67-11e8-89fa-70b06f120697.png">
There's still a lot of work to be done here, but I thought I'd share my proof of concept as well! π