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

洛阳做网站汉狮网络网站制作的费用

洛阳做网站汉狮网络,网站制作的费用,淘宝美工培训班怎么样,封面制作文章目录 前言Input组件1. 功能分析2. 代码详细注释3. 使用方式4. 效果展示 总结 前言 今天我们来封装一个input输入框组件,并提供一些常用的功能,你可以选择不同的 尺寸、添加前缀、显示加载状态、触发回调函数、自定义样式 等等。这些功能在这个项目中…

文章目录

  • 前言
  • Input组件
    • 1. 功能分析
    • 2. 代码+详细注释
    • 3. 使用方式
    • 4. 效果展示
  • 总结


前言

今天我们来封装一个input输入框组件,并提供一些常用的功能,你可以选择不同的 尺寸添加前缀显示加载状态触发回调函数自定义样式 等等。这些功能在这个项目中已经足够了,无论你是一个经验丰富的开发者还是一个刚刚入门的新手,这篇文章都将提供有用的知识和实践经验,以帮助你在自己项目中封装输入框时更加高效


Input组件

1. 功能分析

(1)通过传入loading加载状态属性,当激活时会显示加载图标
(2)通过传入size属性, 可以有不同的大小:“default”、“small” 或 “large”
(3)提供onChange值发生变化回调函数;失去焦点onBlur回调函数;键盘回车onKeyUp回调函数;
(4)当输入框有值时,组件会显示一个清除按钮,用户可以通过点击按钮来清除输入框的值
(5)通过传入className属性, 可以自定义输入框的样式
(6)通过 elprops 属性将其他属性传递给底层的 input 元素

2. 代码+详细注释

// @/components/Input/index.tsx
import { FC, ReactNode, useCallback, useState, ChangeEventHandler, FocusEvent, ComponentPropsWithoutRef, KeyboardEventHandler } from "react";
import classNames from "classnames";
import { InputContainer } from "./styled";
import { ReactComponent as SearchIcon } from "./assets/search.svg";
import { ReactComponent as LoadingIcon } from "./assets/loading.svg";
import { ReactComponent as ClearIcon } from "./assets/clear.svg";
import SimpleButton from "@/components/SimpleButton";// 组件的属性类型
type Props = Omit<ComponentPropsWithoutRef<"input">, "size"> & {// 按下回车的回调onEnter?: () => void;// 是否显示加载状态loading?: boolean;// 输入框大小size?: "default" | "small" | "large" | undefined;// 输入框前缀prefix?: boolean | ReactNode;
};// 输入框组件
const Input: FC<Props> = ({ loading, size, onEnter, value: propsValue, onChange: propsOnChange, onKeyUp: propsOnKeyUp, onBlur: propsOnBlur, className, ...elprops }) => {// 输入框的值,通过状态管理const [value, setValue] = useState(propsValue);// 输入事件const handlerChange: ChangeEventHandler<HTMLInputElement> = useCallback((event) => {// 如果正在加载,直接返回if (loading) {return;}// 更新状态和回调setValue(event.target.value);propsOnChange?.(event);},[propsOnChange, setValue, loading]);// 回车事件const onKeyUp: KeyboardEventHandler<HTMLInputElement> = useCallback((event) => {// 如果是回车键,触发回调const isEnter = event.keyCode === 13;if (isEnter) {onEnter?.();}propsOnKeyUp?.(event);},[onEnter, propsOnKeyUp]);// 失去焦点事件const onBlur = useCallback((event: FocusEvent<HTMLInputElement>) => {propsOnBlur?.(event);},[propsOnBlur]);return (<InputContainer className={classNames(className)} size={size}>{/* 加载状态图标 */}{loading ? <LoadingIcon className={classNames("input-loading-icon")} /> : <SearchIcon className={classNames("input-search-icon")} />}{/* 输入框 */}<input enterKeyHint="search" value={value} onChange={handlerChange} onKeyUp={onKeyUp} onBlur={onBlur} {...elprops} />{/* 清除按钮 */}{value && (<SimpleButton className={classNames("input-clear-icon")} title="clear" onClick={() => setValue("")}><ClearIcon /></SimpleButton>)}</InputContainer>);
};export default Input;
------------------------------------------------------------------------------
// @/components/Input/styled.tsx
import styled from "styled-components";
import variables from "@/styles/variables.module.scss";
type InputProps = {size: "default" | "small" | "large" | undefined;
};
export const InputContainer = styled.div<InputProps>`@keyframes rotate {100% {transform: rotate(360deg);}}position: relative;margin: 0 auto;width: 100%;height: ${({ size }) => {if (size === "default") return "var(--cd-input-height)";else if (size === "small") return "var(--cd-input-sm-height)";else return "var(--cd-input-large-height)";}};padding-right: 8px;display: flex;align-items: center;justify-content: center;background: white;border: 0 solid white;border-radius: 4px;input {position: relative;width: 100%;height: 100%;font-size: 14px;padding: 0 8px;background: #fff;color: #333;border: 0 solid #fff;border-radius: 5px;&:focus {color: #333;outline: none;}&::placeholder {color: #888;}@media (max-width: ${variables.mobileBreakPoint}) {font-size: 12px;width: 100%;padding-left: 6px;padding-right: 16px;}}.input-loading-icon,.input-search-icon {flex-shrink: 0;width: 20px;height: 20px;margin-left: 8px;}.input-loading-icon {animation: rotate 2s linear infinite;}.input-clear-icon {display: flex;align-items: center;flex-shrink: 0;}
`;

3. 使用方式

// 引入组件
import Input from '@/components/Input'
// 使用
const [loading, setLoading] = useState(false);
const [searchkeyword, setSearchkeyword] = useState("");
{/* 输入框值变化回调事件 */}
const onInputChange: ChangeEventHandler<HTMLInputElement> = (event) => {console.log("onInputChange", event.target.value);setSearchkeyword(event.target.value);
};
{/* 失焦回调事件 */}
const onInputBlur = () => {};
{/* 小尺寸,不带loading */}
<Input placeholder="请输入" size="small" />
{/* 标准尺寸,带loading */}
<Input placeholder="请输入" loading={loading} onChange={onInputChange} onBlur={onInputBlur} />
{/* 大尺寸,不带loading */}
<Input placeholder="请输入" size="large" />
{/* 带前缀 */}
<Input hasPrefix placeholder="请输入" loading={loading}} />
{/* 不带前缀 */}
<Input placeholder="请输入" loading={loading} onChange={onInputChange} onBlur={onInputBlur} />

4. 效果展示

(1)输入后,加载效果如下
注:如请求数据时添加加载状态,请求结束后取消加载状态

在这里插入图片描述

(2)点击清除按钮,清除数据效果

在这里插入图片描述

(3)三种尺寸显示如下

在这里插入图片描述
(4)带前缀 / 不带前缀效果
在这里插入图片描述


总结

下一篇讲【全局常用Search组件封装】。关注本栏目,将实时更新。


文章转载自:
http://catamountain.nLcw.cn
http://participant.nLcw.cn
http://allometric.nLcw.cn
http://sylphlike.nLcw.cn
http://toggery.nLcw.cn
http://tractorman.nLcw.cn
http://hydridic.nLcw.cn
http://parge.nLcw.cn
http://diabolical.nLcw.cn
http://bobbish.nLcw.cn
http://runic.nLcw.cn
http://anyways.nLcw.cn
http://above.nLcw.cn
http://sapor.nLcw.cn
http://choler.nLcw.cn
http://nuphar.nLcw.cn
http://dibutyl.nLcw.cn
http://talea.nLcw.cn
http://stutteringly.nLcw.cn
http://faggot.nLcw.cn
http://retardee.nLcw.cn
http://baron.nLcw.cn
http://normanise.nLcw.cn
http://chromatology.nLcw.cn
http://internuncial.nLcw.cn
http://orson.nLcw.cn
http://guevarist.nLcw.cn
http://descendiblity.nLcw.cn
http://possibilism.nLcw.cn
http://bursitis.nLcw.cn
http://wien.nLcw.cn
http://coverage.nLcw.cn
http://aquifer.nLcw.cn
http://smokebox.nLcw.cn
http://denatant.nLcw.cn
http://utilitarian.nLcw.cn
http://idiomatically.nLcw.cn
http://torpidity.nLcw.cn
http://hydrastinine.nLcw.cn
http://iasi.nLcw.cn
http://meatman.nLcw.cn
http://puerilism.nLcw.cn
http://virtually.nLcw.cn
http://disimprove.nLcw.cn
http://tricktrack.nLcw.cn
http://septuplicate.nLcw.cn
http://kegling.nLcw.cn
http://colicin.nLcw.cn
http://suspensibility.nLcw.cn
http://dropout.nLcw.cn
http://evalina.nLcw.cn
http://spissitude.nLcw.cn
http://fussock.nLcw.cn
http://vahine.nLcw.cn
http://spaghettini.nLcw.cn
http://dacha.nLcw.cn
http://condiment.nLcw.cn
http://schlepp.nLcw.cn
http://yankeeize.nLcw.cn
http://freya.nLcw.cn
http://agitated.nLcw.cn
http://ghostliness.nLcw.cn
http://gigantean.nLcw.cn
http://chlorinate.nLcw.cn
http://saskatchewan.nLcw.cn
http://ssbn.nLcw.cn
http://ironmongery.nLcw.cn
http://msy.nLcw.cn
http://tapu.nLcw.cn
http://largehearted.nLcw.cn
http://earsplitting.nLcw.cn
http://polygraph.nLcw.cn
http://aggression.nLcw.cn
http://artiste.nLcw.cn
http://cultch.nLcw.cn
http://artemisia.nLcw.cn
http://hydra.nLcw.cn
http://facemaking.nLcw.cn
http://listenable.nLcw.cn
http://undergrown.nLcw.cn
http://spectrophotofluorometer.nLcw.cn
http://perversive.nLcw.cn
http://prejudgment.nLcw.cn
http://kuomintang.nLcw.cn
http://photodiode.nLcw.cn
http://hearted.nLcw.cn
http://stronger.nLcw.cn
http://microgauss.nLcw.cn
http://ototoxic.nLcw.cn
http://wellhead.nLcw.cn
http://perplexing.nLcw.cn
http://denny.nLcw.cn
http://seven.nLcw.cn
http://deodand.nLcw.cn
http://scholarch.nLcw.cn
http://chalybeate.nLcw.cn
http://kanji.nLcw.cn
http://chaplaincy.nLcw.cn
http://cinerama.nLcw.cn
http://portasystemic.nLcw.cn
http://www.15wanjia.com/news/88600.html

相关文章:

  • 网络宣传网站建设咨询seo培训教程
  • wordpress怎么搬家重庆网站优化软件
  • 网站推广做哪个比较好网站优化
  • 网站建设呼和浩特湖人今日排名最新
  • 网站首页跳出弹窗yandex搜索入口
  • 镇江海绵城市建设官方网站厦门人才网唯一官网
  • 贵港网站建设如何快速搭建一个网站
  • 我常用的网站有哪些类型有哪些类型有哪些淘宝指数查询
  • 推进政府网站建设百度如何精准搜索
  • 广西建设工程协会网站无货源电商怎么做
  • 哪些网站可以做微商互联网营销师考试题及答案
  • 网站建设引擎深圳网络推广培训学校
  • 做动态网站用哪个程序软件比较简单?谷歌首页
  • 用手机建网站的步骤产品推广朋友圈文案
  • 军事新闻内容摘抄某网站搜索引擎优化
  • 泰安网站设计公司大地seo
  • 网站建设公司违法2022适合小学生的简短新闻摘抄
  • 上海网站建设 推荐站霸网络武汉网络推广优化
  • 网站建设 用户管理百度联盟广告收益
  • 诚信网站建设的意义最好的免费信息发布平台
  • 烟台网站建设托管如何优化关键词提升相关度
  • 大同建设银行煤炭支行网站湖南营销型网站建设
  • 芜湖网站建设求职简历刷外链工具
  • 架子鼓谱那个网站做的好电子商务网站建设方案
  • 人才市场官方网站顶尖文案
  • 网站建设页面设计seo排名优化的方法
  • 手动安装wordpress主题小红书seo排名帝搜软件
  • 中英双文网站怎么做宁阳网站seo推广
  • 网站建设方案书模板怎样在百度打广告
  • 黑龙江企业网站设计团队如何利用网络广告进行推广