以下代码是初期写的代码,后来对代码进行优化,解决了初期的bug。完整的选择城市三级联动组件可以参考我的github项目中的代码,这是后期调试成功上传上去的React选择城市三级联动组件

<SelectArea allAreaInfo={allAreaInfo} province={province}
city={city} district={district}
cancel={this.closeModal} confirm={this.SelectAddress} />
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router';
import rest from '../../lib/rest';const HEIGHT = 36;
export class Picker extends Component {constructor(props) {super(props);this.state = {selectedIndex: this.props.index};this.onScroll = this.onScroll.bind(this);this.resetPosition = this.resetPosition.bind(this);}componentDidMount() {this.refs.scroller.scrollTop = this.state.selectedIndex * HEIGHT;this.refs.scroller.addEventListener('touchstart', this.touchStart, false);this.refs.scroller.addEventListener('touchend', this.touchEnd, false);this.refs.scroller.addEventListener('mousedown', this.touchStart, false);this.refs.scroller.addEventListener('mouseup', this.touchEnd, false);}touchStart() {this.isTouchStart = true;}touchEnd() {this.isTouchStart = false;if (this.timer) clearTimeout(this.timer);this.timer = setTimeout(this.resetPosition, 100);}onScroll() {if (this.timer) clearTimeout(this.timer);this.timer = setTimeout(this.resetPosition, 100);}resetPosition() {let provinceList = [], cityList = [], districtList = [];const { type = '', allAreaInfo = [], callback = () => { } } = this.props;if (this.isTouchStart) return;const top = this.refs.scroller.scrollTop;const distance = top % HEIGHT;let target;if (distance > HEIGHT / 2) {target = top + HEIGHT - distance;this.refs.scroller.scrollTop = target;} else {target = top - distance;this.refs.scroller.scrollTop = target;}const selectedIndex = target / HEIGHT;this.setState({selectedIndex}, () => {callback(type, selectedIndex);});}render() {const { list = [] } = this.props;return (<div className="ul-area" onScroll={this.onScroll} ref="scroller"><ul><li></li>{list.map((item, index) => (<li key={index} className={`${index === this.state.selectedIndex && 'selected'}`}>{item}</li>))}<li></li></ul></div>)}
}class SelectArea extends Component {constructor(props) {super(props);this.state = {provinceList: [],cityList: [],districtList: [],provinceIndex: 0,cityIndex: 0,districtIndex: 0,showPicker: false};this.handleSelect = this.handleSelect.bind(this);}componentDidMount() {const { allAreaInfo = [], province = '', city = '', district = '' } = this.props;let provinceList = [], cityList = [], districtList = [], provinceIndex = 0, cityIndex = 0, districtIndex = 0;if (province) {provinceIndex = allAreaInfo.findIndex(item => item.name === province);cityIndex = allAreaInfo[provinceIndex].city.findIndex(item => item.name === city);districtIndex = allAreaInfo[provinceIndex].city[cityIndex].area.findIndex(item => item === (district || '其他'));this.setState({provinceIndex,cityIndex,districtIndex,showPicker: true});}allAreaInfo.forEach(item => provinceList.push(item.name));if (allAreaInfo[provinceIndex].city.length) {allAreaInfo[provinceIndex].city.forEach(item => cityList.push(item.name));}this.setState({provinceList,cityList,districtList: allAreaInfo[provinceIndex].city[cityIndex] && allAreaInfo[provinceIndex].city[cityIndex].area,showPicker: true});}handleSelect(type, index) {let provinceList = [], cityList = [], districtList = [];const { provinceIndex, cityIndex, districtIndex } = this.state;const { allAreaInfo = [] } = this.props;switch(type) {case 'province':allAreaInfo[index].city.forEach(item => cityList.push(item.name));this.setState({provinceIndex: index,cityList,districtList: allAreaInfo[index].city[0] && allAreaInfo[index].city[0].area});break;case 'city':this.setState({cityIndex: index,districtList: allAreaInfo[provinceIndex].city[index] && allAreaInfo[provinceIndex].city[index].area});break;case 'district':this.setState({districtIndex: index,districtList: allAreaInfo[provinceIndex].city[cityIndex].area});break;default:break;}}render() {const { allAreaInfo = [], cancel = () => {}, confirm = () => {} } = this.props;const { provinceList, cityList, districtList, provinceIndex, cityIndex, districtIndex } = this.state;return (<div id="select-area"><div className="col-xs-12 select-area" onClick={e => e.stopPropagation()}><div className="row border-bottom-grey"><div className="col-xs-6" onClick={cancel}>取消</div><div className="col-xs-6 text-right font-orange"onClick={confirm.bind(null, provinceIndex, cityIndex, districtIndex)}>确定</div></div>{this.state.showPicker && (<div className="row list"><Picker type="province" allAreaInfo={allAreaInfo} list={provinceList} index={this.state.provinceIndex} callback={this.handleSelect} /><Picker type="city" allAreaInfo={allAreaInfo} list={cityList} index={cityIndex} callback={this.handleSelect} /><Picker type="district" allAreaInfo={allAreaInfo} list={districtList} index={districtIndex} callback={this.handleSelect} /></div>)}</div></div>)}
}SelectArea.contextTypes = {router: PropTypes.object.isRequired
};
SelectArea.propTypes = {// test: PropTypes.string.isRequired,
};
export default connect(state => (state))(SelectArea);

使用React实现选择城市三级联动组件相关推荐

  1. vue城市三级联动组件 vue-area-linkage

    Install the pkg with npm: // v5之前的版本 npm i --save vue-area-linkage// v5及之后的版本 npm i --save vue-area- ...

  2. uniapp自定义picker城市多级联动组件

    uniapp自定义picker城市多级联动组件 支持多端--h5.app.微信小程序.支付宝小程序... 支持自定义配置picker插件级数 支持无限级 注意事项:插件传入数据格式为children树 ...

  3. picker封装 uniapp_uniapp自定义picker城市多级联动组件

    uniapp自定义picker城市多级联动组件 支持多端--h5.app.微信小程序.支付宝小程序... 支持自定义配置picker插件级数 支持无限级 注意事项:插件传入数据格式为children树 ...

  4. 收货地址的JavaScript城市三级联动【干货拿走不谢!>_<】

    城市三级联动 在我们网上购物时会有收货地址一栏让我们选择收货地址,当中有省.市.区等选择项,如下图: 在添加收货地址时我们会先选择省再选市最后选所在区,这些都是下拉列表式的选择,选择完成才会填写具体地 ...

  5. 【web前端性能优化】13.城市三级联动

    最近做项目遇到一个城市三级联动的前端问题,感觉一个城市三级联动如果引入一个jquery库,有点太重了,于是就在网上找到了原始的js写法,感觉还挺好用的就记录一下,如下图所示: pay.html < ...

  6. 城市三级联动数据及地区代码对照表

    城市三级联动数据 介绍 使用说明 数据说明 做前端需要城市选择,在网上找到了别人的数据,但是没台湾省.作为爱国中年码农,婶可忍叔不可忍.所以就查阅了[中华人民共和国民政部]的网站,根据最新行程区划生成 ...

  7. 台湾、香港、澳门的城市三级联动json

    台湾.香港.澳门的城市三级联动json =========== 台湾 ============ {"citys": [{"areas": [{"are ...

  8. 城市三级联动功能实现

    背景: 最近在进行商品购买流程的开发,需要用户填写自己的收货地址,为了保证地址的准确性,需要使用到全国城市的三级联动功能 其中可以有三个思路: 1.使用 js 直接加载城市信息: 2.自己建立数据库, ...

  9. iOS 开发 带区号的城市三级联动(xml解析)

    iOS 开发 带区号的城市三级联动(xml解析) demo下载地址: http://download.csdn.net/detail/qq_20176153/9514906

最新文章

  1. RANK() OVER(PARTITION BY deptno ORDER BY empno)
  2. HTML5 跨文档消息传输
  3. 一场开发与视觉的对话引发的思考
  4. 在 ASP.NET Core 中集成 Skywalking APM
  5. 百度UEditor控件中的map组件不支持https使用的问题解决
  6. 博弈论与逻辑思维(传教士与妻子忠贞的问题)
  7. android 权限
  8. matlab 求导数
  9. 通过两个栈实现一个队列(C语言)
  10. 学习uni-app之微信登录
  11. VASP+Phono3py计算声子linewidth
  12. NMOS和PMOS导通电流 走向
  13. 如何用计算机完成一篇文稿制作步骤,第5章 计算机一级演示文稿制作经典教程.ppt...
  14. nanopi 2 fire s5p4418 初次体验 (1)uboot,linux kernel编译
  15. 线性代数笔记33——基变换和图像压缩
  16. css碎步测量,隧洞测量实习日记.doc
  17. OPPO手机进水不读卡,修复
  18. Windows10指纹识别设置
  19. 【大学生数学竞赛】公式大全(补充中)
  20. 疫情控制住了,公司却倒闭了!

热门文章

  1. C#Url下载图片(jpg\pdf\png\jpeg...)
  2. ulimit -SHn 65535含义
  3. 2018-8-30-router的自我反思与总结二
  4. 5月26日来IGS大会腾讯云游戏新文娱分论坛和TcaplusDB约会吧!
  5. 一大波年终工作总结PPT来袭赶紧收藏!
  6. mysql临时表 表变量_TSQL--临时表和表变量
  7. 登陆进管理后台的首页
  8. 迅雷看看 V4.9.14.2052绿色版 - 高清在线影院
  9. 能源管理系统的主要功能|瑜岿科技|能源监测
  10. Away3D 基础5 - 3D基本元素(3)