最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React-View-UI組件庫封裝Loading加載中源碼

 更新時間:2022年06月23日 14:54:19   作者:馮心心愛吃肉  
這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

組件介紹

Loading組件是日常開發(fā)用的很多的組件,這次封裝主要包含兩種狀態(tài)的Loading,旋轉、省略號,話不多說先看一下組件的文檔頁面吧:

Loading API能力

組件一共提供了如下的API能力,可以在使用時更靈活:

  1. type表示loading類型,默認是default,當用戶需要使用省略樣式,設置type=dot即可;
  2. mask配置蒙層,可在loading時遮擋覆蓋內容為半透明狀態(tài),適用于內容未加載時的遮蓋;
  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組件庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • React中常用的Hook有哪些

    React中常用的Hook有哪些

    這篇文章主要介紹了react hooks實現(xiàn)原理,文中給大家介紹了useState dispatch 函數(shù)如何與其使用的 Function Component 進行綁定,節(jié)后實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • 一篇文章教你用React實現(xiàn)菜譜系統(tǒng)

    一篇文章教你用React實現(xiàn)菜譜系統(tǒng)

    本篇文章主要介紹了React實現(xiàn)菜譜軟件的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • React?中的列表渲染要加?key的原因分析

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

    這篇文章主要介紹了React?中的列表渲染為什么要加?key,在?React?中我們經常需要渲染列表,比如展示好友列表,文中給大家介紹了列表渲染不提供?key?會如何,通過實例代碼給大家介紹的非常詳細,需要的朋友一起看看吧
    2022-07-07
  • React實現(xiàn)二級聯(lián)動(左右聯(lián)動)

    React實現(xiàn)二級聯(lián)動(左右聯(lián)動)

    這篇文章主要為大家詳細介紹了React實現(xiàn)二級聯(lián)動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • React使用fullpage.js實現(xiàn)整屏翻頁功能

    React使用fullpage.js實現(xiàn)整屏翻頁功能

    最近筆者在完成一個移動端小項目的過程中需要實現(xiàn)整屏翻頁的效果;調研完畢之后,最終決定使用pullPage.js實現(xiàn)此功能,fullPage.js使用起來比較方便,并且官網(wǎng)上也給了在react項目中使用的demo,本文記錄了這個第三方庫的使用過程,感興趣的朋友可以參考下
    2023-11-11
  • ReactNative?狀態(tài)管理redux使用詳解

    ReactNative?狀態(tài)管理redux使用詳解

    這篇文章主要介紹了ReactNative?狀態(tài)管理redux使用詳解
    2023-03-03
  • Ant?Design?組件庫之步驟條實現(xiàn)

    Ant?Design?組件庫之步驟條實現(xiàn)

    這篇文章主要為大家介紹了Ant?Design組件庫之步驟條實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 詳解react-router 4.0 下服務器如何配合BrowserRouter

    詳解react-router 4.0 下服務器如何配合BrowserRouter

    這篇文章主要介紹了詳解react-router 4.0 下服務器如何配合BrowserRouter,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • React使用Hooks從服務端獲取數(shù)據(jù)的完整指南

    React使用Hooks從服務端獲取數(shù)據(jù)的完整指南

    本文將從基礎到高級用法,詳細介紹如何在 React 項目中優(yōu)雅地使用 Hooks 進行服務端數(shù)據(jù)獲取,涵蓋錯誤處理、加載狀態(tài)、性能優(yōu)化等核心場景,并提供可直接復用的代碼模板,需要的朋友可以參考下
    2025-03-03
  • React?SSG實現(xiàn)Demo詳解

    React?SSG實現(xiàn)Demo詳解

    這篇文章主要為大家介紹了React?SSG實現(xiàn)Demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>
    2023-07-07

最新評論

金秀| 富顺县| 汶上县| 西贡区| 应城市| 女性| 逊克县| 东海县| 耒阳市| 平远县| 东莞市| 江孜县| 马边| 吕梁市| 岳普湖县| 黎平县| 平邑县| 通城县| 竹溪县| 额尔古纳市| 玉山县| 佛学| 南澳县| 中阳县| 大新县| 郧西县| 桃园市| 海安县| 宝鸡市| 马尔康县| 通河县| 中山市| 定日县| 克什克腾旗| 长顺县| 栾城县| 呼玛县| 横峰县| 烟台市| 荥经县| 华宁县|