Carousel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2017 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React, { Component } from 'react';
  19. import { bool, func, node, string } from 'prop-types';
  20. import Button from 'instructure-ui/lib/components/Button';
  21. import ArrowOpenLeft from 'instructure-icons/lib/Line/IconArrowOpenLeftLine';
  22. import ArrowOpenRight from 'instructure-icons/lib/Line/IconArrowOpenRightLine';
  23. export default class Carousel extends Component {
  24. componentDidUpdate (prevProps) {
  25. const selectedLast = prevProps.displayRightArrow && !this.props.displayRightArrow;
  26. const selectedFirst = prevProps.displayLeftArrow && !this.props.displayLeftArrow;
  27. if (selectedFirst) {
  28. this.rightArrow.focus();
  29. } else if (selectedLast) {
  30. this.leftArrow.focus();
  31. }
  32. }
  33. handleLeftArrowClick = () => {
  34. this.props.onLeftArrowClick();
  35. this.leftArrow.focus();
  36. }
  37. handleRightArrowClick = () => {
  38. this.props.onRightArrowClick();
  39. this.rightArrow.focus();
  40. }
  41. render () {
  42. const leftArrow = (
  43. <Button
  44. disabled={this.props.disabled}
  45. ref={(button) => { this.leftArrow = button }}
  46. variant="icon"
  47. onClick={this.handleLeftArrowClick}
  48. size="small"
  49. >
  50. <ArrowOpenLeft title={this.props.leftArrowDescription} />
  51. </Button>
  52. );
  53. const rightArrow = (
  54. <Button
  55. disabled={this.props.disabled}
  56. ref={(button) => { this.rightArrow = button }}
  57. variant="icon"
  58. onClick={this.handleRightArrowClick}
  59. size="small"
  60. >
  61. <ArrowOpenRight title={this.props.rightArrowDescription} />
  62. </Button>
  63. );
  64. return (
  65. <div id={this.props.id} className="carousel">
  66. <div className="left-arrow-button-container">
  67. { this.props.displayLeftArrow && leftArrow }
  68. </div>
  69. <div style={{ flex: 1 }}>
  70. {this.props.children}
  71. </div>
  72. <div className="right-arrow-button-container">
  73. { this.props.displayRightArrow && rightArrow }
  74. </div>
  75. </div>
  76. );
  77. }
  78. }
  79. Carousel.defaultProps = {
  80. id: null,
  81. showBorderBottom: true
  82. };
  83. Carousel.propTypes = {
  84. id: string,
  85. children: node.isRequired,
  86. disabled: bool.isRequired,
  87. displayLeftArrow: bool.isRequired,
  88. displayRightArrow: bool.isRequired,
  89. onLeftArrowClick: func.isRequired,
  90. onRightArrowClick: func.isRequired,
  91. leftArrowDescription: string.isRequired,
  92. rightArrowDescription: string.isRequired
  93. };