import React, { Component } from 'react' import { Modal, Tag, Row, Col } from 'antd' import './style.scss' import { selectedInfo, selectAreaType } from './type' import { getDictInfoByCode, getDictDataByFirstLetter } from './utils' interface CityChoosProps { type?: selectAreaType tagData: Array onOk(values: selectedInfo): void onCancel(e: any): void visible: boolean } interface CityChooseState { tagValues: Array chooseCityType: string dictData: any } const PinYinList = [ '热门城市', 'A B C', 'D E F G', 'H I', 'J K', 'L M N', 'O P Q R', 'S T U', 'V W X', 'Y Z', '所有城市', ] const InitCityType = PinYinList[0] class CityChoose extends Component { constructor(props: CityChoosProps) { super(props) this.state = { tagValues: [], chooseCityType: InitCityType, dictData: {}, } } componentDidMount() { this.getData() } UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.tagData !== this.props.tagData) { this.setState({ tagValues: nextProps.tagData, }) } } getData = (type = PinYinList[0]) => { // 获取数据,平铺数据 this.setState({ dictData: getDictDataByFirstLetter(type), }) } handleOk = (e: React.MouseEvent) => { e.preventDefault() const { tagValues } = this.state const selectedDicts = [] // 计算去重后的所有城市code tagValues.forEach(item => { const tagInfo = getDictInfoByCode(item.code) selectedDicts.push(tagInfo) }) this.props.onOk({ selectedDicts, }) } changeCityType = (value: string) => { this.getData(value) this.setState({ chooseCityType: value, }) } chooseCity = city => { let { tagValues } = this.state const { type } = this.props const code = city.code const name = city.name const tagCodes = tagValues.map(item => item.code) const index = tagCodes.indexOf(code) if (type === selectAreaType.MULTI) { if (~tagCodes.indexOf('00')) { tagValues = [{ code: '00', name: '全国' }] } else { tagValues = [] if (index === -1) { tagValues.push({ code, name }) } else { tagValues.splice(index, 1) } } } this.setState({ tagValues, }) } closeTag = index => { const { tagValues } = this.state tagValues.splice(index, 1) this.setState({ tagValues, }) } render() { const { tagValues, chooseCityType, dictData } = this.state return (
{tagValues.length > 0 && (
{tagValues.map((item, index) => { const tagInfo = getDictInfoByCode(item.code) return ( this.closeTag(index)} key={item.code}> {tagInfo.name} ) })}
)} {PinYinList.map((e, index) => { return (
this.changeCityType(e)} className={`city-type-nochoose ${chooseCityType === e ? 'city-type-choose' : null}`} > {e}
) })} {dictData[chooseCityType] && dictData[chooseCityType].map(e => { const code = e.code const tagCodes = tagValues.map(item => item.code) return ( this.chooseCity(e)} > {e.name} ) })}
) } } export default CityChoose