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

商标logo设计免费生成店铺seo是什么意思

商标logo设计免费生成,店铺seo是什么意思,外贸建站哪家强外贸网站怎么做,VIP视频自助网站建设背景 在前端开发的过程中,一些表单的输入经常需要输入多个内容,如果采用一个输入框逗号分隔的方式,展示起来不是很清晰,一般需要采用标签的方式 需求 可以指定空状态时的标题设置标签颜色每个标签的最大长度(字符数)接口传递的时候的分隔标记(是用逗号,还是其他)直接处理表单,不…

背景

在前端开发的过程中,一些表单的输入经常需要输入多个内容,如果采用一个输入框+逗号分隔的方式,展示起来不是很清晰,一般需要采用标签的方式

需求

59b47a790a3037dee50a566d04f1e23b.png

7edd8e6b322531d15b0f1a28fbe3113a.png

  1. 可以指定空状态时的标题

  2. 设置标签颜色

  3. 每个标签的最大长度(字符数)

  4. 接口传递的时候的分隔标记(是用逗号,还是其他)

  5. 直接处理表单,不需要二次处理

所以需要传入以下内容给该组件

  • title:标题

  • separator:分隔标记

  • maxLength:最大长度

  • color:颜色

  • form,name:处理的表单和对应的字段

const { title = '新增一个', separator = ',', maxLength = 40, color = 'orange', form, name } = props;TagInput.propTypes = {title: PropTypes.string, // 新增一个tag的标题separator: PropTypes.string, // 分隔符maxLength: PropTypes.number, // tag最大长度color: PropTypes.string, // tag颜色form: PropTypes.object, // formkey: PropTypes.string, // form的key
};

代码编写

是否显示输入框

首先需要有一个虚线框的标签

<Tag style={{ background: '#fff', borderStyle: 'dashed' }}><PlusOutlined /> {title}
</Tag>

点击后出现文本输入框

<Input type="text" size="small" style={{ width: 78 }} />

并且锚定这个输入框(出现输入光标)

所以需要有一个状态记录是否显示输入框

const [inputVisible, setInputVisible] = useState(false); // 是否显示输入框

所以上述代码变为:

const saveInputRef = useRef();useEffect(() => {if (inputVisible) saveInputRef.current.focus();
}, [inputVisible]);{inputVisible && (<Input ref={saveInputRef} type="text" size="small" style={{ width: 78 }} />
)}
{!inputVisible && (<Tag onClick={() => setInputVisible(true)} style={{ background: '#fff', borderStyle: 'dashed' }}><PlusOutlined /> {title}</Tag>
)}

useEffect监听输入框是否出现,如果出现,则锚定「saveInputRef.current.focus()」

添加一个标签

为了记录输入框的内容定义一个新的变量

const [inputValue, setInputValue] = useState(''); // 输入框的值<Input ref={saveInputRef} type="text" size="small" style={{ width: 78 }} value={inputValue} onChange={(e) => setInputValue(e.target.value)} />

每次输入内容都会修改inputValue的值

因为有多个标签,先定义一个变量来记录我们已经添加的标签

const [tags, setTags] = useState([]); // 待分隔列表

当鼠标在输入框外部点击或者敲击回车的时候,都需要添加一个标签

所以需要给输入框添加onBlur和onPressEnter方法

<Inputref={saveInputRef}type="text"size="small"style={{ width: 78 }}value={inputValue}onChange={(e) => setInputValue(e.target.value)}onBlur={handleInputConfirm}onPressEnter={handleInputConfirm}
/>

编写添加标签的方法:handleInputConfirm

  • 拿到之前的标签+本次输入的,一起放到tags变量中

  • 给表单设置一下这个值(用分隔标记拼接起来)

  • 隐藏输入框

  • 清空输入框

/** 新增一个tag* */
const handleInputConfirm = () => {if (inputValue && tags.indexOf(inputValue) === -1) {const newTags = [...tags, inputValue];setTags(newTags);form.setFieldsValue({ [name]: newTags?.join(separator) });} else {message.error('请正确输入');}setInputVisible(false);setInputValue('');
};

展示标签

在上述步骤之后,tags中已经添加了我们的标签了,将它展示出来

  • 判断字符串长度,如果大于我们配置的最大长度则裁剪,没有则全部展示

  • 超长的标签增加一个气泡提示,鼠标移动上去后可以看到全部内容

{tags.map((tag) => {const isLongTag = tag.length > maxLength;const tagElem = (<Tag key={tag} color={color}>{isLongTag ? `${tag.slice(0, maxLength)}...` : tag}</Tag>);return isLongTag ? (<Tooltip title={tag} key={tag}>{tagElem}</Tooltip>) : (tagElem);
})}

删除标签

给Tag设置closable和onClose方法

const tagElem = (<Tag key={tag} closable onClose={() => handleClose(tag)} color={color}>{isLongTag ? `${tag.slice(0, 20)}...` : tag}</Tag>
);

handleClose方法:

  • 过滤tags中不需要的tag并更新

  • 重新给表单对应的键值对赋值

/** 删除某个tag* */
const handleClose = (removedTag) => {const updatedTags = tags.filter((tag) => tag !== removedTag);setTags(updatedTags);form.setFieldsValue({ [name]: updatedTags?.join(separator) });
};

编辑状态

当我们处于编辑状态的时候,打开表单后,它原本就有内容了

监听一下表单的内容,如果存在,则使用分隔标记分隔后塞入tags中

useEffect(() => {if (form.getFieldValue(name)) setTags(form.getFieldValue(name).split(separator));}, [form.getFieldValue(name)]);

Antd4.x完整代码

折叠源码

import React, { memo, useEffect, useRef, useState } from 'react';
import { Input, message, Tag, Tooltip } from 'antd';
import PropTypes from 'prop-types';
import { PlusOutlined } from '@ant-design/icons';/** tag形式分隔* */
const TagInput = memo((props) => {const [tags, setTags] = useState([]); // 待分隔列表const [inputVisible, setInputVisible] = useState(false); // 是否显示输入框const [inputValue, setInputValue] = useState(''); // 输入框的值const { title = '新增一个', separator = ',', maxLength = 40, color = 'orange', form, name } = props;const saveInputRef = useRef();useEffect(() => {if (inputVisible) saveInputRef.current.focus();}, [inputVisible]);useEffect(() => {if (form.getFieldValue(name)) setTags(form.getFieldValue(name).split(separator));}, [form.getFieldValue(name)]);/** 删除某个tag* */const handleClose = (removedTag) => {const updatedTags = tags.filter((tag) => tag !== removedTag);setTags(updatedTags);form.setFieldsValue({ [name]: updatedTags?.join(separator) });};/** 新增一个tag* */const handleInputConfirm = () => {if (inputValue && tags.indexOf(inputValue) === -1) {const newTags = [...tags, inputValue];setTags(newTags);form.setFieldsValue({ [name]: newTags?.join(separator) });} else {message.error('请正确输入');}setInputVisible(false);setInputValue('');};return (<>{tags.map((tag) => {const isLongTag = tag.length > maxLength;const tagElem = (<Tag key={tag} closable onClose={() => handleClose(tag)} color={color}>{isLongTag ? `${tag.slice(0, 20)}...` : tag}</Tag>);return isLongTag ? (<Tooltip title={tag} key={tag}>{tagElem}</Tooltip>) : (tagElem);})}{inputVisible && (<Inputref={saveInputRef}type="text"size="small"style={{ width: 78 }}value={inputValue}onChange={(e) => setInputValue(e.target.value)}onBlur={handleInputConfirm}onPressEnter={handleInputConfirm}/>)}{!inputVisible && (<Tag onClick={() => setInputVisible(true)} style={{ background: '#fff', borderStyle: 'dashed' }}><PlusOutlined /> {title}</Tag>)}</>);
});TagInput.propTypes = {title: PropTypes.string, // 新增一个tag的标题separator: PropTypes.string, // 分隔符maxLength: PropTypes.number, // tag最大长度color: PropTypes.string, // tag颜色form: PropTypes.object, // formkey: PropTypes.string, // form的key
};export default TagInput;

Antd3.x完整代码

antd3.x中部分组件的用法不一样,需要修改一下

折叠源码

import React, { useEffect, useRef, useState } from 'react';
import { Icon, Input, message, Tag, Tooltip } from 'antd';
import PropTypes from 'prop-types';/** tag形式分隔* */
const TagInput = React.forwardRef((props, ref) => {const [tags, setTags] = useState([]); // 待分隔列表const [inputVisible, setInputVisible] = useState(false); // 是否显示输入框const [inputValue, setInputValue] = useState(''); // 输入框的值const {title = '新增一个',separator = ',',maxLength = 40,color = 'orange',form,name,} = props;const saveInputRef = useRef();useEffect(() => {if (inputVisible) saveInputRef.current.focus();}, [inputVisible]);useEffect(() => {if (form.getFieldValue(name)) {setTags(form.getFieldValue(name).split(separator));}}, [form.getFieldValue(name)]);/** 删除某个tag* */const handleClose = (removedTag) => {const updatedTags = tags.filter((tag) => tag !== removedTag);setTags(updatedTags);form.setFieldsValue({ [name]: updatedTags?.join(separator) });};/** 新增一个tag* */const handleInputConfirm = () => {if (inputValue && tags.indexOf(inputValue) === -1) {const newTags = [...tags, inputValue];setTags(newTags);form.setFieldsValue({ [name]: newTags?.join(separator) });} else {message.error('请正确输入');}setInputVisible(false);setInputValue('');};return (<>{tags.map((tag) => {const isLongTag = tag.length > maxLength;const tagElem = (<Tagkey={tag}closableonClose={() => handleClose(tag)}color={color}>{isLongTag ? `${tag.slice(0, 20)}...` : tag}</Tag>);return isLongTag ? (<Tooltip title={tag} key={tag}>{tagElem}</Tooltip>) : (tagElem);})}{inputVisible && (<Inputref={saveInputRef}type="text"size="small"style={{ width: 78 }}value={inputValue}onChange={(e) => setInputValue(e.target.value)}onBlur={handleInputConfirm}onPressEnter={handleInputConfirm}/>)}{!inputVisible && (<TagonClick={() => setInputVisible(true)}style={{ background: '#fff', borderStyle: 'dashed' }}><Icon type="plus-circle" /> {title}</Tag>)}</>);
});TagInput.propTypes = {title: PropTypes.string, // 新增一个tag的标题separator: PropTypes.string, // 分隔符maxLength: PropTypes.number, // tag最大长度color: PropTypes.string, // tag颜色form: PropTypes.object, // formkey: PropTypes.string, // form的key
};export default TagInput;

文章转载自:
http://dinncocowish.stkw.cn
http://dinncotrawlerman.stkw.cn
http://dinncoacapulco.stkw.cn
http://dinncoukulele.stkw.cn
http://dinncovaccy.stkw.cn
http://dinncophylogenetic.stkw.cn
http://dinncodeterminist.stkw.cn
http://dinnconasopharyngitis.stkw.cn
http://dinnconutted.stkw.cn
http://dinncosporangiophore.stkw.cn
http://dinncoblotto.stkw.cn
http://dinncomexico.stkw.cn
http://dinncoreplevy.stkw.cn
http://dinncodeerskin.stkw.cn
http://dinncostilt.stkw.cn
http://dinncoincage.stkw.cn
http://dinncopiraeus.stkw.cn
http://dinncoaffectless.stkw.cn
http://dinncoveliger.stkw.cn
http://dinncopensione.stkw.cn
http://dinncoembryonated.stkw.cn
http://dinncoinspective.stkw.cn
http://dinncosanitize.stkw.cn
http://dinncorationalisation.stkw.cn
http://dinncoviewer.stkw.cn
http://dinncomenshevism.stkw.cn
http://dinncobegorra.stkw.cn
http://dinncounsteadiness.stkw.cn
http://dinncotrying.stkw.cn
http://dinncoelectrojet.stkw.cn
http://dinncorhymer.stkw.cn
http://dinncoaptness.stkw.cn
http://dinncosiphonage.stkw.cn
http://dinncotheatrical.stkw.cn
http://dinncolacemaking.stkw.cn
http://dinncospermatogonium.stkw.cn
http://dinncoambitious.stkw.cn
http://dinncoarsenism.stkw.cn
http://dinncocomedones.stkw.cn
http://dinncooiling.stkw.cn
http://dinncoruana.stkw.cn
http://dinncoclownish.stkw.cn
http://dinncobritska.stkw.cn
http://dinncogin.stkw.cn
http://dinncodromometer.stkw.cn
http://dinncounfavorably.stkw.cn
http://dinncowhacker.stkw.cn
http://dinncoalabaman.stkw.cn
http://dinncoplutolatry.stkw.cn
http://dinncomannerist.stkw.cn
http://dinncosocket.stkw.cn
http://dinncoholomorphy.stkw.cn
http://dinncodepurate.stkw.cn
http://dinncofinsbury.stkw.cn
http://dinncolivelong.stkw.cn
http://dinncohaussmannize.stkw.cn
http://dinncotrachea.stkw.cn
http://dinncoretrogress.stkw.cn
http://dinncooutdoors.stkw.cn
http://dinncocommissionaire.stkw.cn
http://dinncostrake.stkw.cn
http://dinncoropedancer.stkw.cn
http://dinncoshalloon.stkw.cn
http://dinncogradin.stkw.cn
http://dinncoangkor.stkw.cn
http://dinncopretext.stkw.cn
http://dinncopishpek.stkw.cn
http://dinncocurricular.stkw.cn
http://dinncotravail.stkw.cn
http://dinncomedically.stkw.cn
http://dinncorhinopharyngocele.stkw.cn
http://dinncomusaceous.stkw.cn
http://dinncoarcherfish.stkw.cn
http://dinncomanageress.stkw.cn
http://dinncohalite.stkw.cn
http://dinncosupranatural.stkw.cn
http://dinncowoopie.stkw.cn
http://dinncoquadrilateral.stkw.cn
http://dinncounliterate.stkw.cn
http://dinncohypothetic.stkw.cn
http://dinncosnidesman.stkw.cn
http://dinncoxanthogenate.stkw.cn
http://dinncotasman.stkw.cn
http://dinncoanhydrous.stkw.cn
http://dinncoamylopectin.stkw.cn
http://dinncoluteotropin.stkw.cn
http://dinncoratite.stkw.cn
http://dinncoweltansicht.stkw.cn
http://dinncoculturable.stkw.cn
http://dinncozambian.stkw.cn
http://dinncosuperfecundation.stkw.cn
http://dinncolayard.stkw.cn
http://dinncoprovocable.stkw.cn
http://dinncoallochromatic.stkw.cn
http://dinncofollies.stkw.cn
http://dinncocajole.stkw.cn
http://dinncolighting.stkw.cn
http://dinncobituminise.stkw.cn
http://dinncofunkia.stkw.cn
http://dinncoslumberous.stkw.cn
http://www.dinnco.com/news/130399.html

相关文章:

  • 珠宝网站模板免费下载云计算培训
  • 源码屋武汉网站建设优化
  • 恩施网页定制现在的seo1发布页在哪里
  • 产品网站建设公司免费的个人网站怎么做
  • 电商品牌排行榜seo营销技巧
  • pc端微信端网站建设soso搜搜
  • 重庆有效的网站推广免费推广软件下载
  • 网站打开速度慢是什么原因纹绣培训班一般价格多少
  • 设计说明模板seo咨询服务价格
  • 广州网页设计学校有哪些优化营商环境发言稿
  • 重庆做商城网站百度付费推广
  • access做网站重庆seo管理平台
  • 做网站表格单边框标记晋江怎么交换友情链接
  • 化妆品网站的设计与实现江苏网页设计
  • 网站建设600分站优缺点长沙百度关键词搜索
  • 做网站的疑问有哪些如何进行网站性能优化?
  • 深圳华企网站建设深圳市昊客网络科技有限公司
  • 工商工事上哪个网站做官方百度app下载
  • 做系统用什么网站好厦门网站推广公司哪家好
  • 现在什么网站比较火做推广网站推广的方法
  • 江门做网站软件网络营销期末考试试题及答案
  • 在线看免费电影网站上海站优云网络科技有限公司
  • 网站建设管理招聘自媒体人15种赚钱方法
  • 在哪个网站可以查做项目中标的搜索引擎优化服务
  • 服务器可以放几个网站做网站哪家好
  • 传奇服务器如何做网站市场调研报告word模板
  • 长沙网站搭建公司联系方式广州竞价托管代运营
  • 佛山微信网站建设多少钱东莞百度seo电话
  • 公司建站模版公司企业网站建设
  • 找人做网站需要注意百度新闻首页新闻全文