Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 942939e

Browse files
somombotinayuangao
authored andcommitted
fix(fxFlex): prevent setting min/max-size when grow/shrink is zero (#160)
handle various scenarios of grow/shrink being zero: * set `min-width`/`min-height` only if `grow != 0` * set `max-width`/`max-height` only if `shink != 0` * set both min and max if both `grow == 0` AND `shink == 0` * add tests for these scenarios * remove max-width from test "should set a min-width when the shrink == 0" fixes #153 Signed-off-by: Somo <somo@mombo.solutions>
1 parent f57a63d commit 942939e

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/lib/flexbox/api/flex.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,52 @@ describe('flex directive', () => {
151151
it('should set a min-width when the shrink == 0', () => {
152152
expectDOMFrom(`<div fxFlex="1 0 37px"></div>`).toHaveCssStyle({
153153
'flex': '1 0 37px',
154-
'max-width': '37px',
155154
'min-width': '37px',
156155
'box-sizing': 'border-box',
157156
});
158157
});
158+
it('should set a min-width and max-width when the grow == 0 and shrink == 0', () => {
159+
expectDOMFrom(`<div fxFlex="0 0 375px"></div>`).toHaveCssStyle({
160+
'flex': '0 0 375px',
161+
'max-width': '375px',
162+
'min-width': '375px',
163+
'box-sizing': 'border-box',
164+
});
165+
});
166+
167+
168+
it('should not set max-width to 69px when fxFlex="1 0 69px"', () => {
169+
expectDOMFrom(`<div fxFlex="1 0 69px"></div>`).not.toHaveCssStyle({
170+
'max-width': '69px',
171+
});
172+
});
173+
174+
it('should not set a max-width when the shrink == 0', () => {
175+
let fRef = componentWithTemplate(`<div fxFlex="1 0 303px"></div>`);
176+
fRef.detectChanges();
177+
178+
let dom = fRef.debugElement.children[0].nativeElement;
179+
let maxWidthStyle = getDOM().getStyle(dom, 'max-width');
180+
181+
expect(maxWidthStyle).toBeFalsy();
182+
});
183+
184+
it('should not set min-width to 96px when fxFlex="0 1 96px"', () => {
185+
expectDOMFrom(`<div fxFlex="0 1 96px"></div>`).not.toHaveCssStyle({
186+
'min-width': '96px',
187+
});
188+
});
189+
190+
it('should not set a min-width when the grow == 0', () => {
191+
let fRef = componentWithTemplate(`<div fxFlex="0 1 313px"></div>`);
192+
fRef.detectChanges();
193+
194+
let dom = fRef.debugElement.children[0].nativeElement;
195+
let minWidthStyle = getDOM().getStyle(dom, 'min-width');
196+
197+
expect(minWidthStyle).toBeFalsy();
198+
});
199+
159200
it('should set a min-width when basis is a Px value', () => {
160201
expectDOMFrom(`<div fxFlex="312px"></div>`).toHaveCssStyle({
161202
'flex': '1 1 312px',

src/lib/flexbox/api/flex.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
226226
'column' :
227227
'row';
228228

229+
if ( grow == "0" ) {
230+
grow = 0;
231+
}
232+
233+
if ( shrink == "0" ) {
234+
shrink = 0;
235+
}
236+
229237
// flex-basis allows you to specify the initial/starting main-axis size of the element,
230238
// before anything else is computed. It can either be a percentage or an absolute value.
231239
// It is, however, not the breaking point for flex-grow/shrink properties
@@ -250,6 +258,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
250258
break;
251259
case 'initial': // default
252260
case 'nogrow':
261+
grow = 0;
253262
css = extendObject(clearStyles, {'flex': '0 1 auto'});
254263
break;
255264
case 'grow':
@@ -263,6 +272,7 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
263272
css = extendObject(clearStyles, {'flex': `${grow} ${shrink} auto`});
264273
break;
265274
case 'none':
275+
grow = 0;
266276
shrink = 0;
267277
css = extendObject(clearStyles, {'flex': '0 0 auto'});
268278
break;
@@ -300,8 +310,13 @@ export class FlexDirective extends BaseFxDirective implements OnInit, OnChanges,
300310
let usingCalc = String(basis).indexOf('calc') > -1;
301311
let isPx = String(basis).indexOf('px') > -1 || usingCalc;
302312

303-
css[min] = (basis == '0%') ? 0 : isPx ? basis : null;
304-
css[max] = (basis == '0%') ? 0 : usingCalc ? null : basis;
313+
314+
// make box inflexible when shrink and grow are both zero
315+
// should not set a min when the grow is zero
316+
// should not set a max when the shrink is zero
317+
let isFixed = !grow && !shrink;
318+
css[min] = (basis == '0%') ? 0 : isFixed || (isPx && grow) ? basis : null;
319+
css[max] = (basis == '0%') ? 0 : isFixed || (!usingCalc && shrink) ? basis : null;
305320

306321
return extendObject(css, {'box-sizing': 'border-box'});
307322
}

0 commit comments

Comments
 (0)