|
| 1 | +import { Label, Stack } from '@patternfly/react-core' |
| 2 | +import { |
| 3 | + Table, |
| 4 | + Thead, |
| 5 | + Th, |
| 6 | + Tr, |
| 7 | + Tbody, |
| 8 | + Td, |
| 9 | + TableText, |
| 10 | +} from '@patternfly/react-table' |
| 11 | +import { css } from '@patternfly/react-styles' |
| 12 | +import accessibleStyles from '@patternfly/react-styles/css/utilities/Accessibility/accessibility' |
| 13 | +import textStyles from '@patternfly/react-styles/css/utilities/Text/text' |
| 14 | + |
| 15 | +type ComponentProp = { |
| 16 | + name: string |
| 17 | + isRequired?: boolean |
| 18 | + isBeta?: boolean |
| 19 | + isDeprecated?: boolean |
| 20 | + type?: string |
| 21 | + defaultValue?: string |
| 22 | + description: string |
| 23 | +} |
| 24 | + |
| 25 | +type PropsTableProps = { |
| 26 | + componentName: string |
| 27 | + componentDescription?: string |
| 28 | + componentProps?: ComponentProp[] |
| 29 | +} |
| 30 | + |
| 31 | +export const PropsTable: React.FunctionComponent<PropsTableProps> = ({ |
| 32 | + componentName, |
| 33 | + componentDescription, |
| 34 | + componentProps, |
| 35 | +}) => { |
| 36 | + const hasPropsToRender = !!componentProps?.length |
| 37 | + const betaDeprecatedProps = hasPropsToRender |
| 38 | + ? componentProps.filter((prop) => prop.isBeta && prop.isDeprecated) |
| 39 | + : [] |
| 40 | + |
| 41 | + if (betaDeprecatedProps.length) { |
| 42 | + throw new Error( |
| 43 | + `The following ${componentName} props have both the isBeta and isDeprecated tag: ${betaDeprecatedProps.map((prop) => prop.name).join(', ')}`, |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + const renderTagLabel = (componentProp: ComponentProp) => { |
| 48 | + const { isBeta, isDeprecated } = componentProp |
| 49 | + if (!isBeta && !isDeprecated) { |
| 50 | + return null |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <Label |
| 55 | + // Would need design eyes on outline vs filled |
| 56 | + variant="outline" |
| 57 | + color={`${isBeta ? 'blue' : 'orange'}`} |
| 58 | + isCompact |
| 59 | + > |
| 60 | + {isBeta ? 'Beta' : 'Deprecated'} |
| 61 | + </Label> |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <h3>{componentName}</h3> |
| 68 | + <Stack hasGutter> |
| 69 | + {componentDescription && ( |
| 70 | + <div className={css(textStyles.textColorSubtle)}> |
| 71 | + {componentDescription} |
| 72 | + </div> |
| 73 | + )} |
| 74 | + {hasPropsToRender && ( |
| 75 | + <> |
| 76 | + <div id={`${componentName}-required`}> |
| 77 | + <span className={css(textStyles.textColorRequired)}>*</span>{' '} |
| 78 | + <span className={css(textStyles.textColorSubtle)}> |
| 79 | + indicates a required prop |
| 80 | + </span> |
| 81 | + </div> |
| 82 | + <Table |
| 83 | + variant="compact" |
| 84 | + aria-label={`Props for ${componentName}`} |
| 85 | + aria-describedby={`${componentName}-required`} |
| 86 | + gridBreakPoint="grid-lg" |
| 87 | + > |
| 88 | + <Thead> |
| 89 | + <Tr> |
| 90 | + <Th width={20}>Name</Th> |
| 91 | + <Th width={20}>Type</Th> |
| 92 | + <Th>Default</Th> |
| 93 | + <Th>Description</Th> |
| 94 | + </Tr> |
| 95 | + </Thead> |
| 96 | + <Tbody> |
| 97 | + {componentProps?.map((prop: ComponentProp) => ( |
| 98 | + <Tr key={prop.name}> |
| 99 | + <Td> |
| 100 | + <TableText wrapModifier="breakWord"> |
| 101 | + {prop.name} |
| 102 | + {prop.isRequired ? ( |
| 103 | + <> |
| 104 | + <span |
| 105 | + aria-hidden="true" |
| 106 | + className={css(textStyles.textColorRequired)} |
| 107 | + > |
| 108 | + * |
| 109 | + </span> |
| 110 | + <span |
| 111 | + className={css(accessibleStyles.screenReader)} |
| 112 | + > |
| 113 | + required |
| 114 | + </span> |
| 115 | + </> |
| 116 | + ) : ( |
| 117 | + '' |
| 118 | + )}{' '} |
| 119 | + {renderTagLabel(prop)} |
| 120 | + </TableText> |
| 121 | + </Td> |
| 122 | + <Td> |
| 123 | + <TableText wrapModifier="breakWord"> |
| 124 | + {prop.type || 'No type info'} |
| 125 | + </TableText> |
| 126 | + </Td> |
| 127 | + <Td> |
| 128 | + <TableText wrapModifier="breakWord"> |
| 129 | + {prop.defaultValue} |
| 130 | + </TableText> |
| 131 | + </Td> |
| 132 | + <Td> |
| 133 | + <TableText wrapModifier="breakWord"> |
| 134 | + {prop.description} |
| 135 | + </TableText> |
| 136 | + </Td> |
| 137 | + </Tr> |
| 138 | + ))} |
| 139 | + </Tbody> |
| 140 | + </Table> |
| 141 | + </> |
| 142 | + )} |
| 143 | + </Stack> |
| 144 | + </> |
| 145 | + ) |
| 146 | +} |
0 commit comments