mirror of https://github.com/kern/filepizza
Extract a Centered component.
parent
1e1648410e
commit
5344bbda28
@ -0,0 +1,63 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default class Centered extends React.Component {
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const h = this.props.hor
|
||||||
|
const v = this.props.ver
|
||||||
|
|
||||||
|
if (h && v) {
|
||||||
|
|
||||||
|
return <div style={{
|
||||||
|
display: 'table',
|
||||||
|
width: '100%',
|
||||||
|
height: '100%'}}>
|
||||||
|
<div style={{
|
||||||
|
display: 'table-cell',
|
||||||
|
verticalAlign: 'middle',
|
||||||
|
textAlign: 'center'}}>
|
||||||
|
<div style={{display: 'inline-block'}}>
|
||||||
|
{this.props.children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
} else if (h && !v) {
|
||||||
|
|
||||||
|
return <div style={{textAlign: 'center'}}>
|
||||||
|
<div style={{display: 'inline-block'}}>
|
||||||
|
{this.props.children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
} else if (!h && v) {
|
||||||
|
|
||||||
|
return <div style={{
|
||||||
|
display: 'table',
|
||||||
|
width: '100%',
|
||||||
|
height: '100%'}}>
|
||||||
|
<div style={{
|
||||||
|
display: 'table-cell',
|
||||||
|
verticalAlign: 'middle'}}>
|
||||||
|
{this.props.children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
return this.props.children
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Centered.propTypes = {
|
||||||
|
hor: React.PropTypes.bool,
|
||||||
|
ver: React.PropTypes.bool
|
||||||
|
}
|
||||||
|
|
||||||
|
Centered.defaultProps = {
|
||||||
|
hor: false,
|
||||||
|
ver: false
|
||||||
|
}
|
||||||
@ -1,43 +1,52 @@
|
|||||||
import React from 'react';
|
import React from 'react'
|
||||||
import classnames from 'classnames';
|
import Centered from './Centered'
|
||||||
|
|
||||||
export default class DropZone extends React.Component {
|
export default class DropZone extends React.Component {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.state = { focus: false };
|
this.state = { focus: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragEnter() {
|
onDragEnter() {
|
||||||
this.setState({ focus: true });
|
this.setState({ focus: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragLeave() {
|
onDragLeave(e) {
|
||||||
this.setState({ focus: false });
|
if (e.target !== this.refs.overlay.getDOMNode()) return
|
||||||
|
this.setState({ focus: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragOver(e) {
|
onDragOver(e) {
|
||||||
e.preventDefault();
|
e.preventDefault()
|
||||||
e.dataTransfer.dropEffect = 'copy';
|
e.dataTransfer.dropEffect = 'copy'
|
||||||
}
|
}
|
||||||
|
|
||||||
onDrop(e) {
|
onDrop(e) {
|
||||||
e.preventDefault();
|
e.preventDefault()
|
||||||
this.setState({ focus: false });
|
this.setState({ focus: false })
|
||||||
|
|
||||||
let file = e.dataTransfer.files[0];
|
let file = e.dataTransfer.files[0]
|
||||||
if (this.props.onDrop && file) this.props.onDrop(file);
|
if (this.props.onDrop && file) this.props.onDrop(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let classes = classnames('drop-zone', {
|
return <div className="drop-zone" ref="root"
|
||||||
'drop-zone-focus': this.state.focus
|
|
||||||
});
|
|
||||||
|
|
||||||
return <div className={classes}
|
|
||||||
onDragEnter={this.onDragEnter.bind(this)}
|
onDragEnter={this.onDragEnter.bind(this)}
|
||||||
onDragLeave={this.onDragLeave.bind(this)}
|
onDragLeave={this.onDragLeave.bind(this)}
|
||||||
onDragOver={this.onDragOver.bind(this)}
|
onDragOver={this.onDragOver.bind(this)}
|
||||||
onDrop={this.onDrop.bind(this)} />;
|
onDrop={this.onDrop.bind(this)}>
|
||||||
|
|
||||||
|
<div className="drop-zone-overlay"
|
||||||
|
hidden={!this.state.focus}
|
||||||
|
ref="overlay" />
|
||||||
|
|
||||||
|
{this.props.children}
|
||||||
|
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DropZone.propTypes = {
|
||||||
|
onDrop: React.PropTypes.func.isRequired
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue