React-View-UI组件库封装Loading加载中源码

 更新时间:2022年06月23日 14:54:19   作者:冯心心爱吃肉  
这篇文章主要介绍了React-View-UI组件库封装Loading加载样式,主要包括组件介绍,组件源码及组件测试源码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

组件介绍

Loading组件是日常开发用的很多的组件,这次封装主要包含两种状态的Loading,旋转、省略号,话不多说先看一下组件的文档页面吧:

Loading API能力

组件一共提供了如下的API能力,可以在使用时更灵活:

  1. type表示loading类型,默认是default,当用户需要使用省略样式,设置type=dot即可;
  2. mask配置蒙层,可在loading时遮挡覆盖内容为半透明状态,适用于内容未加载时的遮盖;
  3. loadingText配置加载文字,在图标下显示;
  4. icon配置自定义图标,可配置自己所需要的Icon或svg图标;
  5. width配置自定义宽度;
  6. height配置自定义高度;
  7. style配置loading整体自定义样式;

组件源码

index.tsx:

import React, { FC, useEffect, useRef, useState, Fragment, useMemo } from 'react';
import { LoadingProps } from './interface';
import './index.module.less';
const Loading: FC<LoadingProps> = (props) => {
  const {
    type = 'default',
    mask = false,
    loadingText,
    icon,
    width = '2em',
    height = '2em',
    style = {},
  } = props;
  const timer = useRef<any>(null);
  const [activeDotIndex, setActiveDotIndex] = useState(0);
  useEffect(() => {
    timer.current = setInterval(() => {
      setActiveDotIndex((old) => {
        if (old === 2) {
          old = 0;
        } else {
          old++;
        }
        return old;
      });
    }, 500);
    return () => {
      clearInterval(timer.current);
    };
  }, []);
  const loadingStyle = useMemo(() => {
    const returnStyle = style;
    returnStyle.width = width;
    returnStyle.height = height;
    return returnStyle;
  }, [width, height, style]);
  return (
    <Fragment>
      {mask && <div className="dialog" />}
      {type === 'default' ? (
        <div className="loading" style={loadingStyle}>
          <div className="loading-container">
            {icon || (
              <svg
                fill="none"
                stroke="currentColor"
                stroke-width="4"
                width={width}
                height={height}
                viewBox="0 0 48 48"
                aria-hidden="true"
                focusable="false"
              >
                <path d="M42 24c0 9.941-8.059 18-18 18S6 33.941 6 24 14.059 6 24 6"></path>
              </svg>
            )}
          </div>
          {loadingText && <div className="text">{loadingText}</div>}
        </div>
      ) : (
        <div className="dot-loading">
          {new Array(3).fill('').map((item, index) => {
            return <div className={activeDotIndex === index ? 'dot-active' : 'dot'}>{item}</div>;
          })}
        </div>
      )}
    </Fragment>
  );
};
export default Loading;

组件测试源码

loading.test.tsx:

import React from 'react';
import Loading from '../../Loading/index';
import Enzyme from '../setup';
import mountTest from '../mountTest';
import ReactDOM from 'react-dom';
const { mount } = Enzyme;
let container: HTMLDivElement | null;
mountTest(Loading);
describe('loading', () => {
  beforeEach(() => {
    container = document.createElement('div');
    document.body.appendChild(container);
  });
  afterEach(() => {
    document.body.removeChild(container as HTMLDivElement);
    container = null;
  });
  it('test loading show correctly', () => {
    //测试基础加载
    const loading = mount(<Loading />);
    expect(loading.find('.loading .loading-container svg')).toHaveLength(1);
    expect(loading.find('.loading .text')).toHaveLength(0);
  });
  it('test dot loading show correctly', () => {
    //测试省略号加载
    const loading = mount(<Loading type="dot" />);
    expect(loading.find('.dot-loading')).toHaveLength(1);
  });
  it('test mask loading has dialog', () => {
    //测试加载蒙层
    const loading = mount(<Loading mask />);
    expect(loading.find('.dialog')).toHaveLength(1);
  });
  it('test mask loading has dialog', () => {
    //测试加载蒙层
    const loading = mount(<Loading loadingText="test loading" />);
    expect(loading.find('.loading .text').text()).toBe('test loading');
  });
  it('test diffenent size loading show correctly', () => {
    //测试不同大小loading、loading自定义样式
    const component = <Loading width="3em" height="3em" style={{ marginLeft: '100px' }} />;
    ReactDOM.render(component, container);
    const loadingDom = container?.querySelector('.loading');
    expect(
      loadingDom?.getAttribute('style')?.includes('margin-left: 100px; width: 3em; height: 3em;'),
    );
    const svgDom = loadingDom?.querySelector('svg');
    expect(
      svgDom?.getAttribute('width') === '3em' && svgDom?.getAttribute('height') === '3em',
    ).toBe(true);
  });
});

组件库线上地址

React-View-UI组件库线上链接:http://react-view-ui.com:92/#
github:https://github.com/fengxinhhh/React-View-UI-fs
npm:https://www.npmjs.com/package/react-view-ui

到此这篇关于React-View-UI组件库封装——Loading加载中的文章就介绍到这了,更多相关React-View-UI组件库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React中常用的Hook有哪些

    React中常用的Hook有哪些

    这篇文章主要介绍了react hooks实现原理,文中给大家介绍了useState dispatch 函数如何与其使用的 Function Component 进行绑定,节后实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • 一篇文章教你用React实现菜谱系统

    一篇文章教你用React实现菜谱系统

    本篇文章主要介绍了React实现菜谱软件的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-09-09
  • React 中的列表渲染要加 key的原因分析

    React 中的列表渲染要加 key的原因分析

    这篇文章主要介绍了React 中的列表渲染为什么要加 key,在 React 中我们经常需要渲染列表,比如展示好友列表,文中给大家介绍了列表渲染不提供 key 会如何,通过实例代码给大家介绍的非常详细,需要的朋友一起看看吧
    2022-07-07
  • React实现二级联动(左右联动)

    React实现二级联动(左右联动)

    这篇文章主要为大家详细介绍了React实现二级联动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • React使用fullpage.js实现整屏翻页功能

    React使用fullpage.js实现整屏翻页功能

    最近笔者在完成一个移动端小项目的过程中需要实现整屏翻页的效果;调研完毕之后,最终决定使用pullPage.js实现此功能,fullPage.js使用起来比较方便,并且官网上也给了在react项目中使用的demo,本文记录了这个第三方库的使用过程,感兴趣的朋友可以参考下
    2023-11-11
  • ReactNative 状态管理redux使用详解

    ReactNative 状态管理redux使用详解

    这篇文章主要介绍了ReactNative 状态管理redux使用详解
    2023-03-03
  • Ant Design 组件库之步骤条实现

    Ant Design 组件库之步骤条实现

    这篇文章主要为大家介绍了Ant Design组件库之步骤条实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 详解react-router 4.0 下服务器如何配合BrowserRouter

    详解react-router 4.0 下服务器如何配合BrowserRouter

    这篇文章主要介绍了详解react-router 4.0 下服务器如何配合BrowserRouter,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • React使用Hooks从服务端获取数据的完整指南

    React使用Hooks从服务端获取数据的完整指南

    本文将从基础到高级用法,详细介绍如何在 React 项目中优雅地使用 Hooks 进行服务端数据获取,涵盖错误处理、加载状态、性能优化等核心场景,并提供可直接复用的代码模板,需要的朋友可以参考下
    2025-03-03
  • React SSG实现Demo详解

    React SSG实现Demo详解

    这篇文章主要为大家介绍了React SSG实现Demo详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪<BR>
    2023-07-07

最新评论