From 8589fc0a1dc40a6a62f07f74bb11020c5d198185 Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Wed, 24 Aug 2016 13:05:28 +0200 Subject: [PATCH] FileField: show image thumbnails --- fields/types/file/FileField.js | 80 +++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/fields/types/file/FileField.js b/fields/types/file/FileField.js index a4dc6c8324..bd39f2360b 100644 --- a/fields/types/file/FileField.js +++ b/fields/types/file/FileField.js @@ -10,6 +10,48 @@ import { Button, FormField, FormInput, FormNote } from 'elemental'; import FileChangeMessage from '../../components/FileChangeMessage'; import HiddenFileInput from '../../components/HiddenFileInput'; +const FileThumb = ({ url }) => { + const isPicture = url && url.match(/\.(jpeg|jpg|gif|png)$/i) != null; + if (!isPicture) { + // TODO generic icons + return false; + } + return ( +
+ +
+ ); +}; + +const FileDom = ({ url, filename }) => { + return ( +
+ +
+ + {url ? ( + {filename} + ) : ( + filename + )} + + {url && ( + + url: {url} + + )} +
+
+ ); +}; + let uploadInc = 1000; const buildInitialState = (props) => ({ @@ -43,8 +85,10 @@ module.exports = Field.create({ return this.props.collapse && !this.hasExisting(); }, componentWillUpdate (nextProps) { + const value = this.props.value || {}; + const nextVal = nextProps.value || {}; // Show the new filename when it's finished uploading - if (this.props.value.filename !== nextProps.value.filename) { + if (value.filename !== nextVal.filename) { this.setState(buildInitialState(nextProps)); } }, @@ -60,9 +104,11 @@ module.exports = Field.create({ return this.props.value && !!this.props.value.filename; }, getFilename () { - return this.state.userSelectedFile - ? this.state.userSelectedFile.name - : this.props.value.filename; + const { value } = this.props; + const { userSelectedFile } = this.state; + return userSelectedFile + ? userSelectedFile.name + : value && (value.originalname || value.filename); }, // ============================== @@ -70,7 +116,7 @@ module.exports = Field.create({ // ============================== triggerFileBrowser () { - this.refs.fileInput.clickDomNode(); + this.fileInput && this.fileInput.clickDomNode(); }, handleFileChange (event) { const userSelectedFile = event.target.files[0]; @@ -113,14 +159,16 @@ module.exports = Field.create({ // ============================== renderFileNameAndChangeMessage () { - const href = this.props.value ? this.props.value.url : undefined; + const { value } = this.props; + let url; + let filename; + if (this.hasFile() && !this.state.removeExisting) { + url = value && value.url; + filename = this.getFilename(); + } return (
- {(this.hasFile() && !this.state.removeExisting) ? ( - - {this.getFilename()} - - ) : null} + {filename && } {this.renderChangeMessage()}
); @@ -202,15 +250,17 @@ module.exports = Field.create({ key={this.state.uploadFieldPath} name={this.state.uploadFieldPath} onChange={this.handleFileChange} - ref="fileInput" + ref={el => { this.fileInput = el; }} /> {this.renderActionInput()} ) : (
- {this.hasFile() - ? this.renderFileNameAndChangeMessage() - : no file} + {this.hasFile() ? ( + this.renderFileNameAndChangeMessage() + ) : ( + no file + )}
)} {!!this.props.note && }