npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-read-more-text

v0.0.6

Published

支持查看更多的折叠文本组件

Downloads

47

Readme

rn-read-more-text

npm npm npm npm

这是一个支持设置行数的可折叠文本组件,具备以下功能:

  • Text 组件使用方式一致,只是多了两个参数 limitLinesrenderFooter
  • 如当前文本内容所显示的行数,与最大行数一致,则不显示控制折叠的 footer 组件
  • renderFooter 基于 render-props 方式实现,自定义样式和事件响应更自由方便

运行示例

demo

安装

使用 npm

npm install rn-read-more-text --save

yarn

yarn add rn-read-more-text

使用示例

基础使用

<ReadMoreText
  limitLines={2}
  renderFooter={this.renderFooter}
>
  {this.content}
</ReadMoreText>

// render footer
renderFooter = ({ isShowingAll, toggle }) => (
  <Text
    style={{ color: "blue", alignSelf: "flex-end" }}
    onPress={() => toggle()}
  >
    {isShowingAll ? "Show less" : "Show more"}
  </Text>
);

renderFooter 接收一个带有参数的函数,该参数将包含以下属性:isShowingAlltoggleisShowingAll 代表当前组件是否展开,可用于控制渲染不同状态下的UI;toggle 用于控制切换组件状态(折叠/展开)。

特殊情况

当最终文本内容的行数与设置的最大行数相等时,将不显示 footer,如以下情况:

<ReadMoreText limitLines={1} renderFooter={this.renderFooter}>
  This is one line, need not show footer
</ReadMoreText>

// 文本内容搞好一行,与 limitLines 相等,将不显示 footer
renderFooter = () => <View />

实例引用

另可通过 ref 引用主动控制组件折叠和展开:

<TouchableOpacity
  activeOpacity={0.8}
  style={{ backgroundColor: "rgba(1,1,1,0.1)", padding: 10 }}
  onPress={this.handleManualToggle}
>
  <ReadMoreText
    ref={r => (this.textRef = r)}
    style={{ color: "red", fontSize: 16 }}
    limitLines={2}
    renderFooter={this.renderFooter}
  >
    {this.content}
  </ReadMoreText>
</TouchableOpacity>

// click event
handleManualToggle = () => this.textRef && this.textRef.toggle();

// render footer
renderFooter = ({ isShowingAll, toggle }) => <View />

完整示例见 example

参数说明

名称|类型|默认值|描述 ---|---|---|--- ...TextProps| | |理论上所有 Text 组件的 props 都可用,另注意 numberOfLines 设置无效,改用 limitLines 控制最大行数 limitLines|数字|3|表示最大显示行数 renderFooter|函数|无|用于渲染文本展开,或是折叠时不同的底部组件,其回调参数为:({ isShowingAll, toggle }) => React.Element<any>isShowingAll : boolean,代表文本实时状态,用于渲染不同状态组件toggle : func,调用后折叠/展开文本 onToggleFinish|函数|无|组件状态修改后的回调,将返回对象,格式如:onToggleFinish: ({ isShowingAll }) => void