import React, { Component, PropTypes } from 'react'; import { injectIntl, intlShape } from 'react-intl'; import classNames from 'classnames'; import { ResponsiveImage } from '../../components'; import * as propTypes from '../../util/propTypes'; import css from './ImageCarousel.css'; const KEY_CODE_LEFT_ARROW = 37; const KEY_CODE_RIGHT_ARROW = 39; const imageScaledSmall = 'scaled-small'; // width 320 const imageScaledMedium = 'scaled-medium'; // width 750 const imageScaledLarge = 'scaled-large'; // width 1024 const imageScaledXLarge = 'scaled-xlarge'; // width 2400 class ImageCarousel extends Component { constructor(props) { super(props); this.state = { selectedImageIndex: 0 }; this.onKeyUp = this.onKeyUp.bind(this); this.prev = this.prev.bind(this); this.next = this.next.bind(this); } componentDidMount() { window.addEventListener('keyup', this.onKeyUp); } componentWillUnmount() { window.removeEventListener('keyup', this.onKeyUp); } onKeyUp(e) { if (e.keyCode === KEY_CODE_LEFT_ARROW) { this.prev(); } else if (e.keyCode === KEY_CODE_RIGHT_ARROW) { this.next(); } } prev() { const count = this.props.images.length; this.setState(prevState => { const newIndex = count > 0 ? (count + prevState.selectedImageIndex - 1) % count : 0; return { selectedImageIndex: newIndex }; }); } next() { const count = this.props.images.length; this.setState(prevState => { const newIndex = count > 0 ? (count + prevState.selectedImageIndex + 1) % count : 0; return { selectedImageIndex: newIndex }; }); } render() { const { rootClassName, className, images, intl } = this.props; const classes = classNames(rootClassName || css.root, className); const naturalIndex = this.state.selectedImageIndex + 1; const imageIndex = images.length > 0 ? {naturalIndex}/{images.length} : null; const prevButton = images.length > 1 ? : null; const nextButton = images.length > 1 ? : null; const imageAltText = intl.formatMessage( { id: 'ImageCarousel.imageAltText', }, { index: naturalIndex, count: images.length, } ); return (