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