All Projects → Rahim-Chan → Taro Listview

Rahim-Chan / Taro Listview

taro框架长列表方案 :集成下拉刷新、骨架屏、无限滚动、图片懒加载;

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Taro Listview

Node.js
一步一步学习Node.js,带你从零开始学习Node.js!本仓库是自己总结的Node.js学习图文教程,里面有学习案列和源代码(pubdreamcc原创,欢迎转载,欢迎star)
Stars: ✭ 181 (-8.12%)
Mutual labels:  npm
React In Viewport
Detect if React component is in viewport
Stars: ✭ 183 (-7.11%)
Mutual labels:  lazyload
Tink
a dependency unwinder for javascript
Stars: ✭ 2,176 (+1004.57%)
Mutual labels:  npm
Atom Autocomplete Module Import
⚛️ Search & install npm packages from import/require statements.
Stars: ✭ 182 (-7.61%)
Mutual labels:  npm
Darkmode.js
🌓 Add a dark-mode / night-mode to your website in a few seconds
Stars: ✭ 2,339 (+1087.31%)
Mutual labels:  npm
Dep
A little Node.js dependency installer
Stars: ✭ 186 (-5.58%)
Mutual labels:  npm
Ni
💡 Use the right package manager
Stars: ✭ 179 (-9.14%)
Mutual labels:  npm
100 Days Of Code Frontend
Curriculum for learning front-end development during #100DaysOfCode.
Stars: ✭ 2,419 (+1127.92%)
Mutual labels:  npm
Cheat Sheets
Cheat Sheets 🐭🤖👀
Stars: ✭ 185 (-6.09%)
Mutual labels:  npm
Pronote Api
(Tout langage) API compatible Pronote 2020/2021
Stars: ✭ 186 (-5.58%)
Mutual labels:  npm
Horus
🎯 A gRPC-Node Distributed Tracing and Monitoring Tool.
Stars: ✭ 184 (-6.6%)
Mutual labels:  npm
React Material Components Web
React wrapper of Google's Material Components Web
Stars: ✭ 184 (-6.6%)
Mutual labels:  npm
Skeletonlayout
Skeleton view pattern for Android
Stars: ✭ 186 (-5.58%)
Mutual labels:  skeleton
Greenkeeper Lockfile
🔒 Your lockfile, up to date, all the time
Stars: ✭ 181 (-8.12%)
Mutual labels:  npm
Skeleton Nova Tool
A skeleton repository for Spatie's Nova Packages
Stars: ✭ 191 (-3.05%)
Mutual labels:  skeleton
Npm Run
Run locally-installed node module executables.
Stars: ✭ 180 (-8.63%)
Mutual labels:  npm
Polymer Skeleton
💀 Skeleton for Polymer 3 app with Webpack, PostCSS and Service Workers ready.
Stars: ✭ 185 (-6.09%)
Mutual labels:  skeleton
Corepack
Zero-runtime-dependency package acting as bridge between Node projects and their package managers
Stars: ✭ 196 (-0.51%)
Mutual labels:  npm
Npmvet
A simple CLI tool for vetting npm package versions
Stars: ✭ 193 (-2.03%)
Mutual labels:  npm
Lazy Progressive Enhancement
A lazy image loader designed to enforce progressive enhancement and valid HTML.
Stars: ✭ 188 (-4.57%)
Mutual labels:  lazyload

📦🚀taro-listview

semantic-release npm latest version GitHub Actions status

  • [x] 列表内部懒模块-解决小程序图片过多内存问题
  • [ ] skeleton 拓展状态屏
  • [ ] virtual-list(WIP)【虚拟列表】
  • [x] 支持TaroV3 、NPM版本2.x.x

Documents

安装方式

安装:npm i taro-listviewyarn add taro-listview

使用案例

引入组件

列表 Demo

!!!需给组件设置固定高度

API

ListView

可配置参数

页面状态控制参数

属性 说明 类型 默认值 必传
style - - - -
lazy 开启懒加载(传入字符串为父元素 className 且开启) boolean or string false or '.lazy-view' -
hasMore 加载更多 boolean true true
isEmpty 展示空凭页 boolean - -
isError 展示错误页 boolean - -
needInit 初始化&自动调用下拉刷新方法 boolean - -
distanceToRefresh 下拉刷新距离 number - -
damping 最大下拉距离 number - -
autoHeight 开启自适应高度 boolean - -
lazyStorage Storage Key值用于区分ListView string box -

自定义页面 UI(状态提示语,空白屏、错误屏、底部状态的 UI 自定义)

属性 说明 类型 默认值 必传
color 下拉加载时 loading 的颜色 string #667baf -
emptyText 空白页提示语 string - -
footerLoadedText 列表底部加载完毕提示语 string -
footerLoadingText 列表底部加载中提示语 string 加载中 -
launch *是否开启状态屏的渲染,状态屏节点对应以下 renderXX 属性(如下) object { launchError = false, launchEmpty = false, launchFooterLoaded = false, launchFooterLoading = false } -
renderError 错误页 Taro.Node - -
renderEmpty 空白页 Taro.Node - -
renderFooterLoaded 自定义底部加载完毕 Taro.Node - -
renderFooterLoading 自定义底部加载 Taro.Node - -
indicator 下拉提示语 Object { release = '加载中', activate = '下拉刷新', deactivate = '释放刷新'} -

*错误屏中重新初始化方法与下拉刷新方法一致

import Taro, { Component } from "@tarojs/taro";
import { View, Image } from "@tarojs/components";
import ListView, { LazyBlock } from "taro-listview";

const blankList = [
  {
    author: {},
    title: "this is a example"
  },
  {
    author: {},
    title: "this is a example"
  },
  {
    author: {},
    title: "this is a example"
  },
  {
    author: {},
    title: "this is a example"
  }
];

let pageIndex = 1;

export default class Index extends Component {
  state = {
    isLoaded: false,
    error: false,
    hasMore: true,
    isEmpty: false,
    list: blankList
  };

  getData = async (pIndex = pageIndex) => {
    if (pIndex === 1) this.setState({ isLoaded: false });
    const {
      data: { data }
    } = await Taro.request({
      url: "https://cnodejs.org/api/v1/topics",
      data: {
        limit: 10,
        page: pIndex
      }
    });
    console.log({ data });
    return { list: data, hasMore: true, isLoaded: pIndex === 1 };
  };

  componentDidMount() {
    this.refList.fetchInit();
  }

  pullDownRefresh = async () => {
    pageIndex = 1;
    const res = await this.getData(1);
    this.setState(res);
  };

  onScrollToLower = async fn => {
    const { list } = this.state;
    const { list: newList, hasMore } = await this.getData(++pageIndex);
    this.setState({
      list: list.concat(newList),
      hasMore
    });
    fn();
  };

  refList = {};

  insRef = node => {
    this.refList = node;
  };

  render() {
    const { isLoaded, error, hasMore, isEmpty, list } = this.state;
    return (
      <View className="skeleton lazy-view">
        <ListView
          lazy
          ref={node => this.insRef(node)}
          isLoaded={isLoaded}
          isError={error}
          hasMore={hasMore}
          style={{ height: "100vh" }}
          isEmpty={isEmpty}
          onPullDownRefresh={this.pullDownRefresh}
          onScrollToLower={this.onScrollToLower}
          lazyStorage='lazyView'
        >
          {list.map((item, index) => {
            return (
              <View className="item skeleton-bg" key={index}>
                <LazyBlock current={index} className="avatar" lazyStorage='lazyView'>
                  <Image
                    className="avatar skeleton-radius"
                    src={item.author.avatar_url}
                  />
                </LazyBlock>
                <View className="title skeleton-rect">{item.title}</View>
              </View>
            );
          })}
        </ListView>
      </View>
    );
  }
}

模块懒加载

1.模块需固定同一高度。

2.只能在 ListView 组件内使用,并开启 lazy 模式,且父元素的 className='lazy-view'!!!

3.组件需传入模块遍历后的下标。

API

LazyBlock
属性 说明 类型 默认值 必传
current 传入模块遍历后的下标 number null true
lazyStorage Storage Key值用于区分ListView(获取是哪一个ListView) string box -
import Taro, { Component } from "@tarojs/taro";
import { View, Image } from "@tarojs/components";
import ListView, { LazyBlock } from "taro-listView";

let pageIndex = 1;

export default class Index extends Component {
  state = {
    isLoaded: false,
    error: false,
    hasMore: true,
    isEmpty: false,
    list: []
  };

  getData = async (pIndex = pageIndex) => {
    if (pIndex === 1) this.setState({ isLoaded: false });
    const {
      data: { data }
    } = await Taro.request({
      url: "https://cnodejs.org/api/v1/topics",
      data: {
        limit: 10,
        page: pIndex
      }
    });
    return { list: data, hasMore: true, isLoaded: pIndex === 1 };
  };

  componentDidMount() {
    this.getData();
  }

  onScrollToLower = async fn => {
    const { list } = this.state;
    const { list: newList, hasMore } = await this.getData(++pageIndex);
    this.setState({
      list: list.concat(newList),
      hasMore
    });
    fn();
  };

  render() {
    const { isLoaded, error, hasMore, isEmpty, list } = this.state;
    return (
      <View className="lazy-view">
        <ListView
          lazy
          isLoaded={isLoaded}
          hasMore={hasMore}
          style={{ height: "100vh" }}
          onScrollToLower={this.onScrollToLower}
          lazyStorage='lazyViewBlock'
        >
          {list.map((item, index) => {
            return (
              <View className='item' key={index}>
                <LazyBlock current={index} className='avatar' lazyStorage='lazyViewBlock'>
                  <Image className='avatar' src={item.author.avatar_url} />
                </LazyBlock>
                <View className="title">{item.title}</View>
              </View>
            );
          })}
        </ListView>
      </View>
    );
  }
}

骨架屏

1.因骨架屏是捕捉已有占位数据的样式,再绘制出骨架,所以要先注入默认空白占位数据。

2.且需要一个传入父元素的 className(默认获取为“skeleton”),以便寻找元素下的所有“关节”元素。可通过传入 selector 参数自定义 className。

有且只有捕捉以下提供的“关节”样式名:背景('skeleton-bg')、矩阵('skeleton-rect')、圆形('skeleton-redius')。

3.ListView 组件已嵌套 Skeleton 组件,直接调用对应属性即可。

*“关节”元素需内容撑开,或者固定高度。

API

Skeleton
属性 说明 类型 默认值 必传
isLoaded 骨架屏是否显示(eg:加载第一页时开启) boolean false -
selector 骨架屏外层 class 名称 skeleton - -

骨架屏 Demo

import Taro, { useState } from "@tarojs/taro";
import { View, Button } from "@tarojs/components";
import { Skeleton } from "components";
import "./index.scss";

export default () => {
  const [isLoaded, setLoaded] = useState(false);
  return (
    <View>
      <Button onClick={() => setLoaded(!isLoaded)}>toggle</Button>
      <View className="skeleton">
        <Skeleton isLoaded={isLoaded}>
          {Array(4)
            .fill(1)
            .map(i => (
              <View className="item skeleton-bg" key={i}>
                <View className="avatar skeleton-radius" />
                <View className="title skeleton-rect">title</View>
              </View>
            ))}
        </Skeleton>
      </View>
    </View>
  );
};

方法参数

事件名称 说明 类型 默认值 必传
onPullDownRefresh 下拉刷新触发函数 function - -
onScrollToLower 上拉底触发函数 function - -
onPullDownRefresh = () => {
  getData();
};

onScrollToLower = async fn => {
  await getData();
  fn();
};
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].