diff --git a/build/config.js b/build/config.js index e30a733bf..7ff43da6b 100644 --- a/build/config.js +++ b/build/config.js @@ -1,5 +1,5 @@ module.exports = { dev: { - componentName: 'spin', // dev components + componentName: 'grid', // dev components }, }; diff --git a/components/grid/Col.jsx b/components/grid/Col.jsx index 0f9b125d3..06520e922 100644 --- a/components/grid/Col.jsx +++ b/components/grid/Col.jsx @@ -55,10 +55,11 @@ export default { let sizeClassObj = {}; ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => { let sizeProps = {}; - if (typeof this[size] === 'number') { - sizeProps.span = this[size]; - } else if (typeof this[size] === 'object') { - sizeProps = this[size] || {}; + const propSize = this[size]; + if (typeof propSize === 'number') { + sizeProps.span = propSize; + } else if (typeof propSize === 'object') { + sizeProps = propSize || {}; } sizeClassObj = { @@ -72,6 +73,7 @@ export default { }; }); const classes = { + [`${prefixCls}`]: true, [`${prefixCls}-${span}`]: span !== undefined, [`${prefixCls}-order-${order}`]: order, [`${prefixCls}-offset-${offset}`]: offset, @@ -86,10 +88,20 @@ export default { }; if (rowContext) { const gutter = rowContext.getGutter(); - if (gutter > 0) { + if (gutter) { divProps.style = { - paddingLeft: `${gutter / 2}px`, - paddingRight: `${gutter / 2}px`, + ...(gutter[0] > 0 + ? { + paddingLeft: `${gutter[0] / 2}px`, + paddingRight: `${gutter[0] / 2}px`, + } + : {}), + ...(gutter[1] > 0 + ? { + paddingTop: `${gutter[1] / 2}px`, + paddingBottom: `${gutter[1] / 2}px`, + } + : {}), }; } } diff --git a/components/grid/Row.jsx b/components/grid/Row.jsx index e72b649b0..2d48fc7b4 100644 --- a/components/grid/Row.jsx +++ b/components/grid/Row.jsx @@ -1,22 +1,7 @@ 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'); -} +import ResponsiveObserve from '../_util/responsiveObserve'; const BreakpointMap = PropTypes.shape({ xs: PropTypes.number, @@ -28,30 +13,21 @@ const BreakpointMap = PropTypes.shape({ }).loose; const RowProps = { - gutter: PropTypes.oneOfType([PropTypes.number, BreakpointMap]), + gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]), type: PropTypes.oneOf(['flex']), - align: PropTypes.oneOf(['top', 'middle', 'bottom']), + 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']; -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), + gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]).def(0), }, provide() { return { @@ -69,51 +45,40 @@ export default { 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() {}, - }), - ); + 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; + } + }); }); }, beforeDestroy() { - Object.keys(responsiveMap).map(screen => enquire.unregister(responsiveMap[screen])); + ResponsiveObserve.unsubscribe(this.token); }, 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]; + 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 gutter; + }); + return results; }, }, @@ -129,13 +94,20 @@ export default { [`${prefixCls}-${type}-${justify}`]: type && justify, [`${prefixCls}-${type}-${align}`]: type && align, }; - const rowStyle = - gutter > 0 + const rowStyle = { + ...(gutter[0] > 0 ? { - marginLeft: `${gutter / -2}px`, - marginRight: `${gutter / -2}px`, + marginLeft: `${gutter[0] / -2}px`, + marginRight: `${gutter[0] / -2}px`, } - : {}; + : {}), + ...(gutter[1] > 0 + ? { + marginTop: `${gutter[1] / -2}px`, + marginBottom: `${gutter[1] / -2}px`, + } + : {}), + }; return (
{$slots.default} diff --git a/components/grid/__tests__/__snapshots__/demo.test.js.snap b/components/grid/__tests__/__snapshots__/demo.test.js.snap index 7b5541965..a212657bb 100644 --- a/components/grid/__tests__/__snapshots__/demo.test.js.snap +++ b/components/grid/__tests__/__snapshots__/demo.test.js.snap @@ -3,19 +3,19 @@ exports[`renders ./components/grid/demo/basic.md correctly 1`] = `
-
col-12
-
col-12
+
col-12
+
col-12
-
col-8
-
col-8
-
col-8
+
col-8
+
col-8
+
col-8
-
col-6
-
col-6
-
col-6
-
col-6
+
col-6
+
col-6
+
col-6
+
col-6
`; @@ -24,38 +24,38 @@ exports[`renders ./components/grid/demo/flex.md correctly 1`] = `

sub-element align left

-
col-4
-
col-4
-
col-4
-
col-4
+
col-4
+
col-4
+
col-4
+
col-4

sub-element align center

-
col-4
-
col-4
-
col-4
-
col-4
+
col-4
+
col-4
+
col-4
+
col-4

sub-element align right

-
col-4
-
col-4
-
col-4
-
col-4
+
col-4
+
col-4
+
col-4
+
col-4

sub-element monospaced arrangement

-
col-4
-
col-4
-
col-4
-
col-4
+
col-4
+
col-4
+
col-4
+
col-4

sub-element align full

-
col-4
-
col-4
-
col-4
-
col-4
+
col-4
+
col-4
+
col-4
+
col-4
`; @@ -64,46 +64,46 @@ exports[`renders ./components/grid/demo/flex-align.md correctly 1`] = `

Align Top

-
+

col-4

-
+

col-4

-
+

col-4

-
+

col-4

Align Center

-
+

col-4

-
+

col-4

-
+

col-4

-
+

col-4

Align Bottom

-
+

col-4

-
+

col-4

-
+

col-4

-
+

col-4

@@ -113,10 +113,10 @@ exports[`renders ./components/grid/demo/flex-align.md correctly 1`] = ` exports[`renders ./components/grid/demo/flex-order.md correctly 1`] = `
-
1 col-order-4
-
2 col-order-3
-
3 col-order-2
-
4 col-order-1
+
1 col-order-4
+
2 col-order-3
+
3 col-order-2
+
4 col-order-1
`; @@ -124,16 +124,16 @@ exports[`renders ./components/grid/demo/flex-order.md correctly 1`] = ` exports[`renders ./components/grid/demo/gutter.md correctly 1`] = `
-
+
col-6
-
+
col-6
-
+
col-6
-
+
col-6
@@ -143,22 +143,31 @@ exports[`renders ./components/grid/demo/gutter.md correctly 1`] = ` exports[`renders ./components/grid/demo/offset.md correctly 1`] = `
-
col-8
-
col-8
+
col-8
+
col-8
-
col-6 col-offset-6
-
col-6 col-offset-6
+
col-6 col-offset-6
+
col-6 col-offset-6
-
col-12 col-offset-6
+
col-12 col-offset-6
`; exports[`renders ./components/grid/demo/playfround.md correctly 1`] = `
-
Gutter (px): +
Horizontal Gutter (px): +
+
+
+
+
+
+
81624324048
+
+
Vertical Gutter (px):
@@ -178,49 +187,68 @@ exports[`renders ./components/grid/demo/playfround.md correctly 1`] = `
-
-
+
+
Column
-
+
Column
-
+
Column
-
+
Column
-
<Row :gutter="16">
-  <Col :span="6"/>
-  <Col :span="6"/>
-  <Col :span="6"/>
-  <Col :span="6"/>
-</Row>
+
+
+
+
Column
+
+
+
Column
+
+
+
Column
+
+
+
Column
+
+
<a-row :gutter="[16,16]">
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+</a-row>
<a-row :gutter="[16,16]">
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+  <a-col :span="6"/>
+</a-row>
`; exports[`renders ./components/grid/demo/responsive.md correctly 1`] = `
-
Col
-
Col
-
Col
+
Col
+
Col
+
Col
`; exports[`renders ./components/grid/demo/responsive-more.md correctly 1`] = `
-
Col
-
Col
-
Col
+
Col
+
Col
+
Col
`; exports[`renders ./components/grid/demo/sort.md correctly 1`] = `
-
col-18 col-push-6
-
col-6 col-pull-18
+
col-18 col-push-6
+
col-6 col-pull-18
`; diff --git a/components/grid/__tests__/__snapshots__/index.test.js.snap b/components/grid/__tests__/__snapshots__/index.test.js.snap index ada8f278b..d0976028f 100644 --- a/components/grid/__tests__/__snapshots__/index.test.js.snap +++ b/components/grid/__tests__/__snapshots__/index.test.js.snap @@ -3,12 +3,12 @@ exports[`Grid renders wrapped Col correctly 1`] = `
-
+
-
+
`; -exports[`Grid should render Col 1`] = `
`; +exports[`Grid should render Col 1`] = `
`; exports[`Grid should render Row 1`] = `
`; diff --git a/components/grid/__tests__/index.test.js b/components/grid/__tests__/index.test.js index f1ea5b24d..f32a8f832 100644 --- a/components/grid/__tests__/index.test.js +++ b/components/grid/__tests__/index.test.js @@ -1,7 +1,10 @@ import { mount } from '@vue/test-utils'; import { Col, Row } from '..'; +import mountTest from '../../../tests/shared/mountTest'; describe('Grid', () => { + mountTest(Row); + mountTest(Col); it('should render Col', () => { const wrapper = mount(Col, { propsData: { diff --git a/components/grid/demo/gutter.md b/components/grid/demo/gutter.md index d38d4e29a..4bd4fb30f 100644 --- a/components/grid/demo/gutter.md +++ b/components/grid/demo/gutter.md @@ -2,12 +2,16 @@ #### 区块间隔 栅格常常需要和间隔进行配合,你可以使用 `Row` 的 `gutter` 属性,我们推荐使用 `(16+8n)px` 作为栅格间隔。(n 是自然数) 如果要支持响应式,可以写成 `{ xs: 8, sm: 16, md: 24, lg: 32 }`。 +如果需要垂直间距,可以写成数组形式 `[水平间距, 垂直间距]` `[16, { xs: 8, sm: 16, md: 24, lg: 32 }]`。 +> 数组形式垂直间距在 `1.5.0` 之后支持。 #### Grid Gutter You can use the `gutter` property of `Row` as grid spacing, we recommend set it to `(16 + 8n) px`. (`n` stands for natural number.) You can set it to a object like `{ xs: 8, sm: 16, md: 24, lg: 32 }` for responsive design. +You can use a array to set vertical spacing, `[horizontal, vertical]` `[16, { xs: 8, sm: 16, md: 24, lg: 32 }]`. +> vertical gutter was supported after `1.5.0`. ```tpl diff --git a/components/grid/demo/index.vue b/components/grid/demo/index.vue index e011c0e37..fefeca913 100644 --- a/components/grid/demo/index.vue +++ b/components/grid/demo/index.vue @@ -22,13 +22,13 @@ const md = { }; const md2 = { cn: ` - 在多数业务情况下,Ant Design需要在设计区域内解决大量信息收纳的问题,因此在 12 栅格系统的基础上,我们将整个设计建议区域按照 24 等分的原则进行划分。 + 在多数业务情况下,Ant Design Vue 需要在设计区域内解决大量信息收纳的问题,因此在 12 栅格系统的基础上,我们将整个设计建议区域按照 24 等分的原则进行划分。 划分之后的信息区块我们称之为『盒子』。建议横向排列的盒子数量最多四个,最少一个。『盒子』在整个屏幕上占比见上图。设计部分基于盒子的单位定制盒子内部的排版规则,以保证视觉层面的舒适感。 ## 概述 布局的栅格化系统,我们是基于行(row)和列(col)来定义信息区块的外部框架,以保证页面的每个区域能够稳健地排布起来。下面简单介绍一下它的工作原理: - 通过\`row\`在水平方向建立一组\`column\`(简写col) - 你的内容应当放置于\`col\`内,并且,只有\`col\`可以作为\`row\`的直接元素 -- 栅格系统中的列是指1到24的值来表示其跨越的范围。例如,三个等宽的列可以使用\`.col-8\`来创建 +- 栅格系统中的列是指 1 到 24 的值来表示其跨越的范围。例如,三个等宽的列可以使用 \`\` 来创建 - 如果一个\`row\`中的\`col\`总和超过 24,那么多余的\`col\`会作为一个整体另起一行排列 ## Flex 布局 我们的栅格化系统支持 Flex 布局,允许子元素在父节点内的水平对齐方式 - 居左、居中、居右、等宽排列、分散排列。子元素与子元素之间,支持顶部对齐、垂直居中对齐、底部对齐的方式。同时,支持使用 order 来定义元素的排列顺序。 @@ -36,7 +36,7 @@ Flex 布局是基于 24 栅格来定义每一个『盒子』的宽度,但不 ## 代码演示 `, us: ` - In most business situations, Ant Design needs to solve a lot of information storage problems within the design area, so based on 12 Grids System, we divided the design area into 24 aliquots. + In most business situations, Ant Design Vue needs to solve a lot of information storage problems within the design area, so based on 12 Grids System, we divided the design area into 24 sections. We name the divided area 'box'. We suggest four boxes for horizontal arrangement at most, one at least. Boxes are proportional to the entire screen as shown in the picture above. To ensure a high level of visual comfort, we customize the typography inside of the box based on the box unit. @@ -48,7 +48,7 @@ Following is a brief look at how it works: - Establish a set of \`column\` in the horizontal space defined by \`row\` (abbreviated col) - Your content elements should be placed directly in the \`col\`, and only \`col\` should be placed directly in \`row\` -- The column grid system is a value of 1-24 to represent its range spans. For example, three columns of equal width can be created by \`.col-8\` (\`span=8\`). +- The column grid system is a value of 1-24 to represent its range spans. For example, three columns of equal width can be created by \`\`. - If the sum of \`col\` spans in a \`row\` are more than 24, then the overflowing \`col\` as a whole will start a new line arrangement. ## Flex layout diff --git a/components/grid/demo/playfround.md b/components/grid/demo/playfround.md index 9589f9e35..d48380cbf 100644 --- a/components/grid/demo/playfround.md +++ b/components/grid/demo/playfround.md @@ -11,29 +11,48 @@ A simple playground for column count and gutter. ```tpl