| 1 |
import styles from '@patternfly/react-styles/css/layouts/Grid/grid'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import { DeviceSizes } from '../../styles/sizes'; |
| 4 |
import * as gridToken from '@patternfly/react-tokens/dist/esm/l_grid_item_Order'; |
| 5 |
|
| 6 |
import { setBreakpointCssVars } from '../../helpers/util'; |
| 7 |
|
| 8 |
export type gridSpans = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; |
| 9 |
|
| 10 |
export interface GridItemProps extends React.HTMLProps<HTMLDivElement> { |
| 11 |
/** content rendered inside the Grid Layout Item */ |
| 12 |
children?: React.ReactNode; |
| 13 |
/** additional classes added to the Grid Layout Item */ |
| 14 |
className?: string; |
| 15 |
/** the number of columns the grid item spans. Value should be a number 1-12 */ |
| 16 |
span?: gridSpans; |
| 17 |
/** the number of rows the grid item spans. Value should be a number 1-12 */ |
| 18 |
rowSpan?: gridSpans; |
| 19 |
/** the number of columns a grid item is offset */ |
| 20 |
offset?: gridSpans; |
| 21 |
/** the number of columns the grid item spans on small device. Value should be a number 1-12 */ |
| 22 |
sm?: gridSpans; |
| 23 |
/** the number of rows the grid item spans on medium device. Value should be a number 1-12 */ |
| 24 |
smRowSpan?: gridSpans; |
| 25 |
/** the number of columns the grid item is offset on small device. Value should be a number 1-12 */ |
| 26 |
smOffset?: gridSpans; |
| 27 |
/** the number of columns the grid item spans on medium device. Value should be a number 1-12 */ |
| 28 |
md?: gridSpans; |
| 29 |
/** the number of rows the grid item spans on medium device. Value should be a number 1-12 */ |
| 30 |
mdRowSpan?: gridSpans; |
| 31 |
/** the number of columns the grid item is offset on medium device. Value should be a number 1-12 */ |
| 32 |
mdOffset?: gridSpans; |
| 33 |
/** the number of columns the grid item spans on large device. Value should be a number 1-12 */ |
| 34 |
lg?: gridSpans; |
| 35 |
/** the number of rows the grid item spans on large device. Value should be a number 1-12 */ |
| 36 |
lgRowSpan?: gridSpans; |
| 37 |
/** the number of columns the grid item is offset on large device. Value should be a number 1-12 */ |
| 38 |
lgOffset?: gridSpans; |
| 39 |
/** the number of columns the grid item spans on xLarge device. Value should be a number 1-12 */ |
| 40 |
xl?: gridSpans; |
| 41 |
/** the number of rows the grid item spans on large device. Value should be a number 1-12 */ |
| 42 |
xlRowSpan?: gridSpans; |
| 43 |
/** the number of columns the grid item is offset on xLarge device. Value should be a number 1-12 */ |
| 44 |
xlOffset?: gridSpans; |
| 45 |
/** the number of columns the grid item spans on 2xLarge device. Value should be a number 1-12 */ |
| 46 |
xl2?: gridSpans; |
| 47 |
/** the number of rows the grid item spans on 2xLarge device. Value should be a number 1-12 */ |
| 48 |
xl2RowSpan?: gridSpans; |
| 49 |
/** the number of columns the grid item is offset on 2xLarge device. Value should be a number 1-12 */ |
| 50 |
xl2Offset?: gridSpans; |
| 51 |
/** Modifies the flex layout element order property */ |
| 52 |
order?: { |
| 53 |
default?: string; |
| 54 |
md?: string; |
| 55 |
lg?: string; |
| 56 |
xl?: string; |
| 57 |
'2xl'?: string; |
| 58 |
}; |
| 59 |
/** Sets the base component to render. defaults to div */ |
| 60 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 61 |
} |
| 62 |
|
| 63 |
export const GridItem: React.FunctionComponent<GridItemProps> = ({ |
| 64 |
children = null, |
| 65 |
className = '', |
| 66 |
component = 'div', |
| 67 |
span = null, |
| 68 |
rowSpan = null, |
| 69 |
offset = null, |
| 70 |
order, |
| 71 |
style, |
| 72 |
...props |
| 73 |
}: GridItemProps) => { |
| 74 |
const classes = [ |
| 75 |
styles.gridItem, |
| 76 |
span && styles.modifiers[`${span}Col` as keyof typeof styles.modifiers], |
| 77 |
rowSpan && styles.modifiers[`${rowSpan}Row` as keyof typeof styles.modifiers], |
| 78 |
offset && styles.modifiers[`offset_${offset}Col` as keyof typeof styles.modifiers] |
| 79 |
]; |
| 80 |
const Component: any = component; |
| 81 |
|
| 82 |
Object.entries(DeviceSizes).forEach(([propKey, classModifier]) => { |
| 83 |
const key = propKey as keyof typeof DeviceSizes; |
| 84 |
const rowSpanKey = `${key}RowSpan` as 'smRowSpan' | 'mdRowSpan' | 'lgRowSpan' | 'xlRowSpan' | 'xl2RowSpan'; |
| 85 |
const offsetKey = `${key}Offset` as 'smOffset' | 'mdOffset' | 'lgOffset' | 'xlOffset' | 'xl2Offset'; |
| 86 |
|
| 87 |
const spanValue = props[key] as gridSpans; |
| 88 |
const rowSpanValue = props[rowSpanKey] as gridSpans; |
| 89 |
const offsetValue = props[offsetKey] as gridSpans; |
| 90 |
|
| 91 |
if (spanValue) { |
| 92 |
classes.push(styles.modifiers[`${spanValue}ColOn${classModifier}` as keyof typeof styles.modifiers]); |
| 93 |
} |
| 94 |
if (rowSpanValue) { |
| 95 |
classes.push(styles.modifiers[`${rowSpanValue}RowOn${classModifier}` as keyof typeof styles.modifiers]); |
| 96 |
} |
| 97 |
if (offsetValue) { |
| 98 |
classes.push(styles.modifiers[`offset_${offsetValue}ColOn${classModifier}` as keyof typeof styles.modifiers]); |
| 99 |
} |
| 100 |
|
| 101 |
delete props[key]; |
| 102 |
delete props[rowSpanKey]; |
| 103 |
delete props[offsetKey]; |
| 104 |
}); |
| 105 |
|
| 106 |
return ( |
| 107 |
<Component |
| 108 |
className={css(...classes, className)} |
| 109 |
style={ |
| 110 |
style || order ? { ...style, ...setBreakpointCssVars(order, gridToken.l_grid_item_Order.name) } : undefined |
| 111 |
} |
| 112 |
{...props} |
| 113 |
> |
| 114 |
{children} |
| 115 |
</Component> |
| 116 |
); |
| 117 |
}; |
| 118 |
GridItem.displayName = 'GridItem'; |