Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 00343a3

Browse files
Merge pull request #407 from IHIutch/feat/grid-item
feat: Adds CGridItem
2 parents 5a4f228 + 8a0b375 commit 00343a3

File tree

8 files changed

+185
-17
lines changed

8 files changed

+185
-17
lines changed

packages/chakra-ui-core/src/CGrid/CGrid.js

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,65 @@
1010
*/
1111

1212
import { createStyledAttrsMixin } from '../utils'
13-
import { SNA } from '../config/props/props.types'
13+
import { SNA, StringArray } from '../config/props/props.types'
14+
15+
/**
16+
* @description Map "span" values to accommodate breakpoint values
17+
* @param {Array} value
18+
* @returns {(String|Array)} String or Array of breakpoint values
19+
*/
20+
const spanFn = (value) => {
21+
if (Array.isArray(value)) {
22+
return value.map(v =>
23+
v === 'auto' ? 'auto' : `span ${v}/span ${v}`
24+
)
25+
} else {
26+
return value === 'auto' ? 'auto' : `span ${value}/span ${value}`
27+
}
28+
}
29+
30+
/**
31+
* CGridItem component
32+
*
33+
* A primitive component useful for grid layouts.
34+
*
35+
* @extends CBox
36+
* @see Docs https://vue.chakra-ui.com/grid
37+
*/
38+
39+
const CGridItem = {
40+
name: 'CGridItem',
41+
mixins: [createStyledAttrsMixin('CGridItem')],
42+
props: {
43+
colSpan: { type: StringArray },
44+
rowSpan: { type: StringArray },
45+
colStart: { type: StringArray },
46+
colEnd: { type: StringArray },
47+
rowStart: { type: StringArray },
48+
rowEnd: { type: StringArray }
49+
},
50+
computed: {
51+
componentStyles () {
52+
return {
53+
gridColumn: this.colSpan ? spanFn(this.colSpan) : null,
54+
gridRow: this.rowSpan ? spanFn(this.rowSpan) : null,
55+
gridColumnStart: this.colStart,
56+
gridColumnEnd: this.colEnd,
57+
gridRowStart: this.rowStart,
58+
gridRowEnd: this.rowEnd
59+
}
60+
}
61+
},
62+
render (h) {
63+
return h('div',
64+
{
65+
class: this.className,
66+
attrs: this.computedAttrs
67+
},
68+
this.$slots.default
69+
)
70+
}
71+
}
1472

1573
/**
1674
* CGrid component
@@ -61,11 +119,18 @@ const CGrid = {
61119
}
62120
},
63121
render (h) {
64-
return h(this.as, {
65-
class: this.className,
66-
attrs: this.computedAttrs
67-
}, this.$slots.default)
122+
return h(
123+
this.as,
124+
{
125+
class: this.className,
126+
attrs: this.computedAttrs
127+
},
128+
this.$slots.default
129+
)
68130
}
69131
}
70132

71-
export default CGrid
133+
export {
134+
CGrid,
135+
CGridItem
136+
}

packages/chakra-ui-core/src/CGrid/CGrid.stories.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { storiesOf } from '@storybook/vue'
2-
import { CReset, CGrid, CBox } from '..'
2+
import { CReset, CGrid, CGridItem, CBox } from '..'
33

44
storiesOf('UI | Grid', module)
55
.add('Default Grid', () => ({
@@ -17,3 +17,17 @@ storiesOf('UI | Grid', module)
1717
</div>
1818
`
1919
}))
20+
21+
storiesOf('UI | Grid', module)
22+
.add('Grid Items', () => ({
23+
components: { CReset, CGrid, CGridItem },
24+
template: `
25+
<div>
26+
<CReset />
27+
<CGrid w="600px" template-columns="repeat(5, 1fr)" gap="6">
28+
<CGridItem col-span="2" h="10" bg="blue.500" />
29+
<CGridItem col-start="4" col-end="6" h="10" bg="red.500" />
30+
</CGrid>
31+
</div>
32+
`
33+
}))
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import CGrid from './CGrid'
2-
export default CGrid
1+
export * from './CGrid'

packages/chakra-ui-core/src/CGrid/test/CGrid.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import CGrid from '..'
1+
import { CGrid, CGridItem } from '..'
22
import { render } from '@/tests/test-utils'
33

44
const renderComponent = (props) => {
55
const inlineAttrs = (props && props.inlineAttrs) || ''
66
const base = {
7-
components: { CGrid },
7+
components: { CGrid, CGridItem },
88
template: `<CGrid ${inlineAttrs}>Grid Me</CGrid>`,
99
...props
1010
}
@@ -23,3 +23,25 @@ it('should change gap', () => {
2323

2424
expect(asFragment()).toMatchSnapshot()
2525
})
26+
27+
it('should offset columns', () => {
28+
const inlineAttrs = 'col-start="4" col-end="6"'
29+
const { asFragment } = renderComponent({
30+
template: `
31+
<CGrid w="600px" template-columns="repeat(5, 1fr)" gap="6">
32+
<CGridItem ${inlineAttrs}>I'm in a grid item</CGridItem>
33+
</CGrid>`
34+
})
35+
expect(asFragment()).toMatchSnapshot()
36+
})
37+
38+
it('should span columns', () => {
39+
const inlineAttrs = 'col-span="2"'
40+
const { asFragment } = renderComponent({
41+
template: `
42+
<CGrid w="600px" template-columns="repeat(5, 1fr)" gap="6">
43+
<CGridItem ${inlineAttrs}>I'm in a grid item</CGridItem>
44+
</CGrid>`
45+
})
46+
expect(asFragment()).toMatchSnapshot()
47+
})

packages/chakra-ui-core/src/CGrid/test/__snapshots__/CGrid.test.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ exports[`should change gap 1`] = `
1111
</DocumentFragment>
1212
`;
1313

14+
exports[`should offset columns 1`] = `
15+
<DocumentFragment>
16+
<div
17+
class="css-12kxrxg"
18+
data-chakra-component="CGrid"
19+
>
20+
<div
21+
class="css-1i8ejg0"
22+
data-chakra-component="CGridItem"
23+
>
24+
I'm in a grid item
25+
</div>
26+
</div>
27+
</DocumentFragment>
28+
`;
29+
1430
exports[`should render correctly 1`] = `
1531
<DocumentFragment>
1632
<div
@@ -21,3 +37,19 @@ exports[`should render correctly 1`] = `
2137
</div>
2238
</DocumentFragment>
2339
`;
40+
41+
exports[`should span columns 1`] = `
42+
<DocumentFragment>
43+
<div
44+
class="css-12kxrxg"
45+
data-chakra-component="CGrid"
46+
>
47+
<div
48+
class="css-tuh9u2"
49+
data-chakra-component="CGridItem"
50+
>
51+
I'm in a grid item
52+
</div>
53+
</div>
54+
</DocumentFragment>
55+
`;

packages/chakra-ui-core/src/CSimpleGrid/CSimpleGrid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CSimpleGrid/CSimpleGrid.js
99
*/
1010

11-
import CGrid from '../CGrid'
11+
import { CGrid } from '../CGrid'
1212
import { SNA } from '../config/props/props.types'
1313
import { createStyledAttrsMixin } from '../utils'
1414
import { countToColumns, widthToColumns } from './utils/grid.styles'

packages/chakra-ui-core/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export { default as CFormHelperText } from './CFormHelperText'
5555
export { default as CFragment } from './CFragment'
5656

5757
// G
58-
export { default as CGrid } from './CGrid'
58+
export * from './CGrid'
5959

6060
// H
6161
export { default as CHeading } from './CHeading'

website/pages/grid.mdx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import SEO from "../components/SEO";
1+
import SEO from '../components/SEO'
22

33
<SEO
44
title="Grid"
@@ -15,10 +15,9 @@ comes with helpful style shorthand. It renders a `div` element.
1515
## Import
1616

1717
```js
18-
import { CGrid } from "@chakra-ui/vue";
18+
import { CGrid, CGridItem } from '@chakra-ui/vue'
1919
```
2020

21-
2221
## Usage
2322

2423
Using the `Grid` component, here are some helpful shorthand props:
@@ -51,6 +50,43 @@ time.
5150
</c-grid>
5251
```
5352

53+
## Spanning Columns
54+
55+
In some layouts, you may need certain grid items to span specific amount of columns or rows instead of an even distribution. To achieve this, you need to pass the `colSpan` prop to the `CGridItem` component to span across columns and also pass the `rowSpan` component to span across rows. You also need to specify the `templateColumns` and `templateRows`.
56+
57+
```vue live=true
58+
<c-grid w="600px" template-columns="repeat(5, 1fr)" gap="6">
59+
<c-grid-item row-span="2" col-span="1" bg="blue.300"" />
60+
<c-grid-item col-span="2" bg="red.300" />
61+
<c-grid-item col-span="2" bg="red.300" />
62+
<c-grid-item col-span="4" bg="blue.300" />
63+
</c-grid>
64+
```
65+
66+
## Starting and Ending Lines
67+
68+
Pass the `colStart` and `colEnd` prop to `CGridItem` component to make an element start or end at the `nth` grid position.
69+
70+
```vue live=true
71+
<c-grid w="600px" template-columns="repeat(5, 1fr)" gap="6">
72+
<c-grid-item col-span="2" h="10" bg="blue.500" />
73+
<c-grid-item col-start="4" col-end="6" h="10" bg="red.500" />
74+
</c-grid>
75+
```
76+
5477
## Props
5578

56-
> `CGrid` composes the `CBox` component. So it accepts all Box props along with the related CSS grid props. See [Box](/box#props) component for list of props
79+
> `CGrid` composes the `CBox` component. So it accepts all Box props along with the related CSS grid props. See [Box](/box#props) component for list of props
80+
81+
### `CGridItem` Props
82+
83+
> `CGridItem` composes `CBox` so you can pass all `CBox` props.
84+
85+
| Name | Type | Description |
86+
| -------- | -------- | ------------------------------------------------ |
87+
| colSpan | `number` | The number of columns the grid item should span. |
88+
| colStart | `number` | The column number the grid item should start. |
89+
| colEnd | `number` | The column number the grid item should end. |
90+
| rowSpan | `number` | The number of rows the grid item should span. |
91+
| rowStart | `number` | The row number the grid item should start. |
92+
| rowEnd | `number` | The row number the grid item should end. |

0 commit comments

Comments
 (0)