Buttons

Buttons - 图1

  1. import React from 'react';
  2. import { Button } from 'reactstrap';
  3. export default class Example extends React.Component {
  4. render() {
  5. return (
  6. <div>
  7. <Button color="primary">primary</Button>{' '}
  8. <Button color="secondary">secondary</Button>{' '}
  9. <Button color="success">success</Button>{' '}
  10. <Button color="info">info</Button>{' '}
  11. <Button color="warning">warning</Button>{' '}
  12. <Button color="danger">danger</Button>{' '}
  13. <Button color="link">link</Button>
  14. </div>
  15. );
  16. }
  17. }

Properties

  1. Button.propTypes = {
  2. active: PropTypes.bool,
  3. 'aria-label': PropTypes.string,
  4. block: PropTypes.bool,
  5. color: PropTypes.string, // default: 'secondary'
  6. disabled: PropTypes.bool,
  7. outline: PropTypes.bool,
  8. // Pass in a Component to override default button element
  9. // example: react-router Link
  10. // default: 'button'
  11. tag: PropTypes.oneOfType([
  12. PropTypes.func,
  13. PropTypes.string,
  14. PropTypes.shape({ $$typeof: PropTypes.symbol, render: PropTypes.func }),
  15. PropTypes.arrayOf(PropTypes.oneOfType([
  16. PropTypes.func,
  17. PropTypes.string,
  18. PropTypes.shape({ $$typeof: PropTypes.symbol, render: PropTypes.func }),
  19. ]))
  20. ]),
  21. // ref will only get you a reference to the Button component, use innerRef to get a reference to the DOM element (for things like focus management).
  22. innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  23. onClick: PropTypes.func,
  24. size: PropTypes.string,
  25. children: PropTypes.node,
  26. className: PropTypes.string,
  27. cssModule: PropTypes.object,
  28. // use close prop for BS4 close icon utility
  29. close: PropTypes.bool,
  30. }
  31. Button.defaultProps = {
  32. color: 'secondary',
  33. tag: 'button',
  34. }

Outline Buttons

Buttons - 图2

  1. import React from 'react';
  2. import { Button } from 'reactstrap';
  3. export default class Example extends React.Component {
  4. render() {
  5. return (
  6. <div>
  7. <Button outline color="primary">primary</Button>{' '}
  8. <Button outline color="secondary">secondary</Button>{' '}
  9. <Button outline color="success">success</Button>{' '}
  10. <Button outline color="info">info</Button>{' '}
  11. <Button outline color="warning">warning</Button>{' '}
  12. <Button outline color="danger">danger</Button>
  13. </div>
  14. );
  15. }
  16. }

Sizes

  1. <Button color="primary" size="lg">Large Button</Button>{' '}
  2. <Button color="secondary" size="lg">Large Button</Button>
  1. <Button color="primary" size="sm">Small Button</Button>{' '}
  2. <Button color="secondary" size="sm">Small Button</Button>
  1. <Button color="primary" size="lg" block>Block level button</Button>
  2. <Button color="secondary" size="lg" block>Block level button</Button>

Active State

  1. <Button color="primary" size="lg" active>Primary link</Button>{' '}
  2. <Button color="secondary" size="lg" active>Link</Button>

Disabled State

  1. <Button color="primary" size="lg" disabled>Primary button</Button>{' '}
  2. <Button color="secondary" size="lg" disabled>Button</Button>

Checkbox and Radio Buttons (Stateful Buttons)

In order to have checkbox and radio buttons, your component needs to manage the state of which button(s) are active/select. It is not in the opinion of this library to manage state within it's components so it is left up to you. Below is a simple example showcasing how this could be done using the components which already exist in this library.

Buttons - 图3

  1. import React, { Component } from 'react';
  2. import { Button, ButtonGroup } from 'reactstrap';
  3. class Example extends Component {
  4. constructor (props) {
  5. super(props);
  6. this.state = { cSelected: [] };
  7. this.onRadioBtnClick = this.onRadioBtnClick.bind(this);
  8. this.onCheckboxBtnClick = this.onCheckboxBtnClick.bind(this);
  9. }
  10. onRadioBtnClick(rSelected) {
  11. this.setState({ rSelected });
  12. }
  13. onCheckboxBtnClick(selected) {
  14. const index = this.state.cSelected.indexOf(selected);
  15. if (index < 0) {
  16. this.state.cSelected.push(selected);
  17. } else {
  18. this.state.cSelected.splice(index, 1);
  19. }
  20. this.setState({ cSelected: [...this.state.cSelected] });
  21. }
  22. render() {
  23. return (
  24. <div>
  25. <h5>Radio Buttons</h5>
  26. <ButtonGroup>
  27. <Button color="primary" onClick={() => this.onRadioBtnClick(1)} active={this.state.rSelected === 1}>One</Button>
  28. <Button color="primary" onClick={() => this.onRadioBtnClick(2)} active={this.state.rSelected === 2}>Two</Button>
  29. <Button color="primary" onClick={() => this.onRadioBtnClick(3)} active={this.state.rSelected === 3}>Three</Button>
  30. </ButtonGroup>
  31. <p>Selected: {this.state.rSelected}</p>
  32. <h5>Checkbox Buttons</h5>
  33. <ButtonGroup>
  34. <Button color="primary" onClick={() => this.onCheckboxBtnClick(1)} active={this.state.cSelected.includes(1)}>One</Button>
  35. <Button color="primary" onClick={() => this.onCheckboxBtnClick(2)} active={this.state.cSelected.includes(2)}>Two</Button>
  36. <Button color="primary" onClick={() => this.onCheckboxBtnClick(3)} active={this.state.cSelected.includes(3)}>Three</Button>
  37. </ButtonGroup>
  38. <p>Selected: {JSON.stringify(this.state.cSelected)}</p>
  39. </div>
  40. );
  41. }
  42. }
  43. export default Example;

Close icon

Use a generic close icon to dismiss content. Use <Button close /> for the default icon. Otherwise, custom content for the button may be defined. (e.g. JSX: <Button close><span aria-hidden="true">–</span></Button>) The default aria-label is "Close".

Buttons - 图4

  1. import React, { Component } from 'react';
  2. import { Button, Card, CardBody, CardText, CardGroup, CardTitle } from 'reactstrap';
  3. const Example = () => (
  4. <div>
  5. <CardGroup>
  6. <Card>
  7. <CardBody>
  8. <CardTitle>
  9. <Button close />
  10. </CardTitle>
  11. <CardText>Default close icon</CardText>
  12. </CardBody>
  13. </Card>
  14. <Card>
  15. <CardBody>
  16. <CardTitle>
  17. <Button close aria-label="Cancel">
  18. <span aria-hidden>&ndash;</span>
  19. </Button>
  20. </CardTitle>
  21. <CardText>
  22. Custom content and aria-label
  23. </CardText>
  24. </CardBody>
  25. </Card>
  26. </CardGroup>
  27. </div>
  28. );
  29. export default Example;