From d7426fe3c94c7b3c665bd347b49668f8352cf77b Mon Sep 17 00:00:00 2001 From: Samuel Valdes Gutierrez Date: Fri, 27 Oct 2023 13:52:47 +0100 Subject: [PATCH] adding shrink and basis props for OuiFlexItem Signed-off-by: Samuel Valdes Gutierrez --- src/components/flex/_flex_item.scss | 28 +++++++++++-- src/components/flex/flex_item.tsx | 63 ++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/components/flex/_flex_item.scss b/src/components/flex/_flex_item.scss index 49cd501345..d9e0a1c4a6 100644 --- a/src/components/flex/_flex_item.scss +++ b/src/components/flex/_flex_item.scss @@ -24,18 +24,38 @@ /* * 1. We need the extra specificity here to override the FlexGroup > FlexItem styles. - * 2. FlexItem can be manually set to not grow if needed. + * 2. FlexItem can be manually set to not grow if needed (default). + * 3. FlexItem also can be given values for grow, shrink and basis: + * - grow: 1..10 + * - shrink: 1..10 + * - basis: auto, 0%, 25%, 50%, 75% or 100% */ + &.ouiFlexItem--flexGrowZero { /* 1 */ flex-grow: 0; /* 2 */ flex-basis: auto; /* 2 */ } - @for $i from 1 through 10 { - &.ouiFlexItem--flexGrow#{$i} { - flex-grow: $i; + @for $i from 1 through 10 { /* 3 */ + &.ouiFlexItem--flexGrow#{$i} { /* 3 */ + flex-grow: $i; /* 3 */ } } + + @for $i from 1 through 10 { /* 3 */ + &.ouiFlexItem--flexShrink#{$i} { /* 3 */ + flex-shrink: $i; /* 3 */ + } + } + + $flexBasisOptions: 'auto', '0%', '25%', '50%', '75%', '100%'; /* 3 */ + + @each $basis in $flexBasisOptions { /* 3 */ + .ouiFlexItem--flexBasis#{$basis} { /* 3 */ + flex-basis: $basis; /* 3 */ + } + } + } // On mobile we force them to stack and act the same. diff --git a/src/components/flex/flex_item.tsx b/src/components/flex/flex_item.tsx index 9d558c01a0..f77a4d2d6a 100644 --- a/src/components/flex/flex_item.tsx +++ b/src/components/flex/flex_item.tsx @@ -46,13 +46,43 @@ export type FlexItemGrowSize = | true | false | null; +export type FlexItemShrinkSize = + | 1 + | 2 + | 3 + | 4 + | 5 + | 6 + | 7 + | 8 + | 9 + | 10 + | true + | false + | null; +export type FlexItemBasisValue = string | true | false | null; export interface OuiFlexItemProps { grow?: FlexItemGrowSize; + shrink?: FlexItemShrinkSize; + basis?: FlexItemBasisValue; component?: keyof JSX.IntrinsicElements; } export const GROW_SIZES: FlexItemGrowSize[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +export const SHRINK_SIZES: FlexItemShrinkSize[] = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, +]; +export const BASIS_VALUES:FlexItemBasisValue[] = ['auto','0%','25%','50%','75%','100%'] export const OuiFlexItem: FunctionComponent< CommonProps & @@ -61,11 +91,15 @@ export const OuiFlexItem: FunctionComponent< > = ({ children, className, - grow = true, + grow = true, // default true -> keep grow 1 coming from flex_grid + shrink = 1, // default 1 for shrink + basis = 'auto', // default 'auto' basis component: Component = 'div', ...rest }) => { validateGrowValue(grow); + validateShrinkValue(shrink); + validateBasisValue(basis); const classes = classNames( 'ouiFlexItem', @@ -73,6 +107,10 @@ export const OuiFlexItem: FunctionComponent< 'ouiFlexItem--flexGrowZero': !grow, [`ouiFlexItem--flexGrow${grow}`]: typeof grow === 'number' ? GROW_SIZES.indexOf(grow) >= 0 : undefined, + [`ouiFlexItem--flexShrink${shrink}`]: + typeof shrink === 'number' ? SHRINK_SIZES.indexOf(shrink) >= 0 : undefined, + [`ouiFlexItem--flexBasis${basis}`]: + typeof shrink === 'string' ? BASIS_VALUES.indexOf(shrink) >= 0 : undefined, }, className ); @@ -94,3 +132,26 @@ function validateGrowValue(value: OuiFlexItemProps['grow']) { ); } } + +function validateShrinkValue(value: OuiFlexItemProps['shrink']) { + // New function + const validValues = [null, undefined, true, false, ...SHRINK_SIZES]; + + if (validValues.indexOf(value) === -1) { + throw new Error( + `Prop \`shrink\` passed to \`OuiFlexItem\` must be a boolean or an integer between 1 and 10, received \`${value}\`` + ); + } +} + +function validateBasisValue(value: OuiFlexItemProps['basis']) { + // Define the valid values for 'flex-basis'. These can be 'auto' or specific percentages. + const validValues = [null, undefined, true, false, ...BASIS_VALUES]; + + // Check if the passed value is one of the valid values. + if (!validValues.includes(value)) { + throw new Error( + `Prop \`basis\` passed to \`OuiFlexItem\` must be one of ['auto', '0%', '25%', '50%', '75%', '100%'], received \`${value}\`` + ); + } +}