使用React Hooks实现三列选择对话框

效果如下图所示:

 

import { FC, useState } from 'react';
import { Button } from 'antd-mobile';

export interface LocationCityType {
  cityId: string;
  cityName: string;
}

//城市列表
const activityCityList = [{
  cityId: "110100", cityName: "北京"
}, {
  cityId: "310100", cityName: "上海"
}, {
  cityId: "440100", cityName: "广州"
}, {
  cityId: "440300", cityName: "深圳"
}, {
  cityId: "410100", cityName: "郑州"
}, {
  cityId: "330100", cityName: "杭州"
}, {
  cityId: "510100", cityName: "成都"
},{
  cityId: "-1", cityName: "其他"
}];

const ChooseCityModal: FC<Props> = props => {
  const [selectCityObj, setSelectCityObj] = useState<LocationCityType>({
    cityId: '',
    cityName: '',
  }); // 选中的城市对象

    /* 点击某个城市选项 */
  const _clickOneCity = (e: any) => {
    const cityName = e.target.innerHTML;
    const cityObj = activityCityList.filter(item => {
      return item.cityName === cityName;
    });
    setSelectCityObj(cityObj[0]);
  };

    /* 点击确定按钮 */
  const _clickChooseCityOk = () => {
    console.log(`选中的城市为:${selectCityObj.cityName}`);
  };

    /* 点击随便看看按钮 */
  const _clickChooseCityCancel = () => {
   console.log('此处可执行关闭对话框的函数');
  };

  return (
    <div className='choose-city-box'>
      <div className='choose-city-content'>
        <div className='choose-city-form'>

          <div className='city-choose-part'>
            {activityCityList.length > 0 &&
              activityCityList.map((item: LocationCityType) => {
                return (
                  <div
                    key={item.cityId}
                    className={
                      item?.cityId === selectCityObj?.cityId
                        ? 'city-part city-part-select'
                        : 'city-part'
                    }
                    onClick={_clickOneCity}
                  >
                    {item.cityName}
                  </div>
                );
              })}
          </div>
        </div>

        <div className='button-part'>
          <Button
            className='close-modal-botton'
            onClick={_clickChooseCityCancel}
          >
            随便看看
          </Button>
          <Button className='submit-modal-button' onClick={_clickChooseCityOk}>
            确定
          </Button>
        </div>

      </div>
    </div>
  );
};
export default ChooseCityModal;

.choose-city-box {
	position: fixed;
	width: 100%;
	top: 0;
	bottom: 0;
	z-index: 100;
	background: rgba(0, 0, 0, .7);

	.choose-city-content {
		position: relative;
		background: #FFF;
		top: 142.5px;
		left: 37.5px;
		width: 300px;
		height: 324px;
		border-radius: 4px;

		.choose-city-form {
			padding: 24px 20px;
		}
	}
}

//display: grid实现多行多列布局,存在兼容问题,只兼容iOS10.3及以上版本
.city-choose-part {
	margin-top: 20px;
	display: grid;  
	grid-template-columns: repeat(auto-fill, 76px);
	grid-row-gap: 12px;
	grid-column-gap: 16px;

	.city-part {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 76px;
		height: 38px;
		border: .5px solid #D2D2D2;
		border-radius: 2px;
		font-size: 14px;
		color: #7C7D7E;
	}

	.city-part-select {
		border: .5px solid #FF6000;
		color: #FF4D00;
	}
}


 //display: flex实现多行多列布局,兼容性较好,ios都可以兼容
.city-choose-part {
  display: flex;  
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-top: 20px;

  .city-part {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
    margin-right: 16px;
    width: 76px;
    height: 38px;
    border: .5px solid #D2D2D2;
    border-radius: 2px;
    font-size: 14px;
    color: #7C7D7E;
  }

  .city-part-select {
    border: .5px solid #FF6000;
    color: #FF4D00;
  }
}

.city-choose-part .city-part:nth-of-type(3n) {
  margin-right: 0;
}



.button-part {
	position: absolute;
	left: 20px;
	bottom: 20px;
	height: 44px;

	.close-modal-botton {
		color: #7C7D7E;
		background: #FFF; 
	}

	.submit-modal-button {
		margin-left: 12px;
		color: #FFF;
		background: #FF6000;
	}

	.close-modal-botton,
	.submit-modal-button {
		height: 100%;
		width: 124px;
		font-size: 14px;
		border-radius: 2px;
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值