ant-design-vue/components/grid/Row.jsx

146 lines
3.7 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
2019-03-15 15:12:28 +08:00
import { ConfigConsumerProps } from '../config-provider';
2018-03-08 13:55:49 +08:00
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
2019-01-12 11:33:27 +08:00
let enquire = null;
2018-03-08 13:55:49 +08:00
if (typeof window !== 'undefined') {
2019-01-12 11:33:27 +08:00
const matchMediaPolyfill = mediaQuery => {
2018-03-08 13:55:49 +08:00
return {
media: mediaQuery,
matches: false,
2019-01-12 11:33:27 +08:00
addListener() {},
removeListener() {},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
enquire = require('enquire.js');
2018-03-08 13:55:49 +08:00
}
const BreakpointMap = PropTypes.shape({
2018-08-08 10:13:28 +08:00
xs: PropTypes.number,
sm: PropTypes.number,
md: PropTypes.number,
lg: PropTypes.number,
xl: PropTypes.number,
xxl: PropTypes.number,
2019-01-12 11:33:27 +08:00
}).loose;
2018-03-08 13:55:49 +08:00
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,
2019-01-12 11:33:27 +08:00
};
2018-03-08 13:55:49 +08:00
2019-01-12 11:33:27 +08:00
const responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
2018-03-08 13:55:49 +08:00
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)',
2019-01-12 11:33:27 +08:00
};
2018-03-08 13:55:49 +08:00
export default {
2018-04-08 21:17:20 +08:00
name: 'ARow',
2018-03-08 13:55:49 +08:00
mixins: [BaseMixin],
props: {
...RowProps,
gutter: PropTypes.oneOfType([PropTypes.number, BreakpointMap]).def(0),
},
2019-01-12 11:33:27 +08:00
provide() {
2018-12-09 10:58:50 +08:00
return {
rowContext: this,
2019-01-12 11:33:27 +08:00
};
2018-12-09 10:58:50 +08:00
},
2019-03-15 15:12:28 +08:00
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2019-03-15 15:12:28 +08:00
},
2019-01-12 11:33:27 +08:00
data() {
2018-03-08 13:55:49 +08:00
return {
screens: {},
2019-01-12 11:33:27 +08:00
};
2018-03-08 13:55:49 +08:00
},
2019-01-12 11:33:27 +08:00
mounted() {
2018-03-08 13:55:49 +08:00
this.$nextTick(() => {
2019-01-12 11:33:27 +08:00
Object.keys(responsiveMap).map(screen =>
enquire.register(responsiveMap[screen], {
2018-03-08 13:55:49 +08:00
match: () => {
if (typeof this.gutter !== 'object') {
2019-01-12 11:33:27 +08:00
return;
2018-03-08 13:55:49 +08:00
}
2019-01-12 11:33:27 +08:00
this.setState(prevState => ({
2018-03-08 13:55:49 +08:00
screens: {
...prevState.screens,
[screen]: true,
},
2019-01-12 11:33:27 +08:00
}));
2018-03-08 13:55:49 +08:00
},
unmatch: () => {
if (typeof this.gutter !== 'object') {
2019-01-12 11:33:27 +08:00
return;
2018-03-08 13:55:49 +08:00
}
2019-01-12 11:33:27 +08:00
this.setState(prevState => ({
2018-03-08 13:55:49 +08:00
screens: {
...prevState.screens,
[screen]: false,
},
2019-01-12 11:33:27 +08:00
}));
2018-03-08 13:55:49 +08:00
},
// Keep a empty destory to avoid triggering unmatch when unregister
2019-01-12 11:33:27 +08:00
destroy() {},
}),
);
});
2018-03-08 13:55:49 +08:00
},
2019-01-12 11:33:27 +08:00
beforeDestroy() {
Object.keys(responsiveMap).map(screen => enquire.unregister(responsiveMap[screen]));
2018-03-08 13:55:49 +08:00
},
methods: {
2019-01-12 11:33:27 +08:00
getGutter() {
const { gutter } = this;
2018-03-08 13:55:49 +08:00
if (typeof gutter === 'object') {
2019-01-03 20:51:56 +08:00
for (let i = 0; i < responsiveArray.length; i++) {
2019-01-12 11:33:27 +08:00
const breakpoint = responsiveArray[i];
2018-08-08 10:13:28 +08:00
if (this.screens[breakpoint] && gutter[breakpoint] !== undefined) {
2019-01-12 11:33:27 +08:00
return gutter[breakpoint];
2018-03-08 13:55:49 +08:00
}
}
}
2019-01-12 11:33:27 +08:00
return gutter;
},
2018-03-08 13:55:49 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
2019-03-15 15:12:28 +08:00
const { type, justify, align, prefixCls: customizePrefixCls, $slots } = this;
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-03-15 15:12:28 +08:00
const prefixCls = getPrefixCls('row', customizePrefixCls);
2019-01-12 11:33:27 +08:00
const gutter = this.getGutter();
2018-03-08 13:55:49 +08:00
const classes = {
[prefixCls]: !type,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${type}-${justify}`]: type && justify,
[`${prefixCls}-${type}-${align}`]: type && align,
2019-01-12 11:33:27 +08:00
};
const rowStyle =
gutter > 0
? {
marginLeft: `${gutter / -2}px`,
marginRight: `${gutter / -2}px`,
}
: {};
return (
<div class={classes} style={rowStyle}>
{$slots.default}
</div>
);
2018-03-08 13:55:49 +08:00
},
2019-01-12 11:33:27 +08:00
};