1
1
import PropTypes from '../_util/vue-types' ;
2
2
import BaseMixin from '../_util/BaseMixin' ;
3
3
import { ConfigConsumerProps } from '../config-provider' ;
4
-
5
- // matchMedia polyfill for
6
- // https://github.com/WickyNilliams/enquire.js/issues/82
7
- let enquire = null ;
8
- if ( typeof window !== 'undefined' ) {
9
- const matchMediaPolyfill = mediaQuery => {
10
- return {
11
- media : mediaQuery ,
12
- matches : false ,
13
- addListener ( ) { } ,
14
- removeListener ( ) { } ,
15
- } ;
16
- } ;
17
- window . matchMedia = window . matchMedia || matchMediaPolyfill ;
18
- enquire = require ( 'enquire.js' ) ;
19
- }
4
+ import ResponsiveObserve from '../_util/responsiveObserve' ;
20
5
21
6
const BreakpointMap = PropTypes . shape ( {
22
7
xs : PropTypes . number ,
@@ -28,30 +13,21 @@ const BreakpointMap = PropTypes.shape({
28
13
} ) . loose ;
29
14
30
15
const RowProps = {
31
- gutter : PropTypes . oneOfType ( [ PropTypes . number , BreakpointMap ] ) ,
16
+ gutter : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . number , PropTypes . array ] ) ,
32
17
type : PropTypes . oneOf ( [ 'flex' ] ) ,
33
- align : PropTypes . oneOf ( [ 'top' , 'middle' , 'bottom' ] ) ,
18
+ align : PropTypes . oneOf ( [ 'top' , 'middle' , 'bottom' , 'stretch' ] ) ,
34
19
justify : PropTypes . oneOf ( [ 'start' , 'end' , 'center' , 'space-around' , 'space-between' ] ) ,
35
20
prefixCls : PropTypes . string ,
36
21
} ;
37
22
38
23
const responsiveArray = [ 'xxl' , 'xl' , 'lg' , 'md' , 'sm' , 'xs' ] ;
39
24
40
- const responsiveMap = {
41
- xs : '(max-width: 575px)' ,
42
- sm : '(min-width: 576px)' ,
43
- md : '(min-width: 768px)' ,
44
- lg : '(min-width: 992px)' ,
45
- xl : '(min-width: 1200px)' ,
46
- xxl : '(min-width: 1600px)' ,
47
- } ;
48
-
49
25
export default {
50
26
name : 'ARow' ,
51
27
mixins : [ BaseMixin ] ,
52
28
props : {
53
29
...RowProps ,
54
- gutter : PropTypes . oneOfType ( [ PropTypes . number , BreakpointMap ] ) . def ( 0 ) ,
30
+ gutter : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . number , PropTypes . array ] ) . def ( 0 ) ,
55
31
} ,
56
32
provide ( ) {
57
33
return {
@@ -69,51 +45,40 @@ export default {
69
45
70
46
mounted ( ) {
71
47
this . $nextTick ( ( ) => {
72
- Object . keys ( responsiveMap ) . map ( screen =>
73
- enquire . register ( responsiveMap [ screen ] , {
74
- match : ( ) => {
75
- if ( typeof this . gutter !== 'object' ) {
76
- return ;
77
- }
78
- this . setState ( prevState => ( {
79
- screens : {
80
- ...prevState . screens ,
81
- [ screen ] : true ,
82
- } ,
83
- } ) ) ;
84
- } ,
85
- unmatch : ( ) => {
86
- if ( typeof this . gutter !== 'object' ) {
87
- return ;
88
- }
89
- this . setState ( prevState => ( {
90
- screens : {
91
- ...prevState . screens ,
92
- [ screen ] : false ,
93
- } ,
94
- } ) ) ;
95
- } ,
96
- // Keep a empty destory to avoid triggering unmatch when unregister
97
- destroy ( ) { } ,
98
- } ) ,
99
- ) ;
48
+ this . token = ResponsiveObserve . subscribe ( screens => {
49
+ const { gutter } = this ;
50
+ if (
51
+ typeof gutter === 'object' ||
52
+ ( Array . isArray ( gutter ) &&
53
+ ( typeof gutter [ 0 ] === 'object' || typeof gutter [ 1 ] === 'object' ) )
54
+ ) {
55
+ this . screens = screens ;
56
+ }
57
+ } ) ;
100
58
} ) ;
101
59
} ,
102
60
beforeDestroy ( ) {
103
- Object . keys ( responsiveMap ) . map ( screen => enquire . unregister ( responsiveMap [ screen ] ) ) ;
61
+ ResponsiveObserve . unsubscribe ( this . token ) ;
104
62
} ,
105
63
methods : {
106
64
getGutter ( ) {
107
- const { gutter } = this ;
108
- if ( typeof gutter === 'object' ) {
109
- for ( let i = 0 ; i < responsiveArray . length ; i ++ ) {
110
- const breakpoint = responsiveArray [ i ] ;
111
- if ( this . screens [ breakpoint ] && gutter [ breakpoint ] !== undefined ) {
112
- return gutter [ breakpoint ] ;
65
+ const results = [ 0 , 0 ] ;
66
+ const { gutter, screens } = this ;
67
+ const normalizedGutter = Array . isArray ( gutter ) ? gutter : [ gutter , 0 ] ;
68
+ normalizedGutter . forEach ( ( g , index ) => {
69
+ if ( typeof g === 'object' ) {
70
+ for ( let i = 0 ; i < responsiveArray . length ; i ++ ) {
71
+ const breakpoint = responsiveArray [ i ] ;
72
+ if ( screens [ breakpoint ] && g [ breakpoint ] !== undefined ) {
73
+ results [ index ] = g [ breakpoint ] ;
74
+ break ;
75
+ }
113
76
}
77
+ } else {
78
+ results [ index ] = g || 0 ;
114
79
}
115
- }
116
- return gutter ;
80
+ } ) ;
81
+ return results ;
117
82
} ,
118
83
} ,
119
84
@@ -129,13 +94,20 @@ export default {
129
94
[ `${ prefixCls } -${ type } -${ justify } ` ] : type && justify ,
130
95
[ `${ prefixCls } -${ type } -${ align } ` ] : type && align ,
131
96
} ;
132
- const rowStyle =
133
- gutter > 0
97
+ const rowStyle = {
98
+ ... ( gutter [ 0 ] > 0
134
99
? {
135
- marginLeft : `${ gutter / - 2 } px` ,
136
- marginRight : `${ gutter / - 2 } px` ,
100
+ marginLeft : `${ gutter [ 0 ] / - 2 } px` ,
101
+ marginRight : `${ gutter [ 0 ] / - 2 } px` ,
137
102
}
138
- : { } ;
103
+ : { } ) ,
104
+ ...( gutter [ 1 ] > 0
105
+ ? {
106
+ marginTop : `${ gutter [ 1 ] / - 2 } px` ,
107
+ marginBottom : `${ gutter [ 1 ] / - 2 } px` ,
108
+ }
109
+ : { } ) ,
110
+ } ;
139
111
return (
140
112
< div class = { classes } style = { rowStyle } >
141
113
{ $slots . default }
0 commit comments