项目作者: YIZHUANG

项目描述 :
A react collapsible library/accordion supports nested elements with custom styling and common use cases
高级语言: JavaScript
项目地址: git://github.com/YIZHUANG/react-simple-collapsible-element.git


react-simple-collapsible-element

npm Version

Demo

  1. See example for demo, npm install and npm start

Why use this?

  1. Simple, Lightweight, Production-ready, has all the common use cases covered. Data for the collapsible can be jsx or
  2. pure JSON data coming from a CMS by non-technical people.

Install

  1. $ npm install react-simple-collapsible-element --save

Usage

  1. $ Production-ready Simple react collapsible supports nested element and custom styling.

example1.

  1. import Collapsible from "react-simple-collapsible-element";
  2. const data = [
  3. {
  4. title: "title 1",
  5. // title: <myReactComponent ></myReactComponent> this is ok too!
  6. // title: (open) => open ? <PurchaseForm ></PurchaseForm> : <ProductCard ></ProductCard> this is ok too!
  7. content: "content 1" // content: <PayButton ></PayButton> or content: (open) => <PayButton isActive={open} ></PayButton>
  8. },
  9. {
  10. // title and content can be a function or non-function.
  11. // open props is only true when the children which is content is open.
  12. title: open => (
  13. <h1 style={{ color: open ? "orange" : "white " }}>title 2</h1>
  14. ),
  15. // title: <h1>title 2</h1> this is ok too!
  16. contentStyle: { backgroundColor: "red" }, // can be a classname also.
  17. activeContentStyle: "activeContentStyle", // classname
  18. content: open => (
  19. <ul>
  20. <li>{open ? "This content is currently open" : ""}</li>
  21. <li>content 2</li>
  22. <li>content 2</li>
  23. </ul>
  24. )
  25. }
  26. ];
  27. return <Collapsible data={data} />;

example2.

  1. import Collapsible from 'react-simple-collapsible-element';
  2. const data = [
  3. {
  4. title: (open) => <h1>title 1 { open ? 'this is current opened!' : 'its close ATM'}</h1>,
  5. content: <span>content 1</span>,
  6. contentStyle: 'content-class'
  7. },
  8. {
  9. title: 'title 2',
  10. content: [
  11. {
  12. title: 'title 2 title 1',
  13. content: 'title 2 title 1 content',
  14. titleStyle: { backgroundColor: 'black' }, // can be a classname also.
  15. activeTitleStyle: 'activeTitleStyle',
  16. contentStyle: { backgroundColor: 'red' }, // can be a classname also.
  17. activeContentStyle: 'activeContentStyle'
  18. }
  19. ]
  20. }
  21. ];
  22. return <Collapsible keepOpen={true or false} data={data} />;

Title and Content as a function

  1. When title and content are functions, it takes a boolean as a params, it is false by default if the Collapsible is current closed.

Props

Name Type Default Description
title Element, function, string 'div' parent
content Element, string, function, array div child
keepOpen bool false whether or not keep the current element open when click on other element
customTransition String height .3s ease-out custom transition
titleStyle String / Object undefined Can be a style object or multiple classnames together.
contentStyle String / Object undefined Can be a style object or multiple classnames together.
activeTitleStyle String undefined a classname to pass to when the title is active.
activeContentStyle String undefined a classname to pass to when the content is active.