import React from 'react'; import {themeable, ClassNamesFn, ThemeProps} from '../theme'; import {autobind} from '../utils/helper'; import Modal from './Modal'; import {Icon} from './icons'; import {LocaleProps, localeable} from '../locale'; export interface ImageGalleryProps extends ThemeProps, LocaleProps { children: React.ReactNode; modalContainer?: () => HTMLElement; } export interface ImageGalleryState { isOpened: boolean; index: number; items: Array<{ src: string; originalSrc: string; title?: string; caption?: string; }>; } export class ImageGallery extends React.Component< ImageGalleryProps, ImageGalleryState > { state: ImageGalleryState = { isOpened: false, index: -1, items: [] }; @autobind handleImageEnlarge(info: { src: string; originalSrc: string; list?: Array<{ src: string; originalSrc: string; title?: string; caption?: string; }>; title?: string; caption?: string; index?: number; }) { this.setState({ isOpened: true, items: info.list ? info.list : [info], index: info.index || 0 }); } @autobind close() { this.setState({ isOpened: false }); } @autobind prev() { const index = this.state.index; this.setState({ index: index - 1 }); } @autobind next() { const index = this.state.index; this.setState({ index: index + 1 }); } @autobind handleItemClick(e: React.MouseEvent) { const index = parseInt(e.currentTarget.getAttribute('data-index')!, 10); this.setState({ index }); } render() { const {children, classnames: cx, modalContainer} = this.props; const {index, items} = this.state; const __ = this.props.translate; return ( <> {React.cloneElement(children as any, { onImageEnlarge: this.handleImageEnlarge })} {~index && items[index] ? ( <>
{items[index].title}
{items.length > 1 ? ( <> = items.length - 1 ? 'is-disabled' : '' )} onClick={this.next} > ) : null}
) : null} {items.length > 1 ? (
{items.map((item, i) => (
))}
) : null}
); } } export default themeable(localeable(ImageGallery));