当前位置: 首页 > news >正文

网站如何做分站系统网站建设与运营的课程总结

网站如何做分站系统,网站建设与运营的课程总结,wordpress最新手册,跨境电商资讯网1、效果 是你要的效果,咱们继续往下看,搜索面板实现省市区下拉,原本有antd的Cascader组件,但是级联组件必须选到子节点,不能只选省,满足不了页面的需求 2、环境准备 1、react18 2、antd 4 3、功能实现 …

 1、效果

是你要的效果,咱们继续往下看,搜索面板实现省市区下拉,原本有antd的Cascader组件,但是级联组件必须选到子节点,不能只选省,满足不了页面的需求

2、环境准备

1、react18

2、antd 4+

3、功能实现

原理:封装一个受控组件,该组件就是两select基本组件

1、首先,导入需要的组件:

import { Select, Space, Tag } from 'antd';

 2、定义2个状态变量来存储选中省和市的下拉枚举

  const [firstOptions, setFirstOptions] = useState<any>([]);const [secondOptions, setSecondOptions] = useState<any>([]);

 3、组件可接收的props子属性 如下:

  •  options: 省市级联数据
  •  value: 已选中的值
  •  width:slect框的宽度
  •  firstPlaceholder 第一个select框的placeholder
  • secondPlaceholder第二个select框的placeholder
  •  onChange: 选中的值发生变化时回调

 4、创建handleFirstChange函数来处理第一个select框的change事件,更新第二个select框的下拉项和值

  // 第一个select生变化const handleFirstChange = (data: any) => {if (!isEmpty(data) && data.value) {let insertIndex = (options || []).findIndex((item: any) => {return item?.value === data?.value;});setSecondOptions(options?.[insertIndex]?.children || []);onChange({ first: [data] });} else {setSecondOptions([]);onChange(null);}};

 5、创建onSecondChange 函数来处理第二个select框的change事件,将选中的值回传给父组件

  // 第二个select发生变化const onSecondChange = (data: any) => {if (!isEmpty(value) && value.first) {if (!isEmpty(data)) {onChange({...value,second: mode === 'multiple' ? (data || []).filter((item: any) => !isNil(item?.label)) : [data],});} else {onChange({ first: value.first, second: null });}} else {onChange(null);}};

 6、最后,使用2个select组件渲染,并将选中状态和change事件绑定到对应的属性上:

return (<><Space wrap={false} direction="horizontal" size={12}><SelectdefaultValue={firstOptions[0]}style={{ width: width }}onChange={handleFirstChange}placeholder={firstPlaceholder || '请选择'}value={value?.first}options={firstOptions}labelInValueallowClear/><Selectstyle={{ width: width }}value={value?.second || []}onChange={onSecondChange}placeholder={secondPlaceholder || '请选择'}options={secondOptions}{...mode === "multiple" ? { mode: "multiple", maxTagCount: 'responsive', tagRender: tagRender } : {}}labelInValueallowClear/></Space></>
)

 7、完整代码如下:

import { Select, Space, Tag } from 'antd';
import clsx from 'clsx';
import { isEmpty, isNil } from 'lodash';
import { useEffect, useState } from 'react';
import './index.less';const MultipleCascaderSelect = (props: any) => {const {options,value,onChange,width = 160,firstPlaceholder,secondPlaceholder,mode = 'multiple'} = props;const [firstOptions, setFirstOptions] = useState<any>([]);const [secondOptions, setSecondOptions] = useState<any>();useEffect(() => {setFirstOptions(options || []);if (Array.isArray(value?.first) && value.first.length) {let findIndex = (options || []).findIndex((item: any) => {return item.value === value.first?.[0].value;});setSecondOptions(options[findIndex]?.children || []);} else {setSecondOptions([]);}}, [options, value]);// 第一个select生变化const handleFirstChange = (data: any) => {if (!isEmpty(data) && data.value) {let insertIndex = (options || []).findIndex((item: any) => {return item?.value === data?.value;});setSecondOptions(options?.[insertIndex]?.children || []);onChange({ first: [data] });} else {setSecondOptions([]);onChange(null);}};// 第二个select发生变化const onSecondChange = (data: any) => {if (!isEmpty(value) && value.first) {if (!isEmpty(data)) {onChange({...value,second: mode === 'multiple' ? (data || []).filter((item: any) => !isNil(item?.label)) : [data],});} else {onChange({ first: value.first, second: null });}} else {onChange(null);}};const tagRender = ({ label, closable, onClose }: any) => {const isLongTag = `${label}`.length > 4;return (<Tagcolor={label.props?.color}closable={closable}onClose={onClose}className={clsx(['text-sky-400 bg-sky-400/10 text-sm font-normal leading-5',// 'border border-solid border-sky-400/50','max-w-[110px] border-none',// 'whitespace-nowrap text-ellipsis overflow-hidden'])}><span>{isLongTag ? `${label.slice(0, 4)}...` : label}</span>{/* {isLongTag ? (<Tooltiptitle={label}key={label}rootClassName={clsx('toolTipCard')}placement="top"><span>{label.slice(0, 4)}...</span></Tooltip>) : (<span>{label}</span>)} */}</Tag>);};return (<><Space wrap={false} direction="horizontal" size={12}><SelectdefaultValue={firstOptions[0]}style={{ width: width }}onChange={handleFirstChange}placeholder={firstPlaceholder || '请选择'}value={value?.first}options={firstOptions}labelInValueallowClear/><Selectstyle={{ width: width }}value={value?.second || []}onChange={onSecondChange}placeholder={secondPlaceholder || '请选择'}options={secondOptions}{...mode === "multiple" ? { mode: "multiple", maxTagCount: 'responsive', tagRender: tagRender } : {}}labelInValueallowClear/></Space></>);
};export default MultipleCascaderSelect;

组件调用

 <MultipleCascaderSelectwidth={162}options={enumData|| []}firstPlaceholder="请选择"secondPlaceholder="请选择"/>

http://www.yayakq.cn/news/525081/

相关文章:

  • 不限流量网站空间新能源汽车价格及图片
  • 宿州网站建设电话wordpress yuti
  • 在线 网站建设开个免费一代发网店
  • 销售平台网站建设做网站如何防止被坑
  • 旅游网站策划方案图片链接生成网站
  • 做网站外链需要多少钱注册文化传媒公司流程和费用
  • 如何开 网站建设公司专业团队原图
  • 云南省建设厅标准员网站手机网站模板制作
  • 网网站建设的公司全国楼市走势最新消息
  • 网站推广论坛北辰做网站公司
  • 影视资源api接口深圳外贸seo网站推广
  • 网站怎么做成小程序wordpress无法安装主题
  • 网站建设费用怎么核算网店美工毕业设计
  • 网站建设设计哪个济南兴田德润简介改域名 wordpress
  • wordpress关停网站新建网站做优化
  • 青岛做网站要多少钱做的网站 为什么百度搜不到
  • 学技能的免费网站温州市住房建设局网站
  • 现在做网站有前途吗多语种网站建设公司
  • 网站设置域名电子商务做网站实训体会
  • 网站内容策划方案wordpress+播放列表
  • 中国网站服务器哪个好南京十大广告公司
  • 重庆装修网站建设设计动漫的软件
  • 思淘网站建设网站举报官网
  • 南通高端网站建设开发中国城投建设集团有限公司网站
  • 如果自己建立网站平面设计适合女生学吗
  • 安庆做网站电话站长之家
  • 网站建设服务商网页设计在线培训班
  • 成都建设项目环境影响登记网站域名注册以后怎样做网站
  • 域名注册空间网站建设网站技术人员先进事迹
  • 上海网站优化排名公司wordpress导入文件