| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/DescriptionList/description-list'; |
| 3 |
import { formatBreakpointMods } from '../../helpers'; |
| 4 |
import cssGridTemplateColumnsMin from '@patternfly/react-tokens/dist/esm/c_description_list_GridTemplateColumns_min'; |
| 5 |
import cssTermWidth from '@patternfly/react-tokens/dist/esm/c_description_list__term_width'; |
| 6 |
import cssHorizontalTermWidth from '@patternfly/react-tokens/dist/esm/c_description_list_m_horizontal__term_width'; |
| 7 |
|
| 8 |
export interface BreakpointModifiers { |
| 9 |
default?: string; |
| 10 |
md?: string; |
| 11 |
lg?: string; |
| 12 |
xl?: string; |
| 13 |
'2xl'?: string; |
| 14 |
} |
| 15 |
|
| 16 |
export interface DescriptionListProps extends Omit<React.HTMLProps<HTMLDListElement>, 'type'> { |
| 17 |
/** Anything that can be rendered inside of the list */ |
| 18 |
children?: React.ReactNode; |
| 19 |
/** Additional classes added to the list */ |
| 20 |
className?: string; |
| 21 |
/** Sets the description list to auto fit. */ |
| 22 |
isAutoFit?: boolean; |
| 23 |
/** Sets the description list component term and description pair to a horizontal layout. */ |
| 24 |
isHorizontal?: boolean; |
| 25 |
/** Sets the description list to format automatically. */ |
| 26 |
isAutoColumnWidths?: boolean; |
| 27 |
/** Modifies the description list display to inline-grid. */ |
| 28 |
isInlineGrid?: boolean; |
| 29 |
/** Sets the description list to compact styling. */ |
| 30 |
isCompact?: boolean; |
| 31 |
/** Sets a horizontal description list to have fluid styling. */ |
| 32 |
isFluid?: boolean; |
| 33 |
/** Sets the the default placement of description list groups to fill from top to bottom. */ |
| 34 |
isFillColumns?: boolean; |
| 35 |
/** Sets the display size of the descriptions in the description list.*/ |
| 36 |
displaySize?: 'default' | 'lg' | '2xl'; |
| 37 |
/** Sets the number of columns on the description list at various breakpoints */ |
| 38 |
columnModifier?: { |
| 39 |
default?: '1Col' | '2Col' | '3Col'; |
| 40 |
sm?: '1Col' | '2Col' | '3Col'; |
| 41 |
md?: '1Col' | '2Col' | '3Col'; |
| 42 |
lg?: '1Col' | '2Col' | '3Col'; |
| 43 |
xl?: '1Col' | '2Col' | '3Col'; |
| 44 |
'2xl'?: '1Col' | '2Col' | '3Col'; |
| 45 |
}; |
| 46 |
/** Indicates how the menu will align at various breakpoints. */ |
| 47 |
orientation?: { |
| 48 |
sm?: 'vertical' | 'horizontal'; |
| 49 |
md?: 'vertical' | 'horizontal'; |
| 50 |
lg?: 'vertical' | 'horizontal'; |
| 51 |
xl?: 'vertical' | 'horizontal'; |
| 52 |
'2xl'?: 'vertical' | 'horizontal'; |
| 53 |
}; |
| 54 |
/** Sets the minimum column size for the auto-fit (isAutoFit) layout at various breakpoints. */ |
| 55 |
autoFitMinModifier?: { |
| 56 |
default?: string; |
| 57 |
sm?: string; |
| 58 |
md?: string; |
| 59 |
lg?: string; |
| 60 |
xl?: string; |
| 61 |
'2xl'?: string; |
| 62 |
}; |
| 63 |
/** Sets the description list's term column width.*/ |
| 64 |
termWidth?: string; |
| 65 |
/** Sets the horizontal description list's term column width at various breakpoints. */ |
| 66 |
horizontalTermWidthModifier?: { |
| 67 |
default?: string; |
| 68 |
sm?: string; |
| 69 |
md?: string; |
| 70 |
lg?: string; |
| 71 |
xl?: string; |
| 72 |
'2xl'?: string; |
| 73 |
}; |
| 74 |
} |
| 75 |
|
| 76 |
const setBreakpointModifiers = (prefix: string, modifiers: BreakpointModifiers) => { |
| 77 |
const mods = modifiers as Partial<{ [k: string]: string }>; |
| 78 |
return Object.keys(mods || {}).reduce( |
| 79 |
(acc, curr) => |
| 80 |
curr === 'default' ? { ...acc, [prefix]: mods[curr] } : { ...acc, [`${prefix}-on-${curr}`]: mods[curr] }, |
| 81 |
{} |
| 82 |
); |
| 83 |
}; |
| 84 |
|
| 85 |
export const DescriptionList: React.FunctionComponent<DescriptionListProps> = ({ |
| 86 |
className = '', |
| 87 |
children = null, |
| 88 |
isHorizontal = false, |
| 89 |
isAutoColumnWidths, |
| 90 |
isAutoFit, |
| 91 |
isInlineGrid, |
| 92 |
isCompact, |
| 93 |
isFluid, |
| 94 |
isFillColumns, |
| 95 |
displaySize = 'default', |
| 96 |
columnModifier, |
| 97 |
autoFitMinModifier, |
| 98 |
termWidth, |
| 99 |
horizontalTermWidthModifier, |
| 100 |
orientation, |
| 101 |
style, |
| 102 |
...props |
| 103 |
}: DescriptionListProps) => { |
| 104 |
if (isAutoFit && autoFitMinModifier) { |
| 105 |
style = { |
| 106 |
...style, |
| 107 |
...setBreakpointModifiers(cssGridTemplateColumnsMin.name, autoFitMinModifier) |
| 108 |
}; |
| 109 |
} |
| 110 |
if (termWidth) { |
| 111 |
style = { |
| 112 |
...style, |
| 113 |
...{ [cssTermWidth.name]: termWidth } |
| 114 |
}; |
| 115 |
} |
| 116 |
if (isHorizontal && horizontalTermWidthModifier) { |
| 117 |
style = { |
| 118 |
...style, |
| 119 |
...setBreakpointModifiers(cssHorizontalTermWidth.name, horizontalTermWidthModifier) |
| 120 |
}; |
| 121 |
} |
| 122 |
|
| 123 |
return ( |
| 124 |
<dl |
| 125 |
className={css( |
| 126 |
styles.descriptionList, |
| 127 |
(isHorizontal || isFluid) && styles.modifiers.horizontal, |
| 128 |
isAutoColumnWidths && styles.modifiers.autoColumnWidths, |
| 129 |
isAutoFit && styles.modifiers.autoFit, |
| 130 |
formatBreakpointMods(columnModifier, styles), |
| 131 |
formatBreakpointMods(orientation, styles), |
| 132 |
isInlineGrid && styles.modifiers.inlineGrid, |
| 133 |
isCompact && styles.modifiers.compact, |
| 134 |
isFluid && styles.modifiers.fluid, |
| 135 |
isFillColumns && styles.modifiers.fillColumns, |
| 136 |
displaySize === 'lg' && styles.modifiers.displayLg, |
| 137 |
displaySize === '2xl' && styles.modifiers.display_2xl, |
| 138 |
className |
| 139 |
)} |
| 140 |
style={style} |
| 141 |
{...props} |
| 142 |
> |
| 143 |
{children} |
| 144 |
</dl> |
| 145 |
); |
| 146 |
}; |
| 147 |
|
| 148 |
DescriptionList.displayName = 'DescriptionList'; |