import React from 'react'; import { Modal, Carousel } from '../../../'; function isGood(className) { return /\bgood\b/i.test(className); } function isBad(className) { return /\bbad\b/i.test(className); } const parser = new DOMParser(); export default class ImagePreview extends React.Component { constructor(props) { super(props); this.state = { leftVisible: false, rightVisible: false, }; } handleClick(side) { this.setState({ [`${side}Visible`]: true }); } handleCancel() { this.setState({ leftVisible: false, rightVisible: false, }); } render() { const { className, imgs } = this.props; const imgsMeta = imgs.map((img) => { const xml = parser.parseFromString(img, 'application/xml'); const attributes = xml.firstChild.attributes; const { alt, description, src } = attributes; const imgClassName = attributes.class.nodeValue; return { className: imgClassName, alt: alt && alt.nodeValue, description: description && description.nodeValue, src: src.nodeValue, isGood: isGood(imgClassName), isBad: isBad(imgClassName), }; }); const cover = imgsMeta[0]; const imagesList = imgsMeta.map((meta, index) => { return ; }); const comparable = imgs.length === 2 && (imgsMeta[0].isGood || imgsMeta[0].isBad) && (imgsMeta[1].isGood || imgsMeta[1].isBad); const style = comparable ? { width: '50%' } : null; return (
Sample Picture
{cover.alt}
{cover.description}
{ comparable ? imagesList[0] : imagesList } { comparable || imagesList.length === 1 ?
{cover.alt}
: null }
{ comparable ?
Sample Picture
{imgsMeta[1].alt}
{imgsMeta[1].description}
{ comparable ? imagesList[1] : imagesList }
{imagesList[1].alt}
: null }
); } }