• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

taro小程序地址选择组件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

效果图:

address_picker.tsx:

import Taro, { Component } from '@tarojs/taro'
import { View, PickerView, PickerViewColumn } from '@tarojs/components'
import PropTypes from 'prop-types'
import address from '../../utils/city.js'
import './address_picker.scss'

class AddressPicker extends Component {

    state = {
        value: [18, 11, 0],
        provinces: address.provinces,
        citys: address.citys[440000],
        areas: address.areas[441400],
        areaInfo: '',
    }

    cityChange (e) {
        const pickerValue = e.detail.value
        const { provinces, citys, value } = this.state
        const provinceNum = pickerValue[0]
        const cityNum = pickerValue[1]
        const countyNum = pickerValue[2]
        // 如果省份选择项和之前不一样,表示滑动了省份,此时市默认是省的第一组数据,
        if (value[0] != provinceNum) {
            const id = provinces[provinceNum].id
            this.setState({
                value: [provinceNum, 0, 0],
                citys: address.citys[id],
                areas: address.areas[address.citys[id][0].id]
            })
        } else if (value[1] != cityNum) {
            // 滑动选择了第二项数据,即市,此时区显示省市对应的第一组数据
            const id = citys[cityNum].id
            console.log(id)
            this.setState({
                value: [provinceNum, cityNum, 0],
                areas: address.areas[citys[cityNum].id]
            })
        } else {
            // 滑动选择了区
            this.setState({
                value: [provinceNum, cityNum, countyNum]
            })
        }
    }

    //  params true代表传递地址,false不传递
    handlePickerShow (params: boolean) {
        
        if (params) {
            console.log(this.props)
            const { provinces, citys, areas, value, areaInfo } = this.state
            // 将选择的城市信息显示到输入框
            const tempAreaInfo = provinces[value[0]].name + '' + citys[value[1]].name + areas[value[2]].name
            this.setState({
                areaInfo: tempAreaInfo
            }, () => {
                this.props.onHandleToggleShow(this.state.areaInfo)
            })
        }
    }

    render () {
        const { provinces, citys, areas, value } = this.state
        const { pickerShow } = this.props
        return (
            <View className={pickerShow? 'address-picker-container show': 'address-picker-container'} onClick={this.handlePickerShow.bind(this,true)}>
                <View className="picker-content">
                    <View className="dialog-header">
                        <View className="dialog-button cancel" onClick={this.handlePickerShow.bind(this,false)}>取消</View>
                        <View className="dialog-title">请选择省市区</View>
                        <View className="dialog-button" onClick={this.handlePickerShow.bind(this, true)}>确定</View>
                    </View>
                    <PickerView onChange={this.cityChange} value={value} className='picker-view-wrap'>
                        <PickerViewColumn>
                            {
                                provinces.map((province, index) => {
                                    return <View className="picker-item" key={index}>{province.name}</View>
                                })
                            }
                        </PickerViewColumn>
                        <PickerViewColumn>
                            {
                                citys.map((city, index) => {
                                    return <View className="picker-item" key={index}>{city.name}</View>
                                })
                            }
                        </PickerViewColumn>
                        <PickerViewColumn>
                            {
                                areas.map((area, index) => {
                                    return <View className="picker-item" key={index}>{area.name}</View>
                                })
                            }
                        </PickerViewColumn>
                    </PickerView>
                </View>
            </View>
        )
    }
}

AddressPicker.propTypes = {
    pickerShow: PropTypes.bool.isRequired,
    onHandleToggleShow: PropTypes.func.isRequired,
}

export default AddressPicker

样式文件: 

.address-picker-container {
    width: 100%;
    height: 100vh;
    display: flex;
    z-index: 12;
    background: rgba(0, 0, 0, 0.7);
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: fixed;
    bottom: 0px;
    left: 0px;
    visibility: hidden;
    &.show {
        visibility: visible;
        .picker-content {
            transform: translateY(0);
            transition: all 0.4s ease;
        }
    }
}

.picker-content {
    position: absolute;
    width: 100%;
    bottom: 0;
    background-color: #fff;
    transform: translateY(150%);
    transition: all 0.4s ease;

    .picker-view-wrap {
        width: 100%;
        height: 400px;
    }
}

.picker-item {
    line-height: 70px;
    font-size: 36px;
    text-align: center;
}

.dialog-header {
    width: 100%;
    background: #ededed;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .dialog-title {
        color: #333;
    }

    .dialog-button {
        display: inline-block;
        text-align: center;
        font-size: 32px;
        color: #E28E81;
        padding: 30px;
        &.cancel {
            color: #666;
        }
    }
}

地址信息存放在city.js,传送门:https://pan.baidu.com/s/1G2gCoiJ7ujt2x8sEXKuLQg

注意:

父组件调用子组件,传递函数时,函数名需要以on开头命名,否则子组件无法识别该函数,比如:

 

<AddressPicker pickerShow={pickerShow} onHandleToggleShow={this.toggleAddressPicker.bind(this)}/>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
评论星星微信小程序发布时间:2022-07-18
下一篇:
四则运算计算器的微信小程序_1界面发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap