Skip to content

Commit

Permalink
Add extractTypeDescribedValue to PropsTable (#38)
Browse files Browse the repository at this point in the history
* fix(docz): add extractTypeDescribedValue to PropsTable

* fix(docz): show PropsTable shape in brackets
  • Loading branch information
renatorib authored and pedronauck committed Jun 15, 2018
1 parent f30c139 commit e2ccc51
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/docz/src/components/PropsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ export interface FlowTypeArgs {
}
}

export interface PropType {
name: string
value?: any
}

export interface Prop {
required: boolean
description?: string
type: {
name: string
value?: EnumValue[]
}
type: PropType
defaultValue?: {
value: string
computed: boolean
}
flowType?: {
elements: FlowTypeElement[]
Expand Down Expand Up @@ -61,7 +64,39 @@ export type TooltipComponent = React.ComponentType<{
children: React.ReactNode
}>

const getValue = (value: string) => value.replace(/\'/g, '')
const extractTypeDescribedValue = (type: PropType): string => {
const { name, value } = type

// instanceOf, computed shape, uknown enum
if (typeof value === 'string') {
return value
}

// oneOf, oneOfType
if (Array.isArray(value)) {
return value.map(valueType => {
if (valueType.name === 'custom') {
return `custom(${valueType.raw})`
}

return valueType.name || valueType.value
}).join(' | ')
}

// arrayOf, objectOf
if (typeof value === 'object' && name !== 'shape') {
return value.name
}

// shape
if (typeof value === 'object' && name === 'shape') {
// show only keys due to a recursive limitation
return `{ ${Object.keys(value).join(', ')} }`
}

// untreated
return ''
}

const getPropType = (prop: Prop, Tooltip?: TooltipComponent) => {
const name = prop.flowType ? prop.flowType.name : prop.type.name
Expand All @@ -75,7 +110,7 @@ const getPropType = (prop: Prop, Tooltip?: TooltipComponent) => {
return prop.flowType ? (
<Tooltip text={prop.flowType.raw}>{name}</Tooltip>
) : (
<Tooltip text={value && value.map(val => getValue(val.value)).join(' | ')}>
<Tooltip text={extractTypeDescribedValue(prop.type)}>
{name}
</Tooltip>
)
Expand Down

0 comments on commit e2ccc51

Please sign in to comment.