import { getPropDoc } from '@chakra-ui/props-docs'
import { chakra, Code, Flex, HStack, Stack } from '@chakra-ui/react'
import { InlineCode } from 'components/mdx-components/inline-code'
import * as React from 'react'
import { convertBackticksToInlineCode } from 'utils/convert-backticks-to-inline-code'
import { t } from 'utils/i18n'
export type PropsTableProps = {
/**
* displayName of the target component
*/
of: string
/**
* prop names to omit
*/
omit?: string[] | null
/**
* Render only given prop names
* Has precedence over `omit`
*/
only?: string[] | null
}
const PropsTable = ({
of,
omit = ['layerStyle', 'noOfLines', 'textStyle', 'orientation', 'styleConfig'],
only,
}: PropsTableProps) => {
const propList = React.useMemo(
() => makePropsTable({ of, omit, only }),
[of, omit, only],
)
if (!propList.length) return null
return (
{propList.map((prop) => (
{prop.name}
{prop.required && (
{t('component.props-table.required')}
)}
{prop.description && (
{t('component.props-table.description')}
{convertBackticksToInlineCode(prop.description)}
)}
{t('component.props-table.type')}
{prop.type}
{prop.defaultValue && (
{t('component.props-table.default')}
{prop.defaultValue}
)}
))}
)
}
export default PropsTable
type MakePropsTableOptions = PropsTableProps
// TODO: Remove this when we update props-docs
const customTable: Record = {
Stepper: {
index: {
type: 'number',
required: true,
description: 'The active step index',
},
orientation: {
type: "'horizontal' | 'vertical'",
defaultValue: "'horizontal'",
description: 'The orientation of the stepper',
},
children: {
type: 'ReactElement[]',
description: ' The children of the stepper. Must be `Step` components',
},
},
StepStatus: {
complete: {
type: 'React.ReactNode | ((props: StepContext) => React.ReactNode)',
description: 'The element to show when the step is complete',
},
incomplete: {
type: 'React.ReactNode | ((props: StepContext) => React.ReactNode)',
description: 'The element to show when the step is incomplete',
},
active: {
type: 'React.ReactNode | ((props: StepContext) => React.ReactNode)',
description: 'The element to show when the step is current',
},
},
}
function makePropsTable({ of, omit, only }: MakePropsTableOptions) {
const props = customTable[of] ?? getPropDoc(of)
if (!props) return []
return Object.entries(props)
.filter(([name]) => {
if (Array.isArray(only) && !only.includes(name)) {
return false
}
if (Array.isArray(omit) && omit.includes(name)) {
return false
}
return true
})
.map(([name, value]: any[]) => ({
name,
...value,
type: cleanType(value.type),
defaultValue: cleanDefaultValue(value.defaultValue),
}))
}
function cleanType(value: any) {
return typeof value === 'string' ? value.replace(';', '') : value
}
function cleanDefaultValue(value: any) {
return typeof value === 'boolean' ? value.toString() : value
}