React 实现自定义进度条(类似于ant design中的progress)

一 说明

antd中的进度条不太符合项目需求,因此需要自己实现一款进度条。具体功能有:**颜色的自定义、最大百分比可以超过100%,自定义百分比文字标签的位置或者不显示标签。**其他功能可以自定义拓展。

二 实现源码

1.react 代码

javascript 复制代码
import React, {useEffect, useState} from 'react';
import styles from './index.module.less';

interface IProgressBarProps {
  percent: number; // 当前进度值
  maxPercent?: number; // 最大百分比(进度)
  color?: string; // 进度值颜色
  showText?: "right" | "center"  // 标签显示在哪里:右边、中间

}

const ProgressBar = (props: IProgressBarProps) => {
  const {percent, color, maxPercent, showText} = props;
  const [textStyle, setTextStyle] = useState<string>("progressTextCenter");
  const newMaxPercent = maxPercent ? maxPercent : 100; // 判断最大百分比
  const progressPercentage = (percent / newMaxPercent) * 100; // 计算百分比
  const newColor = color ? color : "#1677ff"; // 判断颜色(默认为#1677ff)

  useEffect(() => {
    switch (showText) {
      case "right":
        setTextStyle("progressTextRight");
        break;
      case "center":
        setTextStyle("progressTextCenter");
        break;
      default:
        setTextStyle("");
        break;
    }
  }, [showText])

  return (
    <div className={styles.outerContainer}>
      <div className={styles.progressContainer}>
        <div className={styles.progressBar} style={
  
  {width: `${progressPercentage}%`, backgroundColor: newColor}}></div>
      </div>
      {textStyle ? <div className={styles[textStyle]}>{percent}%</div> : ""}
    </div>
  );
};

export default ProgressBar;
  1. less样式 代码
css 复制代码
.outerContainer {
  display: flex;
  justify-content: space-between;
}

.progressContainer {
  width: 100%;
  height: 8px;
  background-color: #E9E9E9;
  border-radius: 10px;
  overflow: hidden;
  position: relative;
}


.progressBar {
  height: 100%;
  transition: width 0.3s ease;
  border-radius: 10px;
}

.progressTextCenter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #000;
  font-size: 12px;
}

.progressTextRight {
  margin-left: 10px;
  margin-top: -3px;
  font-size: 12px;
}

三 代码的使用和效果

  1. 最常规样式:总长度100%,占比80%,文字标签在进度条中间(占比颜色可自定义)。

使用代码:

javascript 复制代码
<MyProgress color={"#1677ff"} maxPercent={100} percent={80} showText={"center"}/>
  1. 扩展样式:总长度150%,占比135%,文字标签在进度条右边(占比颜色可自定义)。

使用代码:

javascript 复制代码
 <MyProgress color={"gold"} maxPercent={150} percent={135} showText={"right"}/>
  1. 扩展样式:不显示文字标签

使用代码

javascript 复制代码
   <MyProgress color={"red"} maxPercent={150} percent={135}/>
相关推荐
岛泪2 分钟前
把 el-cascader 的 options 平铺为一维数组(只要叶子节点)
前端·javascript·vue.js
摘星编程16 分钟前
React Native for OpenHarmony 实战:SecureStorage 安全存储详解
安全·react native·react.js
Kiyra1 小时前
阅读 Netty 源码关于 NioEventLoop 和 Channel 初始化部分的思考
运维·服务器·前端
冰暮流星1 小时前
javascript的switch语句介绍
java·前端·javascript
做科研的周师兄1 小时前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
da_vinci_x2 小时前
图标量产:从“手绘地狱”到“风格克隆”?Style Reference 的工业化实战
前端·游戏·ui·prompt·aigc·设计师·游戏美术
利刃大大2 小时前
【ES6】变量与常量 && 模板字符串 && 对象 && 解构赋值 && 箭头函数 && 数组 && 扩展运算符 && Promise/Await/Async
开发语言·前端·javascript·es6
天若有情6732 小时前
ES6 模块与 CommonJS 的区别详解
前端·javascript·es6
大猫会长2 小时前
postgreSQL中,RLS的using与with check
开发语言·前端·javascript
摘星编程2 小时前
React Native for OpenHarmony 实战:ProgressBar 进度条详解
javascript·react native·react.js