forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRow.jsx
113 lines (108 loc) · 3.15 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
import { inject, provide, reactive } from 'vue';
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
import ResponsiveObserve from '../_util/responsiveObserve';
const RowProps = {
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
type: PropTypes.oneOf(['flex']),
align: PropTypes.oneOf(['top', 'middle', 'bottom', 'stretch']),
justify: PropTypes.oneOf(['start', 'end', 'center', 'space-around', 'space-between']),
prefixCls: PropTypes.string,
};
const responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
export default {
name: 'ARow',
mixins: [BaseMixin],
props: {
...RowProps,
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]).def(0),
},
setup() {
const rowContext = reactive({
getGutter: undefined,
});
provide('rowContext', rowContext);
return {
configProvider: inject('configProvider', ConfigConsumerProps),
rowContext,
};
},
data() {
return {
screens: {},
};
},
created() {
this.rowContext.getGutter = this.getGutter;
},
mounted() {
this.$nextTick(() => {
this.token = ResponsiveObserve.subscribe(screens => {
const { gutter } = this;
if (
typeof gutter === 'object' ||
(Array.isArray(gutter) &&
(typeof gutter[0] === 'object' || typeof gutter[1] === 'object'))
) {
this.screens = screens;
}
});
});
},
beforeUnmount() {
ResponsiveObserve.unsubscribe(this.token);
},
methods: {
getGutter() {
const results = [0, 0];
const { gutter, screens } = this;
const normalizedGutter = Array.isArray(gutter) ? gutter : [gutter, 0];
normalizedGutter.forEach((g, index) => {
if (typeof g === 'object') {
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint = responsiveArray[i];
if (screens[breakpoint] && g[breakpoint] !== undefined) {
results[index] = g[breakpoint];
break;
}
}
} else {
results[index] = g || 0;
}
});
return results;
},
},
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] > 0
? {
marginLeft: `${gutter[0] / -2}px`,
marginRight: `${gutter[0] / -2}px`,
}
: {}),
...(gutter[1] > 0
? {
marginTop: `${gutter[1] / -2}px`,
marginBottom: `${gutter[1] / -2}px`,
}
: {}),
};
return (
<div class={classes} style={rowStyle}>
{$slots.default && $slots.default()}
</div>
);
},
};