2018-03-19 10:16:27 +08:00
|
|
|
|
2018-03-08 13:55:49 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
|
|
|
import { cloneElement } from '../_util/vnode'
|
|
|
|
import { isEmptyElement, getStyle, getOptionProps } from '../_util/props-util'
|
|
|
|
// 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 () {
|
2017-10-27 13:24:22 +08:00
|
|
|
},
|
2018-03-08 13:55:49 +08:00
|
|
|
removeListener () {
|
2017-10-27 13:24:22 +08:00
|
|
|
},
|
2018-03-08 13:55:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.matchMedia = window.matchMedia || matchMediaPolyfill
|
|
|
|
enquire = require('enquire.js')
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2018-03-08 13:55:49 +08:00
|
|
|
}).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 {
|
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),
|
|
|
|
},
|
|
|
|
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 () {},
|
2017-10-27 13:24:22 +08:00
|
|
|
},
|
2018-03-08 13:55:49 +08:00
|
|
|
))
|
|
|
|
})
|
|
|
|
},
|
|
|
|
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]
|
2018-08-08 10:13:28 +08:00
|
|
|
if (this.screens[breakpoint] && gutter[breakpoint] !== undefined) {
|
2018-03-08 13:55:49 +08:00
|
|
|
return gutter[breakpoint]
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 13:24:22 +08:00
|
|
|
}
|
2018-03-08 13:55:49 +08:00
|
|
|
return gutter
|
2017-10-27 13:24:22 +08:00
|
|
|
},
|
2018-03-08 13:55:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
type, justify, align,
|
|
|
|
prefixCls = 'ant-row', $slots,
|
|
|
|
} = this
|
|
|
|
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`,
|
|
|
|
} : {}
|
|
|
|
const cols = ($slots.default || []).map((col) => {
|
|
|
|
if (isEmptyElement(col)) {
|
|
|
|
return null
|
2017-11-07 14:41:41 +08:00
|
|
|
}
|
2018-03-08 13:55:49 +08:00
|
|
|
if (getOptionProps(col) && gutter > 0) {
|
|
|
|
return cloneElement(col, {
|
|
|
|
style: {
|
|
|
|
paddingLeft: `${gutter / 2}px`,
|
|
|
|
paddingRight: `${gutter / 2}px`,
|
|
|
|
...getStyle(col, true),
|
|
|
|
},
|
|
|
|
})
|
2017-10-27 13:24:22 +08:00
|
|
|
}
|
2018-03-08 13:55:49 +08:00
|
|
|
return col
|
|
|
|
})
|
|
|
|
return <div class={classes} style={rowStyle}>{cols}</div>
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|