ant-design/site/component/ImagePreview/index.jsx

121 lines
3.5 KiB
React
Raw Normal View History

2016-03-02 17:12:43 +08:00
import React from 'react';
2016-03-17 15:00:06 +08:00
import classNames from 'classnames';
2016-04-11 10:12:48 +08:00
import { Modal, Carousel } from 'antd';
2016-03-02 17:12:43 +08:00
function isGood(className) {
return /\bgood\b/i.test(className);
}
function isBad(className) {
return /\bbad\b/i.test(className);
}
2016-03-16 17:05:41 +08:00
function PreviewImageBox({ cover, coverMeta, imgs, style, previewVisible,
comparable, onClick, onCancel }) {
2016-03-17 15:00:06 +08:00
const onlyOneImg = comparable || imgs.length === 1;
2016-03-16 17:05:41 +08:00
return (
<div className="preview-image-box"
style={style}
onClick={onClick}>
<div className={`preview-image-wrapper ${coverMeta.isGood && 'good'} ${coverMeta.isBad && 'bad'}`}>
<img className={coverMeta.className} src={coverMeta.src} alt={coverMeta.alt} />
2016-03-16 17:05:41 +08:00
</div>
<div className="preview-image-title">{coverMeta.alt}</div>
2016-03-16 17:46:35 +08:00
<div className="preview-image-description"
dangerouslySetInnerHTML={{ __html: coverMeta.description }} />
2016-03-16 17:05:41 +08:00
2016-04-07 15:34:55 +08:00
<Modal className="image-modal" width={960} visible={previewVisible} title={null} footer={null}
2016-03-16 17:05:41 +08:00
onCancel={onCancel}>
2016-03-17 15:00:06 +08:00
<Carousel className={`${onlyOneImg ? 'image-modal-single' : ''}`} adaptiveHeight>
{comparable ? cover : imgs}
2016-03-16 17:05:41 +08:00
</Carousel>
<div className="preview-image-title">{coverMeta.alt}</div>
</Modal>
</div>
);
}
2016-03-02 17:12:43 +08:00
export default class ImagePreview extends React.Component {
constructor(props) {
super(props);
this.state = {
leftVisible: false,
rightVisible: false,
};
2016-03-30 16:16:18 +08:00
this.handleLeftClick = this.handleClick.bind(this, 'left');
this.handleRightClick = this.handleClick.bind(this, 'right');
2016-03-02 17:12:43 +08:00
}
handleClick(side) {
this.setState({ [`${side}Visible`]: true });
}
2016-03-30 16:16:18 +08:00
handleCancel = () => {
2016-03-02 17:12:43 +08:00
this.setState({
leftVisible: false,
rightVisible: false,
});
}
render() {
2016-03-17 15:00:06 +08:00
const { imgs } = this.props;
2016-03-02 17:12:43 +08:00
const imgsMeta = imgs.map((img) => {
const { alt, description, src } = img;
const imgClassName = img.class;
2016-03-02 17:12:43 +08:00
return {
2016-03-02 17:58:06 +08:00
className: imgClassName,
alt, description, src,
2016-03-02 17:12:43 +08:00
isGood: isGood(imgClassName),
isBad: isBad(imgClassName),
};
});
const imagesList = imgsMeta.map((meta, index) => {
2016-03-17 10:43:50 +08:00
return (
<div key={index}>
<div className="image-modal-container">
<img {...meta} alt={meta.alt} />
2016-03-17 10:43:50 +08:00
</div>
</div>
);
2016-03-02 17:12:43 +08:00
});
const comparable = imgs.length === 2 &&
(imgsMeta[0].isGood || imgsMeta[0].isBad) &&
(imgsMeta[1].isGood || imgsMeta[1].isBad);
const style = comparable ? { width: '50%' } : null;
2016-03-17 15:00:06 +08:00
const hasCarousel = imgs.length > 1 && !comparable;
const previewClassName = classNames({
'preview-image-boxes': true,
clearfix: true,
'preview-image-boxes-with-carousel': hasCarousel,
});
2016-03-02 17:12:43 +08:00
return (
2016-03-17 15:00:06 +08:00
<div className={previewClassName}>
2016-03-16 17:05:41 +08:00
<PreviewImageBox style={style}
comparable={comparable}
previewVisible={this.state.leftVisible}
cover={imagesList[0]}
coverMeta={imgsMeta[0]}
imgs={imagesList}
2016-03-30 16:16:18 +08:00
onClick={this.handleLeftClick}
onCancel={this.handleCancel}
2016-03-16 17:05:41 +08:00
/>
2016-03-02 17:12:43 +08:00
{
comparable ?
2016-03-16 17:05:41 +08:00
<PreviewImageBox style={style}
comparable
previewVisible={this.state.rightVisible}
cover={imagesList[1]}
coverMeta={imgsMeta[1]}
imgs={imagesList}
2016-03-30 16:16:18 +08:00
onClick={this.handleRightClick}
onCancel={this.handleCancel}
2016-03-16 17:05:41 +08:00
/> : null
2016-03-02 17:12:43 +08:00
}
</div>
);
}
}