Modals

Modals - 图1

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  21. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
  22. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  23. <ModalBody>
  24. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  25. </ModalBody>
  26. <ModalFooter>
  27. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  28. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  29. </ModalFooter>
  30. </Modal>
  31. </div>
  32. );
  33. }
  34. }
  35. export default ModalExample;

Properties

  1. Modal.propTypes = {
  2. // boolean to control the state of the popover
  3. isOpen: PropTypes.bool,
  4. autoFocus: PropTypes.bool,
  5. // if modal should be centered vertically in viewport
  6. centered: PropTypes.bool,
  7. // corresponds to bootstrap's modal sizes, ie. 'lg' or 'sm'
  8. size: PropTypes.string,
  9. // callback for toggling isOpen in the controlling component
  10. toggle: PropTypes.func,
  11. role: PropTypes.string, // defaults to "dialog"
  12. // used to reference the ID of the title element in the modal
  13. labelledBy: PropTypes.string,
  14. keyboard: PropTypes.bool,
  15. // control backdrop, see http://v4-alpha.getbootstrap.com/components/modal/#options
  16. backdrop: PropTypes.oneOfType([
  17. PropTypes.bool,
  18. PropTypes.oneOf(['static'])
  19. ]),
  20. // if body of modal should be scrollable when content is long
  21. scrollable: PropTypes.bool,
  22. // allows for a node/component to exist next to the modal (outside of it). Useful for external close buttons
  23. // external: PropTypes.node,
  24. // called on componentDidMount
  25. onEnter: PropTypes.func,
  26. // called on componentWillUnmount
  27. onExit: PropTypes.func,
  28. // called when done transitioning in
  29. onOpened: PropTypes.func,
  30. // called when done transitioning out
  31. onClosed: PropTypes.func,
  32. className: PropTypes.string,
  33. wrapClassName: PropTypes.string,
  34. modalClassName: PropTypes.string,
  35. backdropClassName: PropTypes.string,
  36. contentClassName: PropTypes.string,
  37. // boolean to control whether the fade transition occurs (default: true)
  38. fade: PropTypes.bool,
  39. cssModule: PropTypes.object,
  40. // zIndex defaults to 1000.
  41. zIndex: PropTypes.oneOfType([
  42. PropTypes.number,
  43. PropTypes.string,
  44. ]),
  45. // backdropTransition - controls backdrop transition
  46. // timeout is 150ms by default to match bootstrap
  47. // see Fade for more details
  48. backdropTransition: PropTypes.shape(Fade.propTypes),
  49. // modalTransition - controls modal transition
  50. // timeout is 300ms by default to match bootstrap
  51. // see Fade for more details
  52. modalTransition: PropTypes.shape(Fade.propTypes),
  53. innerRef: PropTypes.object,
  54. // if modal should be destructed/removed from DOM after closing
  55. unmountOnClose: PropTypes.bool // defaults to true
  56. }

Backdrop

Modals - 图2

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label, Form, FormGroup } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false,
  9. backdrop: true
  10. };
  11. this.toggle = this.toggle.bind(this);
  12. this.changeBackdrop = this.changeBackdrop.bind(this);
  13. }
  14. toggle() {
  15. this.setState(prevState => ({
  16. modal: !prevState.modal
  17. }));
  18. }
  19. changeBackdrop(e) {
  20. let value = e.target.value;
  21. if (value !== 'static') {
  22. value = JSON.parse(value);
  23. }
  24. this.setState({ backdrop: value });
  25. }
  26. render() {
  27. return (
  28. <div>
  29. <Form inline onSubmit={(e) => e.preventDefault()}>
  30. <FormGroup>
  31. <Label for="backdrop">Backdrop value</Label>{' '}
  32. <Input type="select" name="backdrop" id="backdrop" onChange={this.changeBackdrop}>
  33. <option value="true">true</option>
  34. <option value="false">false</option>
  35. <option value="static">"static"</option>
  36. </Input>
  37. </FormGroup>
  38. {' '}
  39. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  40. </Form>
  41. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className} backdrop={this.state.backdrop}>
  42. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  43. <ModalBody>
  44. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  45. </ModalBody>
  46. <ModalFooter>
  47. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  48. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  49. </ModalFooter>
  50. </Modal>
  51. </div>
  52. );
  53. }
  54. }
  55. export default ModalExample;

Nested Modals

Modals - 图3

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false,
  9. nestedModal: false,
  10. closeAll: false
  11. };
  12. this.toggle = this.toggle.bind(this);
  13. this.toggleNested = this.toggleNested.bind(this);
  14. this.toggleAll = this.toggleAll.bind(this);
  15. }
  16. toggle() {
  17. this.setState(prevState => ({
  18. modal: !prevState.modal
  19. }));
  20. }
  21. toggleNested() {
  22. this.setState({
  23. nestedModal: !this.state.nestedModal,
  24. closeAll: false
  25. });
  26. }
  27. toggleAll() {
  28. this.setState({
  29. nestedModal: !this.state.nestedModal,
  30. closeAll: true
  31. });
  32. }
  33. render() {
  34. return (
  35. <div>
  36. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  37. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
  38. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  39. <ModalBody>
  40. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  41. <br />
  42. <Button color="success" onClick={this.toggleNested}>Show Nested Modal</Button>
  43. <Modal isOpen={this.state.nestedModal} toggle={this.toggleNested} onClosed={this.state.closeAll ? this.toggle : undefined}>
  44. <ModalHeader>Nested Modal title</ModalHeader>
  45. <ModalBody>Stuff and things</ModalBody>
  46. <ModalFooter>
  47. <Button color="primary" onClick={this.toggleNested}>Done</Button>{' '}
  48. <Button color="secondary" onClick={this.toggleAll}>All Done</Button>
  49. </ModalFooter>
  50. </Modal>
  51. </ModalBody>
  52. <ModalFooter>
  53. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  54. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  55. </ModalFooter>
  56. </Modal>
  57. </div>
  58. );
  59. }
  60. }
  61. export default ModalExample;

Modals with Custom Transition Timeouts

Modals - 图4

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  21. <Modal isOpen={this.state.modal} modalTransition={{ timeout: 700 }} backdropTransition={{ timeout: 1300 }}
  22. toggle={this.toggle} className={this.props.className}>
  23. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  24. <ModalBody>
  25. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  26. </ModalBody>
  27. <ModalFooter>
  28. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  29. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  30. </ModalFooter>
  31. </Modal>
  32. </div>
  33. );
  34. }
  35. }
  36. export default ModalExample;

Modals without Fade Effect

Modals - 图5

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  21. <Modal isOpen={this.state.modal} fade={false} toggle={this.toggle} className={this.props.className}>
  22. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  23. <ModalBody>
  24. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  25. </ModalBody>
  26. <ModalFooter>
  27. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  28. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  29. </ModalFooter>
  30. </Modal>
  31. </div>
  32. );
  33. }
  34. }
  35. export default ModalExample;

Modals with external button

Modals - 图6

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. const externalCloseBtn = <button className="close" style={{ position: 'absolute', top: '15px', right: '15px' }} onClick={this.toggle}>&times;</button>;
  19. return (
  20. <div>
  21. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  22. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className} external={externalCloseBtn}>
  23. <ModalHeader>Modal title</ModalHeader>
  24. <ModalBody>
  25. <b>Look at the top right of the page/viewport!</b><br />
  26. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  27. </ModalBody>
  28. <ModalFooter>
  29. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  30. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  31. </ModalFooter>
  32. </Modal>
  33. </div>
  34. );
  35. }
  36. }
  37. export default ModalExample;

Modals with custom close icon

Modals - 图7

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  21. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
  22. <ModalHeader toggle={this.toggle} charCode="Y">Modal title</ModalHeader>
  23. <ModalBody>
  24. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  25. </ModalBody>
  26. <ModalFooter>
  27. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  28. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  29. </ModalFooter>
  30. </Modal>
  31. </div>
  32. );
  33. }
  34. }
  35. export default ModalExample;

Modals with custom close button

Modals - 图8

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false
  9. };
  10. this.toggle = this.toggle.bind(this);
  11. }
  12. toggle() {
  13. this.setState(prevState => ({
  14. modal: !prevState.modal
  15. }));
  16. }
  17. render() {
  18. const closeBtn = <button className="close" onClick={this.toggle}>&times;</button>;
  19. return (
  20. <div>
  21. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  22. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
  23. <ModalHeader toggle={this.toggle} close={closeBtn}>Modal title</ModalHeader>
  24. <ModalBody>
  25. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
  26. dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
  27. ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
  28. fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
  29. mollit anim id est laborum.
  30. </ModalBody>
  31. <ModalFooter>
  32. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  33. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  34. </ModalFooter>
  35. </Modal>
  36. </div>
  37. );
  38. }
  39. }
  40. export default ModalExample;

Destructuring

Modals - 图9

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Input, Label, Form, FormGroup } from 'reactstrap';
  4. class ModalExample extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. modal: false,
  9. unmountOnClose: true
  10. };
  11. this.toggle = this.toggle.bind(this);
  12. this.changeUnmountOnClose = this.changeUnmountOnClose.bind(this);
  13. }
  14. toggle() {
  15. this.setState(prevState => ({
  16. modal: !prevState.modal
  17. }));
  18. }
  19. changeUnmountOnClose(e) {
  20. let value = e.target.value;
  21. this.setState({ unmountOnClose: JSON.parse(value) });
  22. }
  23. render() {
  24. return (
  25. <div>
  26. <Form inline onSubmit={(e) => e.preventDefault()}>
  27. <FormGroup>
  28. <Label for="unmountOnClose">UnmountOnClose value</Label>{' '}
  29. <Input type="select" name="unmountOnClose" id="unmountOnClose" onChange={this.changeUnmountOnClose}>
  30. <option value="true">true</option>
  31. <option value="false">false</option>
  32. </Input>
  33. </FormGroup>
  34. {' '}
  35. <Button color="danger" onClick={this.toggle}>{this.props.buttonLabel}</Button>
  36. </Form>
  37. <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className} unmountOnClose={this.state.unmountOnClose}>
  38. <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
  39. <ModalBody>
  40. <Input type="textarea" placeholder="Write something (data should remain in modal if unmountOnClose is set to false)" rows={5} />
  41. </ModalBody>
  42. <ModalFooter>
  43. <Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
  44. <Button color="secondary" onClick={this.toggle}>Cancel</Button>
  45. </ModalFooter>
  46. </Modal>
  47. </div>
  48. );
  49. }
  50. }
  51. export default ModalExample;

Focus after close

Modals - 图10

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Modal, ModalBody, ModalFooter, Label, Input, FormGroup, Form } from 'reactstrap';
  4. class ModalFocusAfterClose extends React.Component {
  5. constructor() {
  6. super();
  7. this.state = {
  8. open: false,
  9. focusAfterClose: true
  10. };
  11. this.toggle = this.toggle.bind(this);
  12. this.handleSelectChange = this.handleSelectChange.bind(this);
  13. }
  14. toggle() {
  15. this.setState(({ open }) => ({ open: !open }));
  16. }
  17. handleSelectChange({target: { value }}) {
  18. this.setState({ focusAfterClose: JSON.parse(value) });
  19. }
  20. render() {
  21. return (
  22. <div>
  23. <Form inline onSubmit={(e) => e.preventDefault()}>
  24. <FormGroup>
  25. <Label for="focusAfterClose">Focus After Close</Label>
  26. <Input className="mx-2" type="select" id="focusAfterClose" onChange={this.handleSelectChange}>
  27. <option value="true">Yes</option>
  28. <option value="false">No</option>
  29. </Input>
  30. </FormGroup>
  31. <Button color="danger" onClick={this.toggle}>Open</Button>
  32. </Form>
  33. <Modal returnFocusAfterClose={this.state.focusAfterClose} isOpen={this.state.open}>
  34. <ModalBody>
  35. Observe the "Open" button. It will be focused after close when "returnFocusAfterClose" is true and will not be focused if "returnFocusAfterClose" is false.
  36. </ModalBody>
  37. <ModalFooter>
  38. <Button color="primary" onClick={this.toggle}>Close</Button>
  39. </ModalFooter>
  40. </Modal>
  41. </div>
  42. )
  43. }
  44. }
  45. export default ModalFocusAfterClose;