【React】React实现手风琴效果
来源:互联网 发布:烘焙成本核算软件 编辑:程序博客网 时间:2024/06/05 15:54
需求背景
效果图:
由于不确定手风琴的个数,所以无法通过一个全局的state.hidden属性统一设置。
有两种解决方法:
1. 数组的方式
2. 建立索引值查找(本文所使用的方法)
import React, { Component } from 'react';import styles from './List.css';import { connect } from 'react-redux';import { loadList } from '../../actions';@connect( (state, ownProps) => { const selectedMenu = ownProps.location.hash.split('#')[1]; //获取URL中的hash值 return { selectedMenu, } }, { loadList })class List extends Component { constructor(props) { super(props); this.renderSection = this.renderSection.bind(this); } //控制是否展开二级标题 handleSubTitleShow = (evt) => { const node = evt.currentTarget; const idx = node.dataset.idx; const { history, location, selectedMenu } = this.props; //如果双击一级标题,清空hash值,收起当前的二级标题。否则认为是单击,展开二级标题 if (selectedMenu === idx) { history.replace({ ...location, hash: '', }); } else { history.replace({ ...location, hash: idx, }); } } //二级标题 renderSectionRow(item, idx) { const { name } = item; return ( <div className={styles.sectionContentRow} key={`secRow${idx}`}>Q{idx+1}:{name}</div> ); } //一级标题 renderSection(item, idx) { const key = '' + idx; //由于从URL获取的hash值是字符串,所以把idx也改为字符串,方便后面做对比 const { selectedMenu } = this.props; const { name, articleVoList } = item; return ( <div className={styles.section} key={key}> <a data-idx={key} className={styles.secTitle} onClick={this.handleSubTitleShow}> <span>{name}</span> <span className={selectedMenu === key ? styles.secTitleIconCurrent : styles.secTitleIcon}> </span> </a> {selectedMenu === key && articleVoList.map(this.renderSectionRow)} </div> ); } render() { const { list } = this.props; if (!list) { return null; } return ( <div className={styles.wrapper}> {list.map(this.renderSection)} </div> ); }}export default List;
Author:致知
Sign:路漫漫其修远兮,吾将上下而求索。
阅读全文
0 0
- 【React】React实现手风琴效果
- React实现时钟效果
- React实现动画效果
- react实现toggle效果
- juqery实现手风琴效果
- jquery实现手风琴效果
- 实现手风琴效果
- css3实现手风琴效果
- RecyclerView实现手风琴效果
- 手风琴效果实现
- jquery实现手风琴效果
- target实现手风琴效果
- Bootstrap实现手风琴效果
- angularJS实现手风琴效果
- ExpandableListView实现手风琴效果
- ExpandableListView实现手风琴效果
- jQuery实现手风琴效果
- React Native实现Text显示...的效果
- 2017年度总结
- matplotlib——sin,cos
- System.getProperties()系統属性
- Ubuntu16.04下Sublime Text3安装Emmet等插件
- NetBeans 运行java web项目报错 build-impl.xml:1045: 尚未部署该模块
- 【React】React实现手风琴效果
- 中式Dao开源框架(MicroDao)
- $().tab() bootStrap中Tab页签切换
- 关于Picasso回调获取bitmap第一次加载失败的问题
- mybatis报错 There is no getter for property named 'xx' in 'class java.lang.String
- Wireshark抓包分析Yersinia 根据DHCP协议发送的伪造数据包攻击(sending RAW packet)
- 每天一个linux命令(15):tail 命令
- 使用Min Heap实现Top N Elements查找
- 2017/12/19