This repository was archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pengshanglong
committed
May 13, 2020
1 parent
43c3757
commit 9709c03
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import classNames from 'classnames' | ||
import _chunk from 'lodash/chunk' | ||
|
||
export default { | ||
name: 'Grid', | ||
props: { | ||
mode: { | ||
type: String, | ||
default: 'square', | ||
validator: (val) => ['rect', 'square'].includes(val), | ||
}, | ||
hasBorder: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
onClick: { | ||
type: Function, | ||
default: () => () => {}, | ||
}, | ||
columnNum: { | ||
type: Number, | ||
default: 3, | ||
}, | ||
data: { | ||
type: Array, | ||
default: () => [], | ||
}, | ||
className: { | ||
type: [Object, String], | ||
default: '', | ||
}, | ||
}, | ||
methods: { | ||
/** | ||
* | ||
* @param {AtGridItem} item | ||
* @param {number} index | ||
* @param {number} row | ||
* @param {event} event | ||
*/ | ||
handleClick(item, index, row, event) { | ||
const { onClick, columnNum } = this.props | ||
if (typeof onClick === 'function') { | ||
const clickIndex = row * columnNum + index | ||
onClick(item, clickIndex, event) | ||
} | ||
}, | ||
}, | ||
render() { | ||
const { data, mode, columnNum, hasBorder } = this | ||
|
||
if (Array.isArray(data) && data.length === 0) { | ||
return null | ||
} | ||
|
||
const gridGroup = _chunk(data, columnNum) | ||
|
||
const bodyClass = classNames(['at-grid__flex-item', 'at-grid-item', `at-grid-item--${mode}`], { | ||
'at-grid-item--no-border': !hasBorder, | ||
}) | ||
|
||
return ( | ||
<view class={classNames('at-grid', this.className)}> | ||
{gridGroup.map((item, i) => ( | ||
<view class="at-grid__flex" key={`at-grid-group-${i}`}> | ||
{item.map((childItem, index) => ( | ||
<view | ||
key={`at-grid-item-${index}`} | ||
class={classNames(bodyClass, { | ||
'at-grid-item--last': index === columnNum - 1, | ||
})} | ||
onClick={this.handleClick.bind(this, childItem, index, i)} | ||
style={{ | ||
flex: `0 0 ${100 / columnNum}%`, | ||
}}> | ||
<view class="at-grid-item__content"> | ||
<view class="at-grid-item__content-inner"> | ||
<view class="content-inner__icon"> | ||
{childItem.image && ( | ||
<image | ||
class="content-inner__img" | ||
src={childItem.image} | ||
mode="scaleToFill" | ||
/> | ||
)} | ||
{childItem.iconInfo && !childItem.image && ( | ||
<view | ||
class={classNames( | ||
childItem.iconInfo.prefixClass || 'at-icon', | ||
{ | ||
[`${childItem.iconInfo.prefixClass || 'at-icon'}-${ | ||
childItem.iconInfo.value | ||
}`]: childItem.iconInfo.value, | ||
}, | ||
childItem.iconInfo.className | ||
)} | ||
style={this.$mergeStyle( | ||
{ | ||
color: childItem.iconInfo.color, | ||
fontSize: `${childItem.iconInfo.size || 24}px`, | ||
}, | ||
childItem.iconInfo.customStyle | ||
)} | ||
/> | ||
)} | ||
</view> | ||
<view class="content-inner__text">{childItem.value}</view> | ||
</view> | ||
</view> | ||
</view> | ||
))} | ||
</view> | ||
))} | ||
</view> | ||
) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Grid from './grid/index' | ||
|
||
export { Grid } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters