Giter VIP home page Giter VIP logo

rn-read-more-text's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.