fixing wordcloud modal initialSlide index

This commit is contained in:
Sharyn 2018-07-05 15:19:24 +02:00
parent 7e6955e85e
commit 15750b4c6a
2 changed files with 28 additions and 13 deletions

View File

@ -12,22 +12,34 @@ class Resources extends React.Component {
this.state = { this.state = {
isWordCloudModalOpen: false, isWordCloudModalOpen: false,
activeSlide: 1, activeSlide: 0,
}; };
this.toggleWordCloudModal = this.toggleWordCloudModal.bind(this); this.openWordCloudModal = this.openWordCloudModal.bind(this);
this.closeWordCloudModal = this.closeWordCloudModal.bind(this);
} }
toggleWordCloudModal(event) { openWordCloudModal(event) {
const { isWordCloudModalOpen } = this.state; /*
const clickedIndex = event.target.dataset.index; /* Check if clicked element is a child and doesn't have a data-index,
/* then go get data-index from the parentNode
*/
let clic kedElement = event.target;
while (clickedElement.dataset.index === undefined) {
clickedElement = clickedElement.parentNode;
}
const clickedIndex = clickedElement.dataset.index;
this.setState({ this.setState({
isWordCloudModalOpen: !isWordCloudModalOpen, isWordCloudModalOpen: true,
activeSlide: clickedIndex, activeSlide: clickedIndex,
}); });
this.slider.slickGoTo(clickedIndex); this.slider.slickGoTo(clickedIndex);
} }
closeWordCloudModal() {
this.setState({ isWordCloudModalOpen: false });
}
render() { render() {
const { isWordCloudModalOpen, activeSlide } = this.state; const { isWordCloudModalOpen, activeSlide } = this.state;
const settings = { const settings = {
@ -49,7 +61,7 @@ class Resources extends React.Component {
Data.map((wordCloud, index) => Data.map((wordCloud, index) =>
(<WordCloud (<WordCloud
index={index} index={index}
toggleWordCloudModal={this.toggleWordCloudModal} openWordCloudModal={this.openWordCloudModal}
key={wordCloud.title} key={wordCloud.title}
words={wordCloud} words={wordCloud}
/>)) />))
@ -57,7 +69,7 @@ class Resources extends React.Component {
</div> </div>
<Modal <Modal
isModalOpen={isWordCloudModalOpen} isModalOpen={isWordCloudModalOpen}
closeModal={this.toggleWordCloudModal} closeModal={this.closeWordCloudModal}
> >
<Slider ref={slider => (this.slider = slider)} {...settings}> {/* eslint-disable-line */} <Slider ref={slider => (this.slider = slider)} {...settings}> {/* eslint-disable-line */}
{ {

View File

@ -7,14 +7,17 @@ const WordCloud = props => (
<div <div
className="wordcloud" className="wordcloud"
data-index={props.index} data-index={props.index}
onClick={props.toggleWordCloudModal} onClick={props.openWordCloudModal}
role="button" role="button"
tabIndex="0" tabIndex="0"
> >
<h3 data-index={props.index}>{ props.words.title }</h3> <h3>{ props.words.title }</h3>
<div data-index={props.index}> <div>
{ props.words.cloud.map(word => ( { props.words.cloud.map(word => (
<span key={word.word} className={`size-${word.size}`}> <span
key={word.word}
className={`size-${word.size}`}
>
{ word.word } { word.word }
</span> </span>
)) ))
@ -29,7 +32,7 @@ WordCloud.propTypes = {
title: PropTypes.string, title: PropTypes.string,
cloud: PropTypes.array, cloud: PropTypes.array,
}).isRequired, }).isRequired,
toggleWordCloudModal: PropTypes.func.isRequired, openWordCloudModal: PropTypes.func.isRequired,
}; };
export default WordCloud; export default WordCloud;