Collapse

Collapse - 图1

  1. import React, { Component } from 'react';
  2. import { Collapse, Button, CardBody, Card } from 'reactstrap';
  3. class Example extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.toggle = this.toggle.bind(this);
  7. this.state = { collapse: false };
  8. }
  9. toggle() {
  10. this.setState(state => ({ collapse: !state.collapse }));
  11. }
  12. render() {
  13. return (
  14. <div>
  15. <Button color="primary" onClick={this.toggle} style={{ marginBottom: '1rem' }}>Toggle</Button>
  16. <Collapse isOpen={this.state.collapse}>
  17. <Card>
  18. <CardBody>
  19. Anim pariatur cliche reprehenderit,
  20. enim eiusmod high life accusamus terry richardson ad squid. Nihil
  21. anim keffiyeh helvetica, craft beer labore wes anderson cred
  22. nesciunt sapiente ea proident.
  23. </CardBody>
  24. </Card>
  25. </Collapse>
  26. </div>
  27. );
  28. }
  29. }
  30. export default Example;

Properties

  1. Collapse.propTypes = {
  2. ...Transition.propTypes, // see note below
  3. isOpen: PropTypes.bool,
  4. children: PropTypes.oneOfType([
  5. PropTypes.arrayOf(PropTypes.node),
  6. PropTypes.node
  7. ]),
  8. tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  9. className: PropTypes.node,
  10. navbar: PropTypes.bool,
  11. cssModule: PropTypes.object,
  12. innerRef: PropTypes.object,
  13. };

Collapse is wrapped in a Transition component from react-transition-group/transition. Transition props are passed through to this wrapper. Refer to the Transition documentation for details: http://reactcommunity.org/react-transition-group/transition/.

Events

Use the onEnter, onEntering, onEntered, onExiting and onExited props for callbacks when the Collapse has finished opening (entering) or closing (exiting).

Collapse - 图2

  1. import React, { Component } from 'react';
  2. import { Collapse, Button, CardBody, Card } from 'reactstrap';
  3. class Example extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.onEntering = this.onEntering.bind(this);
  7. this.onEntered = this.onEntered.bind(this);
  8. this.onExiting = this.onExiting.bind(this);
  9. this.onExited = this.onExited.bind(this);
  10. this.toggle = this.toggle.bind(this);
  11. this.state = { collapse: false, status: 'Closed' };
  12. }
  13. onEntering() {
  14. this.setState({ status: 'Opening...' });
  15. }
  16. onEntered() {
  17. this.setState({ status: 'Opened' });
  18. }
  19. onExiting() {
  20. this.setState({ status: 'Closing...' });
  21. }
  22. onExited() {
  23. this.setState({ status: 'Closed' });
  24. }
  25. toggle() {
  26. this.setState(state => ({ collapse: !state.collapse }));
  27. }
  28. render() {
  29. return (
  30. <div>
  31. <Button color="primary" onClick={this.toggle} style={{ marginBottom: '1rem' }}>Toggle</Button>
  32. <h5>Current state: {this.state.status}</h5>
  33. <Collapse
  34. isOpen={this.state.collapse}
  35. onEntering={this.onEntering}
  36. onEntered={this.onEntered}
  37. onExiting={this.onExiting}
  38. onExited={this.onExited}
  39. >
  40. <Card>
  41. <CardBody>
  42. Anim pariatur cliche reprehenderit,
  43. enim eiusmod high life accusamus terry richardson ad squid. Nihil
  44. anim keffiyeh helvetica, craft beer labore wes anderson cred
  45. nesciunt sapiente ea proident.
  46. </CardBody>
  47. </Card>
  48. </Collapse>
  49. </div>
  50. );
  51. }
  52. }
  53. export default Example;

Uncontrolled Collapse

For the most basic use-case, an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledCollapse does not require an isOpen prop. Instead pass a toggler prop. The toggler prop is a string which will run querySelectorAll to find dom elements which will trigger toggle.

Collapse - 图3

  1. import React from 'react';
  2. import { UncontrolledCollapse, Button, CardBody, Card } from 'reactstrap';
  3. const Example = () => (
  4. <div>
  5. <Button color="primary" id="toggler" style={{ marginBottom: '1rem' }}>
  6. Toggle
  7. </Button>
  8. <UncontrolledCollapse toggler="#toggler">
  9. <Card>
  10. <CardBody>
  11. Lorem ipsum dolor sit amet consectetur adipisicing elit. Nesciunt magni, voluptas debitis
  12. similique porro a molestias consequuntur earum odio officiis natus, amet hic, iste sed
  13. dignissimos esse fuga! Minus, alias.
  14. </CardBody>
  15. </Card>
  16. </UncontrolledCollapse>
  17. </div>
  18. );
  19. export default Example;