forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: custom upload control for images (elastic#507)
Add a custom file upload component, which is used in the image uploader to provide a nicer, styled upload control than the old file input * chore: make image upload a normal component * fix: only update upload image state if still mounted * feat: create a file upload component useful for wrapping custom upload controls * feat: use FileUpload for image upload argtype * feat: handle drag and drop files this mimics native file input functionality. also sends an activeTarget value into the render child so it can react to things being dragged over it * feat: make drop target more apparent use the activeTarget value to set the button class and text * fix: make exposed prop clearer also clean up and add some comments
- Loading branch information
Showing
3 changed files
with
133 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export class FileUpload extends React.PureComponent { | ||
static propTypes = { | ||
id: PropTypes.string, | ||
className: PropTypes.string, | ||
onUpload: PropTypes.func.isRequired, | ||
children: PropTypes.func.isRequired, | ||
}; | ||
|
||
static defaultProps = { | ||
id: '', | ||
className: 'canvas__file-upload', | ||
}; | ||
|
||
state = { | ||
isHovered: false, | ||
}; | ||
|
||
// track enter count since enter/leave fires with each child node you drag over | ||
_enterCount = 0; | ||
|
||
uploadPrompt = ev => { | ||
ev.preventDefault(); | ||
this.fileInput.click(); | ||
}; | ||
|
||
onDrag = type => ev => { | ||
ev.preventDefault(); | ||
this._enterCount = type === 'enter' ? this._enterCount + 1 : this._enterCount - 1; | ||
this.setState({ isHovered: this._enterCount > 0 }); | ||
}; | ||
|
||
onDragOver = ev => { | ||
// enables the onDrop handler, see https://developer.mozilla.org/en-US/docs/Web/Events/drop#Example | ||
ev.preventDefault(); | ||
}; | ||
|
||
onDrop = ev => { | ||
ev.preventDefault(); | ||
this._enterCount = 0; | ||
this.setState({ isHovered: false }); | ||
|
||
this.props.onUpload({ | ||
files: ev.dataTransfer.files, | ||
}); | ||
}; | ||
|
||
onUpload = ev => { | ||
ev.preventDefault(); | ||
this.props.onUpload({ | ||
files: ev.target.files, | ||
}); | ||
}; | ||
|
||
render() { | ||
const { id, className, children } = this.props; | ||
|
||
return ( | ||
<div | ||
id={id} | ||
className={className} | ||
onDragEnter={this.onDrag('enter')} | ||
onDragLeave={this.onDrag('leave')} | ||
onDragOver={this.onDragOver} | ||
onDrop={this.onDrop} | ||
> | ||
<input | ||
type="file" | ||
style={{ display: 'none' }} | ||
onChange={this.onUpload} | ||
ref={r => (this.fileInput = r)} | ||
/> | ||
{children({ uploadPrompt: this.uploadPrompt, isHovered: this.state.isHovered })} | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { FileUpload } from './file_upload'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters