react项目实战(权限模块开发四) SkinDropDown插件开发

本文介绍了login.js中使用的SkinDropCom插件及其子组件DropItem的实现细节。SkinDropCom是一个可配置皮肤的下拉选择插件,利用React及React-Redux进行组件构建,并通过document.body事件监听实现交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

login.js文件中用到了一个SkinDropDwon插件

import SkinDropCom from '../../components/SkinDropCom';
         ...
          <SkinDropCom skinsList={loginMd.skinsList} visibility={loginMd.visibility}
                         changeCurSkin={this._changeCurSkin} showDropDown={this._showDropDown} width="180px"/>
                         ...

SkinDropCom代码如下:

import React, { Component, PropTypes } from 'react' // 引入React
import { connect } from 'react-redux' // 引入connect
import DropItem from '../components/DropItem'  // 引入展示组件List

const dropDowStyle = (visibility, width)=> {
  return {
    visibility: visibility,
    width: width
  }
}

class SkinDropCom extends Component {
  constructor(...props) {
    super(...props);
    [
      '_onClickHandler'
    ].forEach(func=> {
      this[func] = this[func].bind(this);
    });
  }

  componentDidMount() {
    document.body.addEventListener('click', this._onClickHandler, false);
  }

  componentWillUnmount() {
    document.body.removeEventListener('click');
  }

  _onClickHandler(e) {
    const { showDropDown }=this.props;
    showDropDown(false);
  }

  render() {
    // 通过this.props获取到lists的值
    const { skinsList, visibility,changeCurSkin,width } = this.props;

    var getItemProps=(itemObj, changeCurSkin, index)=>{
      return {
        itemId: itemObj.itemId,
        key: index,
        changeCurSkin: changeCurSkin,
        text: itemObj.text
      }
    }

    return (
      <div className="dhxcombolist_material" style={dropDowStyle(visibility,width)}>
        { skinsList.map((itemObj, index) =>
          <DropItem {...getItemProps(itemObj, changeCurSkin, index)}></DropItem>
        )}
      </div>
    )
  }
}

export default SkinDropCom;


该组件又用到一个无状态的插件DropItem:

 <div className="dhxcombolist_material" style={dropDowStyle(visibility,width)}>
        { skinsList.map((itemObj, index) =>
          <DropItem {...getItemProps(itemObj, changeCurSkin, index)}></DropItem>
        )}
      </div>

DropItem.js文件内容如下:

// 动态数据列表
import React, {PropTypes} from 'react'

export default function DropItem({itemId,text,changeCurSkin}){
  return (
    <div className="dhxcombo_option" id={itemId} onClick={(ev)=>{
         ev.stopPropagation();
        changeCurSkin(text);
      }}>
      <div className="dhxcombo_option_text">{text}</div>
    </div>
  )
}

DropItem.propTypes = {
  itemId: PropTypes.string.isRequired,
  text: PropTypes.string.isRequired
}

该组件对document.body进行了一个事件注册,目的是点击界面其他地方可以将下拉隐藏掉,该事件需要从父界面login传递过来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值