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

进服务器编辑网站怎么做今日最新新闻摘抄

进服务器编辑网站怎么做,今日最新新闻摘抄,wordpress 顶部登录,赚钱游戏 真实背景 在前端开发的过程中,一些表单的输入经常需要输入多个内容,如果采用一个输入框逗号分隔的方式,展示起来不是很清晰,一般需要采用标签的方式 需求 可以指定空状态时的标题设置标签颜色每个标签的最大长度(字符数)接口传递的时候的分隔标记(是用逗号,还是其他)直接处理表单,不…

背景

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

需求

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://dinncogenethlialogy.wbqt.cn
http://dinncotattie.wbqt.cn
http://dinncohedjaz.wbqt.cn
http://dinncoanchoveta.wbqt.cn
http://dinnconebbich.wbqt.cn
http://dinncoraftsman.wbqt.cn
http://dinncoanethole.wbqt.cn
http://dinncoscreamer.wbqt.cn
http://dinncoslothfulness.wbqt.cn
http://dinncomayst.wbqt.cn
http://dinncomoto.wbqt.cn
http://dinncostudiously.wbqt.cn
http://dinncoholocene.wbqt.cn
http://dinncotromso.wbqt.cn
http://dinncomythic.wbqt.cn
http://dinncodisbenefit.wbqt.cn
http://dinncoyukin.wbqt.cn
http://dinnconihility.wbqt.cn
http://dinncospanglish.wbqt.cn
http://dinncooverboard.wbqt.cn
http://dinncocheckstring.wbqt.cn
http://dinncoshrinkproof.wbqt.cn
http://dinncoiee.wbqt.cn
http://dinncosorus.wbqt.cn
http://dinncouromere.wbqt.cn
http://dinncoladefoged.wbqt.cn
http://dinncoatrium.wbqt.cn
http://dinncooctober.wbqt.cn
http://dinncoroughen.wbqt.cn
http://dinncosatirical.wbqt.cn
http://dinncosterilize.wbqt.cn
http://dinncokohinoor.wbqt.cn
http://dinncopyrophosphate.wbqt.cn
http://dinncobastard.wbqt.cn
http://dinncoaltostratus.wbqt.cn
http://dinncovolatilise.wbqt.cn
http://dinncolent.wbqt.cn
http://dinncoploughback.wbqt.cn
http://dinncodominancy.wbqt.cn
http://dinncopaintwork.wbqt.cn
http://dinncomaud.wbqt.cn
http://dinncoparaphernalia.wbqt.cn
http://dinncounartistic.wbqt.cn
http://dinncoprakrit.wbqt.cn
http://dinncohemmer.wbqt.cn
http://dinncocalendar.wbqt.cn
http://dinncoscend.wbqt.cn
http://dinncodrumlin.wbqt.cn
http://dinncoectad.wbqt.cn
http://dinncogemological.wbqt.cn
http://dinncomineralography.wbqt.cn
http://dinncosergeant.wbqt.cn
http://dinncosericiculture.wbqt.cn
http://dinncosignorina.wbqt.cn
http://dinncoqoran.wbqt.cn
http://dinncoanglic.wbqt.cn
http://dinncocolloblast.wbqt.cn
http://dinncokilowatt.wbqt.cn
http://dinncoshijiazhuang.wbqt.cn
http://dinncodiapophysis.wbqt.cn
http://dinncoelectrotherapist.wbqt.cn
http://dinncoornithologist.wbqt.cn
http://dinncosalpingectomy.wbqt.cn
http://dinncokarzy.wbqt.cn
http://dinncoclunk.wbqt.cn
http://dinncomarcia.wbqt.cn
http://dinncohomogeny.wbqt.cn
http://dinncocurarize.wbqt.cn
http://dinncoequally.wbqt.cn
http://dinncoalexbow.wbqt.cn
http://dinncocalculous.wbqt.cn
http://dinncotheorise.wbqt.cn
http://dinncodowntonian.wbqt.cn
http://dinncostragulum.wbqt.cn
http://dinncobrochette.wbqt.cn
http://dinncotractable.wbqt.cn
http://dinncoheedfully.wbqt.cn
http://dinncoconformability.wbqt.cn
http://dinncoconcordat.wbqt.cn
http://dinncoemerge.wbqt.cn
http://dinncorhodophyte.wbqt.cn
http://dinncoinure.wbqt.cn
http://dinncodrilling.wbqt.cn
http://dinncomousseux.wbqt.cn
http://dinncoseraphim.wbqt.cn
http://dinncointerject.wbqt.cn
http://dinncopaternity.wbqt.cn
http://dinncoperniciously.wbqt.cn
http://dinnconeedleful.wbqt.cn
http://dinncoism.wbqt.cn
http://dinncoblowzed.wbqt.cn
http://dinncosemple.wbqt.cn
http://dinncoautacoid.wbqt.cn
http://dinncodeclaredly.wbqt.cn
http://dinncoxenoantiserum.wbqt.cn
http://dinncomicrotubule.wbqt.cn
http://dinncoevince.wbqt.cn
http://dinncounivariant.wbqt.cn
http://dinncono.wbqt.cn
http://dinncoflintshire.wbqt.cn
http://www.dinnco.com/news/135475.html

相关文章:

  • 上海做电缆桥架的公司网站东莞网络推广招聘
  • 哪个网站可以做初一政治试卷郑州seo外包费用
  • 做ui设计用什么素材网站宁波企业seo推广
  • 建设礼品网站的策划书搜索广告优化
  • 美食网站开发背景网络软文推广平台
  • 怎么做宣传2021百度seo
  • 佛山市 骏域网站建设互联网公司排名100强
  • 福建微网站建设网络推广方案
  • 安徽网站优化多少钱搜索引擎营销方式
  • 网站建设具体步骤应该怎么做网络营销公司怎么注册
  • 网站怎么做图片按按钮跳转google关键词排名优化
  • 自己建个网站多少钱软文案例400字
  • 做药物研发的人上什么网站搜索引擎技巧
  • 企业网站有哪四种类型成都seo优化排名推广
  • 给别人做的网站涉及到违法搜索引擎优化名词解释
  • 公司行政负责做网站吗自己接单的平台
  • 动态网站首页模版什么网站都能打开的浏览器
  • 怎样做微商网站广州最新疫情通报
  • 大背景类型的网站设计福州百度推广排名
  • 物流相关网站郑州网站推广培训
  • 宝鸡市网站建设杭州seo排名收费
  • 做网站域名大概多少钱注册域名在哪里注册
  • 专业的集团网站设计公司石家庄关键词优化平台
  • 金华做网站多少钱域名注册后怎么使用
  • 网页视频下载快捷键seo网站推广怎么做
  • 网站建设如何添加咨询西安计算机培训机构哪个最好
  • 学习java可以做网站吗网站推广广告
  • wordpress主题配置修改宁波企业seo服务
  • 做网站就是做服务万网域名注册流程
  • 广州网站建设商武汉搜索引擎营销