Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SangNguyen2810/2d4f23f80ba7fe6b128d87da7ba55019 to your computer and use it in GitHub Desktop.
Save SangNguyen2810/2d4f23f80ba7fe6b128d87da7ba55019 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import '../styles/FileItem.scss'
import { connect } from 'react-redux';
import * as actions from '../actions/index';
class FileItem extends Component {
_isMounted = false;
constructor(props) {
super(props);
this.state = {
isHovering: false,
mouseEnterEvent: null,
showRightClickEventInfo: false
};
}
componentWillReceiveProps(props) {
if (props.mouseHandling.isRightClicked === false) {
// this.props.isOnRightClick(true)
this.setState({ showRightClickEventInfo: false, isHovering: false });
}
}
handleMouseEnter = (e) => {
if (this.state.showRightClickEventInfo === false && this.props.mouseHandling.isRightClicked === false) {
let mouseEnterVar = setTimeout(() => {
this.setState({ isHovering: true })
}, 1500)
this.setState({ mouseEnterEvent: mouseEnterVar });
}
}
handleMouseLeave = () => {
console.log(this.state.mouseEnterEvent)
clearTimeout(this.state.mouseEnterEvent);
this.setState({ mouseEnterEvent: null })
this.setState({ isHovering: false })
}
handleOnRightClick = (e) => {
if (e.type === 'contextmenu' && this.props.mouseHandling.isRightClicked === false) {
this.props.isOnRightClick(true)
this.setState({ showRightClickEventInfo: true, isHovering: false })
}
}
render() {
let widthStyle = {
width: `${90 / this.props.numberOfItemInOneRow}%`
}
const { element } = this.props;
return (
<div style={widthStyle} className="fileItemContainer" onDoubleClick={this.props.onDoubleClick} onContextMenu={(e) => this.handleOnRightClick(e)}
onMouseEnter={(e) => { this.handleMouseEnter(e) }} onMouseLeave={() => { this.handleMouseLeave() }}
>
<div className="fileItemContainer__imageContainer">
<img
className="fileItemContainer__imageContainer-image"
alt={element.fileName}
src={element.fileUrl}
/>
</div>
{this.state.isHovering ? <div className="fileItemContainer__fileInfo">
<div>Name:{element.fileName}</div>
<div>Size:{element.size} bytes</div>
</div> : null}
{this.props.showTitle ? <div className="fileItemContainer__fileTitle">{element.fileName}</div> : null}
{this.state.showRightClickEventInfo ? <div>
<div>Name:{element.fileName}</div>
<div>Size:{element.size} bytes</div>
</div> : null}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
mouseHandling: state.mouseHandling
};
};
const mapDispatchToProps = (dispatch) => {
return {
isOnRightClick: (state) => dispatch(actions.isOnRightClick(state))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(FileItem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment