import React, { Component, } from 'react'; import { Row, Col, FormGroup, Label, Input, Carousel, CarouselItem, CarouselControl, CarouselIndicators, } from 'reactstrap'; import { FaFileImage, } from 'react-icons/fa'; class VegetableCarousel extends Component { constructor(props) { super(props); this.state = { imagesPreviewUrls: this.props.imagesPreviewUrls, activeIndex: 0 }; this.onPictureChange = this.onPictureChange.bind(this); this.next = this.next.bind(this); this.previous = this.previous.bind(this); this.goToIndex = this.goToIndex.bind(this); this.onExited = this.onExited.bind(this); this.onExiting = this.onExiting.bind(this); } componentWillReceiveProps(nextProps) { if ( nextProps.imagesPreviewUrls) { this.setState({ imagesPreviewUrls: nextProps.imagesPreviewUrls }); } if ( nextProps.activeIndex) { this.setState({ activeIndex: nextProps.activeIndex }); } } onPictureChange(e) { e.preventDefault(); const reader = new FileReader(); const file = e.target.files[0]; reader.onloadend = () => { this.props.addPicture(file, reader); }; reader.readAsDataURL(file); } next() { if (this.animating) return; const nextIndex = this.state.activeIndex === this.state.imagesPreviewUrls.length - 1 ? 0 : this.state.activeIndex + 1; this.setState({ activeIndex: nextIndex, }); } previous() { if (this.animating) return; const nextIndex = this.state.activeIndex === 0 ? this.state.imagesPreviewUrls.length - 1 : this.state.activeIndex - 1; this.setState({ activeIndex: nextIndex, }); } goToIndex(newIndex) { if (this.animating) return; this.setState({ activeIndex: newIndex, }); } onExiting() { this.animating = true; } onExited() { this.animating = false; } render() { const { activeIndex, } = this.state; return ( this.onPictureChange(e)} /> {this.state.imagesPreviewUrls && this.state.imagesPreviewUrls.length > 0 ? ( {this.state.imagesPreviewUrls.map( (item, key) => ( Carousel ))} ) : (null) } ) } } export default VegetableCarousel;