需求是这样的。

  • 有一个需要显示若干方块型元素的小区域
  • 数量比较少的时候显示一排
  • 数量比较多的时候显示两排

用 grid 不好,因为当数量为奇数的时候需要两排里面的元素都乖乖的居中显示。

用 flex 的话,直接写需要写很多判断,因为行数不同的时候页面结构也会不同。

所以,我想到用二维数组(矩阵)来表示在什么时候想怎样显示。

上代码。

// 波浪效果测试

import React from 'react'

import Styles from './index.less'

import Wave from '../wave/index.js'
import { Flex } from 'antd-mobile' class WaveContainer extends React.Component {
constructor(props){
super(props)
this.state = {
}
} render(){
console.log(this.props.dataBar) const baseFlex = {
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}
const theStyle = {
main:{
...baseFlex,
width:'17.552vw',
height:'10.521vw',
color:"#fff"
},
tem:{
...baseFlex,
flex:"auto",
color:'#fff'
},
shellA:{
...baseFlex,
width:'100%',
height:'100%'
},
shellB:{
...baseFlex,
width:'100%',
height:'50%'
}
} const dataBar = this.props.dataBar const Container = ((dataBar) => { const flexMatrix = [
[0,0],
[1,0],
[2,0],
[3,0],
[2,2],
[3,2],
[3,3],
[4,3],
[4,4],
[5,4],
[5,5],
[6,5],
[6,6]
] const sData = dataBar.data.sData
const length = sData.length const matrix = flexMatrix[length] ? flexMatrix[length] : flexMatrix[12] if (matrix[0] === 0) {
return ""
} let temShell, temA, temB temA = sData.slice(0, matrix[0]).map((item, index) =>
<div style={theStyle.tem} key={index.toString()}> <Wave data={item} /> </div>
); if (matrix[1] === 0) {
temB = ""
} else {
temB = sData.slice(matrix[0], (matrix[0] + matrix[1])).map((item, index) =>
<div style={theStyle.tem} key={index.toString()}> <Wave data={item} /> </div>
);
} if (matrix[1] === 0) {
temShell = <div style={theStyle.shellA} > {temA} </div>
} else {
temShell = [0,0].map((item, index) =>
<div style={theStyle.shellB} key={"temShell" + index.toString()}> {index === 0 ? temA : temB} </div>
); theStyle.main.flexWrap = "wrap"
} console.log(temShell) return temShell
})(dataBar) return (
<div style={theStyle.main}>
{/* <Wave /> */}
{ Container }
</div>
);
}
} export default WaveContainer

稍微解释一下。

  • 用展开运算符使样式更简洁。
  • flexMatrix 就是记录数据的矩阵。
  • temA, temB 是放一行元素的容器。
  • temShell 是放 temA, temB 的容器。
  • theStyle.main.flexWrap = "wrap" 让元素可以正常换行。
  • slice 的作用是分割数据对象,一行只渲染一行需要的那些数据。
  • [0,0] 是因为需要两行,所以用有两个元素的数组,用 map 输出 JSX。

然后是从页面传入的数据的代码。

render(){
const dataBar = {
data:{
sData:[
{ name: 'a', data: 55, color:'rgb(79,239,223)' },
{ name: 'b', data: 5, color:'rgb(79,239,223)'},
{ name: 'c', data: 36, color:'rgb(79,239,223)'},
{ name: 'd', data: 476, color:'rgb(87,207,30)'},
{ name: 'e', data: 226, color:'rgb(79,239,223)'},
{ name: 'f', data: 273, color:'rgb(251,211,36)'}
]
}
} return (
<div className={Styles.main}>
<WaveContainer dataBar={dataBar} />
</div>
);
}

其实和他也没什么关系。因为直接把对象传入了更里面的元素。

以上。

最新文章

  1. UDS(ISO14229-2006) 汉译(No.2参考标准)
  2. 第三章 EnumUtil根据值获取枚举对象
  3. 《CODE》读后笔记——第21~25章
  4. WPF快速入门系列(1)——WPF布局概览
  5. 纯css3写的仿真图书翻页效果
  6. __FILE__,__LINE__,FUNCTION__
  7. Linux下多任务间通信和同步-概述
  8. State 状态模式
  9. Spark机器学习之协同过滤算法
  10. cookie 与 session
  11. js:防抖动与节流
  12. ConchAPI | 更智能的API监控,提升团队效率
  13. 搭建Android浏览器壳子
  14. laravel 5 优化
  15. Activiti动态设置办理人扩展
  16. TweenMax 动画库,知识点
  17. 最新Dashboard设计实例、技巧和资源集锦,视觉和功能两不误,妥妥的!
  18. 【进阶修炼】&mdash;&mdash;改善C#程序质量(3)
  19. js中的deom ready执行的问题
  20. Linux配置NFS实现共享

热门文章

  1. JAVA设计模式 1 设计模式介绍、单例模式的理解与使用
  2. Blazor带我重玩前端(一)
  3. 都在讲DevOps,但你知道它的发展趋势吗?
  4. MySQL8.0窗口函数实践及小结
  5. python字典套字典
  6. socketserver模块使用与源码分析
  7. ibit-mybatis 2.x 介绍
  8. centos7 mysql8.0替换为5.7版本
  9. RISC-V发展现状
  10. python 的迭代