如何使用结构 UI 在进度条中显示进度完成百分比?

问题描述

我正在使用流畅的 UI 来显示进度条。

我提到的链接https://developer.microsoft.com/en-us/fluentui#/controls/web/progressindicator

我在使用 react 在进度条中显示百分比时遇到了困难。有人可以对此提出建议吗。

谢谢,

解决方法

如果您只想显示百分比数字,那么我所做的就是将百分比添加到 <p> 标签中,并使用用于增加进度条的百分比数字来显示百分比数字。

由于数字按照 docs 从 0 增加到 1,我们可以做的是将输出乘以 100 并取其 Math.floor

Codepen - Demo

const { ProgressIndicator,Fabric: FabricComponent,initializeIcons } = window.Fabric;

// Initialize icons in case this example uses them
initializeIcons();

const intervalDelay = 100;
const intervalIncrement = 0.01;

const ProgressIndicatorBasicExample: React.FunctionComponent = () => {
  const [percentComplete,setPercentComplete] = React.useState(0);

  React.useEffect(() => {
    const id = setInterval(() => {
      setPercentComplete((intervalIncrement + percentComplete) % 1);
    },intervalDelay);
    return () => {
      clearInterval(id);
    };
  });

  return (
    <>
      <ProgressIndicator label="Example title" description="Example description" 
      percentComplete={percentComplete} />
      <p>{Math.floor(percentComplete * 100)}%</p>
    </>
  );
};

const ProgressIndicatorBasicExampleWrapper = () => <FabricComponent><ProgressIndicatorBasicExample /></FabricComponent>;
ReactDOM.render(<ProgressIndicatorBasicExampleWrapper />,document.getElementById('content'))