Skip to content

Commit

Permalink
feat(layout): 完成Grid组件
Browse files Browse the repository at this point in the history
  • Loading branch information
SzHeJason committed Jul 25, 2018
1 parent 5a4d25f commit f9b104c
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 10 deletions.
Empty file added .stylelintignore
Empty file.
2 changes: 0 additions & 2 deletions src/components/action-sheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export default class AtActionSheet extends Taro.Component {
const { children } = this.props
const { isOpened } = this.state

console.log(children)

if (children) {
const index = children.findIndex(
item => item.name === 'AtActionSheetFooter'
Expand Down
8 changes: 6 additions & 2 deletions src/components/flex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class AtFlex extends Taro.Component {
static Item = AtFlexItem

render () {
const { children, ...props } = this.props
const { children, style, ...props } = this.props

const rootClass = ['at-flex']

Expand All @@ -22,6 +22,10 @@ export default class AtFlex extends Taro.Component {
rootClass.push(`at-flex__${key}--${value}`)
})

return <View className={rootClass}>{children}</View>
return (
<View className={rootClass} style={style}>
{children}
</View>
)
}
}
21 changes: 19 additions & 2 deletions src/components/grid/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint taro/custom-component-children: 0 */
import Taro from '@tarojs/taro'
import { View } from '@tarojs/components'

Expand All @@ -9,7 +10,23 @@ export default class AtGrid extends Taro.Component {
static Item = AtGridItem

render () {
const { children } = this.props
return <View className='at-grid'>{children}</View>
const { children, mode, columnNum } = this.props

const newChildren = children
.filter(item => item.type === AtGridItem)
.map((item, index, arr) => {
item.props = Object.assign(
{
mode,
columnNum,
total: arr.length,
order: index + 1
},
item.props
)
return item
})

return <View className='at-grid'>{newChildren}</View>
}
}
4 changes: 4 additions & 0 deletions src/components/grid/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.at-grid {
display: flex;
flex-wrap: wrap;
}
60 changes: 57 additions & 3 deletions src/components/grid/item/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
/* eslint taro/custom-component-children: 0 */
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'

import { View } from '@tarojs/components'
import _isFunction from 'lodash/isFunction'

import AtIcon from '../../icon/index'

import './index.scss'

export default class AtGridItem extends Taro.Component {
handleClick = () => {
const { onClick } = this.props
if (_isFunction(onClick)) {
onClick()
}
}

render () {
const { children } = this.props
return <View className='at-grid-item'>{children}</View>
const {
icon,
mode = 'square',
order,
total,
value,
iconSize,
iconColor
} = this.props
let { columnNum } = this.props

if (mode === 'rect' && !columnNum) {
columnNum = 2
}
if (mode === 'square' && !columnNum) {
columnNum = 3
}

const maxRow = Math.ceil(total / columnNum)
const isLastRow = order > (maxRow - 1) * columnNum
const isLastCol = order % columnNum === 0

const rootStyle = {
flex: `0 0 ${100 / columnNum}%`
}

const contentStyle = {
borderRightColor: isLastCol ? 'transparent' : '',
borderBottomColor: isLastRow ? 'transparent' : ''
}

const rootClass = ['at-grid-item', `at-grid-item--${mode}`]

return (
<View style={rootStyle} className={rootClass} onClick={this.handleClick}>
<View className='at-grid-item__content' style={contentStyle}>
<View className='at-grid-item__content-inner'>
<View className='content-inner__icon'>
<AtIcon value={icon} color={iconColor} size={iconSize} />
</View>
<Text className='content-inner__text'>{value}</Text>
</View>
</View>
</View>
)
}
}
75 changes: 75 additions & 0 deletions src/components/grid/item/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@import "../../../style/mixins/index.scss";
@import "../../../style/theme/default.scss";

.at-grid-item {
display: block;
position: relative;

&__content {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
border-right: 1px solid $color-grey-4;
border-bottom: 1px solid $color-grey-4;

&-inner {
@include display-flex;
@include align-items(center);
@include justify-content(center);

height: 100%;
flex-direction: column;
}
}

&--square {
&::before {
display: block;
content: " ";
padding-bottom: 100%;
}
}

&--rect {
&::before {
display: block;
content: " ";
padding-bottom: 130px;
}

.at-grid-item__content-inner {
flex-direction: row;

&__text {
margin-top: initial;
}
}
}

&--active {
background-color: $color-grey-6;
}
}

.content-inner {
&__text {
flex: 0 0 auto;
font-size: 32px;
margin-top: 10px;
}

&__icon {
flex: 0 0 auto;
line-height: 0;
margin-top: -1px;
}
}

.at-grid-item--rect .content-inner {
&__text {
margin-left: 10px;
margin-top: initial;
}
}
34 changes: 33 additions & 1 deletion src/pages/layout/grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,39 @@ export default class GridPage extends Taro.Component {
</View>
<View className='example__body'>
<AtGrid mode='square'>
<AtGrid.Item />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
</AtGrid>
</View>
</View>
<View className='example'>
<View className='example__header'>
<Text className='example__header-title'>定义最大的列数</Text>
</View>
<View className='example__body'>
<AtGrid mode='square' columnNum={4}>
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
</AtGrid>
</View>
</View>
<View className='example'>
<View className='example__header'>
<Text className='example__header-title'>定义最大的列数</Text>
</View>
<View className='example__body'>
<AtGrid mode='rect'>
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
<AtGrid.Item icon='emoji' value='放置文本' />
</AtGrid>
</View>
</View>
Expand Down

0 comments on commit f9b104c

Please sign in to comment.